added action arguments support to keyboard.js...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-11-05 16:19:37 +03:00
parent 9a903a3f7b
commit d6218a10f0
4 changed files with 65 additions and 18 deletions

View File

@ -293,8 +293,8 @@ stretching in width... */
.shadow { .shadow {
position: absolute; position: absolute;
overflow: visible; overflow: visible;
width: 300px; width: auto;
height: 300px; height: auto;
background: black; background: black;
-webkit-transition: all 0.1s ease-in; -webkit-transition: all 0.1s ease-in;
-moz-transition: all 0.1s ease-in; -moz-transition: all 0.1s ease-in;

View File

@ -361,6 +361,7 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
} else if(handler in keybindings){ } else if(handler in keybindings){
handler = keybindings[handler] handler = keybindings[handler]
/*
// action name... // action name...
} else if(handler in actions){ } else if(handler in actions){
// build a handler... // build a handler...
@ -375,25 +376,57 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
// make this doc-generator compatible -- inherit all // make this doc-generator compatible -- inherit all
// the docs from the actual action... // the docs from the actual action...
f.__proto__ = actions[n] f.__proto__ = actions[n]
// tell the doc generator about the action stuff...
f.action = n
return f return f
}(handler) }(handler)
*/
// action name with '!' -- prevent default... // actions...
} else if(handler.slice(-1) == '!' //
&& handler.slice(0, -1) in actions){ // supported action format:
// build a handler... // <actio-name>[!][: <args>]
// ...for the reasons why this is like it is see notes //
// for the previous if... // <args> can contain space seporated:
handler = function(n){ // - numbers
var f = function(){ // - strings
event.preventDefault() // - non-nested arrays or arrays
return actions[n]() } else if(handler in actions
|| handler.split(/!?\s*:\s*|!/)[0].trim() in actions){
var c = handler.split(':')
handler = c[0].trim()
var no_default = handler.slice(-1) == '!'
handler = no_default ? handler.slice(0, -1) : handler
var args = JSON.parse('['+(
((c[1] || '')
.match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*/gm)
|| [])
.join(','))+']')
handler = function(n, no_default, args){
if(no_default){
var f = function(){
event.preventDefault()
return actions[n].apply(actions, args)
}
} else {
var f = function(){
return actions[n].apply(actions, args)
}
} }
// make this doc-generator compatible -- inherit all // make this doc-generator compatible -- inherit all
// the docs from the actual action... // the docs from the actual action...
f.__proto__ = actions[n] f.__proto__ = actions[n]
// tell the doc generator about the action stuff...
f.action = n
f.args = args
f.no_default = no_default
return f return f
}(handler.slice(0, -1)) }(handler, no_default, args.slice())
// key code... // key code...
} else if(typeof(handler) == typeof(1)) { } else if(typeof(handler) == typeof(1)) {
@ -549,6 +582,18 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
* <alias> will be matched to key definitions in sections and in the key * <alias> will be matched to key definitions in sections and in the key
* binding object itself and in an actions object if given. * binding object itself and in an actions object if given.
* *
* <alias> syntax examples:
* actionName - simple alias / action name
* actionName! - same as above but calls event.preventDefault()
* actionNmae: 1 "2" [3, 4] {5:6, 7:8}
* - action with arguments.
* arguments can be:
* - numbers
* - strings
* - non-nested arrays and/or objects
* actionNmae!: 1 "2" [3, 4] {5:6, 7:8}
* - same as above but calls event.preventDefault()
*
* *
* *
* NOTE: The handler will be called with keybindings as context (this). * NOTE: The handler will be called with keybindings as context (this).

View File

@ -234,6 +234,8 @@ module.RibbonsPrototype = {
// -> <ribbons> // -> <ribbons>
// //
// NOTE: this will also set origin... // NOTE: this will also set origin...
//
// XXX if chrome 38 renders images blurry uncomment the fix...
setScale: function(scale, t, l){ setScale: function(scale, t, l){
var ribbon_set = this.viewer.find('.ribbon-set') var ribbon_set = this.viewer.find('.ribbon-set')

View File

@ -135,10 +135,10 @@ module.GLOBAL_KEYBOARD = {
shift: 'fitRibbon', shift: 'fitRibbon',
ctrl: 'fitOrig!', ctrl: 'fitOrig!',
}, },
'#2': 'fitTwo', '#2': 'fitImage: 2',
'#3': { '#3': {
default: 'fitThree', default: 'fitImage: 3',
shift: 'fitThreeRibbons', shift: 'fitRibbons: 3',
}, },
'#4': 'fitFour', '#4': 'fitFour',
'#5': { '#5': {
@ -176,8 +176,8 @@ $(function(){
window.a = testing.setupActions() window.a = testing.setupActions()
// setup features... // setup features...
// XXX I do not full understand it yet, but PartialRibbons must be // XXX I do not fully understand it yet, but PartialRibbons must be
// setup BEFORE RibbonAlignToFirst, otherwise the later will break // setup BEFORE AlignRibbonsTo*, otherwise the later will break
// on shifting an image to a new ribbon... // on shifting an image to a new ribbon...
// To reproduce: // To reproduce:
// - setupe RibbonAlignToFirst first // - setupe RibbonAlignToFirst first