forked from 10xSebastian/barcode-scanner.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode-scanner.js
112 lines (92 loc) · 3.11 KB
/
barcode-scanner.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
// Generated by CoffeeScript 1.3.3
(function() {
var BarcodeScanner,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
BarcodeScanner = (function() {
function BarcodeScanner() {
this.submit = __bind(this.submit, this);
this.keyPress = __bind(this.keyPress, this);
this.getArguments = __bind(this.getArguments, this);
this.getAction = __bind(this.getAction, this);
this.execute = __bind(this.execute, this);
this.addAction = __bind(this.addAction, this);
this.addChar = __bind(this.addChar, this);
this.actions = [];
this.buffer = null;
this.delay = 50;
this.timer = null;
}
BarcodeScanner.prototype.addChar = function(char) {
var _ref,
_this = this;
if ((_ref = this.buffer) == null) {
this.buffer = "";
}
this.buffer += char;
window.clearTimeout(this.timer);
return this.timer = window.setTimeout((function() {
return _this.buffer = null;
}), this.delay);
};
BarcodeScanner.prototype.addAction = function(string, callback) {
var regexp;
string = "^" + (string.replace(/\(.*?\)/ig, "(\\S*)")) + "$";
regexp = new RegExp(string);
return this.actions.push({
regexp: regexp,
callback: callback
});
};
BarcodeScanner.prototype.execute = function() {
var action, activeElement, code, target;
activeElement = $(document.activeElement);
target = activeElement.is("input, textarea") ? activeElement : $("[data-barcode-scanner-target]:last");
code = this.buffer;
action = this.getAction(code);
if (action != null) {
action.callback.apply(target, this.getArguments(code, action));
} else {
target.val("").val(code).focus();
this.submit(target);
}
return this.buffer = null;
};
BarcodeScanner.prototype.getAction = function(code) {
var action, _i, _len, _ref;
_ref = this.actions;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
action = _ref[_i];
if (action.regexp.test(code)) {
return action;
}
}
};
BarcodeScanner.prototype.getArguments = function(code, action) {
var matches;
matches = action.regexp.exec(code);
return matches.slice(1, matches.length + 1 || 9e9);
};
BarcodeScanner.prototype.keyPress = function(e) {
var char, charCode;
if (e == null) {
e = window.event;
}
charCode = typeof e.which === "number" ? e.which : e.keyCode;
char = String.fromCharCode(charCode);
if ((charCode === 13) && (this.buffer != null)) {
e.preventDefault();
return this.execute();
} else {
return this.addChar(char);
}
};
BarcodeScanner.prototype.submit = function(target) {
if (!target.closest("[data-prevent-barcode-scanner-submit]").length) {
return target.closest("form").submit();
}
};
return BarcodeScanner;
})();
window.BarcodeScanner = new BarcodeScanner();
$(window).keypress(window.BarcodeScanner.keyPress);
}).call(this);