-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathkioskboard.js
953 lines (862 loc) · 45.3 KB
/
kioskboard.js
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*!
* KioskBoard - Virtual Keyboard ('https://github.com/furcan/KioskBoard')
* Version: 2.3.0
* Author: Furkan ('https://github.com/furcan')
* Copyright 2022 KioskBoard - Virtual Keyboard, MIT Licence ('https://opensource.org/licenses/MIT')*
*/
/* global define */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function () {
return factory(root);
});
} else if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory(root);
} else {
root.KioskBoard = factory(root);
}
})(typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this, function (window) {
'use strict';
// SSR check: begin
if (typeof window === 'undefined' && typeof window.document === 'undefined') {
return;
}
// SSR check: end
// KioskBoard: Internal CSS Codes: begin
var kioskBoardInternalCSSCodes = function () {
var internalCSS = '';
return internalCSS || null;
};
// KioskBoard: Internal CSS codes: end
// KioskBoard: Internal CSS: begin
var kioskBoardInternalCSS = function () {
if (kioskBoardInternalCSSCodes() !== null && !window.document.getElementById('KioskBoardInternalCSS')) {
var internalCSS = window.document.createElement('style');
internalCSS.id = 'KioskBoardInternalCSS';
internalCSS.innerHTML = kioskBoardInternalCSSCodes();
window.document.head.appendChild(internalCSS);
}
};
// KioskBoard: Internal CSS: end
// KioskBoard: Default Options: begin
var kioskBoardDefaultOptions = {
keysArrayOfObjects: null,
keysJsonUrl: null,
keysSpecialCharsArrayOfStrings: null,
keysNumpadArrayOfNumbers: null,
language: 'en',
theme: 'light', // "light" || "dark" || "flat" || "material" || "oldschool"
autoScroll: true,
capsLockActive: true,
allowRealKeyboard: false,
allowMobileKeyboard: false,
cssAnimations: true,
cssAnimationsDuration: 360,
cssAnimationsStyle: 'slide', // "slide" || "fade"
keysAllowSpacebar: true,
keysSpacebarText: 'Space',
keysFontFamily: 'sans-serif',
keysFontSize: '22px',
keysFontWeight: 'normal',
keysIconSize: '25px',
keysEnterText: 'Enter',
keysEnterCallback: undefined,
keysEnterCanClose: true,
};
var kioskBoardCachedKeys;
var kioskBoardNewOptions;
var kioskBoardGithubUrl = 'https://github.com/furcan/KioskBoard';
var kioskBoardSpecialCharacters = {
'0': '!',
'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': '`',
};
var kioskBoardNumpadKeysObject = {
'0': '7',
'1': '8',
'2': '9',
'3': '4',
'4': '5',
'5': '6',
'6': '1',
'7': '2',
'8': '3',
'9': '0',
};
var kioskBoardAllKeysNumbersObject = {
'0': '1',
'1': '2',
'2': '3',
'3': '4',
'4': '5',
'5': '6',
'6': '7',
'7': '8',
'8': '9',
'9': '0',
};
var kioskBoardTypes = {
All: 'all',
Keyboard: 'keyboard',
Numpad: 'numpad',
};
var kioskBoardPlacements = {
Bottom: 'bottom',
Top: 'top',
};
// KioskBoard: Default Options: end
// KioskBoard: Extend Options: begin
var kioskBoardExtendObjects = function () {
var extended = {};
var deep = false;
var i = 0;
if (Object.prototype.toString.call(arguments[0]) === '[object Boolean]') {
deep = arguments[0];
i++;
}
var merge = function (obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') {
extended[prop] = kioskBoardExtendObjects(extended[prop], obj[prop]);
} else {
extended[prop] = obj[prop];
}
}
}
};
for (; i < arguments.length; i++) {
merge(arguments[i]);
}
return extended;
};
// KioskBoard: Extend Options: end
// KioskBoard: Check Array of Objects: begin
var kioskBoardCheckArrayOfObjects = function (array) {
if (Array.isArray(array) && array.length > 0) {
var firstChild = array[0];
if (typeof firstChild === 'object' && !Array.isArray(firstChild)) {
for (var key in firstChild) {
if (Object.prototype.hasOwnProperty.call(firstChild, key)) {
return true;
}
}
}
}
return false;
};
// KioskBoard: Check Array of Objects: end
// KioskBoard: Console Error Function: begin
var kioskBoardConsoleError = function (errorMessage) {
return console.error('%c KioskBoard (Error) ', 'padding:2px;border-radius:20px;color:#fff;background:#f44336', '\n' + errorMessage);
};
// KioskBoard: Console Error Function: end
// KioskBoard: Console Log Function: begin
var kioskBoardConsoleLog = function (logMessage) {
return console.log('%c KioskBoard (Info) ', 'padding:2px;border-radius:20px;color:#fff;background:#00bcd4', '\n' + logMessage);
};
// KioskBoard: Console Log Function: end
// KioskBoard: Icons: begin
var kioskBoardIconBackspace = function (width, color) {
if (!width) { width = 25; }
if (!color) { color = '#707070'; }
var icon = ' <svg id="KioskBoardIconBackspace" xmlns="http://www.w3.org/2000/svg" width="' + width + '" height="' + width + '" viewBox="0 0 612 612" style="width:' + width + ';height:' + width + ';fill:' + color + ';"><path d="M561,76.5H178.5c-17.85,0-30.6,7.65-40.8,22.95L0,306l137.7,206.55c10.2,12.75,22.95,22.95,40.8,22.95H561c28.05,0,51-22.95,51-51v-357C612,99.45,589.05,76.5,561,76.5z M484.5,397.8l-35.7,35.7L357,341.7l-91.8,91.8l-35.7-35.7l91.8-91.8l-91.8-91.8l35.7-35.7l91.8,91.8l91.8-91.8l35.7,35.7L392.7,306L484.5,397.8z"/></svg>';
return icon;
};
var kioskBoardIconCapslock = function (width, color) {
if (!width) { width = 25; }
if (!color) { color = '#707070'; }
var icon = ' <svg id="KioskBoardIconCapslock" xmlns="http://www.w3.org/2000/svg" width="' + width + '" height="' + width + '" style="width:' + width + ';height:' + width + ';fill:' + color + ';shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 200 200"><path d="M61.8 148.97l76.4 0c6,0 10.91,4.9 10.91,10.9l0 27.24c0,5.99 -4.91,10.89 -10.91,10.89l-76.4 0c-6,0 -10.91,-4.9 -10.91,-10.89l0 -27.24c0,-6 4.91,-10.9 10.91,-10.9zm105.7 -60.38l-18.39 0 0 37.36c0,5.99 -4.91,10.89 -10.91,10.89l-76.4 0c-6,0 -10.91,-4.9 -10.91,-10.89l0 -37.36 -18.39 0c-2.65,0 -4.91,-1.47 -5.97,-3.89 -1.07,-2.42 -0.63,-5.08 1.16,-7.02l67.5 -73.57c1.28,-1.39 2.91,-2.11 4.81,-2.11 1.9,0 3.53,0.72 4.81,2.11l67.5 73.57c1.79,1.94 2.23,4.6 1.16,7.02 -1.06,2.42 -3.32,3.89 -5.97,3.89z"/></svg>';
return icon;
};
var kioskBoardIconSpecialCharacters = function (width, height, color) {
if (!width) { width = 50; }
if (!height) { width = 25; }
if (!color) { color = '#707070'; }
var icon = ' <svg id="KioskBoardIconSpecialCharacters" xmlns="http://www.w3.org/2000/svg" width="' + width + '" height="' + height + '" style="width:' + width + ';height:' + height + ';fill:' + color + ';shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 300 150"><path d="M34.19 79.43l1.99 -10.86 10.8 0 -1.96 10.86 -10.83 0zm264.98 -17.22l0 -9.63c0,-1.23 -1,-2.23 -2.24,-2.23l-74.48 0c-1.24,0 -2.24,1 -2.24,2.23l0 9.63c0,1.23 1,2.23 2.24,2.23l74.48 0c1.24,0 2.24,-1 2.24,-2.23zm0 35.22l0 -9.62c0,-1.23 -1,-2.23 -2.24,-2.23l-74.48 0c-1.24,0 -2.24,1 -2.24,2.23l0 9.62c0,1.23 1,2.24 2.24,2.24l74.48 0c1.24,0 2.24,-1.01 2.24,-2.24zm-153.98 -61.91l9.63 0c1.23,0 2.23,1.01 2.23,2.25l0 30.19 30.19 0c1.25,0 2.25,1.01 2.25,2.23l0 9.63c0,1.23 -1,2.23 -2.25,2.23l-30.19 0 0 30.19c0,1.25 -1,2.25 -2.23,2.25l-9.63 0c-1.23,0 -2.23,-1 -2.23,-2.25l0 -30.19 -30.19 0c-1.24,0 -2.25,-1 -2.25,-2.23l0 -9.63c0,-1.22 1.01,-2.23 2.25,-2.23l30.19 0 0 -30.19c0,-1.24 1,-2.25 2.23,-2.25zm-67.7 33.05c1.28,0 2.31,-1.03 2.31,-2.31l0 -9.2c0,-1.27 -1.03,-2.31 -2.31,-2.31l-13.93 0 2.95 -16.51c0.12,-0.68 -0.07,-1.37 -0.51,-1.89 -0.44,-0.53 -1.09,-0.83 -1.77,-0.83l-9.36 -0.01c0,0 0,0 0,0 -1.12,0 -2.08,0.8 -2.28,1.9l-3.12 17.34 -10.74 0 3.03 -16.49c0.12,-0.67 -0.06,-1.37 -0.5,-1.89 -0.44,-0.53 -1.09,-0.84 -1.77,-0.84l-9.48 -0.01c0,0 0,0 0,0 -1.12,0 -2.08,0.8 -2.28,1.9l-3.16 17.33 -21.43 0c-1.28,0 -2.31,1.04 -2.31,2.32l0 9.19c0,1.28 1.03,2.31 2.31,2.31l18.91 0 -1.98 10.86 -16.93 0c-1.28,0 -2.31,1.04 -2.31,2.31l0 9.2c0,1.28 1.03,2.31 2.31,2.31l14.41 0 -3.35 18.36c-0.12,0.67 0.06,1.37 0.5,1.89 0.44,0.53 1.09,0.84 1.78,0.84l9.36 0c1.12,0 2.08,-0.8 2.28,-1.9l3.53 -19.19 10.88 0 -3.31 18.42c-0.13,0.67 0.06,1.36 0.49,1.89 0.44,0.52 1.08,0.83 1.76,0.84l9.49 0.09c0,0 0.01,0 0.02,0 1.12,0 2.08,-0.81 2.28,-1.91l3.44 -19.33 20.79 0c1.28,0 2.31,-1.03 2.31,-2.31l0 -9.2c0,-1.27 -1.03,-2.31 -2.31,-2.31l-18.32 0 1.93 -10.86 16.39 0z"/></svg>';
return icon;
};
var kioskBoardIconClose = function (width, color) {
if (!width) { width = 18; }
if (!color) { color = '#707070'; }
var icon = '<svg id="KioskBoardIconClose" width="' + width + '" height="' + width + '" style="width:' + width + ';height:' + width + ';fill:' + color + ';" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 348.333 348.334"><path d="M336.559,68.611L231.016,174.165l105.543,105.549c15.699,15.705,15.699,41.145,0,56.85c-7.844,7.844-18.128,11.769-28.407,11.769c-10.296,0-20.581-3.919-28.419-11.769L174.167,231.003L68.609,336.563c-7.843,7.844-18.128,11.769-28.416,11.769c-10.285,0-20.563-3.919-28.413-11.769c-15.699-15.698-15.699-41.139,0-56.85l105.54-105.549L11.774,68.611c-15.699-15.699-15.699-41.145,0-56.844c15.696-15.687,41.127-15.687,56.829,0l105.563,105.554 L279.721,11.767c15.705-15.687,41.139-15.687,56.832,0C352.258,27.466,352.258,52.912,336.559,68.611z"/></svg>';
return icon;
};
// KioskBoard: Icons: end
// KioskBoard: IE support for Event: begin
(function () {
if (typeof window.Event === 'function') {
return false;
}
function Event(event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = window.document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
}
Event.prototype = window.Event.prototype;
window.Event = Event;
})();
// KioskBoard: IE support for Event: end
// KioskBoard: Check the event target by element: begin
var kioskBoardEventTargetIsElementOrChilds = function (event, element) {
if (event.target === element) {
return true;
} else {
var nodeList = element.querySelectorAll('*');
if (nodeList && nodeList.length > 0) {
for (var ni = 0; ni < nodeList.length; ni++) {
var child = nodeList[ni];
if (event.target === child) {
return true;
}
}
}
}
return false;
};
// KioskBoard: Check the event target by element: end
// KioskBoard: begin
var KioskBoard = {
// Initialize
init: function (initOptions) {
initOptions = typeof initOptions === 'object' && Object.keys(initOptions).length > 0 ? initOptions : {};
kioskBoardNewOptions = kioskBoardExtendObjects(true, kioskBoardDefaultOptions, initOptions);
kioskBoardInternalCSS();
},
// Run
run: function (selectorOrElement, options) {
// Element(s)
var kbElements = [];
// Allowed Element Types
var allowedElementTypes = ['input', 'textarea'];
// If: Check the selector is an element
var isElement = allowedElementTypes.indexOf(((selectorOrElement || {}).nodeName || '').toLocaleLowerCase('en')) > -1;
if (isElement) {
kbElements.push(selectorOrElement);
}
// Else: Check the selector is valid for the querySelector
else {
// Check the selector
var selectorIsValid = typeof selectorOrElement === 'string' && selectorOrElement.length > 0;
if (!selectorIsValid) {
kioskBoardConsoleError('"' + selectorOrElement + '" is not a valid selector.');
return false;
}
// Get the element(s)
kbElements = window.document.querySelectorAll(selectorOrElement);
if (kbElements.length < 1) {
kioskBoardConsoleError('You called the KioskBoard with the "' + selectorOrElement + '" selector, but there is no such element on the document.');
return false;
}
}
// If: Check the options to initialize or extend
if (typeof options === 'object' && Object.keys(options).length > 0) {
if (!kioskBoardNewOptions) {
KioskBoard.init(options);
} else {
// extend the new options by the default options
kioskBoardNewOptions = kioskBoardExtendObjects(true, kioskBoardDefaultOptions, options);
}
}
// Else If: Check the initialize
else if (!kioskBoardNewOptions) {
kioskBoardConsoleError('You have to initialize the KioskBoard first. \n\nVisit to learn more: ' + kioskBoardGithubUrl);
return false;
}
// The final options for each
var opt = kioskBoardNewOptions;
// Keys: Array of Objects
var keysArrayOfObjects = opt.keysArrayOfObjects;
// Keys: Array of Objects has keys
var objectHasKeys = false;
// Step 1: check the "keysArrayOfObjects": begin
if (kioskBoardCheckArrayOfObjects(keysArrayOfObjects)) {
// object has keys
objectHasKeys = true;
// cache the array
kioskBoardCachedKeys = keysArrayOfObjects;
}
// Step 1: check the "keysArrayOfObjects": end
// Step 2: check the "keysJsonUrl": begin
if (!objectHasKeys) {
// keys json url is valid
var keysJsonUrlIsValid = typeof opt.keysJsonUrl === 'string' && opt.keysJsonUrl.length > 0;
// check the "keysJsonUrl"
if (!keysJsonUrlIsValid) {
kioskBoardConsoleError('You have to set the path of KioskBoard Keys JSON file to "keysJsonUrl" option. \n\nVisit to learn more: ' + kioskBoardGithubUrl);
return false;
}
}
// Step 2: check the "keysJsonUrl": end
// Functions: Create Keyboard and AppendTo: begin
var createKeyboardAndAppendTo = function (data, input) {
// all inputs
var allInputs = [];
allInputs.push(input);
// all inputs readonly check for allowing mobile keyboard
var getReadOnlyAttr = input.getAttribute('readonly') !== null;
var allowMobileKeyboard = opt.allowMobileKeyboard === true;
// each input focus listener: begin
var inputFocusListener = function (e) {
// input element variables: begin
var theInput = e.currentTarget;
var theInputSelIndex = 0;
var theInputValArray = [];
var keyboardTypeArray = [kioskBoardTypes.All, kioskBoardTypes.Keyboard, kioskBoardTypes.Numpad];
var theInputKeyboardType = (theInput.dataset.kioskboardType || '').toLocaleLowerCase('en');
var keyboardType = keyboardTypeArray.indexOf(theInputKeyboardType) > -1 ? theInputKeyboardType : kioskBoardTypes.All;
var theInputPlacement = (theInput.dataset.kioskboardPlacement || '').toLocaleLowerCase('en');
var keyboardPlacement = theInputPlacement === kioskBoardPlacements.Top ? theInputPlacement : kioskBoardPlacements.Bottom;
var allowedSpecialCharacters = (theInput.dataset.kioskboardSpecialcharacters || '').toLocaleLowerCase('en') === 'true';
var keyboardLanguage = typeof opt.language === 'string' && opt.language.length > 0 ? opt.language.toLocaleLowerCase('en') : 'en';
// input element variables: end
// check mobile keyboard allowed: begin
if (!allowMobileKeyboard) {
theInput.setAttribute('readonly', 'readonly');
theInput.blur();
}
// check mobile keyboard allowed: end
// update theInputSelIndex on focus
var theInputValLen = (theInput.value || '').length;
theInputSelIndex = theInput.selectionStart || theInputValLen;
// update theInputValArray on focus
theInputValArray = theInput.value.split('');
// row keys element
var keysRowElements = '';
// all keys styles
var fontFamily = typeof opt.keysFontFamily === 'string' && opt.keysFontFamily.length > 0 ? opt.keysFontFamily : 'sans-serif';
var fontSize = typeof opt.keysFontSize === 'string' && opt.keysFontSize.length > 0 ? opt.keysFontSize : '22px';
var fontWeight = typeof opt.keysFontWeight === 'string' && opt.keysFontWeight.length > 0 ? opt.keysFontWeight : 'normal';
// static keys: begin
var isCapsLockActive = opt.capsLockActive === true;
var keysIconWidth = typeof opt.keysIconSize === 'string' && opt.keysIconSize.length > 0 ? opt.keysIconSize : '25px';
var keysIconColor = '#707070';
var keysAllowSpacebar = opt.keysAllowSpacebar === true;
var spaceKeyValue = keysAllowSpacebar ? ' ' : '';
var keysSpacebarText = typeof opt.keysSpacebarText === 'string' && opt.keysSpacebarText.length > 0 ? opt.keysSpacebarText : 'Space';
var keysEnterText = typeof opt.keysEnterText === 'string' && opt.keysEnterText.length > 0 ? opt.keysEnterText : 'Enter';
var spaceKey = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key kioskboard-key-space ' + (keysAllowSpacebar ? 'spacebar-allowed' : 'spacebar-denied') + '" data-value="' + spaceKeyValue + '">' + keysSpacebarText + '</span>';
var capsLockKey = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key-capslock ' + (isCapsLockActive ? 'capslock-active' : '') + '">' + kioskBoardIconCapslock(keysIconWidth, keysIconColor) + '</span>';
var backspaceKey = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key-backspace">' + kioskBoardIconBackspace(keysIconWidth, keysIconColor) + '</span>';
var enterKey = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key-enter">' + keysEnterText + '</span>';
// static keys: end
// keyboard "specialcharacter" setting is "true": begin
var specialCharacterKey = '';
var specialCharactersContent = '';
if (allowedSpecialCharacters) {
var size = parseInt(keysIconWidth) || 25;
var specialKeyWidth = (size * 2) + 'px';
var specialKeyHeight = size + 'px';
specialCharacterKey = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key-specialcharacter">' + kioskBoardIconSpecialCharacters(specialKeyWidth, specialKeyHeight, keysIconColor) + '</span>';
// check "keysSpecialCharsArrayOfStrings" for override: begin
var specialCharacters = opt.keysSpecialCharsArrayOfStrings;
if (Array.isArray(specialCharacters) && specialCharacters.length > 0) {
kioskBoardSpecialCharacters = specialCharacters.reduce(function (scMemo, scKey, scIndex) {
scMemo[scIndex] = scKey;
return scMemo;
}, {});
}
// check "keysSpecialCharsArrayOfStrings" for override: end
for (var key2 in kioskBoardSpecialCharacters) {
if (Object.prototype.hasOwnProperty.call(kioskBoardSpecialCharacters, key2)) {
var index2 = key2;
var value2 = kioskBoardSpecialCharacters[key2];
var eachKey2 = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key" data-index="' + index2.toString() + '" data-value="' + value2.toString() + '">' + value2.toString() + '</span>';
specialCharactersContent += eachKey2;
}
}
}
// keyboard "specialcharacter" setting is "true": begin
// keyboard type is "numpad": begin
if (keyboardType === kioskBoardTypes.Numpad) {
// check "keysNumpadArrayOfNumbers" for override: begin
var numpadKeys = opt.keysNumpadArrayOfNumbers;
if (Array.isArray(numpadKeys) && numpadKeys.length === 10) {
kioskBoardNumpadKeysObject = numpadKeys.reduce(function (numpadMemo, numpadKey, numpadIndex) {
numpadMemo[numpadIndex] = numpadKey;
return numpadMemo;
}, {});
}
// check "keysNumpadArrayOfNumbers" for override: end
var numpadKeysContent = '';
for (var key3 in kioskBoardNumpadKeysObject) {
if (Object.prototype.hasOwnProperty.call(kioskBoardNumpadKeysObject, key3)) {
var index3 = key3;
var value3 = kioskBoardNumpadKeysObject[key3];
var eachKey3 = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key kioskboard-key-' + value3.toString() + ' ' + (index3 === '9' ? 'kioskboard-key-last' : '') + '" data-index="' + index3.toString() + '" data-value="' + value3.toString() + '">' + value3.toString() + '</span>';
numpadKeysContent += eachKey3;
}
}
keysRowElements += '<div class="kioskboard-row kioskboard-row-numpad">' + numpadKeysContent + backspaceKey + enterKey + '</div>';
}
// keyboard type is "numpad": end
// keyboard type is "all" or "keyboard": begin
if (keyboardType === kioskBoardTypes.Keyboard || keyboardType === kioskBoardTypes.All) {
// only keyboard type is "all": begin
if (keyboardType === kioskBoardTypes.All) {
var numberKeysContent = '';
for (var key4 in kioskBoardAllKeysNumbersObject) {
if (Object.prototype.hasOwnProperty.call(kioskBoardAllKeysNumbersObject, key4)) {
var index4 = key4;
var value4 = kioskBoardAllKeysNumbersObject[key4];
var eachKey4 = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key kioskboard-key-' + value4.toString() + '" data-index="' + index4.toString() + '" data-value="' + value4.toString() + '">' + value4.toString() + '</span>';
numberKeysContent += eachKey4;
}
}
keysRowElements += '<div class="kioskboard-row kioskboard-row-top">' + numberKeysContent + '</div>';
}
// only keyboard type is "all": end
// dynamic keys group: begin
for (var i = 0; i < data.length; i++) {
var eachObj = data[i];
var rowKeysContent = '';
for (var key5 in eachObj) {
if (Object.prototype.hasOwnProperty.call(eachObj, key5)) {
var index5 = key5;
var value5 = eachObj[key5];
var eachKey5 = '<span style="font-family:' + fontFamily + ',sans-serif;font-weight:' + fontWeight + ';font-size:' + fontSize + ';" class="kioskboard-key kioskboard-key-' + value5.toString().toLocaleLowerCase(keyboardLanguage) + '" data-index="' + index5.toString() + '" data-value="' + value5.toString() + '">' + value5.toString() + '</span>';
rowKeysContent += eachKey5;
}
}
keysRowElements += '<div class="kioskboard-row kioskboard-row-dynamic">' + rowKeysContent + '</div>';
}
// dynamic keys group: end
// bottom keys group: begin
keysRowElements += '<div class="kioskboard-row kioskboard-row-bottom ' + (allowedSpecialCharacters ? 'kioskboard-with-specialcharacter' : '') + '">' + capsLockKey + specialCharacterKey + spaceKey + enterKey + backspaceKey + '</div>';
// bottom keys group: end
// add if special character keys allowed: begin
if (allowedSpecialCharacters) {
var closeSpecialCharacters = '<span class="kioskboard-specialcharacter-close">' + kioskBoardIconClose('18px', keysIconColor) + '</span>';
var specialCharactersWrapper = '<div class="kioskboard-specialcharacters-content">' + specialCharactersContent + '</div>';
keysRowElements += '<div class="kioskboard-row kioskboard-row-specialcharacters">' + closeSpecialCharacters + specialCharactersWrapper + '</div>';
}
// add if special character keys allowed: end
}
// keyboard type is "all" or "keyboard": end
// create keys wrapper: begin
var wrapKeysElement = function (stringHtml) {
var div = window.document.createElement('div');
div.className = 'kioskboard-wrapper';
div.innerHTML = stringHtml.trim();
return div;
};
var allKeysElement = wrapKeysElement(keysRowElements); // all keyboard element
// create keys wrapper: end
// check "cssAnimations": begin
var cssAnimations = opt.cssAnimations === true;
var cssAnimationsClass = 'no-animation';
var cssAnimationsStyle = 'no-animation';
var cssAnimationsDuration = 0;
if (cssAnimations) {
cssAnimationsClass = 'kioskboard-with-animation';
cssAnimationsStyle = 'kioskboard-fade';
cssAnimationsDuration = typeof opt.cssAnimationsDuration === 'number' ? opt.cssAnimationsDuration : 360;
if (opt.cssAnimationsStyle === 'slide') {
cssAnimationsStyle = 'kioskboard-slide';
}
}
// check "cssAnimations": end
// create the keyboard: begin
var theTheme = typeof opt.theme === 'string' && opt.theme.length > 0 ? opt.theme.trim() : 'light';
var kioskBoardVirtualKeyboardId = 'KioskBoard-VirtualKeyboard';
var kioskBoardVirtualKeyboard = window.document.createElement('div');
kioskBoardVirtualKeyboard.id = kioskBoardVirtualKeyboardId;
kioskBoardVirtualKeyboard.classList.add('kioskboard-theme-' + theTheme);
kioskBoardVirtualKeyboard.classList.add('kioskboard-placement-' + keyboardPlacement);
kioskBoardVirtualKeyboard.classList.add(cssAnimationsClass);
kioskBoardVirtualKeyboard.classList.add(cssAnimationsStyle);
kioskBoardVirtualKeyboard.classList.add((isCapsLockActive ? 'kioskboard-touppercase' : 'kioskboard-tolowercase'));
kioskBoardVirtualKeyboard.lang = keyboardLanguage;
kioskBoardVirtualKeyboard.style.webkitLocale = '"' + keyboardLanguage + '"';
kioskBoardVirtualKeyboard.style.animationDuration = cssAnimations ? (cssAnimationsDuration + 'ms') : '';
kioskBoardVirtualKeyboard.appendChild(allKeysElement);
// create the keyboard: end
// remove the keyboard: begin
var removeKeyboard = function () {
// add remove class
var keyboardElm = window.document.getElementById(kioskBoardVirtualKeyboardId);
if (keyboardElm) {
keyboardElm.classList.add(cssAnimationsStyle + '-remove');
// remove after the animation has been ended
var removeTimeout = setTimeout(function () {
if (keyboardElm.parentNode !== null) {
keyboardElm.parentNode.removeChild(keyboardElm); // remove keyboard
window.document.body.classList.remove('kioskboard-body-padding'); // remove body padding class
}
clearTimeout(removeTimeout);
}, cssAnimationsDuration);
}
};
// remove the keyboard: end
// event for input element trigger change: begin
var changeEvent = new Event('change', {
'bubbles': true,
'cancelable': true,
});
// event for input element trigger change: end
// input element keypress listener: begin
theInput.addEventListener('keypress', function (e) {
// if: allowed real keyboard use
var allowRealKeyboard = opt.allowRealKeyboard === true;
if (allowRealKeyboard) {
// update theInputValArray on keypress
theInputValArray = e.currentTarget.value.split('');
}
// else: stop
else {
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
e.cancelBubble = true;
return false;
}
}, false);
// input element keypress listener: end
// keys event listeners: begin
var keysEventListeners = function (keyElement, onClickHandler) {
if (keyElement) {
var isTouchableDevice = 'ontouchend' in window || window.navigator.maxTouchPoints > 0;
if (isTouchableDevice) {
keyElement.addEventListener('contextmenu', function (event) {
event.preventDefault();
}, false);
keyElement.addEventListener('touchend', onClickHandler);
}
keyElement.addEventListener('click', onClickHandler);
}
};
// keys event listeners: end
// keys click listeners: begin
var keysClickListeners = function (input) {
// each key click listener: begin
var eachKeyElm = window.document.querySelectorAll('.kioskboard-key');
if (eachKeyElm && eachKeyElm.length > 0) {
for (var ekIndex = 0; ekIndex < eachKeyElm.length; ekIndex++) {
var keyElm = eachKeyElm[ekIndex];
keysEventListeners(keyElm, function (e) {
e.preventDefault();
// check input max & maxlength
var maxLength = (input.getAttribute('maxlength') || '') * 1;
var max = (input.getAttribute('max') || '') * 1;
var liveValueLength = (input.value || '').length || 0;
if (maxLength > 0 && liveValueLength >= maxLength) { return false; }
if (max > 0 && liveValueLength >= max) { return false; }
// input trigger focus
input.focus();
// key's value
var keyValue = e.currentTarget.dataset.value || '';
// check capslock
if (isCapsLockActive) {
keyValue = keyValue.toLocaleUpperCase(keyboardLanguage);
} else {
keyValue = keyValue.toLocaleLowerCase(keyboardLanguage);
}
var keyValArr = keyValue.split('');
for (var keyValIndex = 0; keyValIndex < keyValArr.length; keyValIndex++) {
// update the selectionStart
theInputSelIndex = input.selectionStart || (input.value || '').length;
// add value by index
theInputValArray.splice(theInputSelIndex, 0, keyValArr[keyValIndex]);
// update input value
input.value = theInputValArray.join('');
// set next selection index
if (input.type !== 'number') {
input.setSelectionRange(theInputSelIndex + 1, theInputSelIndex + 1);
}
// input trigger change event for update the value
input.dispatchEvent(changeEvent);
}
});
}
}
// each key click listener: end
// capslock key click listener: begin
var capsLockKeyElm = window.document.querySelector('.kioskboard-key-capslock');
if (capsLockKeyElm) {
keysEventListeners(capsLockKeyElm, function (e) {
e.preventDefault();
// focus the input
input.focus();
if (e.currentTarget.classList.contains('capslock-active')) {
e.currentTarget.classList.remove('capslock-active');
kioskBoardVirtualKeyboard.classList.add('kioskboard-tolowercase');
kioskBoardVirtualKeyboard.classList.remove('kioskboard-touppercase');
isCapsLockActive = false;
} else {
e.currentTarget.classList.add('capslock-active');
kioskBoardVirtualKeyboard.classList.remove('kioskboard-tolowercase');
kioskBoardVirtualKeyboard.classList.add('kioskboard-touppercase');
isCapsLockActive = true;
}
});
}
// capslock key click listener: end
// backspace key click listener: begin
var backspaceKeyElm = window.document.querySelector('.kioskboard-key-backspace');
if (backspaceKeyElm) {
keysEventListeners(backspaceKeyElm, function (e) {
e.preventDefault();
// update the selectionStart
theInputSelIndex = input.selectionStart || (input.value || '').length;
// input trigger focus
input.focus();
// remove value by index
theInputValArray.splice((theInputSelIndex - 1), 1);
// update input value
input.value = theInputValArray.join('');
// set next selection index
if (input.type !== 'number') {
input.setSelectionRange(theInputSelIndex - 1, theInputSelIndex - 1);
}
// input trigger change event for update the value
input.dispatchEvent(changeEvent);
});
}
// backspace key click listener: end
// specialcharacter key click listener: begin
var specialCharacterKeyElm = window.document.querySelector('.kioskboard-key-specialcharacter');
var specialCharactersRowElm = window.document.querySelector('.kioskboard-row-specialcharacters');
// open
if (specialCharacterKeyElm && specialCharactersRowElm) {
keysEventListeners(specialCharacterKeyElm, function (e) {
e.preventDefault();
input.focus(); // focus the input
if (e.currentTarget.classList.contains('specialcharacter-active')) {
e.currentTarget.classList.remove('specialcharacter-active');
specialCharactersRowElm.classList.remove('kioskboard-specialcharacter-show');
} else {
e.currentTarget.classList.add('specialcharacter-active');
specialCharactersRowElm.classList.add('kioskboard-specialcharacter-show');
}
});
}
// close
var specialCharCloseElm = window.document.querySelector('.kioskboard-specialcharacter-close');
if (specialCharCloseElm && specialCharacterKeyElm && specialCharactersRowElm) {
keysEventListeners(specialCharCloseElm, function (e) {
e.preventDefault();
input.focus(); // focus the input
specialCharacterKeyElm.classList.remove('specialcharacter-active');
specialCharactersRowElm.classList.remove('kioskboard-specialcharacter-show');
});
}
// specialcharacter key click listener: end
// enter key click listener: begin
var enterKeyElm = window.document.querySelector('.kioskboard-key-enter');
if (enterKeyElm) {
keysEventListeners(enterKeyElm, function () {
if (opt.keysEnterCanClose === true) {
removeKeyboard();
}
if (typeof opt.keysEnterCallback === 'function') {
opt.keysEnterCallback();
}
});
}
// enter key click listener: end
};
// keys click listeners: end
// append keyboard: begin
var keyboardElement = window.document.getElementById(kioskBoardVirtualKeyboardId);
if (!keyboardElement) {
// append the keyboard to body and cache
window.document.body.appendChild(kioskBoardVirtualKeyboard);
// check window and keyboard height: begin
var windowHeight = Math.round(window.innerHeight);
var documentHeight = Math.round(window.document.body.clientHeight);
var keyboardHeight = Math.round(window.document.getElementById(kioskBoardVirtualKeyboardId).offsetHeight);
if (keyboardHeight > Math.round((windowHeight / 3) * 2)) {
var keyboardWrapper = window.document.querySelector('.kioskboard-wrapper');
keyboardWrapper.style.maxHeight = Math.round((windowHeight / 5) * 4) + 'px';
keyboardWrapper.style.overflowX = 'hidden';
keyboardWrapper.style.overflowY = 'auto';
keyboardWrapper.classList.add('kioskboard-overflow');
}
// check window and keyboard height: end
// body padding bottom || top: begin
var isPlacementTop = keyboardPlacement === kioskBoardPlacements.Top;
var inputVisibleEdge = (isPlacementTop ? theInput.getBoundingClientRect().top : theInput.getBoundingClientRect().bottom) || 0;
var docTop = window.document.documentElement.scrollTop || 0;
var theInputOffsetTop = Math.round(inputVisibleEdge + docTop);
var isPaddingTop = (theInputOffsetTop < keyboardHeight) && isPlacementTop;
var isPaddingBottom = documentHeight <= (theInputOffsetTop + keyboardHeight) && !isPlacementTop;
if (isPaddingTop || isPaddingBottom) {
var styleElm = window.document.getElementById('KioskboardBodyPadding');
if (styleElm && styleElm.parentNode !== null) {
styleElm.parentNode.removeChild(styleElm);
}
var style = '<style id="KioskboardBodyPadding">.kioskboard-body-padding {padding-' + (isPaddingTop ? 'top' : 'bottom') + ':' + keyboardHeight + 'px !important;}</style>';
var styleRange = window.document.createRange();
styleRange.selectNode(window.document.head);
var styleFragment = styleRange.createContextualFragment(style);
window.document.head.appendChild(styleFragment);
window.document.body.classList.add('kioskboard-body-padding');
}
// body padding bottom || top: end
// auto scroll: begin
var autoScroll = opt.autoScroll === true;
if (autoScroll) {
var inputThreshold = isPlacementTop ? 20 : 50;
var inputTop = theInput.getBoundingClientRect().top || 0;
var inputScrollOffsetTop = Math.round(inputTop + docTop);
var scrollBehavior = opt.cssAnimations === true ? 'smooth' : 'auto';
var scrollDelay = opt.cssAnimations === true && typeof opt.cssAnimationsDuration === 'number' ? opt.cssAnimationsDuration : 0;
var scrollTop = inputScrollOffsetTop - inputThreshold - (isPlacementTop ? keyboardHeight : 0);
var userAgent = window.navigator.userAgent.toLocaleLowerCase('en');
var isBrowserInternetExplorer = userAgent.indexOf('.net4') > -1;
var isBrowserEdgeLegacy = userAgent.indexOf('edge') > -1;
var isBrowserEdgeWebView = isBrowserEdgeLegacy && userAgent.indexOf('webview') > -1;
if ((!isBrowserEdgeLegacy || isBrowserEdgeWebView) && !isBrowserInternetExplorer) {
var scrollTimeout = setTimeout(function () {
if (isBrowserEdgeWebView) {
window.scrollBy(0, inputScrollOffsetTop);
} else {
window.scrollTo({ top: scrollTop, left: 0, behavior: scrollBehavior });
}
clearTimeout(scrollTimeout);
}, scrollDelay);
} else {
window.document.documentElement.scrollTop = scrollTop;
}
}
// auto scroll: end
// keyboard keys click listeners
keysClickListeners(theInput);
// keyboard click outside listener: begin
var docClickListener = function (e) {
var docClickTimeout = setTimeout(function () {
// check event target to remove keyboard: begin
var keyboardElm = window.document.getElementById(kioskBoardVirtualKeyboardId);
if (
keyboardElm
&& e.target !== theInput
&& !kioskBoardEventTargetIsElementOrChilds(e, keyboardElm)
&& !e.target.classList.contains('kioskboard-body-padding')
) {
removeKeyboard();
window.document.removeEventListener('click', docClickListener);
}
// check event target to remove keyboard: end
// toggle inputs: begin
if (allInputs.indexOf(theInput) > -1) {
var toggleTimeout = setTimeout(function () {
e.target.blur();
e.target.focus();
clearTimeout(toggleTimeout);
}, cssAnimationsDuration);
}
// toggle inputs: end
// clear doc click delay
clearTimeout(docClickTimeout);
}, cssAnimationsDuration);
};
window.document.addEventListener('click', docClickListener); // add document click listener
// keyboard click outside listener: end
}
// append keyboard: end
};
input.addEventListener('focus', inputFocusListener); // add input focus listener
// each input focus listener: end
// each input focusout listener: begin
var inputFocusoutListener = function (e) {
if (!allowMobileKeyboard && !getReadOnlyAttr) {
e.currentTarget.removeAttribute('readonly');
}
};
input.addEventListener('focusout', inputFocusoutListener); // add input focusout listener
// each input focusout listener: end
};
// Functions: Create Keyboard and AppendTo: end
// Functions: Get the Keys from JSON by XMLHttpRequest and AppendTo: begin
var getKeysViaXmlHttpRequest = function (jsonUrl, input) {
// if "kioskBoardCachedKeys" is undefined || null => send XMLHttpRequest
if (!kioskBoardCachedKeys) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', jsonUrl, true);
xmlHttp.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xmlHttp.send();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) { // success
var data = xmlHttp.responseText || []; // data
if (typeof data === 'string' && data.length > 0) {
var parsedData = JSON.parse(data); // JSON parse data
if (kioskBoardCheckArrayOfObjects(parsedData)) {
kioskBoardCachedKeys = parsedData; // cache the keys
createKeyboardAndAppendTo(parsedData, input); // create the keyboard
} else {
kioskBoardConsoleError('Array of objects of the keys are not valid. \n\nVisit to learn more: ' + kioskBoardGithubUrl);
return false;
}
}
} else {
kioskBoardConsoleError('XMLHttpRequest has been failed. Please check your URL path or protocol.');
return false;
}
}
};
}
};
// Functions: Get the Keys from JSON by XMLHttpRequest and AppendTo: end
// Step 3: Select the element(s): begin
for (var kbIndex = 0; kbIndex < kbElements.length; kbIndex++) {
// each element
var eachElement = kbElements[kbIndex];
// each element tag name
var getTagName = ((eachElement || {}).tagName || '').toLocaleLowerCase('en');
// if: an input or textarea element
if (allowedElementTypes.indexOf(getTagName) > -1) {
// if: has cached keys => create the keyboard by using cached keys
if (kioskBoardCachedKeys) {
createKeyboardAndAppendTo(kioskBoardCachedKeys, eachElement);
}
// else: try to get the keys from the JSON file via XmlHttpRequest
else {
getKeysViaXmlHttpRequest(opt.keysJsonUrl, eachElement);
}
}
// else: other elements
else {
kioskBoardConsoleLog('You have to call the "KioskBoard" with an ID/ClassName of an Input or a TextArea element. Your element\'s tag name is: "' + getTagName + '". \n\nYou can visit the Documentation page to learn more. \n\nVisit: ' + kioskBoardGithubUrl);
}
}
// Step 3: Select the element(s): end
},
};
return KioskBoard;
// KioskBoard: end
});