oxjs/source/Ox.UI/js/Core/Keyboard.js

123 lines
3.9 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2012-05-21 10:38:18 +00:00
/*@
Ox.Keyboard <o> Basic keyboard controller
@*/
2011-04-22 22:03:10 +00:00
Ox.Keyboard = (function() {
2011-04-22 22:03:10 +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() {
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
// using event.originalEvent since jquery always sets
// event.metaKey to event.ctrlKey
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('_');
if (
focused === null || (
!$focused.hasClass('OxInput')
2013-02-25 16:40:50 +00:00
&& !$focused.hasClass('OxEditableContent')
&& !$focused.hasClass('OxAutocompleteMenu')
)
) {
bound.forEach(function(id) {
Ox.UI.elements[id].triggerEvent('key_' + key);
});
// Don't open Chrome Inspect Element, used for copyadd
if (bound.length && key == 'control_shift_c') {
event.preventDefault();
}
// Firefox opens quick find on slash and quick link find on quote otherwise
if (keyName == 'slash' || keyName == 'quote') {
event.preventDefault();
}
}
if (focused !== null && bound.indexOf(focused) == -1) {
2012-11-03 16:19:35 +00:00
$focused.triggerEvent('key_' + key);
// prevent Chrome from scrolling, or going back in history
2011-05-05 18:02:56 +00:00
if (
[
'backspace', 'down', 'left', 'right', 'space', 'up'
].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')
) {
ret = false;
}
// prevent cursor in input field from moving to start or end
if (
['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;
}
}
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() {
if (focused !== null) {
2012-11-03 16:19:35 +00:00
$focused.triggerEvent('keys', {keys: buffer});
}
}, 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
// Firefox cancels active XMLHttpRequests when pressing escape
if (keyName == 'escape') {
event.preventDefault();
}
2011-05-05 18:02:56 +00:00
return ret;
2011-04-22 22:03:10 +00:00
}
return {
2012-05-21 10:38:18 +00:00
/*@
bind <f> bind
(id) -> <u> bind id
@*/
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
@*/
unbind: function(id) {
var index = bound.indexOf(id);
index > -1 && bound.splice(index, 1);
}
};
}());