From 7756a3b8ca5488ee0ddd869cb25599e609d73b62 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 3 Apr 2016 20:57:03 +0300 Subject: [PATCH] started refactoring keyboard.js... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/keyboard.js | 21 +++++++++-- ui (gen4)/lib/keyboard.js | 68 +++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/ui (gen4)/features/keyboard.js b/ui (gen4)/features/keyboard.js index 16bc7183..3eb4e490 100755 --- a/ui (gen4)/features/keyboard.js +++ b/ui (gen4)/features/keyboard.js @@ -22,12 +22,19 @@ var core = require('features/core') // XXX add this to the global doc... var GLOBAL_KEYBOARD = module.GLOBAL_KEYBOARD = { + 'Global':{ + doc: 'Global bindings that take priority over other sections.', + pattern: '*', + + // XXX + }, + 'Slideshow': { pattern: '.slideshow-running', ignore: [ 'Esc', 'Up', 'Down', 'Enter', - 'R', 'L', + 'R', 'L', 'G', ], Esc: 'toggleSlideshow: "off"', @@ -57,7 +64,13 @@ module.GLOBAL_KEYBOARD = { Esc: 'toggleSingleImage: "off"', }, - 'Global': { + // XXX cropped -- needs a class to indicate a crop... + 'Cropped': { + // XXX + }, + + // XXX cleanup... + 'Viewer': { doc: 'NOTE: binding priority is the same as the order of sections '+ 'on this page.', pattern: '*', @@ -68,6 +81,7 @@ module.GLOBAL_KEYBOARD = { Q: { meta: 'close', }, + // XXX F5: keyboard.doc('Full reload viewer', function(){ //a.stop() @@ -331,7 +345,8 @@ var KeyboardActions = actions.Actions({ delete this.__keyboard_handler } }, - ['on', 'off'])] + ['on', 'off'])], + }) var Keyboard = diff --git a/ui (gen4)/lib/keyboard.js b/ui (gen4)/lib/keyboard.js index 991b3c14..97093d54 100755 --- a/ui (gen4)/lib/keyboard.js +++ b/ui (gen4)/lib/keyboard.js @@ -224,6 +224,44 @@ function dropRepeatingkeys(handler, max_rate){ } + +// supported action format: +// [!][: ][-- ] +// +// can contain space seporated: +// - numbers +// - strings +// - non-nested arrays or objects +var parseActionCall = +module.parseActionCall = +function parseActionCall(txt){ + // split off the doc... + var c = txt.split('--') + var doc = (c[1] || '').trim() + // the actual code... + c = c[0].split(':') + + // action and no default flag... + var action = c[0].trim() + var no_default = action.slice(-1) == '!' + action = no_default ? action.slice(0, -1) : action + + // parse arguments... + var args = JSON.parse('['+( + ((c[1] || '') + .match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*/gm) + || []) + .join(','))+']') + + return { + action: action, + arguments: args, + doc: doc, + 'no-default': no_default, + } +} + + /* Key handler getter * * For doc on format see makeKeyboardHandler(...) @@ -387,7 +425,7 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action // alias... // XXX should this be before after or combined with ignore handling... - while( handler != null && typeof(handler) != 'function'){ + while(handler != null && typeof(handler) != 'function'){ // do the complex handler aliases... if(handler != null && handler.constructor == Object){ @@ -418,29 +456,12 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action handler = keybindings[handler] // actions... - // - // supported action format: - // [!][: ] - // - // can contain space seporated: - // - numbers - // - strings - // - non-nested arrays or arrays } 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 c = parseActionCall(handler) - var args = JSON.parse('['+( - ((c[1] || '') - .match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*/gm) - || []) - .join(','))+']') - - handler = function(n, no_default, args){ + handler = function(n, no_default, args, doc){ if(no_default){ var f = function(){ event.preventDefault() @@ -459,8 +480,13 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action f.args = args f.no_default = no_default + // use the inherited doc if no specific doc is defined... + if(doc && doc.length > 0){ + f.doc = doc + } + return f - }(handler, no_default, args.slice()) + }(c.action, c['no-default'], c.args, c.doc) // key code... } else if(typeof(handler) == typeof(1)) {