input-duration 2.0.12 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,671 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>JSDoc: Source: index.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"> </script>
8
+ <script src="scripts/prettify/lang-css.js"> </script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14
+ </head>
15
+
16
+ <body>
17
+
18
+ <div id="main">
19
+
20
+ <h1 class="page-title">Source: index.js</h1>
21
+
22
+
23
+
24
+
25
+
26
+
27
+ <section>
28
+ <article>
29
+ <pre class="prettyprint source linenums"><code>/**
30
+ * @fileoverview INPUT-DURATION
31
+ * @file INPUT-DURATION
32
+ * @author Stephan Luis
33
+ * @see &lt;a href="https://www.npmjs.com/package/input-duration">NPM input-duration&lt;/a>
34
+ * @see &lt;a href="https://github.com/StephanLuis/input-duration">GitHub input-duration&lt;/a>
35
+ * @copyright MIT License, Copyright (c) 2022
36
+ */
37
+
38
+
39
+
40
+ /**
41
+ * Class for the Input-Duration webcomponent
42
+ * @class InputDuration
43
+ * @extends HTMLElement
44
+ */
45
+
46
+ class InputDuration extends HTMLElement {
47
+ constructor() {
48
+
49
+ super();
50
+
51
+ this.myElement = this.attachShadow({ mode: 'open' });
52
+
53
+ // needed for setting inputs when value attribute is added to chronlyHMS
54
+ // &lt;chronly-hms id="bobPrepop" value="99:09:09.999">&lt;/chronly-hms>
55
+
56
+ this.value = this.getAttribute('value');
57
+
58
+ this.boot();
59
+
60
+ } // end constructor
61
+
62
+
63
+ /**
64
+ * get the input-duration value, pulls values from each input
65
+ * so in js use var x = document.querySelector("#bobPrepop").value;
66
+ * @return {duration}
67
+ */
68
+
69
+ get value() {
70
+
71
+ // return this.getAttribute('value');
72
+
73
+ if (this.myElement.querySelector('input[name="startHours"]') !== null) {
74
+
75
+ return this.myElement.querySelector('input[name="startHours"]').value +
76
+ ':' + this.myElement.querySelector('input[name="startMinutes"]').value +
77
+ ':' + this.myElement.querySelector('input[name="startSeconds"]').value +
78
+ '.' + this.myElement.querySelector('input[name="startMilliSecs"]').value;
79
+
80
+ }
81
+ else {
82
+
83
+ return null;
84
+
85
+ }
86
+ }
87
+
88
+
89
+ /**
90
+ * set the input-duration value
91
+ * so in js use document.querySelector("#bobPrepop").value = "05:05:05.005"
92
+ * @return {duration}
93
+ * @param {duration} can be html attribute or set in js
94
+ */
95
+
96
+ set value(val) {
97
+
98
+ // for setting the value in js (like document.querySelector("#bobPrepop").value = "05:05:05.005")
99
+ if (val) {
100
+
101
+
102
+ console.log(val);
103
+
104
+
105
+ if (/(^[0-9][0-9]):([0-5][0-9]):([0-5][0-9])\.([0-9][0-9][0-9]$)/.exec(val)) {
106
+
107
+
108
+ // split val
109
+
110
+ var timeArray = val.split(/[.:]+/);
111
+
112
+ console.log('time array is ' + timeArray[0] + ':' + timeArray[1] + ':' + timeArray[2] + ':' + timeArray[3]);
113
+
114
+ console.log('this.id = ' + this.id);
115
+
116
+ setTimeout(() => {
117
+
118
+ this.myElement.querySelector('input[name="startHours"]').value = timeArray[0];
119
+ this.myElement.querySelector('input[name="startMinutes"]').value = timeArray[1];
120
+ this.myElement.querySelector('input[name="startSeconds"]').value = timeArray[2];
121
+ this.myElement.querySelector('input[name="startMilliSecs"]').value = timeArray[3];
122
+
123
+ }, 0);
124
+
125
+
126
+ this.setAttribute('value', val);
127
+
128
+ }
129
+
130
+ else {
131
+
132
+ console.warn(`The specified value ${val} does not conform to the required format. The format is "HH:mm:ss.SSS" where HH is 00-99, mm is 00-59, ss is 00-59, and SSS is 000-999.`);
133
+
134
+ }
135
+
136
+ // I think this is for empty string to clear value
137
+ // gives a False bool and removes value from Webcomponent chronlyHMS
138
+
139
+ } else {
140
+
141
+ setTimeout(() => {
142
+
143
+ this.myElement.querySelector('input[name="startHours"]').value = '00';
144
+ this.myElement.querySelector('input[name="startMinutes"]').value = '00';
145
+ this.myElement.querySelector('input[name="startSeconds"]').value = '00';
146
+ this.myElement.querySelector('input[name="startMilliSecs"]').value = '000';
147
+
148
+ }, 0);
149
+
150
+ this.removeAttribute('value');
151
+
152
+ }
153
+ }
154
+
155
+
156
+ /**
157
+ * Called from constructor, sets up the html and event listeners
158
+ * @method
159
+ */
160
+
161
+ boot() {
162
+
163
+ this.addHTML();
164
+
165
+ // // set up event listeners
166
+ this.add0s();
167
+ this.addNumberLooping();
168
+ this.addArrowKeyInput();
169
+ this.addNumericInput();
170
+ this.addClicksToActivate();
171
+ this.nonNumericBugInHTML();
172
+
173
+ }
174
+
175
+
176
+ /**
177
+ * eventlistener that adds arrow key input of values (up/down) and place toggling (left/right) triggered by
178
+ * the change event of the input
179
+ * @method
180
+ * @see boot()
181
+ */
182
+
183
+ addArrowKeyInput() {
184
+
185
+ console.log('1 this: ' + this);
186
+ console.log('1 this.myElement: ' + this.myElement);
187
+ console.log('1 this.myElement active: ' + this.myElement);
188
+
189
+ var mE = this.myElement;
190
+
191
+ // Left and Right arrow key toggle between HMSmS
192
+ // this depends on data-attributes ex. data-tp="1" in HTML
193
+
194
+ this.myElement.querySelector("div.timeCase")
195
+ .addEventListener('keydown', function (event) {
196
+
197
+ var et = event.target;
198
+ var tp_active = mE.activeElement.dataset.tp;
199
+
200
+ switch (true) {
201
+
202
+ case event.key == "ArrowLeft":
203
+
204
+ if (tp_active >= 2) {
205
+ tp_active--;
206
+ }
207
+
208
+ et.parentElement.querySelector("[data-tp='" + tp_active + "']").focus();
209
+ setTimeout(function () { et.parentElement.querySelector("[data-tp='" + tp_active + "']").select(); }, 5);
210
+
211
+ break;
212
+
213
+ case event.key == "ArrowRight":
214
+ // tp_active = this.myElement.shadowRoot.activeElement.dataset.tp;
215
+
216
+ if (tp_active &lt;= 3) {
217
+ tp_active++;
218
+ }
219
+
220
+ et.parentElement.querySelector("[data-tp='" + tp_active + "']").focus();
221
+ setTimeout(function () { et.parentElement.querySelector("[data-tp='" + tp_active + "']").select(); }, 5);
222
+
223
+ break;
224
+
225
+ // these keep the input selected
226
+ case event.key == "ArrowUp":
227
+
228
+ setTimeout(function () { mE.activeElement.select(); }, 5);
229
+
230
+ break;
231
+
232
+ case event.key == "ArrowDown":
233
+
234
+ setTimeout(function () { mE.activeElement.select(); }, 5);
235
+
236
+ break;
237
+
238
+ // this is good for testing, to determine whether a key press is detected
239
+ case /^([0-9]?)$/.test(event.key):
240
+
241
+ break;
242
+
243
+ }
244
+ });
245
+ }
246
+
247
+
248
+ /**
249
+ * updates inputs-duration marked with shadow html and css
250
+ * @method
251
+ * @see boot()
252
+ */
253
+
254
+ addHTML() {
255
+
256
+ var spanIn = document.createElement("div");
257
+
258
+ spanIn.classList.add('timeCase');
259
+
260
+ var template = document.createElement('template');
261
+
262
+ template.innerHTML = `
263
+ &lt;input id="inShadow" type="number" class="sH D2 ts_digit" name="startHours" data-tp="1" min="-1" max="100" value="00" >
264
+ &lt;span class="bds-h">:&lt;/span>
265
+ &lt;input type="number" class="sM D2 ts_digit" name="startMinutes" data-tp="2" min="-1" max="60" value="00">
266
+ &lt;span class="bds-m">:&lt;/span>
267
+ &lt;input type="number" class="sS D2 ts_digit" name="startSeconds" data-tp="3" min="-1" max="60" value="00">
268
+ &lt;span class="bds-s">.&lt;/span>
269
+ &lt;input type="number" class="sMS D3 ts_digit" name="startMilliSecs" data-tp="4" min="-10" max="1010" step="10" value="000">
270
+ &lt;div id="svgContainer">
271
+ &lt;svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
272
+ focusable="false" data-prefix="fal" data-icon="stopwatch" class="svg-inline--fa fa-stopwatch fa-w-14"
273
+ role="img" viewBox="0 0 448 512">
274
+ &lt;path xmlns="http://www.w3.org/2000/svg" fill="currentColor" d="M393.9 184l22.6-22.6c4.7-4.7 4.7-12.3 0-17l-17-17c-4.7-4.7-12.3-4.7-17 0l-20.7 20.7c-31.1-27.5-70.4-45.9-113.8-50.8V48h28c6.6 0 12-5.4 12-12V12c0-6.6-5.4-12-12-12H172c-6.6 0-12 5.4-12 12v24c0 6.6 5.4 12 12 12h28v49.4C96.4 109.3 16 197.2 16 304c0 114.9 93.1 208 208 208s208-93.1 208-208c0-44.7-14.1-86.1-38.1-120zM224 464c-88.4 0-160-71.6-160-160s71.6-160 160-160 160 71.6 160 160-71.6 160-160 160zm12-112h-24c-6.6 0-12-5.4-12-12V204c0-6.6 5.4-12 12-12h24c6.6 0 12 5.4 12 12v136c0 6.6-5.4 12-12 12z"/>
275
+ &lt;/svg>
276
+ &lt;/div>
277
+ &lt;style>
278
+ *:focus {
279
+ outline: none;
280
+ }
281
+
282
+
283
+ div.timeCase{
284
+ display: inline-flex;
285
+ flex-direction: row;
286
+ max-width: 300px;
287
+ padding: 3px;
288
+ border-style: solid;
289
+ border-width: 1px;
290
+ border-radius: 2px;
291
+ }
292
+
293
+
294
+ /* Each input width set and blinking cursor 'caret' removed */
295
+ div.timeCase input{
296
+ caret-color: transparent;
297
+ width: 15px;
298
+ padding-top:1px;
299
+ }
300
+
301
+
302
+ div.timeCase .D3{
303
+ width: 25px;
304
+ }
305
+
306
+
307
+ /* Hiding the up and down arrow */
308
+ /*https://www.w3schools.com/howto/howto_css_hide_arrow_number.asp*/
309
+ /* Chrome, Safari, Edge, Opera */
310
+
311
+ input::-webkit-outer-spin-button,
312
+ input::-webkit-inner-spin-button {
313
+ -webkit-appearance: none;
314
+ margin: 0;
315
+ }
316
+
317
+
318
+ /* Firefox */
319
+
320
+ input[type=number] {
321
+ /* -moz-appearance: textfield; */
322
+ -moz-appearance: textarea;
323
+ -webkit-appearance: textarea;
324
+ }
325
+
326
+
327
+ div.timeCase:focus-within {
328
+ border: 2px solid black;
329
+ padding: 2px;
330
+ }
331
+
332
+
333
+ .ts_digit {
334
+ border: none;
335
+ }
336
+
337
+ /* between digit span */
338
+ .bds{
339
+ padding-bottom: 1px;
340
+ }
341
+
342
+ #svgContainer {
343
+ width: 12px;
344
+ height: 12px;
345
+ padding-top: 1px;
346
+ padding-right: 2px;
347
+ color: black;
348
+ }
349
+
350
+ *:disabled {
351
+ background-color: grey;
352
+ color: lightgrey;
353
+ opacity: .6;
354
+ }
355
+ &lt;/style> `
356
+
357
+ spanIn.appendChild(template.content);
358
+
359
+ this.myElement.appendChild(spanIn);
360
+
361
+ }
362
+
363
+
364
+ /**
365
+ * eventlistener that adds number looping for up and down keys triggered by
366
+ * the change event of the input
367
+ * @method
368
+ * @see boot()
369
+ */
370
+
371
+ addNumberLooping() {
372
+
373
+ // Numeric Value looping (eventually this can be 'parameterised' refactored)
374
+ // ex. 99 uparrow to 0 hours, 59 uparrow to 0 minutes and seconds, 990 uparrow to 010, downarrow 000 milliseconds
375
+
376
+ const sH = this.myElement.querySelector("input.sH");
377
+ sH.addEventListener('change', function (event) {
378
+
379
+ if (sH.value == 100) {
380
+ sH.value = '00';
381
+ }
382
+ if (sH.value == -1) {
383
+ sH.value = 99;
384
+ }
385
+
386
+ });
387
+
388
+ const sM = this.myElement.querySelector("input.sM");
389
+ sM.addEventListener('change', function (event) {
390
+
391
+ if (sM.value == 60) {
392
+ sM.value = '00';
393
+ }
394
+ if (sM.value == -1) {
395
+ sM.value = 59;
396
+ }
397
+
398
+ });
399
+
400
+ const sS = this.myElement.querySelector("input.sS");
401
+ sS.addEventListener('change', function (event) {
402
+
403
+ if (sS.value == 60) {
404
+ sS.value = '00';
405
+ }
406
+
407
+ if (sS.value == -1) {
408
+ sS.value = 59;
409
+ }
410
+
411
+ });
412
+
413
+ const sMS = this.myElement.querySelector("input.sMS");
414
+ sMS.addEventListener('change', function (event) {
415
+
416
+ if (sMS.value == 1000) {
417
+ sMS.value = "010";
418
+ }
419
+
420
+ if (sMS.value == -10) {
421
+ sMS.value = 990;
422
+ }
423
+
424
+ if (sMS.value == 0) {
425
+ }
426
+
427
+ });
428
+ }
429
+
430
+
431
+ /**
432
+ * eventlistener that adds number input for keyed values triggered by the keyup event
433
+ * @method
434
+ * @see boot()
435
+ */
436
+
437
+ addNumericInput() {
438
+
439
+ const mE = this.myElement;
440
+
441
+ // https://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input
442
+ // Digit input 0-9 (typed in by user)
443
+
444
+ function setInputFilter(inputElement, inputFilter) {
445
+ // this sets a listener and immediately updates the value of the input number (note: can update this from input text, like by removing setSelectionRange )
446
+
447
+ console.log('inputElement: ' + inputElement);
448
+ window.inputElement = inputElement;
449
+
450
+ inputElement.addEventListener('keyup', function (event) {
451
+
452
+ window.kue = event;
453
+
454
+ if (/^([0-9]?)$/.test(event.key)) {
455
+ if (inputFilter(this.value)) {
456
+ this.oldValue = this.value;
457
+ this.oldSelectionStart = this.selectionStart;
458
+ this.oldSelectionEnd = this.selectionEnd;
459
+ } else if (this.hasOwnProperty("oldValue")) {
460
+ this.value = this.oldValue;
461
+ // this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
462
+ } else {
463
+ this.value = "";
464
+ }
465
+ }
466
+ });
467
+ }
468
+
469
+ // three filters, hours or minutes and seconds (are the same) but milliseconds is different
470
+
471
+ var sH = this.myElement.querySelector('input.sH');
472
+
473
+ setInputFilter(sH, function (value) {
474
+
475
+ var newV = /^([0-9]?|[0-9][0-9]?)$/.test(value);
476
+
477
+
478
+ if (value.length === 2) {
479
+
480
+ mE.querySelector('input.sM').focus();
481
+ mE.querySelector('input.sM').select();
482
+
483
+ }
484
+
485
+ return newV;
486
+
487
+ });
488
+
489
+ var sM = this.myElement.querySelector('input.sM');
490
+ setInputFilter(sM, function (value) {
491
+
492
+
493
+ var newV = /^([0-9]?|[0-5][0-9]?)$/.test(value);
494
+
495
+ if (value > 5 || value.length === 2) {
496
+
497
+ mE.querySelector('input.sS').focus();
498
+ mE.querySelector('input.sS').select();
499
+
500
+ }
501
+
502
+ return newV;
503
+
504
+ });
505
+
506
+ var sS = this.myElement.querySelector('input.sS');
507
+ setInputFilter(sS, function (value) {
508
+
509
+ var newV = /^([0-9]?|[0-5][0-9]?)$/.test(value);
510
+
511
+ if (value > 5 || value.length === 2) {
512
+
513
+ mE.querySelector('input.sMS').focus();
514
+ mE.querySelector('input.sMS').select();
515
+
516
+ }
517
+
518
+ return newV;
519
+
520
+ });
521
+
522
+
523
+ var sMS = this.myElement.querySelector('input.sMS');
524
+ setInputFilter(sMS, function (value) {
525
+
526
+ var newV = /^([0-9]?|[0-9][0-9]?|[0-9][0-9][0-9]?)$/.test(value);
527
+
528
+
529
+ if (value.length === 3) {
530
+
531
+ mE.querySelector('input.sMS').blur();
532
+
533
+ }
534
+
535
+ return newV;
536
+
537
+ });
538
+ }
539
+
540
+
541
+ /**
542
+ * eventlistener that adds 0s to preface numbers less than 10 triggered by
543
+ * the change event of the input
544
+ * @method
545
+ * @see boot()
546
+ */
547
+
548
+ add0s() {
549
+
550
+ // input for hours, minutes, seconds
551
+ this.myElement.querySelectorAll('input').forEach(i => i.addEventListener('change', function () {
552
+ if (!isNaN(this.value) &amp;&amp; this.value.length === 1) {
553
+ this.value = '0' + this.value;
554
+ }
555
+ }));
556
+
557
+ // milliseconds
558
+ this.myElement.querySelectorAll("input.sMS").forEach(i => i.addEventListener('change', function () {
559
+
560
+ if (!isNaN(this.value) &amp;&amp; this.value.length === 2) {
561
+ this.value = '0' + this.value;
562
+ }
563
+
564
+ }));
565
+
566
+ }
567
+
568
+
569
+ /**
570
+ * eventlistener that adds click to activate input of values by the keyboard triggered by
571
+ * the click event of the input
572
+ * @method
573
+ * @see boot()
574
+ */
575
+
576
+ addClicksToActivate() {
577
+
578
+ this.myElement.querySelectorAll('input').forEach(i => i.addEventListener("click", function () { this.select(); }))
579
+ }
580
+
581
+
582
+ /**
583
+ * eventlistener that adds key filtering to stop letters (or other non-numerics) from being input triggered by
584
+ * the keypress event of the input
585
+ * @method
586
+ * @see boot()
587
+ */
588
+
589
+ nonNumericBugInHTML() {
590
+
591
+ this.myElement.querySelectorAll(".timeCase input").forEach(item =>
592
+
593
+ item.addEventListener('keypress', function (e) {
594
+
595
+ e = e || window.event;
596
+ var charCode = (typeof e.which == "undefined") ? e.keyCode : e.which;
597
+ var charStr = String.fromCharCode(charCode);
598
+
599
+ if (!charStr.match(/^[0-9]+$/))
600
+ e.preventDefault();
601
+
602
+ }));
603
+ }
604
+
605
+
606
+ // formatting and behaviour options
607
+
608
+ /**
609
+ * disables entire control => read-only
610
+ * @method
611
+ */
612
+
613
+ disable() {
614
+
615
+ var closure = this;
616
+
617
+ // disable chronly inputs on page
618
+
619
+ closure.shadowRoot.querySelectorAll(".timeCase input").forEach(el => el.disabled = 'true')
620
+
621
+ }
622
+
623
+
624
+ /**
625
+ * removes the hours, so the display is mm:ss.uuu
626
+ * @method
627
+ */
628
+
629
+ noHours() {
630
+
631
+ var closure = this;
632
+
633
+ // hide chronly hours inputs on page
634
+
635
+ closure.shadowRoot.querySelectorAll(".timeCase input.sH").forEach(el => el.style.display = 'none');
636
+
637
+
638
+ // hide the hours colon on page
639
+
640
+ closure.shadowRoot.querySelectorAll(".timeCase span.bds-h").forEach(el => el.style.display = 'none');
641
+
642
+ }
643
+ }
644
+ // end class
645
+
646
+
647
+ export default customElements.define('input-duration', InputDuration);
648
+
649
+ // an option is to make all above an IIFE</code></pre>
650
+ </article>
651
+ </section>
652
+
653
+
654
+
655
+
656
+ </div>
657
+
658
+ <nav>
659
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InputDuration.html">InputDuration</a></li></ul>
660
+ </nav>
661
+
662
+ <br class="clear">
663
+
664
+ <footer>
665
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Jul 06 2023 16:38:37 GMT+0000 (GMT)
666
+ </footer>
667
+
668
+ <script> prettyPrint(); </script>
669
+ <script src="scripts/linenumber.js"> </script>
670
+ </body>
671
+ </html>