-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (90 loc) · 12.7 KB
/
script.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
(function($) {
var keytimer, trackedobjects = [], scanbuffer = [];
function processInput(e) {
var idx = $.inArray(e.target, trackedobjects);
var settings = $(e.target).data('inputreaderSettings');
var i = scanbuffer[idx].length;
if (e.key.match(charRegex)) {
scanbuffer[idx].push(e.key);
} else {
switch(e.key) {
case 'Tab': scanbuffer[idx].push("\t"); break;
case 'Enter': scanbuffer[idx].push("\n"); break;
}
}
if (scanbuffer[idx].length != i) {
clearTimeout(keytimer);
e.preventDefault();
keytimer = setTimeout(function() { processScanData(idx); }, $.fn.inputreader.defaults.timeout);
}
}
function processScanData(idx) {
var element = trackedobjects[idx];
if (scanbuffer[idx].length) {
var data = scanbuffer[idx].join('');
scanbuffer[idx].length = 0;
var event = $.Event('inputreader:scan', {scandata: data});
$(element).trigger(event);
if ($.fn.inputreader.defaults.insertText && typeof element.selectionStart !== 'undefined' && !event.isDefaultPrevented()) {
// data was NOT processed - insert it like normal
var value = $(element).val();
var startPos = element.selectionStart;
var endPos = element.selectionEnd;
var newPos = 0;
if (startPos == endPos) {
// no selection
newPos += endPos + data.length;
if (startPos == value.length) {
value += data;
} else if (startPos == 0) {
value = data + value;
} else {
value = value.substring(0, startPos) + data + value.substring(startPos);
}
} else {
newPos = startPos + data.length;
value = value.substring(0, startPos) + data + value.substring(endPos);
}
$(element).val(value);
element.setSelectionRange(newPos, newPos);
}
}
}
$.fn.inputreader = function(state, options) {
if (!state) state = 'listen';
if (state == 'listen') {
var settings = $.extend({
timeout: 50
});
this.data('inputreaderSettings', settings);
this.each(function(index, element) {
var idx = $.inArray(element, trackedobjects);
if (idx < 0 ) {
idx = trackedobjects.push(element) - 1;
scanbuffer[idx] = [];
$(element).on('keydown', processInput);
}
});
} else if (state == 'stop') {
this.removeData('inputreaderSettings');
this.each(function(index, element) {
trackedobjects = $.grep(trackedobjects, function(el, idx) {
if (el === element) {
delete scanbuffer[idx];
$(element).off('keydown', processInput);
return false; //removes from array
}
return true;
});
});
} else {
console.debug('Invalid state: ', state);
}
return this;
};
$.fn.inputreader.defaults = {
timeout: 50,
insertText: true
};
var charRegex = /^[\ -~\u00A0-\u00AC\u00AE-\u02FF\u0370-\u0377\u037A-\u037F\u0384-\u038A\u038C\u038E-\u03A1\u03A3-\u0482\u048A-\u052F\u0531-\u0556\u0559-\u055F\u0561-\u0587\u0589\u058A\u058D-\u058F\u05BE\u05C0\u05C3\u05C6\u05D0-\u05EA\u05F0-\u05F4\u0606-\u060F\u061B\u061E-\u064A\u0660-\u066F\u0671-\u06D5\u06DE\u06E5\u06E6\u06E9\u06EE-\u070D\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07C0-\u07EA\u07F4-\u07FA\u0800-\u0815\u081A\u0824\u0828\u0830-\u083E\u0840-\u0858\u085E\u08A0-\u08B4\u08B6-\u08BD\u0904-\u0939\u093D\u0950\u0958-\u0961\u0964-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09E6-\u09FB\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A66-\u0A6F\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AE6-\u0AF1\u0AF9\u0B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C5\u10C7\u10CD\u10D0-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1360-\u137C\u1380-\u1399\u13A0-\u13F5\u13F8-\u13FD\u1400-\u169C\u16A0-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1735\u1736\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D4-\u17DC\u17E0-\u17E9\u17F0-\u17F9\u1800-\u180A\u1810-\u1819\u1820-\u1877\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1940\u1944-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u19DE-\u1A16\u1A1E-\u1A54\u1A80-\u1A89\u1A90-\u1A99\u1AA0-\u1AAD\u1B05-\u1B33\u1B45-\u1B4B\u1B50-\u1B6A\u1B74-\u1B7C\u1B83-\u1BA0\u1BAE-\u1BE5\u1BFC-\u1C23\u1C3B-\u1C49\u1C4D-\u1C88\u1CC0-\u1CC7\u1CD3\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FC4\u1FC6-\u1FD3\u1FD6-\u1FDB\u1FDD-\u1FEF\u1FF2-\u1FF4\u1FF6-\u1FFE\u2000-\u200A\u2010-\u2029\u202F-\u205F\u2070\u2071\u2074-\u208E\u2090-\u209C\u20A0-\u20BE\u2100-\u218B\u2190-\u23FE\u2400-\u2426\u2440-\u244A\u2460-\u2B73\u2B76-\u2B95\u2B98-\u2BB9\u2BBD-\u2BC8\u2BCA-\u2BD1\u2BEC-\u2BEF\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CEE\u2CF2\u2CF3\u2CF9-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D70\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E00-\u2E44\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u2FF0-\u2FFB\u3000-\u3029\u3030-\u303F\u3041-\u3096\u309B-\u30FF\u3105-\u312D\u3131-\u318E\u3190-\u31BA\u31C0-\u31E3\u31F0-\u321E\u3220-\u32FE\u3300-\u4DB5\u4DC0-\u9FD5\uA000-\uA48C\uA490-\uA4C6\uA4D0-\uA62B\uA640-\uA66E\uA673\uA67E-\uA69D\uA6A0-\uA6EF\uA6F2-\uA6F7\uA700-\uA7AE\uA7B0-\uA7B7\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA828-\uA82B\uA830-\uA839\uA840-\uA877\uA882-\uA8B3\uA8CE-\uA8D9\uA8F2-\uA8FD\uA900-\uA925\uA92E-\uA946\uA95F-\uA97C\uA984-\uA9B2\uA9C1-\uA9CD\uA9CF-\uA9D9\uA9DE-\uA9E4\uA9E6-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA50-\uAA59\uAA5C-\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAAEA\uAAF0-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB65\uAB70-\uABE2\uABEB\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBC1\uFBD3-\uFD3F\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFD\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE66\uFE68-\uFE6B\uFE70-\uFE74\uFE76-\uFEFC\uFF01-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFD\U00010000-\U0001000B\U0001000D-\U00010026\U00010028-\U0001003A\U0001003C\U0001003D\U0001003F-\U0001004D\U00010050-\U0001005D\U00010080-\U000100FA\U00010100-\U00010102\U00010107-\U00010133\U00010137-\U0001018E\U00010190-\U0001019B\U000101A0\U000101D0-\U000101FC\U00010280-\U0001029C\U000102A0-\U000102D0\U000102E1-\U000102FB\U00010300-\U00010323\U00010330-\U0001034A\U00010350-\U00010375\U00010380-\U0001039D\U0001039F-\U000103C3\U000103C8-\U000103D5\U00010400-\U0001049D\U000104A0-\U000104A9\U000104B0-\U000104D3\U000104D8-\U000104FB\U00010500-\U00010527\U00010530-\U00010563\U0001056F\U00010600-\U00010736\U00010740-\U00010755\U00010760-\U00010767\U00010800-\U00010805\U00010808\U0001080A-\U00010835\U00010837\U00010838\U0001083C\U0001083F-\U00010855\U00010857-\U0001089E\U000108A7-\U000108AF\U000108E0-\U000108F2\U000108F4\U000108F5\U000108FB-\U0001091B\U0001091F-\U00010939\U0001093F\U00010980-\U000109B7\U000109BC-\U000109CF\U000109D2-\U00010A00\U00010A10-\U00010A13\U00010A15-\U00010A17\U00010A19-\U00010A33\U00010A40-\U00010A47\U00010A50-\U00010A58\U00010A60-\U00010A9F\U00010AC0-\U00010AE4\U00010AEB-\U00010AF6\U00010B00-\U00010B35\U00010B39-\U00010B55\U00010B58-\U00010B72\U00010B78-\U00010B91\U00010B99-\U00010B9C\U00010BA9-\U00010BAF\U00010C00-\U00010C48\U00010C80-\U00010CB2\U00010CC0-\U00010CF2\U00010CFA-\U00010CFF\U00010E60-\U00010E7E\U00011003-\U00011037\U00011047-\U0001104D\U00011052-\U0001106F\U00011083-\U000110AF\U000110BB\U000110BC\U000110BE-\U000110C1\U000110D0-\U000110E8\U000110F0-\U000110F9\U00011103-\U00011126\U00011136-\U00011143\U00011150-\U00011172\U00011174-\U00011176\U00011183-\U000111B2\U000111C1-\U000111C9\U000111CD\U000111D0-\U000111DF\U000111E1-\U000111F4\U00011200-\U00011211\U00011213-\U0001122B\U00011238-\U0001123D\U00011280-\U00011286\U00011288\U0001128A-\U0001128D\U0001128F-\U0001129D\U0001129F-\U000112A9\U000112B0-\U000112DE\U000112F0-\U000112F9\U00011305-\U0001130C\U0001130F\U00011310\U00011313-\U00011328\U0001132A-\U00011330\U00011332\U00011333\U00011335-\U00011339\U0001133D\U00011350\U0001135D-\U00011361\U00011400-\U00011434\U00011447-\U00011459\U0001145B\U0001145D\U00011480-\U000114AF\U000114C4-\U000114C7\U000114D0-\U000114D9\U00011580-\U000115AE\U000115C1-\U000115DB\U00011600-\U0001162F\U00011641-\U00011644\U00011650-\U00011659\U00011660-\U0001166C\U00011680-\U000116AA\U000116C0-\U000116C9\U00011700-\U00011719\U00011730-\U0001173F\U000118A0-\U000118F2\U000118FF\U00011AC0-\U00011AF8\U00011C00-\U00011C08\U00011C0A-\U00011C2E\U00011C40-\U00011C45\U00011C50-\U00011C6C\U00011C70-\U00011C8F\U00012000-\U00012399\U00012400-\U0001246E\U00012470-\U00012474\U00012480-\U00012543\U00013000-\U0001342E\U00014400-\U00014646\U00016800-\U00016A38\U00016A40-\U00016A5E\U00016A60-\U00016A69\U00016A6E\U00016A6F\U00016AD0-\U00016AED\U00016AF5\U00016B00-\U00016B2F\U00016B37-\U00016B45\U00016B50-\U00016B59\U00016B5B-\U00016B61\U00016B63-\U00016B77\U00016B7D-\U00016B8F\U00016F00-\U00016F44\U00016F50\U00016F93-\U00016F9F\U00016FE0\U00017000-\U000187EC\U00018800-\U00018AF2\U0001B000\U0001B001\U0001BC00-\U0001BC6A\U0001BC70-\U0001BC7C\U0001BC80-\U0001BC88\U0001BC90-\U0001BC99\U0001BC9C\U0001BC9F\U0001D000-\U0001D0F5\U0001D100-\U0001D126\U0001D129-\U0001D164\U0001D16A-\U0001D16C\U0001D183\U0001D184\U0001D18C-\U0001D1A9\U0001D1AE-\U0001D1E8\U0001D200-\U0001D241\U0001D245\U0001D300-\U0001D356\U0001D360-\U0001D371\U0001D400-\U0001D454\U0001D456-\U0001D49C\U0001D49E\U0001D49F\U0001D4A2\U0001D4A5\U0001D4A6\U0001D4A9-\U0001D4AC\U0001D4AE-\U0001D4B9\U0001D4BB\U0001D4BD-\U0001D4C3\U0001D4C5-\U0001D505\U0001D507-\U0001D50A\U0001D50D-\U0001D514\U0001D516-\U0001D51C\U0001D51E-\U0001D539\U0001D53B-\U0001D53E\U0001D540-\U0001D544\U0001D546\U0001D54A-\U0001D550\U0001D552-\U0001D6A5\U0001D6A8-\U0001D7CB\U0001D7CE-\U0001D9FF\U0001DA37-\U0001DA3A\U0001DA6D-\U0001DA74\U0001DA76-\U0001DA83\U0001DA85-\U0001DA8B\U0001E800-\U0001E8C4\U0001E8C7-\U0001E8CF\U0001E900-\U0001E943\U0001E950-\U0001E959\U0001E95E\U0001E95F\U0001EE00-\U0001EE03\U0001EE05-\U0001EE1F\U0001EE21\U0001EE22\U0001EE24\U0001EE27\U0001EE29-\U0001EE32\U0001EE34-\U0001EE37\U0001EE39\U0001EE3B\U0001EE42\U0001EE47\U0001EE49\U0001EE4B\U0001EE4D-\U0001EE4F\U0001EE51\U0001EE52\U0001EE54\U0001EE57\U0001EE59\U0001EE5B\U0001EE5D\U0001EE5F\U0001EE61\U0001EE62\U0001EE64\U0001EE67-\U0001EE6A\U0001EE6C-\U0001EE72\U0001EE74-\U0001EE77\U0001EE79-\U0001EE7C\U0001EE7E\U0001EE80-\U0001EE89\U0001EE8B-\U0001EE9B\U0001EEA1-\U0001EEA3\U0001EEA5-\U0001EEA9\U0001EEAB-\U0001EEBB\U0001EEF0\U0001EEF1\U0001F000-\U0001F02B\U0001F030-\U0001F093\U0001F0A0-\U0001F0AE\U0001F0B1-\U0001F0BF\U0001F0C1-\U0001F0CF\U0001F0D1-\U0001F0F5\U0001F100-\U0001F10C\U0001F110-\U0001F12E\U0001F130-\U0001F16B\U0001F170-\U0001F1AC\U0001F1E6-\U0001F202\U0001F210-\U0001F23B\U0001F240-\U0001F248\U0001F250\U0001F251\U0001F300-\U0001F6D2\U0001F6E0-\U0001F6EC\U0001F6F0-\U0001F6F6\U0001F700-\U0001F773\U0001F780-\U0001F7D4\U0001F800-\U0001F80B\U0001F810-\U0001F847\U0001F850-\U0001F859\U0001F860-\U0001F887\U0001F890-\U0001F8AD\U0001F910-\U0001F91E\U0001F920-\U0001F927\U0001F930\U0001F933-\U0001F93E\U0001F940-\U0001F94B\U0001F950-\U0001F95E\U0001F980-\U0001F991\U0001F9C0\U00020000-\U0002A6D6\U0002A700-\U0002B734\U0002B740-\U0002B81D\U0002B820-\U0002CEA1\U0002F800-\U0002FA1D]$/;
}(jQuery));