-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.js
347 lines (293 loc) · 10 KB
/
keys.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
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
(function (window) {
"use strict";
/**
* Create a new Keys object
*
* options:
*
* @constructor
* @param {Object} syms The json object describing the symbols for the keyboard.
* @param {Object} opt Options (need to document these)
* @return {Object} exports for chaining
*/
var Keys = function (el, syms, opt) {
if(_ === undefined){
console.log("this library requires underscore.js");
throw "underscore.js (_) not found";
}
this.symbols = syms;
this.options = opt ? opt : {};
//we haven't rendered anything yet
this.board = false;
this.input = el; //the currently focused input
this.keys = new Array();
//create the keys
for (var i = this.symbols.length - 1; i >= 0; i--) {
this.addKey(this.symbols[i]);
};
if(!this.options.buildLater){
this.build();
}
return this;
};
/**
* Add a Key
*
* @param {Object} key The symbol to turn into a key, or a Key object
* @return {Object} returns the created key for modification.
*/
Keys.prototype.addKey = function(key){
var self = this;
var newKey = (key instanceof Keys.Key)?key:new Keys.Key(key);
var keyReleased = function(){
newKey.hitButton(self.input);
}
newKey.button.addEventListener('touchend', keyReleased, false);
if (self.options.debug && !Keys.isMobile()) {
newKey.button.addEventListener('click', keyReleased, false);
}
self.keys.push(newKey);
return newKey;
}
/**
* Check if the keyboard element has class cls attached to it
*
* @param {String} cls A class to check for
* @return {Boolean} true if the class exists false otherwise
*/
Keys.prototype.hasClass = function (cls) {
return this.board.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
/**
* add a class to the keyboard if it doesn't already have it
* uses hasClass
*
* @param {String} cls A class to add
*/
Keys.prototype.addClass = function (cls) {
if (!this.hasClass(cls)) this.board.className += " " + cls;
}
/**
* Remove a class to the keyboard
* uses hasClass
*
* @param {String} cls A class to remove
*/
Keys.prototype.removeClass = function (cls) {
if (this.hasClass(cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
this.board.className = this.board.className.replace(reg, ' ');
}
}
/**
* Update the orientation of the device
*
* @return {String} returns the string of the current orientation.
*/
Keys.prototype.orientation = function () {
if (window.orientation == 0 || 180) {
this.removeClass("landscape");
this.addClass("portrait");
return "portrait";
} else {
this.removeClass("portrait");
this.addClass("landscape");
return "landscape";
}
}
/**
* Inserts the given text into the text element at the current cursor position.
* For use with elements that don't support replaceRange
*
* @param {Element} a html editable element.
* @param {String} the text to insert.
*/
Keys.insertAtCaret = function(el,template) {
var txtarea = el;
var scrollPos = txtarea.scrollTop;
var strPos = 0;
strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0,strPos);
var back = (txtarea.value).substring(txtarea.selectionEnd,txtarea.value.length);
var selectedText = txtarea.value.substring(txtarea.selectionStart, txtarea.selectionEnd);
var text = template({selection: selectedText});
txtarea.value = front+text+back;
strPos = strPos + text.length;
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
txtarea.scrollTop = scrollPos;
return true;
}
/**
* Ataches blur and focus listeners to *inputs*
* to control showing and hiding the keyboard.
*
* @param {Array} inputs An Array of inputs, should be text areas, fields, content editable areas etc.
*/
Keys.prototype._attachInputListeners = function(){
var self = this;
// @todo Code mirror specific code
// currentMirror.setOption('onFocus', function () {
// self.input = currentMirror;
// self.show();
// });
// currentMirror.setOption('onBlur', function () {
// self.hide();
// });
self.input.addEventListener('focus', function () {
self.show();
}, false);
self.input.addEventListener('blur', function () {
self.hide();
}, false);
};
/**
* Constructs the actual virtual keyboard.
*
* @todo rebuild
*/
Keys.prototype.build = function () {
var self = this;
//show if we're mobile
if (this.options.debug || Keys.isMobile) {
if (!self.board) {
self.board = document.createElement('div');
self.board.className = "keyboard";
document.body.appendChild(self.board);
//prevent wierd iOS behavior
self.board.addEventListener('selectstart', function(event){event.preventDefault(); return false;}, false);
self.board.addEventListener('select', function(event){event.preventDefault(); return false;}, false);
}
//create the keys
self.keys.forEach(function (key) {
self.board.appendChild(key.button);
});
//get orientation
self.orientation();
document.body.addEventListener('orientationchange', function (event) {
self.orientation();
}, false);
self._attachInputListeners();
window.addEventListener('scroll', function () {
if (self.input) {
self.board.style.top = window.pageYOffset + "px";
self.board.style.left = window.pageXOffset + "px";
}
}, false);
window.addEventListener('resize', function () {
if (self.input) {
self.board.style.top = window.pageYOffset + "px";
self.board.style.left = window.pageXOffset + "px";
self.board.style.width = window.innerWidth + "px";
}
}, false);
}
return this;
};
/**
* Hide the keybaord
* uses removeClass
* calls the onHide funciton from options
*
*/
Keys.prototype.hide = function () {
this.removeClass('visible');
//this.board.style.top = "-60px";
if(this.options.onHide){
return this.options.onHide();
}
};
/**
* Show the keybaord
* uses addClass
* calls the onShow funciton from options
*
*/
Keys.prototype.show = function () {
var self = this;
this.addClass('visible');
self.board.style.top = window.pageYOffset + "px";
self.board.style.left = window.pageXOffset + "px";
self.board.style.width = window.innerWidth + "px";
if(self.options.onShow){
return self.options.onShow();
}
};
/**
* Create a new Key object
*
*
* @constructor
* @param {Object} key the json object defining the key
* @return {Object} exports for chaining
*/
var Key = function(key){
var self = this;
var button = document.createElement('a');
button.value = key.value?key.value:key;
button.innerHTML = key.display?key.display:key;
button.className = "key";
button.addEventListener('touchmove', function(){
self.justMoved = true;
});
button.addEventListener('mousedown', function (event) {
event.preventDefault();
}, false);
button.addEventListener('mouseup', function (event) {
event.preventDefault();
}, false);
this.button = button;
this.behavior = key.behavior;
return this;
};
/**
* The action to be taken when a button is pressed.
*
* Change the prototype to change the behavior of all keys
* or change just one key's hitButton to change only it's behavior
*
* @param {Element} input the input area to take the key action in.
*
*/
Key.prototype.hitButton = function (input) {
var self = this;
if(self.justMoved){
self.justMoved = false;
return;
}
//self.el.removeEventListener('touchend', self.hitButton, false);
event.preventDefault();
// console.log(self);
var button_template = _.template(self.button.value);
//
console.log(input);
Keys.insertAtCaret(input, button_template);
if(self.behavior){
self.behavior();
}
// if (input.replaceRange) {
// var cursor_temp = self.input.getCursor(true);
// input.replaceRange(value, cursor_temp);
// } else {
// Keys.insertAtCaret(input, button_template);
// }
};
Keys.Key = Key;
/**
* Test if we are in a mobile browser, currently only checks for 'i' things
*/
Keys.isMobile = function(){
return (navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1) ||
(navigator.userAgent.indexOf('iPad') != -1);
}
/**
* try and make an AMD module out of this, if we can't return a global
*/
if ( typeof define === "function" && define.amd) {
define( "keys", [], function () { return Keys; } );
} else {
window.Keys = Keys;
}
})(window);