forked from GaretJax/CardReader
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CardReader.js
145 lines (120 loc) · 3.21 KB
/
CardReader.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
/**
* jQuery based magnetic stripe card reader "driver" to be used in web
* applications.
*
* Copyright @ 2009-2010 Jonathan Stoppani (http://garetjax.info/)
* Licensed under the MIT license (see LICENSE for details)
*/
var CardReader = function (error_start, track_start, track_end, timeout) {
this.error_start = error_start || "é";
this.track_start = track_start || "%";
this.track_end = track_end || "_";
this.timeout = timeout || 100;
this.error_start = this.error_start.charCodeAt(0);
this.track_start = this.track_start.charCodeAt(0);
this.track_end = this.track_end.charCodeAt(0);
this.started = false;
this.finished = false;
this.isError = false;
this.input = "";
this.timer = undefined;
this.callbacks = [];
this.errbacks = [];
this.validators = [];
this.isDispatching = false;
};
CardReader.prototype = {
dispatch: function (data, isError) {
if (!isError) {
for (var cb in this.validators) {
if (!this.validators[cb](data)) {
isError = true;
break;
}
}
}
if (this.isDispatching) {
if (isError) {
console.log("Immediate error!");
return;
} else {
clearTimeout(this.isDispatching);
}
}
reader = this;
this.isDispatching = setTimeout(function () {
console.log("Error timeout cleared");
reader.isDispatching = false;
}, 200);
if (isError) {
for (var cb in this.errbacks) {
this.errbacks[cb](this.input);
}
} else {
for (var cb in this.callbacks) {
this.callbacks[cb](this.input);
}
}
},
readObserver: function (e) {
var ob = this;
if (!this.started && (e.which === this.track_start || e.which === this.error_start)) {
e.stopImmediatePropagation();
e.preventDefault();
this.started = true;
this.isError = e.which === this.error_start;
this.timer = setTimeout(function () {
ob.started = false;
ob.finished = false;
ob.isError = false;
ob.input = "";
}, this.timeout);
} else if (this.started && e.which === this.track_end) {
e.stopImmediatePropagation();
e.preventDefault();
this.finished = true;
clearTimeout(this.timer);
this.timer = setTimeout(function () {
ob.started = false;
ob.finished = false;
ob.isError = false;
ob.input = "";
}, this.timeout);
} else if (this.started && this.finished && e.which === 13) {
e.stopImmediatePropagation();
e.preventDefault();
this.dispatch(this.input, this.isError);
this.started = false;
this.finished = false;
this.isError = false;
this.input = "";
clearTimeout(this.timer);
} else if (this.started) {
e.stopImmediatePropagation();
e.preventDefault();
this.input += String.fromCharCode(e.which);
clearTimeout(this.timer);
this.timer = setTimeout(function () {
ob.started = false;
ob.finished = false;
ob.isError = false;
ob.input = "";
}, this.timeout);
}
},
observe: function (element) {
var func = this;
$(element).keypress(function (e) {
CardReader.prototype.readObserver.apply(func, arguments);
});
},
validate: function (validator) {
this.validators.push(validator);
},
cardRead: function (callback) {
this.callbacks.push(callback);
},
cardError: function (errback) {
this.errbacks.push(errback);
},
}