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..."
'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

View File

@ -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
}