fix keyboard handler

This commit is contained in:
rlx 2014-09-24 22:28:56 +02:00
parent 853a402a7a
commit ac50af0509

View file

@ -9,7 +9,7 @@
slash: '/', slash: '/',
space: ' ' space: ' '
}, },
hasCallback = {}, keyboardCallbacks = {},
keyboardEventRegExp = /^key(\.[\w\d.]+)?$/, keyboardEventRegExp = /^key(\.[\w\d.]+)?$/,
keys = '', keys = '',
keysEventRegExp = new RegExp( keysEventRegExp = new RegExp(
@ -21,7 +21,8 @@
function bind(options) { function bind(options) {
var args = Ox.slice(arguments, 1), var args = Ox.slice(arguments, 1),
callbacks = options.callbacks, callbacks = options.callbacks,
that = this; that = this,
oxid = that.oxid || 0;
Ox.forEach( Ox.forEach(
Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args), Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args),
function(originalCallback, event) { function(originalCallback, event) {
@ -36,7 +37,9 @@
: originalCallback : originalCallback
); );
if (isKeyboardEvent(event)) { if (isKeyboardEvent(event)) {
hasCallback[event] = true; keyboardCallbacks[oxid] = (
keyboardCallbacks[oxid] || []
).concat(event);
} }
} }
); );
@ -72,10 +75,8 @@
} }
function onKeydown(e) { function onKeydown(e) {
if (Ox.Focus.focusedElementIsInput()) { var $element = Ox.Focus.focusedElement() || Ox.$body,
return; isInput = Ox.Focus.focusedElementIsInput(),
}
var $element = Ox.Focus.focusedElement(),
keyName = Ox.KEYS[e.keyCode], keyName = Ox.KEYS[e.keyCode],
keyBasename = keyName.split('.')[0], keyBasename = keyName.split('.')[0],
key = Object.keys(Ox.MODIFIER_KEYS).filter(function(key) { key = Object.keys(Ox.MODIFIER_KEYS).filter(function(key) {
@ -85,15 +86,16 @@
}).concat(keyName).join('_'), }).concat(keyName).join('_'),
event = 'key.' + key, event = 'key.' + key,
triggerEvent = function() { triggerEvent = function() {
if ($element) { if (Ox.Focus.focusedElement()) {
$element.triggerEvent.apply($element, arguments); $element.triggerEvent.apply($element, arguments);
} else { } else if (!isInput) {
Ox.Event.trigger.apply( Ox.Event.trigger.apply(
Ox.$body, [{}].concat(Ox.slice(arguments)) $element, [{}].concat(Ox.slice(arguments))
); );
} }
}; };
triggerEvent(event, e); triggerEvent(event, e);
if (!isInput) {
if (isKeysEventKey(key)) { if (isKeysEventKey(key)) {
// don't register leading spaces or trailing double spaces // don't register leading spaces or trailing double spaces
if (keyName != 'space' || ( if (keyName != 'space' || (
@ -112,10 +114,13 @@
resetTimeout = setTimeout(function() { resetTimeout = setTimeout(function() {
keys = ''; keys = '';
}, 1000); }, 1000);
if (hasCallback[event]) { if (
Ox.contains(keyboardCallbacks[$element.oxid || 0] || [], event)
) {
e.preventDefault(); e.preventDefault();
} }
} }
}
function trigger(options) { function trigger(options) {
var args = Ox.slice(arguments, 1), var args = Ox.slice(arguments, 1),
@ -138,7 +143,8 @@
function unbind(options) { function unbind(options) {
var args = Ox.slice(arguments, 1), var args = Ox.slice(arguments, 1),
callbacks = options.callbacks; callbacks = options.callbacks,
oxid = this.oxid || 0;
if (args.length == 0) { if (args.length == 0) {
// unbind all handlers for all events // unbind all handlers for all events
callbacks = []; callbacks = [];
@ -161,8 +167,14 @@
delete callbacks[event]; delete callbacks[event];
} }
} }
if (isKeyboardEvent(event) && !callbacks[event]) { if (isKeyboardEvent(event)) {
delete hasCallback[event]; var index = keyboardCallbacks[oxid].indexOf(event);
keyboardCallbacks[oxid].splice(
keyboardCallbacks[oxid].indexOf(event), 1
)
if (keyboardCallbacks[oxid].length == 0) {
delete keyboardCallbacks[oxid];
}
} }
} }
); );