unified the handler code, no redundency now...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-01-18 18:00:38 +03:00
parent d5549a3813
commit 9c4842b8bd
2 changed files with 42 additions and 65 deletions

View File

@ -120,7 +120,7 @@ module.GLOBAL_KEYBOARD2 = {
}, },
// XXX add "save as collection..." // XXX add "save as collection..."
'Cropped': { 'Crop': {
pattern: '.crop-mode', pattern: '.crop-mode',
Esc: 'uncrop', Esc: 'uncrop',
@ -580,58 +580,12 @@ var KeyboardActions = actions.Actions({
`, `,
{ keepDialogTitle: true }, { keepDialogTitle: true },
function(key, no_match){ function(key, no_match){
var that = this // get/set the handler...
var did_handling = false var handler = this.__key_press_handler =
var res this.__key_press_handler
var evt = event || keyboard.makeKeyboardHandler(this.keyboard, null, this)
// do the call...
//if(key instanceof Event || key instanceof $.Event){ return handler(key, no_match)
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
}], }],
toggleKeyboardHandling: ['- Interface/Keyboard handling', toggleKeyboardHandling: ['- Interface/Keyboard handling',
toggler.Toggler(null, function(_, state){ toggler.Toggler(null, function(_, state){
@ -1121,7 +1075,7 @@ var KeyboardActions = actions.Actions({
}) })
return dialog return dialog
})], })],
// XXX need a way to abort edits... // XXX make fields editable...
editKeyBinding: ['- Interface/Key mapping...', editKeyBinding: ['- Interface/Key mapping...',
widgets.makeUIDialog(function(mode, code){ widgets.makeUIDialog(function(mode, code){
var that = this var that = this
@ -1187,7 +1141,6 @@ var KeyboardActions = actions.Actions({
return dialog return dialog
})], })],
// XXX make fields editable... // XXX make fields editable...
// XXX need a way to abort edits...
editKeyboardMode: ['- Interface/Mode...', editKeyboardMode: ['- Interface/Mode...',
widgets.makeUIDialog(function(mode){ widgets.makeUIDialog(function(mode){
var that = this var that = this

View File

@ -872,6 +872,22 @@ KeyboardWithCSSModes.prototype.__proto__ = Keyboard.prototype
// Base event handler wrapper of Keyboard... // 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 = var makeKeyboardHandler =
module.makeKeyboardHandler = module.makeKeyboardHandler =
function makeKeyboardHandler(keyboard, unhandled, actions){ function makeKeyboardHandler(keyboard, unhandled, actions){
@ -881,11 +897,18 @@ function makeKeyboardHandler(keyboard, unhandled, actions){
//: Keyboard(keyboard, checkGlobalMode) //: Keyboard(keyboard, checkGlobalMode)
: Keyboard(keyboard) : Keyboard(keyboard)
return function(evt){ return function(key, no_match){
var res = undefined no_match = no_match || unhandled
var did_handling = false 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) var handlers = kb.handler('test', key)
Object.keys(handlers).forEach(function(mode){ Object.keys(handlers).forEach(function(mode){
@ -906,23 +929,24 @@ function makeKeyboardHandler(keyboard, unhandled, actions){
if(h && h.action in actions){ if(h && h.action in actions){
did_handling = true did_handling = true
h.no_default evt
&& h.no_default
&& evt.preventDefault() && evt.preventDefault()
// call the handler... // call the handler...
res = actions[h.action].apply(actions, h.arguments) res = actions[h.action].apply(actions, h.arguments)
if(h.stop_propagation){ evt
res = false && h.stop_propagation
evt.stopPropagation() && evt.stopPropagation()
} && (res = false)
} }
} }
}) })
unhandled no_match
&& !did_handling && !did_handling
&& unhandled.call(actions, evt) && no_match.call(actions, evt, key)
return res return res
} }