1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use Closure;
use JsCast;
use ;
/// Get the [`web_sys::Document`](https://docs.rs/web-sys/0.3.56/web_sys/struct.Document.html) Object
///
/// This function is equivalent to javascript's `document` property
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```rust
/// use weblog::console_log;
/// use webru::document;
///
/// // Get the `web_sys::Document` object by calling this function
/// let document = document();
///
/// // Get the url of the page
/// let url = document.url().unwrap();
///
/// // print the url in console
/// console_log!(url.clone());
///
/// assert_eq!(
/// url,
/// web_sys::window()
/// .unwrap()
/// .document()
/// .unwrap()
/// .url()
/// .unwrap()
/// )
/// ```
///
///
/// Get the [`web_sys::Location`](https://docs.rs/web-sys/0.3.56/web_sys/struct.Location.html) object
///
/// This function is equivalent to javascript's `location` property
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```rust
/// use weblog::console_log;
/// use webru::location;
///
/// // Get the `web_sys::Location` object
/// let location = location();
///
/// // print the full path of your webpage
/// console_log!(location.href().unwrap());
///
/// // print the host/domain of your webpage
/// console_log!(location.host().unwrap());
///
/// ```
///
/// Get the full url of the web page.
///
/// This function is equivalent to javascript's [`location.href`](https://developer.mozilla.org/en-US/docs/Web/API/Location/href) property
///
/// It returns the full url of the web page
///
/// If your web page's url is `https://www.example.com/path/name?key=2345` then it will return `https://www.example.com/path/name?key=2345`
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// Get the [`body`] property.
///
/// This function is equivalent to javascript's [`document.body`](https://developer.mozilla.org/en-US/docs/Web/API/Document/body) property
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```rust
/// use webru::create_element;
/// use webru::body;
///
/// // Create an element with `create_element` function
/// let h1: web_sys::Element = create_element("h1");
///
/// // get the `body` field
/// let body = body();
///
/// // insert the element `h1` into the <body>
/// body.append_child(&h1).unwrap();
///
/// assert_eq!(
/// body,
/// web_sys::window()
/// .unwrap()
/// .document()
/// .unwrap()
/// .body()
/// .unwrap()
/// );
///
/// ```
///
/// [`body`]: <https://developer.mozilla.org/en-US/docs/Web/API/Document/body>
/// Get the domain name of the website
///
/// This function is equivalent to javascript's [`location.hostname`](https://developer.mozilla.org/en-US/docs/Web/API/Location/hostname) property
///
/// If your website's url is `https://www.example.com/path/name?key=2345` then it will return `www.example.com`
///
/// If your website's url is `https://sub.example.org` then it will return `sub.example.org`
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// Get the the path of the website
///
/// This function is equivalent to javascript's [`location.pathname`](https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname) property
///
/// If your website's url is `https://www.example.com/path/name?key=234` then it will return `/path/name`
///
/// If your website's url is `https://www.example.com` then it will return `/`
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// Reloads the page
///
/// This function is equivalent to javascript's `location.reload()` function
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// Javascript [`alert`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) method
///
///
/// # Arguments
///
/// * `msg` - The text to display in the alert box
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```rust
/// use webru::alert;
///
/// // show an popup/alert message
/// alert("You do not have access to this site!");
/// ```
///
/// Javascript [`prompt`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) method
///
/// It returns the text the user entered in the dialog box
///
/// If the user clicks on the `Okay` button and if the user typed anything in the prompt, then it will return `Some("whatever user typed")`, else if the user didn't typed anything it will return `Some("")`
///
/// If the user clicks on the `Cancel` button then this function will return `None`
///
///
/// # Arguments
///
/// * `msg` - The text to display in the dialog box
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```rust
/// use webru::prompt;
/// use weblog::console_log;
///
/// // taking user's name
/// let name: Option<String> = prompt("What is your name?");
///
/// if let Some(name) = name {
/// console_log!("The name of the user is: ", name);
/// } else {
/// console_log!("The user didn't specify his/her name")
/// }
///
/// ```
/// Javascript [`Callback`](https://developer.mozilla.org/en-US/docs/Glossary/Callback_function)
///
/// [`web-sys`](https://crates.io/crates/web-sys) uses [`Function`] for taking closures from Rust and pass it to javascript.
///
/// So you can create a [`Closure`] by calling this function
/// and then call the `.as_ref().dyn_ref().unwrap()` method to convert it from [`Closure<dyn Fn()>`] to [`Function`]
///
/// It returns a [`Closure`] which can be used in any JavaScript function that takes a callback/closure as argument
///
/// You can see more info about using rust closures with wasm-bindgen [here](https://rustwasm.github.io/docs/wasm-bindgen/examples/closures.html)
///
///
/// # Arguments
///
/// * `handler` - A Rust closure
///
///
/// # Examples
///
/// Javascript's `setTimeout` function
///
/// ```rust
/// use wasm_bindgen::JsCast;
/// use webru::callback;
/// use weblog::console_log;
///
/// // Getting the `Window` object
/// let window = web_sys::window().unwrap();
///
/// // Getting the `wasm_bindgen::closure::Closure` by passing a Rust closure
/// let callback = callback(|| {
/// console_log!("Hello world");
/// });
///
/// // The first argument of `set_timeout_with_callback_and_timeout_and_arguments_0` is a `&::js_sys::Function`
/// // which can be used by converting the `Closure` into the `&::js_sys::Function`
/// // by calling `.as_ref().unchecked_ref()` method on it.
///
/// // The secend argument of `set_timeout_with_callback_and_timeout_and_arguments_0` is a timer,
/// // how much time do I want to delay
/// window
/// .set_timeout_with_callback_and_timeout_and_arguments_0(
/// callback.as_ref().unchecked_ref(),
/// 2000,
/// )
/// .unwrap();
///
/// // The call of `.forget()` is necessary. If we don't call this method, it will create a memory leak :(
/// callback.forget();
/// ```
///
///
/// `onclick` property
///
/// ```rust
/// use webru::{body, callback, create_element};
/// use web_sys::HtmlElement;
/// use wasm_bindgen::JsCast;
/// use weblog::console_log;
///
///
/// // Creating a <button>
/// let button: HtmlElement = create_element("button")
/// .dyn_ref::<HtmlElement>()
/// .unwrap()
/// .clone();
///
/// // setting the innerHTML field
/// button.set_inner_html("click me");
///
/// // This callback will be called the button will be clicked
/// let callback = callback(|| {
/// console_log!("You clicked the button");
/// });
///
/// // setting `onclick` property to the <button>
/// // converting the `Closure<dyn Fn()>` to `&js_sys::Function` by calling `.as_ref().dyn_ref().unwrap()` method
/// button.set_onclick(Some(callback.as_ref().dyn_ref().unwrap()));
///
/// // The call of `.forget()` is necessary. If we don't call this method, it will create a memory leak :(
/// callback.forget();
///
/// // inserting the button into the <body>
/// body().append_child(&button).unwrap();
///
/// // Now you the button is clicked, the text `You clicked the button` will be in console
/// ```
///
/// [`Closure`]: <https://docs.rs/wasm-bindgen/0.2.79/wasm_bindgen/closure/struct.Closure.html>
/// [`Closure<dyn Fn()>`]: <https://docs.rs/wasm-bindgen/0.2.79/wasm_bindgen/closure/struct.Closure.html>
/// [`Function`]: <https://docs.rs/js-sys/0.3.56/js_sys/struct.Function.html>
///
/// Javascript [`document.createElement`](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement) method
///
/// This function will create a new [`Element`](https://docs.rs/web-sys/0.3.56/web_sys/struct.Element.html) and return it.
///
/// Note that this function will create and return the element, not push to the DOM
///
///
/// # Arguments
///
/// * `element_name` - A string that specifies the type of element to be created.
///
///
/// # Panics
///
/// * This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// * This function will panic if the `element_name` is not a valid tag name. For example if you pass `1h` as argument, it will `panic`. But if you pass `h1` as argument, it will not `panic`
///
///
/// # Example
///
/// ```
/// use webru::{body, create_element};
///
/// // Create a <p> tag
/// let p = create_element("p");
///
/// // Create a <h1> tag
/// let h1 = create_element("h1");
///
/// // Create a <button> tag
/// let button = create_element("button");
///
/// // Create a <div> tag
/// let div = create_element("div");
///
/// // Pushing these elements into the DOM
/// body().append_child(&p).unwrap();
/// body().append_child(&h1).unwrap();
/// body().append_child(&button).unwrap();
/// body().append_child(&div).unwrap();
///```
/// Call a closure in a specific window width with media query
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// # Example
///
/// ```rust
/// use webru::{body, create_element, media_query};
///
/// let p = create_element("p");
/// p.set_inner_html("You cannot see me if you are less then 1000px");
///
/// // render the "p" tag into the DOM
/// body().append_child(&p).unwrap();
///
/// media_query(
/// // if the window width is less then 1000px
/// {
/// let p = p.clone();
/// move || {
/// p.set_inner_html("");
/// }
/// },
/// // if the window width is more then 1000px
/// move || {
/// p.clone()
/// .set_inner_html("You cannot see me if you are less then 1000px");
/// },
/// // the maxium width when the callback will be called
/// 1000,
/// );
/// ```