From cbe48f75dddcfbf2997343d6f9288549da75586a Mon Sep 17 00:00:00 2001 From: jbarata Date: Thu, 7 Feb 2013 22:56:41 +0000 Subject: [PATCH] Added registered lists concept and reactivating previous list on list removal added a registered lists array that serves as a stack for started lists and allows us to get back and reactivate the previous started list on removal of a list. Also added a public method (removeList(list)) that removes a list from Shortcuts object and reactivates the list that was previously started (the last one in the registered array), instead of stopping the whole thing. This is very useful for cases where, for example, we have a main shortcut list that has a shortcut for opening a dialog with its own list of shortcuts (which itself can open other stuff with its own shortcuts). In this scenario, we can press the shortcut to open the dialog, do some stuff in the dialog (using its own shortcuts), then, before closing the dialog we call Shortcuts.removelist('the_dialog_list') and it will reactivate the previous registered list (in this example the main list). --- jquery.shortcuts.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/jquery.shortcuts.js b/jquery.shortcuts.js index 1c404d7..14965f9 100644 --- a/jquery.shortcuts.js +++ b/jquery.shortcuts.js @@ -51,6 +51,9 @@ /** Active shortcut list */ var active; + /** Registered list stack that we can use to activate later*/ + var registered = []; + /** Hash for storing which keys are pressed at the moment. Key - ASCII key code (e.which), value - true/false. */ var pressed = {}; @@ -130,6 +133,18 @@ }); }; + var registerList = function(list){ + //get the list index in registered lists array + var indexInRegisteredList = jQuery.inArray(lists[list], registered); + + //put the list in the last position or top of the registered stack so we can go back to it later + if(indexInRegisteredList > -1){ + registered.remove(lists[list]); + } + registered.push(lists[list]); + + }; + $.Shortcuts = {}; /** @@ -140,6 +155,8 @@ list = list || 'default'; active = lists[list]; // Set the list as active. + registerList(list); + if (isStarted) { return; } // We are going to attach event handlers only once, the first time this method is called. $(document).bind(($.browser.opera ? 'keypress' : 'keydown') + '.shortcuts', function(e) { @@ -170,6 +187,27 @@ $.Shortcuts.stop = function() { $(document).unbind('keypress.shortcuts keydown.shortcuts keyup.shortcuts'); isStarted = false; + registered = []; + return this; + }; + + /** + * Remove shortcut list and reactivate previously started list. + */ + $.Shortcuts.removeList = function(list) { + list = list || 'default'; + + registered.remove(lists[list]); + delete lists[list]; + + //Reactivate previous list if there is one + if (registered.length > 0) { + active = registered[registered.length - 1]; + } else { + active = {}; + $.Shortcuts.stop(); + } + return this; }; @@ -182,11 +220,11 @@ * up – On key up. * hold – On pressing and holding down the key. The handler will be called immediately * after pressing the key and then repeatedly while the key is held down. - * + * * @param {String} params.mask A string specifying the key combination. * Consists of key names separated by a plus sign. Case insensitive. * Examples: 'Down', 'Esc', 'Shift+Up', 'ctrl+a'. - * + * * @param {Function} params.handler A function to be called when the key combination is pressed. The event object will be passed to it. * @param {String} [params.list] You can organize your shortcuts into lists and then switch between them. * By default shortcuts are added to the 'default' list. @@ -208,7 +246,7 @@ var maskObj = getMaskObject(mask); var keys = getKey(type, maskObj); if (!$.isArray(keys)) { keys = [keys]; } - + $.each(keys, function(i, key) { if (!list[key]) { list[key] = []; } list[key].push(params);