2011-04-23 16:45:50 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=js
|
2011-04-22 22:03:10 +00:00
|
|
|
/***
|
|
|
|
Ox.Keyboard
|
|
|
|
***/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
var buffer = '', resetTimeout, triggerTimeout;
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-01 12:32:07 +00:00
|
|
|
/*
|
2011-04-22 22:03:10 +00:00
|
|
|
Ox.UI.ready(function() {
|
|
|
|
// fixme: how to do this better?
|
|
|
|
// in firefox on mac, keypress doesn't fire for up/down
|
|
|
|
// if the cursor is at the start/end of an input element
|
|
|
|
// on linux, it doesn't seem to fire if the input element has focus
|
|
|
|
if ($.browser.mozilla) {
|
|
|
|
Ox.UI.$document.keypress(keypress);
|
|
|
|
Ox.UI.$document.keydown(function(event) {
|
|
|
|
var $element = $('input:focus');
|
|
|
|
if ($element.length) {
|
|
|
|
if (
|
|
|
|
(
|
2011-05-05 18:02:56 +00:00
|
|
|
Ox.KEYS[event.keyCode] == 'up' &&
|
2011-04-22 22:03:10 +00:00
|
|
|
$element[0].selectionStart + $element[0].selectionEnd == 0
|
|
|
|
) || (
|
2011-05-05 18:02:56 +00:00
|
|
|
Ox.KEYS[event.keyCode] == 'down' &&
|
2011-04-22 22:03:10 +00:00
|
|
|
$element[0].selectionStart == $element.val().length &&
|
|
|
|
$element[0].selectionEnd == $element.val().length
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
keypress(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Ox.UI.$document.keydown(keypress);
|
|
|
|
}
|
|
|
|
});
|
2011-05-01 12:32:07 +00:00
|
|
|
*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-01 12:32:07 +00:00
|
|
|
Ox.UI.ready(function() {
|
|
|
|
Ox.UI.$document.keydown(keydown)
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
if (event[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) {
|
|
|
|
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'
|
|
|
|
].indexOf(key) > -1 &&
|
|
|
|
!Ox.UI.elements[focused].hasClass('OxInput') &&
|
|
|
|
!Ox.UI.elements[focused].hasClass('OxAutocompleteMenu')
|
|
|
|
) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
// prevent cursor in input field from moving to start or end
|
|
|
|
if (
|
|
|
|
['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
|
|
|
|
});
|
|
|
|
}, 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
|
|
|
|
|
|
|
Ox.print(ret)
|
|
|
|
return ret;
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|