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
|
|
|
/*@
|
2012-07-04 11:29:18 +00:00
|
|
|
Ox.Keyboard <o> Basic keyboard controller
|
2011-11-01 11:49:46 +00:00
|
|
|
@*/
|
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() {
|
2013-08-04 09:45:20 +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(),
|
2012-11-03 16:19:35 +00:00
|
|
|
$focused = focused === null ? null : Ox.UI.elements[focused],
|
2011-04-22 22:03:10 +00:00
|
|
|
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('_');
|
2012-11-03 16:39:30 +00:00
|
|
|
if (
|
|
|
|
focused === null || (
|
|
|
|
!$focused.hasClass('OxInput')
|
2013-02-25 16:40:50 +00:00
|
|
|
&& !$focused.hasClass('OxEditableContent')
|
2012-11-03 16:39:30 +00:00
|
|
|
&& !$focused.hasClass('OxAutocompleteMenu')
|
|
|
|
)
|
|
|
|
) {
|
2011-11-02 00:46:42 +00:00
|
|
|
bound.forEach(function(id) {
|
|
|
|
Ox.UI.elements[id].triggerEvent('key_' + key);
|
|
|
|
});
|
2013-07-15 11:33:17 +00:00
|
|
|
// Don't open Chrome Inspect Element, used for copyadd
|
|
|
|
if (bound.length && key == 'control_shift_c') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2013-02-19 12:53:08 +00:00
|
|
|
// Firefox opens quick find on slash and quick link find on quote otherwise
|
|
|
|
if (keyName == 'slash' || keyName == 'quote') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2011-11-02 00:46:42 +00:00
|
|
|
}
|
2011-11-01 11:49:46 +00:00
|
|
|
if (focused !== null && bound.indexOf(focused) == -1) {
|
2012-11-03 16:19:35 +00:00
|
|
|
$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
|
2012-11-03 16:19:35 +00:00
|
|
|
&& !$focused.hasClass('OxInput')
|
2013-02-25 16:40:50 +00:00
|
|
|
&& !$focused.hasClass('OxEditableContent')
|
2012-11-03 16:19:35 +00:00
|
|
|
&& !$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
|
2012-11-03 16:19:35 +00:00
|
|
|
&& $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() {
|
2012-06-30 09:21:23 +00:00
|
|
|
if (focused !== null) {
|
2012-11-03 16:19:35 +00:00
|
|
|
$focused.triggerEvent('keys', {keys: buffer});
|
2012-06-30 09:21:23 +00:00
|
|
|
}
|
|
|
|
}, 250);
|
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-30 09:21:23 +00:00
|
|
|
// Firefox cancels active XMLHttpRequests when pressing escape
|
2012-06-29 08:23:59 +00:00
|
|
|
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
|
|
|
}());
|