Show
Ignore:
Timestamp:
05/19/07 16:01:29 (18 months ago)
Author:
mbonetti
Message:

configuration of action keys

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/gregarius/plugins/keyboardnavigation.php

    r1711 r1712  
    2626/// Author: Marco Bonetti 
    2727/// Description: Navigate between items without using your mouse. 
    28 /// Version: 0.3 
    29  
    30 $__kbnav_version = 'kbnav_0.3'; 
     28/// Version: 0.4 
     29/// Configuration: __kbnav_config 
     30 
     31$__kbnav_version = 'kbnav_0.4'; 
    3132 
    3233/** 
     
    3536* 0.2 - f: toggle flag, c: lilina theme un/collapse 
    3637* 0.3 - o: open URL for item, shift-o: open URL in new window, fix: item-collapse/expand for non-logged in users. 
     38* 0.4 - configuration 
    3739*/ 
    3840 
     
    4446*/ 
    4547 
     48define ('KBNAVIGATIONPLUGIN_KMAP_CONFIG_OPTIONS', 'kbnav.options.keymapping'); 
     49/** 
     50 * Labels 
     51 */ 
     52function __kbnav_config_actions() { 
     53    return array( 
     54        '__kbnav_Next' => 'Navigate to next item', 
     55        '__kbnav_Prev' => 'Navigate to previous item', 
     56        '__kbnav_ToggleSticky' => 'Toggle Sticky state of the current item', 
     57        '__kbnav_ToggleFlag' => 'Toggle Flagged state of the current item', 
     58        '__kbnav_NextMarkRead' => 'Mark current item as read, move to the next one', 
     59        '__kbnav_ScrollTop' => 'Scroll to the top of the window', 
     60        '__kbnav_ToggleCollapse' => 'In the Lilina theme, toggle the collapsed state of the current item', 
     61        '__kbnav_OpenUrl' => 'Navigate to the URL of the current item', 
     62        '__kbnav_OpenUrlNW' => 'Navigate to the URL of the current item in a new window',         
     63    ); 
     64} 
     65/** 
     66 * Fetch the config from the config, add default values for missing keys 
     67 */ 
     68function __kbnav_config_action_keys() { 
     69    $kmap = rss_plugins_get_option(KBNAVIGATIONPLUGIN_KMAP_CONFIG_OPTIONS); 
     70    if (!isset($kmap['__kbnav_Next']['key'])) {$kmap['__kbnav_Next']['key'] = 'j';} 
     71    if (!isset($kmap['__kbnav_Prev']['key'])) {$kmap['__kbnav_Prev']['key'] = 'k';} 
     72    if (!isset($kmap['__kbnav_ToggleSticky']['key'])) {$kmap['__kbnav_ToggleSticky']['key'] = 's';} 
     73    if (!isset($kmap['__kbnav_ToggleFlag']['key'])) {$kmap['__kbnav_ToggleFlag']['key'] = 'f';} 
     74    if (!isset($kmap['__kbnav_NextMarkRead']['key'])) {$kmap['__kbnav_NextMarkRead']['key'] = 'm';} 
     75    if (!isset($kmap['__kbnav_ScrollTop']['key'])) {$kmap['__kbnav_ScrollTop']['key'] = 'h';} 
     76    if (!isset($kmap['__kbnav_ToggleCollapse']['key'])) {$kmap['__kbnav_ToggleCollapse']['key'] = 'c';} 
     77    if (!isset($kmap['__kbnav_OpenUrl']['key'])) {$kmap['__kbnav_OpenUrl']['key'] = 'o';} 
     78    if (!isset($kmap['__kbnav_OpenUrlNW']['key'])) { 
     79        $kmap['__kbnav_OpenUrlNW']['key'] = 'o'; 
     80        $kmap['__kbnav_OpenUrlNW']['modifier'] = 'shift'; 
     81    } 
     82    return $kmap; 
     83} 
     84 
     85/** 
     86 * Configuration: display a table row for each possible action 
     87 */ 
     88function __kbnav_config() { 
     89    $kmap = __kbnav_config_action_keys(); 
     90    if (rss_plugins_is_submit()) { 
     91        foreach($_POST as $k => $v) { 
     92            if (preg_match('#key_([a-zA-Z_]+)#',$k,$matches)) { 
     93                $action = $matches[1]; 
     94                if(isset($kmap[$action])) { 
     95                    $kmap[$action]['key'] = $v; 
     96                } 
     97            } elseif(preg_match('#mod_([a-zA-Z_]+)#',$k,$matches)) { 
     98                $action = $matches[1]; 
     99                if(isset($kmap[$action])) { 
     100                    $kmap[$action]['modifier'] = $v; 
     101                } 
     102            }  
     103        } 
     104        rss_plugins_add_option(KBNAVIGATIONPLUGIN_KMAP_CONFIG_OPTIONS, $kmap, 'array');  
     105        return; 
     106    }  
     107    $actions = __kbnav_config_actions(); 
     108?> 
     109    <table class="frame"> 
     110        <tr> 
     111            <th>Action</th> 
     112            <th>Modifier</th> 
     113            <th>Key</th> 
     114        </tr> 
     115<?php foreach($kmap as $action => $data) { ?> 
     116    <tr> 
     117        <td><?php echo $actions[$action] ?></td> 
     118        <td><?php  __kbnav_config_modifier_combo($action,@$kmap[$action]['modifier']); ?></td> 
     119        <td><?php  __kbnav_config_key_combo($action,@$kmap[$action]['key']); ?></td> 
     120    </tr> 
     121<?php } ?> 
     122    </table> 
     123<?php    
     124} 
     125 
     126/** 
     127 * Helper: displays a modifier combo 
     128 */ 
     129function __kbnav_config_modifier_combo($name,$modifier = null) { 
     130?> 
     131    <select name="mod_<?php echo $name; ?>"> 
     132    <option value="">None</option> 
     133    <option <?php echo $modifier == 'shift' ? 'selected="selected" ':''; ?> value="shift">Shift</option> 
     134    </select> 
     135<?php 
     136} 
     137 
     138/** 
     139 * Helper: displays a list of keys, from a to z. 
     140 * Fixme: up/down arrows? 
     141 */ 
     142function __kbnav_config_key_combo($name,$key = null) { 
     143?> 
     144    <select name="key_<?php echo $name; ?>"> 
     145<?php for($c=ord('a');$c<=ord('z');$c++) { ?> 
     146    <option <?php echo $key == chr($c) ? 'selected="selected" ':''; ?> value="<?php echo chr($c); ?>"><?php echo strtoupper(chr($c)); ?></option> 
     147<?php } ?> 
     148    </select> 
     149<?php  
     150} 
     151 
    46152function __kbnav_js_register($js) { 
    47153    $js[] = getPath(). RSS_PLUGINS_DIR . "/keyboardnavigation.php?kbnjs"; 
     
    53159    <script type="text/javascript"> 
    54160    <!-- 
    55     document.onkeypress = function(event) {return __kbnav_getKey(event)}; 
    56      __kbnav_init(); 
     161    document.onkeypress = function(event) { 
     162        event = event || window.event; 
     163        var code = event.which || event.keyCode; 
     164        var target = event.target || event.srcElement; 
     165        if (target.nodeName.toUpperCase() == 'INPUT') { 
     166            return true; 
     167        } 
     168 
     169        switch(String.fromCharCode(code)) { 
     170            <?php 
     171                foreach(__kbnav_config_action_keys() as $action => $data) { 
     172                    printf( "\tcase '%s': return %s();break;\n",  ($data['modifier'] == 'shift' ? strtoupper($data['key']):strtolower($data['key'])), $action); 
     173                } 
     174            ?> 
     175            default : return true; 
     176        } 
     177    }; 
     178    __kbnav_init(); 
    57179    // --> 
    58180    </script> 
     
    75197    var kbNavCurrent = -1; 
    76198    var kbNavItems = new Array(); 
    77      
    78      
    79     function __kbnav_getKey(event) { 
    80         if (!event) { 
    81             event = window.event; 
    82             var code = event.keyCode; 
    83         } else { 
    84             var code = event.which; 
    85         } 
    86         var target = event.target || event.srcElement; 
    87          
    88         if (target.nodeName.toUpperCase() == 'INPUT') { 
    89             return true; 
    90         } 
    91          
    92         switch(String.fromCharCode(code).toLowerCase()) { 
    93             case 'j': return __kbnav_Next(); break; 
    94             case 'k': return __kbnav_Prev(); break; 
    95             case 's': return __kbnav_ToggleSticky(); break; 
    96             case 'f': return __kbnav_ToggleFlag(); break; 
    97             case 'm': return __kbnav_NextMarkRead(); break; 
    98             case 'h': return __kbnav_ScrollTop(); break; 
    99             case 'c': return __kbnav_ToggleCollapse(); break; 
    100             case 'o': return (event.shiftKey ? __kbnav_OpenUrlNW() : __kbnav_OpenUrl()); break; 
    101             default : return true; 
    102         } 
    103     } 
    104      
    105     function __kbnav_init() { 
     199    var kbNavConfig; 
     200     
     201    function __kbnav_init(cfg) { 
    106202        kbNavItems = new Array(); 
     203        kbNavConfig = cfg; 
    107204        var list = document.getElementsByTagName('li'); 
    108205        for(var i=0;i<list.length;i++) { 
     
    179276        if (kbNavItems[kbNavCurrent+i]) { 
    180277            var y = kbNavItems[kbNavCurrent+i].offsetTop - 10; 
    181             //kbNavItems[kbNavCurrent+i].style. // do something 
    182278            var span = document.getElementById('kbnavptr'); 
    183279