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

View File

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

View File

@ -84,40 +84,61 @@ function toKeyCode(c){
// if set to false the event handlers will always return false...
var KEYBOARD_HANDLER_PROPAGATE = true
/* Basic key format:
/* Basic key binding format:
*
* <key-def> : <callback>,
* {
* <css-selector>: {
* // meta-data used to generate user docs/help/config
* title: <text>,
* doc: <text>,
*
* <key-def> : {
* 'default': <callback>,
* // a modifier can be any single modifier, like shift or a
* // combination of modifers like 'ctrl+shift', given in order
* // of priority.
* // supported modifiers are (in order of priority):
* // - ctrl
* // - alt
* // - shift
* <modifer>: [...]
* // 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> : {
* 'default': <callback>,
* // a modifier can be any single modifier, like shift or a
* // combination of modifers like 'ctrl+shift', given in order
* // of priority.
* // supported modifiers are (in order of priority):
* // - ctrl
* // - alt
* // - shift
* <modifer>: [...],
* ...
* },
*
* <key-def> : [
* // this can be any type of handler except for an alias...
* <handler>,
* <doc>
* ],
*
* // alias...
* <key-def-a> : <key-def-b>,
*
* ...
* },
*
* <key-def> : [
* // this can be any type of handler except for an alias...
* <handler>,
* <doc>
* ],
*
* // alias...
* <key-def-a> : <key-def-b>,
* ...
* }
*
* <key-def> can be:
* - explicit key code
* - key name, if present in _SPECIAL_KEYS)
* - 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
* event propagation chain and exit the handler.
* 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...
*/
@ -135,13 +156,6 @@ function makeKeyboardHandler(keybindings, unhandled){
var key = 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...
var modifers = evt.ctrlKey ? 'ctrl' : ''
modifers += evt.altKey ? (modifers != '' ? '+alt' : 'alt') : ''
@ -166,6 +180,14 @@ function makeKeyboardHandler(keybindings, unhandled){
}
// no handler...
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
}
// Array, lisp style with docs...
@ -192,6 +214,7 @@ function makeKeyboardHandler(keybindings, unhandled){
// if the handler explicitly returned false break out...
if(res === false){
// XXX is this corrent???
// XXX should we just break here instead of return...
return KEYBOARD_HANDLER_PROPAGATE ? res : null
}
did_handling = true