reworked the keyboard handler + some cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-03-10 04:58:10 +04:00
parent a21b85185d
commit 10ce03ebdf
3 changed files with 63 additions and 39 deletions

View File

@ -211,27 +211,26 @@ function removePage(page){
/*********************************************************************/ /*********************************************************************/
// XXX this needs revision... // XXX this needs revision...
// XXX need better separation between full screen and ribbon modes...
// XXX need to split this into more generic parts... // XXX need to split this into more generic parts...
// XXX STUB // XXX STUB
// XXX setCurrentPage after each action... function _finalize(n){
function _finalize(direction, n){
refreshInlineEditor() refreshInlineEditor()
setCurrentPage(direction == 'before'? n : n+1) setCurrentPage(n)
removeOverlay() removeOverlay()
} }
function addPage(direction){ function addPage(direction){
var n = getPageNumber() var n = getPageNumber()
n = direction == 'before'? n : n+1
return function(){ return function(){
showInOverlay($( showInOverlay($(
'<div>'+ '<div>'+
'<h1>Templates</h1>'+ '<h1>Templates</h1>'+
'<a href="javascript:$(\'.current.page\').'+direction+'(RawPage());_finalize(\''+direction+'\', '+n+')"><h3>Raw Page</h3></a>'+ '<a href="javascript:$(\'.current.page\').'+direction+'(RawPage());_finalize('+n+')"><h3>Raw Page</h3></a>'+
'<a href="javascript:$(\'.current.page\').'+direction+'(TextPage());_finalize(\''+direction+'\', '+n+')"><h3>Text Page</h3></a>'+ '<a href="javascript:$(\'.current.page\').'+direction+'(TextPage());_finalize('+n+')"><h3>Text Page</h3></a>'+
'<a href="javascript:$(\'.current.page\').'+direction+'(CaptionPage());_finalize(\''+direction+'\', '+n+')"><h3>Caption Page</h3></a>'+ '<a href="javascript:$(\'.current.page\').'+direction+'(CaptionPage());_finalize('+n+')"><h3>Caption Page</h3></a>'+
'<a href="javascript:$(\'.current.page\').'+direction+'(ImagePage());_finalize(\''+direction+'\', '+n+')"><h3>Image Page</h3></a>'+ '<a href="javascript:$(\'.current.page\').'+direction+'(ImagePage());_finalize('+n+')"><h3>Image Page</h3></a>'+
'<a href="javascript:$(\'.current.page\').'+direction+'(ImageFitHeightPage());_finalize(\''+direction+'\', '+n+'))"><h3>Vertical Image Page</h3></a>'+ '<a href="javascript:$(\'.current.page\').'+direction+'(ImageFitHeightPage());_finalize('+n+'))"><h3>Vertical Image Page</h3></a>'+
'</div>')) '</div>'))
} }
} }

View File

@ -143,11 +143,13 @@ function showInfo(){
'</div>')) '</div>'))
} }
var keyboard_config = { var KEYBOARD_CONFIG = {
'.overlay': { '.overlay': {
title: 'Overlay mode.', title: 'Overlay mode.',
doc: '', doc: '',
ignore: '*',
Esc: function(){ Esc: function(){
removeOverlay() removeOverlay()
return false return false
@ -255,7 +257,7 @@ $(document).ready(function(){
// keyboard... // keyboard...
$(document) $(document)
.keydown(makeKeyboardHandler(keyboard_config, .keydown(makeKeyboardHandler(KEYBOARD_CONFIG,
function(k){console.log(k)})) function(k){console.log(k)}))
window.MagazineScroller = makeScrollHandler($('.viewer'), { window.MagazineScroller = makeScrollHandler($('.viewer'), {

View File

@ -84,7 +84,18 @@ function toKeyCode(c){
// if set to false the event handlers will always return false... // if set to false the event handlers will always return false...
var KEYBOARD_HANDLER_PROPAGATE = true var KEYBOARD_HANDLER_PROPAGATE = true
/* Basic key format: /* Basic key binding format:
*
* {
* <css-selector>: {
* // meta-data used to generate user docs/help/config
* title: <text>,
* doc: <text>,
*
* // this defines the list of keys to ignore by the handler.
* // NOTE: use "*" to ignore all keys other than explicitly
* // defined in the current section.
* ignore: <ignored-keys>
* *
* <key-def> : <callback>, * <key-def> : <callback>,
* *
@ -97,7 +108,8 @@ var KEYBOARD_HANDLER_PROPAGATE = true
* // - ctrl * // - ctrl
* // - alt * // - alt
* // - shift * // - shift
* <modifer>: [...] * <modifer>: [...],
* ...
* }, * },
* *
* <key-def> : [ * <key-def> : [
@ -109,15 +121,24 @@ var KEYBOARD_HANDLER_PROPAGATE = true
* // alias... * // alias...
* <key-def-a> : <key-def-b>, * <key-def-a> : <key-def-b>,
* *
* ...
* },
*
* ...
* }
*
* <key-def> can be: * <key-def> can be:
* - explicit key code * - explicit key code
* - key name, if present in _SPECIAL_KEYS) * - key name, if present in _SPECIAL_KEYS)
* - key char (uppercase), as is returned by String.fromCharCode(...) * - key char (uppercase), as is returned by String.fromCharCode(...)
* *
* * NOTE: all fields are optional.
* NOTE: if a handler explicitly returns false then that will break the * NOTE: if a handler explicitly returns false then that will break the
* event propagation chain and exit the handler. * event propagation chain and exit the handler.
* i.e. no other matching handlers will be called. * i.e. no other matching handlers will be called.
* NOTE: a <css-selector> is used as a predicate to select a section to
* use. if multiple selectors match something then multiple sections
* will be resolved in order of occurance.
* *
* XXX might need to add meta information to generate sensible help... * XXX might need to add meta information to generate sensible help...
*/ */
@ -135,13 +156,6 @@ function makeKeyboardHandler(keybindings, unhandled){
var key = evt.keyCode var key = evt.keyCode
var chr = toKeyName(evt.keyCode) var chr = toKeyName(evt.keyCode)
if(bindings.ignore == '*'
|| bindings.ignore != null && bindings.ignore.indexOf(key) != -1){
// return true
res = res == null ? true : res
did_handling = true
continue
}
// XXX ugly... // XXX ugly...
var modifers = evt.ctrlKey ? 'ctrl' : '' var modifers = evt.ctrlKey ? 'ctrl' : ''
modifers += evt.altKey ? (modifers != '' ? '+alt' : 'alt') : '' modifers += evt.altKey ? (modifers != '' ? '+alt' : 'alt') : ''
@ -166,6 +180,14 @@ function makeKeyboardHandler(keybindings, unhandled){
} }
// no handler... // no handler...
if(handler == null){ if(handler == null){
// if something is ignored then just breakout and stop handling...
if(bindings.ignore == '*'
|| bindings.ignore != null && bindings.ignore.indexOf(key) != -1){
res = res == null ? true : res
did_handling = true
// ignoring a key will stop processing it...
break
}
continue continue
} }
// Array, lisp style with docs... // Array, lisp style with docs...
@ -192,6 +214,7 @@ function makeKeyboardHandler(keybindings, unhandled){
// if the handler explicitly returned false break out... // if the handler explicitly returned false break out...
if(res === false){ if(res === false){
// XXX is this corrent??? // XXX is this corrent???
// XXX should we just break here instead of return...
return KEYBOARD_HANDLER_PROPAGATE ? res : null return KEYBOARD_HANDLER_PROPAGATE ? res : null
} }
did_handling = true did_handling = true