-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjk-keyboard.js
123 lines (106 loc) · 2.9 KB
/
jk-keyboard.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
(function (root, factory) {
root.JK = factory();
} (this, function () {
var JK = function() {};
var Keyboard = function(options) {
options = options || {};
if (!options.container) {
return false;
}
options.decimal = options.decimal || 2;
options.open = options.open || false;
this.options = options;
this.data = {};
this.init();
};
Keyboard.prototype.clear = function() {
this.data.result = 0;
this.data.number = [];
this.data.decimal = false;
this.data.decimalNum = 0;
};
Keyboard.prototype.init = function() {
this.clear();
var el = document.querySelector(this.options.container);
this.onTouchStart = Keyboard.prototype.onTouchStart.bind(this);
this.el = el;
this.keys = el.querySelectorAll('.jk-key[data-code]');
for (var key of this.keys) {
key.addEventListener('touchstart', this.onTouchStart);
}
if (this.options.open) {
this.open();
}
if (typeof this.options.onStart === 'function') {
this.options.onStart(this);
}
};
Keyboard.prototype.destroy = function() {
this.clear();
this.$keys.off('touchstart', this.onTouchStart);
};
Keyboard.prototype.open = function() {
this.el.classList.add('open');
};
Keyboard.prototype.close = function() {
this.el.classList.remove('open');
};
Keyboard.prototype.onTouchStart = function(e) {
e.preventDefault();
var key = e.currentTarget;
var result = this.handleKeyPress(key.dataset.code);
key.classList.add('down');
setTimeout(function() {
key.classList.remove('down');
}, 100);
if (typeof this.options.onKeyUp === 'function') {
this.options.onKeyUp(result);
}
};
Keyboard.prototype.handleKeyPress = function(code) {
var data = this.data;
if (code === 'backspace') {
var tail = data.number.pop();
if (tail === '.') {
data.decimal = false;
} else if (data.decimal) {
data.decimalNum--;
}
} else if (code === '.') {
if (!data.decimal) {
data.decimal = true;
if (!data.number.length) {
data.number.push(0);
}
data.number.push(code);
}
} else {
code = parseInt(code);
if (data.number.length === 1 && data.number[0] === 0) {
data.number.pop();
}
if (data.decimal) {
if (data.decimalNum < this.options.decimal) {
data.decimalNum++;
data.number.push(code);
}
} else {
data.number.push(code);
}
}
var result = data.number.join('');
if (typeof this.options.onKeyDown === 'function') {
var callbackValue = this.options.onKeyDown(result);
if (callbackValue === false) {
data.number.pop();
result = data.number.join('');
}
}
return result;
};
JK.Keyboard = Keyboard;
JK.start = function(options) {
return new Keyboard(options);
};
return JK;
}));