-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkeyboard.js
More file actions
363 lines (313 loc) · 11.1 KB
/
keyboard.js
File metadata and controls
363 lines (313 loc) · 11.1 KB
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
if( !exports ) var exports = {};
(function(exports, global){
"use strict";
function splice(str, index, count, add) {
return str.slice(0, index) + (add || "") + str.slice(index + count);
}
function checkSelectionSupport(type){
if (["text", "search", "URL", "tel", "password"].indexOf(type) == -1 ) return false;
return true;
}
function dispatchKeyboardOpenEvent(){
document.dispatchEvent(new CustomEvent('keyboard-open'));
}
function dispatchKeyboardCloseEvent(){
document.dispatchEvent(new CustomEvent('keyboard-close'));
}
function Element(Keyboard, el){
this.Keyboard = Keyboard;
el.layout = global.Keyboard.layout[el.type] ? el.type : '_default';
function dispatchEvent(event, keyInfo){
var event = new KeyboardEvent(event,{
key: keyInfo.symbol,
code: 'Key' + keyInfo.symbol,
keyCode : keyInfo.symbol.charCodeAt(0),
which : keyInfo.symbol.charCodeAt(0),
altKey : false,
ctrlKey : false,
shiftKey : false,
metaKey : false
});
event.virtual = true;
el.dispatchEvent(event);
}
this.onEvent = function(keyInfo){
dispatchEvent('keydown', keyInfo);
dispatchEvent('keypress', keyInfo);
dispatchEvent('keyup', keyInfo);
dispatchEvent('input', keyInfo);
dispatchEvent('change', keyInfo);
};
this.keydownfunc = function(e){
// If keydown is authentic, skip our internal update and let the default play out
if (!e.virtual) return;
e.preventDefault();
var selectionSupported = checkSelectionSupport(this.type);
if (e.key == "backspace") {
if ( !selectionSupported ) {
this.value = this.value.slice(0, -1);
} else {
var pos = el.selectionStart;
var offset = el.selectionEnd-el.selectionStart? 0:1;
this.value = this.value.substring(0, el.selectionStart-offset) + this.value.slice(el.selectionEnd);
this.setSelectionRange(pos-offset, pos-offset);
}
return;
}
// Toggle will change the layout to a different one that is pre-determined
// An empty toggle key will toggle the keyboard back to default
if (e.key.substring(0, 6) == "toggle") {
var newType = e.key.substring(6);
if(newType.length >= 0 && global.Keyboard.layout[newType]) {
return this.Keyboard.Keyboard.show(newType);
}
return;
}
if( this.value.length >= this.maxLength && this.maxLength != -1 ){
return;
}
var update = e.key || String.fromCharCode(e.keyCode);
// Support an input case that will capitalize letters as needed
if(( e.target.dataset.case === 'sentence' && (this.value.length === 0 || this.value.slice(-1) === "." || this.value.slice(-2) === ". " ))
|| ( e.target.dataset.case === 'capitalize' && this.value.slice(-1) === " " ))
{
update = update.toUpperCase();
}
// TODO Mimic selection for input elements that don't support selection api as well
// IDEA: selection-polyfill? get caret's pixel location instead?
if ( !selectionSupported ) {
this.value += update;
} else {
var pos = el.selectionStart + update.length;
this.value = splice(
this.value,
el.selectionStart,
el.selectionEnd-el.selectionStart,
update
);
this.setSelectionRange(pos, pos); // reset the position after the splice
}
// TODO Calculate scroll amount based on caret position
this.scrollLeft = this.scrollWidth;
};
el.addEventListener('focus', this.focus.bind(this));
el.addEventListener('blur', this.blur.bind(this));
el.addEventListener('keydown', this.keydownfunc);
}
Element.prototype.focus = function(e){
this.Keyboard.show(e.target.layout);
this.Keyboard.on('key', this.onEvent);
};
Element.prototype.blur = function(e){
this.Keyboard.hide(e.target.layout);
this.Keyboard.off('key', this.onEvent);
};
function Keyboard(inputs, holder){
var self = this;
Array.prototype.slice.call(inputs, 0).forEach(function(input){
input.Keyboard = new Element(self, input);
});
this.active = false;
this.listeners = {key:[]};
this.keyboardEl = null;
this.layout = null;
this.keyboardEl = document.createElement("div");
this.keyboardEl.classList.add("keyboard-container");
this.keyboardEl.addEventListener("mousedown", handleKeydownEvents.bind(this));
this.keyboardEl.addEventListener("mouseup", handleKeyupEvents.bind(this));
// TODO - find a way to enable this touchstart event again.
// It prevents the :active state from being triggered on keys
this.keyboardEl.addEventListener("touchstart", handleKeydownEvents.bind(this));
this.keyboardEl.addEventListener("touchend", handleKeyupEvents.bind(this));
// Generate keyboard HTML, bind events, insert them to given element
this.show = function (layout) {
// Clear any timers relating to keyhold
this.clearKeyHoldTimers();
if (!global.Keyboard.layout[layout]) throw new Error("keyboard initiation: Missing layout: " + layout);
if( self.layout && layout === self.layout && this.active){
return;
}
this.active = true;
self.layout = layout;
this.keyboardEl.innerHTML = "";
this.keyboardEl.classList.remove('keyboard-container-hidden');
var closeButton = document.createElement("span");
closeButton.classList.add('keyboard-close-button');
closeButton.innerHTML = '✖';
this.keyboardEl.appendChild(closeButton);
closeButton.onclick = function() {
document.activeElement.blur()
}.bind(this);
closeButton.ontouchstart = function() {
document.activeElement.blur()
}.bind(this);
function foreachLayout(row, rowIndex, layout) {
var rowEl = document.createElement("div");
rowEl.classList.add("keyboard-row");
rowEl.classList.add("keyboard-row--" + rowIndex);
function foreachRow(key, keyIndex, row) {
var keyEl = document.createElement("div");
keyEl.classList.add("keyboard-key");
keyEl.classList.add("keyboard-key--" + keyIndex);
// Parse the layout configuration
for (var dataName in key) {
switch (dataName) {
case "symbol":
if (!key.label) keyEl.innerHTML = key[dataName];
keyEl.dataset.symbol = key[dataName];
break;
case "label":
keyEl.innerHTML = key[dataName];
break;
default:
keyEl.dataset[dataName] = key[dataName];
}
}
rowEl.appendChild(keyEl);
}
row.forEach(foreachRow);
self.keyboardEl.appendChild(rowEl);
}
global.Keyboard.layout[self.layout].forEach(foreachLayout);
// Append keys to el
// Send an event signifying open of keyboard
dispatchKeyboardOpenEvent()
holder.appendChild(self.keyboardEl);
};
this.hide = function(){
self.active = false;
// Clear any timers relating to keyhold to cleanup
this.clearKeyHoldTimers();
// Send an event signifying close of keyboard
dispatchKeyboardCloseEvent()
setTimeout(function(){
if( self.active ) return;
self.keyboardEl.classList.add("keyboard-container-hidden");
self.keyboardEl.innerHTML = "";
},25);
}
this.on = function(evt, action){
if( !this.listeners[evt] ){
this.listeners[evt] = [];
}
this.listeners[evt].push(action);
};
this.off = function(evt, action){
if( !this.listeners[evt] ) return;
this.listeners[evt] = this.listeners[evt].filter(function(listener){
return action.toString() !== listener.toString();
});
};
/**
* Add input(s) after the fact
* @param inputs
*/
this.add = function(inputs){
if(!Array.isArray(inputs))
inputs = [inputs];
Array.prototype.slice.call(inputs, 0).forEach(function(input){
input.Keyboard = new Element(self, input);
});
};
/**
* Clear timer events bound to the keyboard
*/
this.clearKeyHoldTimers = function () {
// Clear timeout to make sure multiple keypress does not start
if(this.keyHoldTimeout) {
window.clearTimeout(this.keyHoldTimeout);
}
// Clear interval to make sure multiple keypress does not continue
if(this.keyHoldInterval) {
window.clearInterval(this.keyHoldInterval);
}
}
};
// We would like to pipe all keyboard events through one handler
var handleKeydownEvents = function (e) {
var self = this;
e.preventDefault();
// Check to make sure it's a key that's pressed
if (!e.target.classList.contains("keyboard-key")) return;
// the :active pseudo class is not working with touch events
// use active class to get around this issue
e.target.classList.add('active');
var keyInfo = e.target.dataset;
self.listeners['key'].forEach(function(action){
action(keyInfo);
});
// Allow for a key to be input multiple times by holding it down
// Ensure that any current timers are cleared before continuing
self.clearKeyHoldTimers();
// Timeout will provide a delay to prevent accidental holding
// After that, the interval will provide repeated input
self.keyHoldTimeout = window.setTimeout(function() {
self.keyHoldInterval = window.setInterval(function() {
self.listeners['key'].forEach(function(action){
action(keyInfo);
});
}, 100)
}, 400)
};
var handleKeyupEvents = function (e) {
e.target.classList.remove('active');
// Clear timeout to make sure multiple keypress does not start
this.clearKeyHoldTimers();
}
global.Keyboard = Keyboard;
global.Keyboard.layout = {
_default: [
[
{"symbol": "Q"},
{"symbol": "W"},
{"symbol": "E"},
{"symbol": "R"},
{"symbol": "T"},
{"symbol": "Y"},
{"symbol": "U"},
{"symbol": "I"},
{"symbol": "O"},
{"symbol": "P"},
{"label": ".com", "symbol": ".COM"},
{"symbol": "7"},
{"symbol": "8"},
{"symbol": "9"},
{"label": "\u21E6", "symbol":"backspace"}
],
[
// {"label": "tab", "func": "tab"},
{"symbol": "A"},
{"symbol": "S"},
{"symbol": "D"},
{"symbol": "F"},
{"symbol": "G"},
{"symbol": "H"},
{"symbol": "J"},
{"symbol": "K"},
{"symbol": "L"},
{"symbol": "@"},
{"label": ".net", "symbol": ".NET"},
{"symbol": "4"},
{"symbol": "5"},
{"symbol": "6"}
],
[
{"symbol": "Z"},
{"symbol": "X"},
{"symbol": "C"},
{"symbol": "V"},
{"symbol": "B"},
{"symbol": "N"},
{"symbol": "M"},
{"symbol": "."},
{"symbol": "_"},
{"symbol": "-"},
{"symbol": " ", "label": "space"},
{"symbol": "0"},
{"symbol": "1"},
{"symbol": "2"},
{"symbol": "3"}
]
]
};
})(exports || {}, window);