From 62c8346993a9e6ce5ae3b2164483cb972971efe9 Mon Sep 17 00:00:00 2001 From: ricardovf Date: Wed, 11 Jul 2012 15:36:16 -0300 Subject: [PATCH 1/4] Added the ability to make more then one list active at the same time --- jquery.shortcuts.js | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/jquery.shortcuts.js b/jquery.shortcuts.js index 1c404d7..fddeb46 100644 --- a/jquery.shortcuts.js +++ b/jquery.shortcuts.js @@ -48,8 +48,8 @@ /** Hash for shortcut lists */ var lists = {}; - /** Active shortcut list */ - var active; + /** Active shortcut lists */ + var active = {}; /** Hash for storing which keys are pressed at the moment. Key - ASCII key code (e.which), value - true/false. */ var pressed = {}; @@ -101,7 +101,7 @@ }; var run = function(type, e) { - if (!active) { return; } + if (active.length === 0) { return; } var maskObj = { ctrl: e.ctrlKey, @@ -111,7 +111,14 @@ }; var key = getKey(type, maskObj); - var shortcuts = active[key]; // Get shortcuts from the active list. + + var shortcuts; + + $.each(active, function(list, list_activated) { + if (list_activated === true && lists[list][key]) { + shortcuts = lists[list][key]; + } + }); if (!shortcuts) { return; } @@ -125,7 +132,7 @@ e.preventDefault(); isPrevented = true; } - shortcut.handler(e); // Run the shortcut's handler. + shortcut.handler(shortcut, e); // Run the shortcut's handler. } }); }; @@ -138,7 +145,9 @@ */ $.Shortcuts.start = function(list) { list = list || 'default'; - active = lists[list]; // Set the list as active. + + // Set the list as active. + active[list] = true; if (isStarted) { return; } // We are going to attach event handlers only once, the first time this method is called. @@ -167,9 +176,22 @@ /** * Stop reacting to shortcuts (unbind event handlers). */ - $.Shortcuts.stop = function() { - $(document).unbind('keypress.shortcuts keydown.shortcuts keyup.shortcuts'); - isStarted = false; + $.Shortcuts.stop = function(list) { + list = list || 'default'; + active[list] = false; + + var has_active = false; + + $.each(active, function(list, list_activated) { + if (list_activated === true) { + has_active = true + } + }); + + if(has_active === false){ + $(document).unbind('keypress.shortcuts keydown.shortcuts keyup.shortcuts'); + isStarted = false; + } return this; }; @@ -200,7 +222,10 @@ var listNames = params.list ? params.list.replace(/\s+/g, '').split(',') : ['default']; $.each(listNames, function(i, name) { - if (!lists[name]) { lists[name] = {}; } + if (!lists[name]) { + lists[name] = {}; + active[name] = false; + } var list = lists[name]; var masks = params.mask.toLowerCase().replace(/\s+/g, '').split(','); From f291ae0fb78ef8407e1685e7d398c35bbf83be97 Mon Sep 17 00:00:00 2001 From: ricardovf Date: Wed, 11 Jul 2012 15:50:54 -0300 Subject: [PATCH 2/4] Corrected little bug when we add a shortcut but does not start the list --- jquery.shortcuts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.shortcuts.js b/jquery.shortcuts.js index fddeb46..2a482fe 100644 --- a/jquery.shortcuts.js +++ b/jquery.shortcuts.js @@ -115,7 +115,7 @@ var shortcuts; $.each(active, function(list, list_activated) { - if (list_activated === true && lists[list][key]) { + if (list_activated === true && lists[list] && lists[list][key]) { shortcuts = lists[list][key]; } }); From 2a22ad0ba598645205c8b4bc4f5bf2b954568648 Mon Sep 17 00:00:00 2001 From: ricardovf Date: Wed, 11 Jul 2012 16:12:44 -0300 Subject: [PATCH 3/4] Added the replace option to the start method and a method to return the list of active lists --- jquery.shortcuts.js | 46 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/jquery.shortcuts.js b/jquery.shortcuts.js index 2a482fe..af61488 100644 --- a/jquery.shortcuts.js +++ b/jquery.shortcuts.js @@ -101,8 +101,6 @@ }; var run = function(type, e) { - if (active.length === 0) { return; } - var maskObj = { ctrl: e.ctrlKey, alt: e.altKey, @@ -141,13 +139,23 @@ /** * Start reacting to shortcuts in the specified list. - * @param {String} [list] List name + * @param {String|Array} [list] List name or array of names + * @param {Boolean} [list] Should we replace the list of active lists? */ - $.Shortcuts.start = function(list) { - list = list || 'default'; - - // Set the list as active. - active[list] = true; + $.Shortcuts.start = function(list, replace) { + if (list === undefined) + list = 'default'; + + if (typeof(list) === 'string') + list = [list] + + // Set the lists as active + $.each(active, function(k, v) { + if (list.indexOf(k) !== -1) + active[k] = true; + else + active[k] = (replace === true ? false : active[k]); + }); if (isStarted) { return; } // We are going to attach event handlers only once, the first time this method is called. @@ -173,9 +181,9 @@ return this; }; - /** - * Stop reacting to shortcuts (unbind event handlers). - */ + /** + * Stop reacting to shortcuts (unbind event handlers). + */ $.Shortcuts.stop = function(list) { list = list || 'default'; active[list] = false; @@ -195,6 +203,22 @@ return this; }; + /** + * Return the list of active lists + * @return {Array} + */ + $.Shortcuts.getActiveLists = function() { + var activies = []; + + $.each(active, function(list, list_activated) { + if (list_activated === true) { + activies.push(list); + } + }); + + return activies; + }; + /** * Add a shortcut. * @param {Object} params Shortcut parameters. From 44dbb36afa5e3a2dbb80577c1f95f1fce57bd299 Mon Sep 17 00:00:00 2001 From: ricardovf Date: Wed, 11 Jul 2012 16:25:04 -0300 Subject: [PATCH 4/4] Removed indexOf (Internet Explorer sucks!) --- jquery.shortcuts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.shortcuts.js b/jquery.shortcuts.js index af61488..9e48055 100644 --- a/jquery.shortcuts.js +++ b/jquery.shortcuts.js @@ -151,7 +151,7 @@ // Set the lists as active $.each(active, function(k, v) { - if (list.indexOf(k) !== -1) + if ($.inArray(k, list) !== -1) active[k] = true; else active[k] = (replace === true ? false : active[k]);