From 42f6dc686f313ba294e3cbcfb0ebde50678580fe Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Wed, 27 May 2015 02:48:15 +0200 Subject: Prioritize escape actions with a label hierarchy instead of an integer --- client/components/boards/router.js | 2 +- client/components/forms/inlinedform.js | 6 ++---- client/components/main/editor.js | 2 +- client/components/sidebar/sidebar.js | 2 +- client/lib/keyboard.js | 33 +++++++++++++++++++++++++-------- client/lib/popup.js | 5 ++++- 6 files changed, 34 insertions(+), 16 deletions(-) (limited to 'client') diff --git a/client/components/boards/router.js b/client/components/boards/router.js index 80fadd9e..81fc3d91 100644 --- a/client/components/boards/router.js +++ b/client/components/boards/router.js @@ -57,7 +57,7 @@ Router.route('/boards/:boardId/:slug/:cardId', { }); // Close the card details pane by pressing escape -EscapeActions.register(50, +EscapeActions.register('detailedPane', function() { return ! Session.equals('currentCard', null); }, function() { Utils.goBoardId(Session.get('currentBoard')); } ); diff --git a/client/components/forms/inlinedform.js b/client/components/forms/inlinedform.js index 200a6f9d..b84952d2 100644 --- a/client/components/forms/inlinedform.js +++ b/client/components/forms/inlinedform.js @@ -17,8 +17,6 @@ // keyboard.js var currentlyOpenedForm = new ReactiveVar(null); -var inlinedFormEscapePriority = 30; - BlazeComponent.extendComponent({ template: function() { return 'inlinedForm'; @@ -37,7 +35,7 @@ BlazeComponent.extendComponent({ // if (currentlyOpenedForm.get() !== null) { // currentlyOpenedForm.get().close(); // } - EscapeActions.executeLowerThan(inlinedFormEscapePriority); + EscapeActions.executeLowerThan('inlinedForm'); this.isOpen.set(true); currentlyOpenedForm.set(this); }, @@ -97,7 +95,7 @@ BlazeComponent.extendComponent({ }).register('inlinedForm'); // Press escape to close the currently opened inlinedForm -EscapeActions.register(inlinedFormEscapePriority, +EscapeActions.register('inlinedForm', function() { return currentlyOpenedForm.get() !== null; }, function() { currentlyOpenedForm.get().close(); } ); diff --git a/client/components/main/editor.js b/client/components/main/editor.js index 95a8dc5d..a35ecd06 100644 --- a/client/components/main/editor.js +++ b/client/components/main/editor.js @@ -60,7 +60,7 @@ Template.editor.onRendered(function() { }); }); -EscapeActions.register(10, +EscapeActions.register('textcomplete', function() { return dropdownMenuIsOpened; }, function() {} ); diff --git a/client/components/sidebar/sidebar.js b/client/components/sidebar/sidebar.js index 729bc42b..ce7925ea 100644 --- a/client/components/sidebar/sidebar.js +++ b/client/components/sidebar/sidebar.js @@ -97,7 +97,7 @@ BlazeComponent.extendComponent({ } }).register('sidebar'); -EscapeActions.register(40, +EscapeActions.register('sidebarView', function() { return Sidebar && Sidebar.getView() !== defaultView; }, function() { Sidebar.setView(defaultView); } ); diff --git a/client/lib/keyboard.js b/client/lib/keyboard.js index 723d498b..0fbdbfd5 100644 --- a/client/lib/keyboard.js +++ b/client/lib/keyboard.js @@ -35,15 +35,28 @@ Mousetrap.bind(['down', 'up'], function(evt, key) { }); // Pressing `Escape` should close the last opened “element” and only the last -// one. Components can register themself using a priority number (smaller is -// closed first), a condition, and an action.This is used by Popup or -// inlinedForm for instance. When we press escape we execute the action which -// condition is valid with the highest priority. +// one. Components can register themselves using a label a condition, and an +// action. This is used by Popup or inlinedForm for instance. When we press +// escape we execute the action which have a condition is valid and his the the +// highest in the label hierarchy. EscapeActions = { _actions: [], - register: function(priority, condition, action) { + // Executed in order + hierarchy: [ + 'textcomplete', + 'popup', + 'inlinedForm', + 'sidebarView', + 'detailedPane' + ], + + register: function(label, condition, action) { // XXX Rewrite this with ES6: .push({ priority, condition, action }) + var priority = this.hierarchy.indexOf(label); + if (priority === -1) { + throw Error('You must define the label in the EscapeActions hierarchy'); + } this._actions.push({ priority: priority, condition: condition, @@ -60,9 +73,13 @@ EscapeActions = { return topActiveAction && topActiveAction.action(); }, - executeLowerThan: function(maxPriority) { - maxPriority = maxPriority || Infinity; - var currentAction; + executeLowerThan: function(label) { + var maxPriority, currentAction; + if (! label) + maxPriority = Infinity; + else + maxPriority = this.hierarchy.indexOf(label); + for (var i = 0; i < this._actions.length; i++) { currentAction = this._actions[i]; if (currentAction.priority > maxPriority) diff --git a/client/lib/popup.js b/client/lib/popup.js index 70f2660f..6298ba81 100644 --- a/client/lib/popup.js +++ b/client/lib/popup.js @@ -204,4 +204,7 @@ $(document).on('click', function(evt) { // Press escape to close the popup. var bindPopup = function(f) { return _.bind(f, Popup); }; -EscapeActions.register(20, bindPopup(Popup.isOpen), bindPopup(Popup.close)); +EscapeActions.register('popup', + bindPopup(Popup.isOpen), + bindPopup(Popup.close) +); -- cgit v1.2.3-1-g7c22