-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
581 lines (567 loc) · 30.6 KB
/
index.html
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<!DOCTYPE html>
<html>
<head>
<title>Functionality without compromises</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"/>
<script src="https://kit.fontawesome.com/c23a7997f1.js" crossorigin="anonymous"></script>
<script type="module" src="fetchEvents.js"></script>
<script type="module" src="httpHiddenMethod.js"></script>
</head>
<body>
<div class="container">
<h1 id="title" class="text-center">Functionality without compromises</h1>
<br/>
<p class="lead">Helpful utilities implemented with pure javascript modules.</p>
<br/>
<form>
<div class="form-group">
<h2>fetchEvents.js - Window Scope Fetch Events</h2>
<br/>
<p>Add event listeners to the window object and execute code when a fetch happens.</p>
<table class="table table-sm">
<caption>Available events</caption>
<thead>
<tr>
<th>Event Name</th>
<th>Description</th>
<th>Event Detail Properties</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>fetchStart</code></td>
<td>Right before the fetch is started</td>
<td><code>event.detail.url, event.detail.init</code></td>
</tr>
<tr>
<td><code>fetchResponseSuccess</code></td>
<td>When the response status is in 200-299 range.</td>
<td><code>event.detail.url, event.detail.init, event.detail.response</code></td>
</tr>
<tr>
<td><code>fetchResponseError</code></td>
<td>When the response status is in 400-599 range.</td>
<td><code>event.detail.url, event.detail.init, event.detail</code></td>
</tr>
<tr>
<td><code>fetchError</code></td>
<td>When there was any network error.</td>
<td><code>event.detail.url, event.detail.init, event.detail.error</code></td>
</tr>
<tr>
<td><code>fetchComplete</code></td>
<td>Final event independently if there was an error or response.</td>
<td><code>event.detail.url, event.detail.init, event.detail.response (if there was a response), event.detail.error (if there was an error)</code></td>
</tr>
</tbody>
</table>
<p>Example:</p>
<button type="button" onclick="fetchUrl('GET', 'data.json', 'logFetch')" class="btn btn-primary">
Fetch numbers
</button>
<button type="button" onclick="fetchUrl('GET', 'example.json', 'logFetch')" class="btn btn-primary">
Fetch non existing data
</button>
<br/>
<br/>
<p id="logFetch" class="font-italic"></p>
<br/>
<h4>
Use it:
</h4>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
<-- Include de javascript in the page -->
<br/>
<script type="module" src="fetchEvents.js"></script>
</code>
</p>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
//Register listeners in javascript
<br/>
window.addEventListener("fetchStart", event => {});
<br/>
window.addEventListener("fetchResponseSuccess", event => {});
<br/>
window.addEventListener("fetchError", event => {});
</code>
</p>
<div>
<div class="float-right">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">CDN</span>
</div>
<input type="text" class="form-control" value="https://cdn.jsdelivr.net/gh/hmvictor/[email protected]/fetchEvents.js" size="90" readonly="readonly">
<div class="input-group-append">
<button type="button" class="btn btn-info btn-sm copyInput">
<i class="far fa-clipboard"></i>
</button>
</div>
</div>
<a href="fetchEvents.js" download="fetchEvents.js" class="btn btn-success float-right" role="button">
<i class="fas fa-file-download"></i>
Download fetchEvents.js
</a>
</div>
</div>
<br/>
<br/>
<br/>
</div>
<script type="module">
window.addEventListener("fetchStart", event => {
if(window.logId) {
document.getElementById(window.logId).innerHTML="Fetch start. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]";
}
});
window.addEventListener("fetchResponseSuccess", event => {
if(window.logId) {
event.detail.response.json().then(jsonResponse => {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response success. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Response: "+jsonResponse+".";
});
}
});
window.addEventListener("fetchResponseError", event => {
if(window.logId) {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response error. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Status: ["+event.detail.response.status+"]";
}
});
window.addEventListener("fetchError", event => {
if(window.logId) {
document.getElementById(window.logId).innerHTML+="<br/>Fetch error. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Error: "+event.detail.error;
}
});
</script>
<div class="form-group">
<h2>httpHiddenMethod.js - Hidden Method Param</h2>
<br/>
<p>Use a hidden method param in your application when an HTTP PUT or DELETE method is not allowed in a host or server (maybe because of security policies not controlled by you).</p>
<p>The HTTP method is converted to POST and a param is used instead.</p>
<p>Example:</p>
<button type="button" onclick="fetchUrl('DELETE', 'data.json', 'logHiddenMethod')" class="btn btn-primary">
Fetch with PUT method.
</button>
<br/>
<br/>
<p id="logHiddenMethod" class="font-italic"></p>
<br/>
<h4>
Use it:
</h4>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
<-- Include the javascript in the page -->
<br/>
<script type="module" src="httpHiddenMethod.js"></script>
<br/>
<br/>
<-- Default param name is _method but you can change it with a meta tag with named hiddenMethodParam -->
<br/>
<meta name="hiddenMethodParam" content="_httpMethod"/>
</code>
</p>
<div>
<div class="float-right">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">CDN</span>
</div>
<input type="text" class="form-control" value="https://cdn.jsdelivr.net/gh/hmvictor/[email protected]/httpHiddenMethod.js" size="90" readonly="readonly">
<div class="input-group-append">
<button type="button" class="btn btn-info btn-sm copyInput">
<i class="far fa-clipboard"></i>
</button>
</div>
</div>
<a href="httpHiddenMethod.js" download="httpHiddenMethod.js" class="btn btn-success float-right" role="button">
<i class="fas fa-file-download"></i>
Download httpHiddenMethod.js
</a>
</div>
</div>
<br/>
</div>
<br/>
<br/>
<br/>
<br/>
<div class="form-group">
<h2>hashLocationTracker.js - Module for Hash Location Functions</h2>
<br/>
<p>
Call functions when the hash location of the windows is changed.
</p>
<p>
<code>hashTracker.given("#/init").then(fn);</code>
</p>
<p id="exampleLabel">Example:</p>
<a href="#/init" class="btn btn-link">
Init
</a>
<br/>
<a href="#/hello/world" class="btn btn-link">
Hello
</a>
<br/>
<br/>
<h4>
Use it:
</h4>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
/*
<br/>
Import the module. See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules</a>
<br/>
Your code has to be a module itself in order to use modules. For inline scripts use: <script type="module"> <em>javascript code</em> </script>
<br/>
*/
<br/>
import {hashLocationTracker} from "./hashLocationTracker.js";
<br/>
<br/>
/*
<br/>
Register function to execute for a regexp pattern, start and end line characters are addded automatically to match
the whole hash location.
When invoked, the function receives the match result to extract groups for parameterized locations.
<br/>
*/
<br/>
hashLocationTracker.given("#/index").then(matchResult => {});
<br/>
<br/>
//Execute when the page is loaded or after some kind of page initialization.
<br/>
hashLocationTracker.processCurrent();
</code>
</p>
<div>
<div class="float-right">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">CDN</span>
</div>
<input type="text" class="form-control" value="https://cdn.jsdelivr.net/gh/hmvictor/[email protected]/hashLocationTracker.js" size="90" readonly="readonly">
<div class="input-group-append">
<button type="button" class="btn btn-info btn-sm copyInput">
<i class="far fa-clipboard"></i>
</button>
</div>
</div>
<a href="hashLocationTracker.js" download="hashLocationTracker.js" class="btn btn-success float-right" role="button">
<i class="fas fa-file-download"></i>
Download hashLocationTracker.js
</a>
</div>
</div>
<br/>
<br/>
</div>
<script type="module">
import {hashLocationTracker} from "./hashLocationTracker.js";
hashLocationTracker.given("#/init").then(()=> {
document.getElementById("exampleLabel").innerHTML="Init";
});
hashLocationTracker.given("#/hello/(.+)").then(matchResult => {
document.getElementById("exampleLabel").innerHTML="Hello, "+matchResult[1]+"!";
});
hashLocationTracker.given("").then(matchResult => {
document.getElementById("exampleLabel").innerHTML="Example:";
});
hashLocationTracker.matchNow();
</script>
<br/>
<br/>
<div class="form-group">
<h2>fetchUrlRewriter.js - URL fetch rewriter.</h2>
<br/>
<p>
Add a base prefix domain/context to relative urls invoked by fetch.
</p>
<br/>
<button type="button" onclick="fetchRewrite('GET', 'data.json', 'logUrlRewriter')" class="btn btn-primary">
Fetch
</button>
<br/>
<br/>
<p id="logUrlRewriter" class="font-italic"></p>
<br/>
<h4>
Use it:
</h4>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
/*
<br/>
If you want to configure the prefix in javascript. Import it in an inline script module.
<br/>
Import the module. See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules</a>
<br/>
Your code has to be a module itself in order to use modules. For inline scripts use: <script type="module"> <em>javascript code</em> </script>
<br/>
*/
<br/>
import {urlRewriter} from "./fetchUrlRewriter.js";
<br/>
<br/>
//Javascript
<br/>
urlRewriter.prefix="https://my.domain.com/hello-application";
<br/>
</code>
</p>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
<-- If you want to configure in HTML. -->
<br/>
<br/>
<-- Define the prefix with a meta tag named fetchUrlPrefix -->
<br/>
<meta name="fetchUrlPrefix" content="https://my.domain.com/hello-application"/>
<br/>
<br/>
<-- Import the module with a script element -->
<br/>
<script type="module" src="fetchUrlRewriter.js"></script>
</code>
</p>
<div>
<div class="float-right">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">CDN</span>
</div>
<input type="text" class="form-control" value="https://cdn.jsdelivr.net/gh/hmvictor/[email protected]/fetchUrlRewriter.js" size="90" readonly="readonly">
<div class="input-group-append">
<button type="button" class="btn btn-info btn-sm copyInput">
<i class="far fa-clipboard"></i>
</button>
</div>
</div>
<a href="fetchUrlRewriter.js" download="fetchUrlRewriter.js" class="btn btn-success float-right" role="button">
<i class="fas fa-file-download"></i>
Download fetchUrlRewriter.js
</a>
</div>
</div>
<br/>
</div>
<script type="module">
import {urlRewriter} from "./fetchUrlRewriter.js";
window.fetchRewrite = function(method, url, logId) {
urlRewriter.prefix=window.location.protocol+"//"+window.location.host+(window.location.host.indexOf("localhost") === -1? "/directJavascripts/": "/dnx-runner/directJs");
let effectiveUrl = urlRewriter.rewrite(url);
let promise=window.fetchUrl(method, url, logId);
urlRewriter.prefix=null;
return promise;
};
</script>
<br/>
<br/>
<div class="form-group">
<h2>tasks.js - Task Class</h2>
<br/>
<p>
A class to encapsulate a long running function. You can give some feedback or block some UI components while the task is running.
It has a status property and supports event listeners.
</p>
<p>
<strong>Tip:</strong> You can bind the status of the task with an element attribute in your favorite framework.
</p>
<p>
<code>
<button :disabled="task.status === Task.Status.RUNNING" type="button"></button>
</code>
</p>
<button id="taskButton" :disabled="task.status === Task.Status.RUNNING" type="button" onclick="executeTask()" class="btn btn-primary">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="description">
Task Description
</span>
</button>
<br/>
<br/>
<p id="logTasks" class="font-italic"></p>
<br/>
<h4>
Use it:
</h4>
<p class="text-light bg-dark p-3">
<button type="button" class="btn btn-info btn-sm float-right copyCode">
Copy
</button>
<code>
/*
<br/>
Import the module. See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules</a>
<br/>
Your code has to be a module itself in order to use modules. For inline scripts use: <script type="module"> <em>javascript code</em> </script>
<br/>
*/
<br/>
import {Task} from "./tasks.js";
<br/>
<br/>
// Create a task with a description and a function that returns a promise (fetch, for example).
<br/>
const getUserTask=new Task("Get Users", id => fetch("user"+Id+".json")); // Receives a parameter.
<br/>
<br/>
// Starts the task, this could be when a button is clicked too.
<br/>
getUsersTask.start(1); // Parameters are passed to the function in the task.
</code>
</p>
<div>
<div class="float-right">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">CDN</span>
</div>
<input type="text" class="form-control" value="https://cdn.jsdelivr.net/gh/hmvictor/[email protected]/tasks.js" size="90" readonly="readonly">
<div class="input-group-append">
<button type="button" class="btn btn-info btn-sm copyInput">
<i class="far fa-clipboard"></i>
</button>
</div>
</div>
<a href="tasks.js" download="tasks.js" class="btn btn-success float-right" role="button">
<i class="fas fa-file-download"></i>
Download tasks.js
</a>
</div>
</div>
<br/>
</div>
<script type="module">
import {Task} from "./tasks.js";
const task=new Task("Get Users", () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
fetchRewrite("GET", "/users.json")
.then(response =>{
response.json().then(data => {
resolve(data);
});
}, error =>{
reject(error);
});
}, 2000);
});
});
document.getElementById("taskButton").querySelector(".description").innerHTML=task.description;
document.getElementById("taskButton").querySelector("[role=status]").style.display='none';
task.addEventListener("start", (event) => {
document.getElementById("logTasks").innerHTML="";
document.getElementById("taskButton").querySelector(".description").innerHTML="Getting users";
document.getElementById("taskButton").disabled=true;
document.getElementById("taskButton").querySelector("[role=status]").style.display='';
console.log(task.status.description);
});
task.addEventListener("complete", () => {
document.getElementById("taskButton").disabled=false;
document.getElementById("taskButton").querySelector(".description").innerHTML=task.description;
document.getElementById("taskButton").querySelector("[role=status]").style.display='none';
});
task.addEventListener("fail", () => {
document.getElementById("taskButton").disabled=false;
document.getElementById("taskButton").querySelector(".description").innerHTML=task.description;
document.getElementById("taskButton").querySelector("[role=status]").style.display='none';
});
window.executeTask=function() {
task.start().then(users => {
document.getElementById("logTasks").innerHTML="";
let counter=0;
for(let user of users) {
if(counter > 0) {
document.getElementById("logTasks").innerHTML+=", ";
}
document.getElementById("logTasks").innerHTML+=user.name;
counter++;
}
});
};
</script>
</form>
</div>
<br/>
<br/>
<script type="module">
window.fetchUrl=function(method, url, logId) {
window.logId=logId;
return window.fetch(url, {
method: method
});
}
function selectText(node) {
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
} else {
console.warn("Could not select text in node: Unsupported browser.");
}
}
document.querySelectorAll("button.copyCode").forEach(element =>{
element.addEventListener("click", () => {
selectText(element.parentNode.querySelector("code"));
document.execCommand("copy");
});
});
document.querySelectorAll("button.copyInput").forEach(element =>{
element.addEventListener("click", () => {
element.parentNode.parentNode.querySelector("input").select();
document.execCommand("copy");
});
});
function jsonPromise(promiseFetch){
return new Promise((resolve, reject) => {
promiseFetch
.then(response => {
if(response.status >= 200 && response.status < 300) {
response.json().then(data => {
resolve(data);
});
} else {
response.json().then(data => {
//process error data
reject(data);
});
}
}, error => {
reject(error);
});
});
}
</script>
</body>
</html>