From 9c4842b8bd939541170fe8b9363303c380d12df4 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 18 Jan 2017 18:00:38 +0300 Subject: [PATCH] unified the handler code, no redundency now... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/keyboard.js | 63 +++++----------------------------- ui (gen4)/lib/keyboard.js | 44 ++++++++++++++++++------ 2 files changed, 42 insertions(+), 65 deletions(-) diff --git a/ui (gen4)/features/keyboard.js b/ui (gen4)/features/keyboard.js index 0c032fd7..554c7955 100755 --- a/ui (gen4)/features/keyboard.js +++ b/ui (gen4)/features/keyboard.js @@ -120,7 +120,7 @@ module.GLOBAL_KEYBOARD2 = { }, // XXX add "save as collection..." - 'Cropped': { + 'Crop': { pattern: '.crop-mode', Esc: 'uncrop', @@ -580,58 +580,12 @@ var KeyboardActions = actions.Actions({ `, { keepDialogTitle: true }, function(key, no_match){ - var that = this - var did_handling = false - var res - var evt = event - - //if(key instanceof Event || key instanceof $.Event){ - if(typeof(key) != typeof('str')){ - evt = key - key = keyboard.event2key(evt) - } - - var handlers = this.keyboard.handler('?', key) - - Object.keys(handlers).forEach(function(mode){ - if(res === false){ - return - } - - var handler = handlers[mode] - - // raw function handler... - if(handler instanceof Function){ - res = handler.call(that) - - // action call syntax... - } else { - var h = keyboard.parseActionCall(handler) - - if(h && h.action in that){ - did_handling = true - - evt - && h.no_default - && evt.preventDefault() - - // call the handler... - res = that[h.action].apply(that, h.arguments) - - evt - && h.stop_propagation - && evt.stopPropagation() - && (res = false) - } - } - }) - - no_match - && !did_handling - && no_match.call(this, evt, key) - - // XXX not sure if this is the right way to go... - return res + // get/set the handler... + var handler = this.__key_press_handler = + this.__key_press_handler + || keyboard.makeKeyboardHandler(this.keyboard, null, this) + // do the call... + return handler(key, no_match) }], toggleKeyboardHandling: ['- Interface/Keyboard handling', toggler.Toggler(null, function(_, state){ @@ -1121,7 +1075,7 @@ var KeyboardActions = actions.Actions({ }) return dialog })], - // XXX need a way to abort edits... + // XXX make fields editable... editKeyBinding: ['- Interface/Key mapping...', widgets.makeUIDialog(function(mode, code){ var that = this @@ -1187,7 +1141,6 @@ var KeyboardActions = actions.Actions({ return dialog })], // XXX make fields editable... - // XXX need a way to abort edits... editKeyboardMode: ['- Interface/Mode...', widgets.makeUIDialog(function(mode){ var that = this diff --git a/ui (gen4)/lib/keyboard.js b/ui (gen4)/lib/keyboard.js index e9501655..113bed6f 100755 --- a/ui (gen4)/lib/keyboard.js +++ b/ui (gen4)/lib/keyboard.js @@ -872,6 +872,22 @@ KeyboardWithCSSModes.prototype.__proto__ = Keyboard.prototype // Base event handler wrapper of Keyboard... // +// This will produce a handler that can be used in one of two ways: +// - event handler +// - an event is passed as the only argument +// - the function can be used directly as an event handler +// - direct key handler +// - a key and optionally a no_match handler are passed +// +// Example: +// var handler = makeKeyboardHandler(kb, null, action) +// +// // event handler... +// $(window).keydown(handler) +// +// // used directly... +// handler('ctrl_C', function(k){ console.log('Not bound:', k) }) +// var makeKeyboardHandler = module.makeKeyboardHandler = function makeKeyboardHandler(keyboard, unhandled, actions){ @@ -881,11 +897,18 @@ function makeKeyboardHandler(keyboard, unhandled, actions){ //: Keyboard(keyboard, checkGlobalMode) : Keyboard(keyboard) - return function(evt){ - var res = undefined + return function(key, no_match){ + no_match = no_match || unhandled var did_handling = false + var evt = event + var res + + //if(key instanceof Event || key instanceof $.Event){ + if(typeof(key) != typeof('str')){ + evt = key + key = kb.event2key(evt) + } - var key = kb.event2key(evt) var handlers = kb.handler('test', key) Object.keys(handlers).forEach(function(mode){ @@ -906,23 +929,24 @@ function makeKeyboardHandler(keyboard, unhandled, actions){ if(h && h.action in actions){ did_handling = true - h.no_default + evt + && h.no_default && evt.preventDefault() // call the handler... res = actions[h.action].apply(actions, h.arguments) - if(h.stop_propagation){ - res = false - evt.stopPropagation() - } + evt + && h.stop_propagation + && evt.stopPropagation() + && (res = false) } } }) - unhandled + no_match && !did_handling - && unhandled.call(actions, evt) + && no_match.call(actions, evt, key) return res }