2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
2012-05-21 10:38:18 +00:00
|
|
|
|
2011-11-01 11:49:46 +00:00
|
|
|
/*@
|
|
|
|
Ox.Keyboard <o> Basic keyboard handler
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-11-01 11:49:46 +00:00
|
|
|
Ox.Keyboard = (function() {
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-11-01 11:49:46 +00:00
|
|
|
var buffer = '', bound = [], resetTimeout, triggerTimeout;
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-01 12:32:07 +00:00
|
|
|
Ox.UI.ready(function() {
|
2012-02-15 10:43:40 +00:00
|
|
|
Ox.$document.keydown(keydown)
|
2011-05-01 12:32:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function keydown(event) {
|
2011-05-05 18:02:56 +00:00
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
var focused = Ox.Focus.focused(),
|
|
|
|
key,
|
2011-05-05 18:02:56 +00:00
|
|
|
keyName = Ox.KEYS[event.keyCode] || '',
|
|
|
|
keyNames = keyName ? [keyName] : [],
|
2011-05-01 12:32:07 +00:00
|
|
|
keyBasename = keyName.split('.')[0],
|
2011-05-05 18:02:56 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
Ox.forEach(Ox.MODIFIER_KEYS, function(v, k) {
|
2011-05-01 12:32:07 +00:00
|
|
|
// avoid pushing modifier twice
|
2012-04-23 10:28:42 +00:00
|
|
|
// using event.originalEvent since jquery always sets
|
|
|
|
// event.metaKey to event.ctrlKey
|
2011-06-04 12:14:39 +00:00
|
|
|
if (event.originalEvent[k] && keyBasename != v) {
|
2011-05-05 18:02:56 +00:00
|
|
|
keyNames.splice(-1, 0, v);
|
2011-05-01 12:32:07 +00:00
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-05-05 18:02:56 +00:00
|
|
|
key = keyNames.join('_');
|
2011-11-02 00:46:42 +00:00
|
|
|
if (focused === null || !Ox.UI.elements[focused].hasClass('OxInput')) {
|
|
|
|
bound.forEach(function(id) {
|
|
|
|
Ox.UI.elements[id].triggerEvent('key_' + key);
|
|
|
|
});
|
|
|
|
}
|
2011-11-01 11:49:46 +00:00
|
|
|
if (focused !== null && bound.indexOf(focused) == -1) {
|
2011-05-05 18:02:56 +00:00
|
|
|
Ox.UI.elements[focused].triggerEvent('key_' + key);
|
2011-05-20 07:14:24 +00:00
|
|
|
// prevent Chrome from scrolling, or going back in history
|
2011-05-05 18:02:56 +00:00
|
|
|
if (
|
|
|
|
[
|
2011-05-20 07:14:24 +00:00
|
|
|
'backspace', 'down', 'left', 'right', 'space', 'up'
|
2011-11-02 00:46:42 +00:00
|
|
|
].indexOf(key) > -1
|
|
|
|
&& !Ox.UI.elements[focused].hasClass('OxInput')
|
|
|
|
&& !Ox.UI.elements[focused].hasClass('OxAutocompleteMenu')
|
2011-05-20 07:14:24 +00:00
|
|
|
) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
// prevent cursor in input field from moving to start or end
|
|
|
|
if (
|
2011-11-02 00:46:42 +00:00
|
|
|
['down', 'up'].indexOf(key) > -1
|
|
|
|
&& Ox.UI.elements[focused].hasClass('OxAutocompleteMenu')
|
2011-05-05 18:02:56 +00:00
|
|
|
) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 07:14:24 +00:00
|
|
|
if (/^[\w\d](\.numpad)?$|^space$/.test(key)) {
|
2011-05-01 12:32:07 +00:00
|
|
|
// don't register leading spaces or trailing double spaces
|
|
|
|
if (!(keyName == 'space' && (buffer == '' || / $/.test(buffer)))) {
|
|
|
|
buffer += keyName == 'space' ? ' ' : keyBasename;
|
|
|
|
// clear the trigger timeout only if the key went into the buffer
|
|
|
|
clearTimeout(triggerTimeout);
|
|
|
|
triggerTimeout = setTimeout(function() {
|
|
|
|
focused !== null && Ox.UI.elements[focused].triggerEvent('keys', {
|
|
|
|
keys: buffer
|
|
|
|
});
|
2011-05-24 14:32:03 +00:00
|
|
|
}, 25);
|
|
|
|
// fixme: used to be 250, but seemed too much.
|
|
|
|
// if it stays at 25, we may not need a timeout at all
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 12:32:07 +00:00
|
|
|
// clear the reset timeout even if the key didn't go into the buffer
|
|
|
|
clearTimeout(resetTimeout);
|
|
|
|
resetTimeout = setTimeout(function() {
|
|
|
|
buffer = '';
|
|
|
|
}, 1000);
|
2011-05-05 18:02:56 +00:00
|
|
|
|
2012-06-29 08:23:59 +00:00
|
|
|
//Firefox will cancel active XMLHttpRequests otherwise
|
|
|
|
if (keyName == 'escape') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
return ret;
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 11:49:46 +00:00
|
|
|
return {
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
bind <f> bind
|
|
|
|
(id) -> <u> bind id
|
|
|
|
@*/
|
2011-11-01 11:49:46 +00:00
|
|
|
bind: function(id) {
|
|
|
|
var index = bound.indexOf(id);
|
|
|
|
index == -1 && bound.push(id);
|
|
|
|
},
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
unbind <f> unbind
|
|
|
|
(id) -> <u> unbind id
|
|
|
|
@*/
|
2011-11-01 11:49:46 +00:00
|
|
|
unbind: function(id) {
|
|
|
|
var index = bound.indexOf(id);
|
|
|
|
index > -1 && bound.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-26 12:38:45 +00:00
|
|
|
}());
|