Prioritize escape actions with a label hierarchy instead of an integer

This commit is contained in:
Maxime Quandalle 2015-05-27 02:48:15 +02:00
parent 40c2411f2a
commit 42f6dc686f
6 changed files with 34 additions and 16 deletions

View file

@ -57,7 +57,7 @@ Router.route('/boards/:boardId/:slug/:cardId', {
}); });
// Close the card details pane by pressing escape // Close the card details pane by pressing escape
EscapeActions.register(50, EscapeActions.register('detailedPane',
function() { return ! Session.equals('currentCard', null); }, function() { return ! Session.equals('currentCard', null); },
function() { Utils.goBoardId(Session.get('currentBoard')); } function() { Utils.goBoardId(Session.get('currentBoard')); }
); );

View file

@ -17,8 +17,6 @@
// keyboard.js // keyboard.js
var currentlyOpenedForm = new ReactiveVar(null); var currentlyOpenedForm = new ReactiveVar(null);
var inlinedFormEscapePriority = 30;
BlazeComponent.extendComponent({ BlazeComponent.extendComponent({
template: function() { template: function() {
return 'inlinedForm'; return 'inlinedForm';
@ -37,7 +35,7 @@ BlazeComponent.extendComponent({
// if (currentlyOpenedForm.get() !== null) { // if (currentlyOpenedForm.get() !== null) {
// currentlyOpenedForm.get().close(); // currentlyOpenedForm.get().close();
// } // }
EscapeActions.executeLowerThan(inlinedFormEscapePriority); EscapeActions.executeLowerThan('inlinedForm');
this.isOpen.set(true); this.isOpen.set(true);
currentlyOpenedForm.set(this); currentlyOpenedForm.set(this);
}, },
@ -97,7 +95,7 @@ BlazeComponent.extendComponent({
}).register('inlinedForm'); }).register('inlinedForm');
// Press escape to close the currently opened inlinedForm // Press escape to close the currently opened inlinedForm
EscapeActions.register(inlinedFormEscapePriority, EscapeActions.register('inlinedForm',
function() { return currentlyOpenedForm.get() !== null; }, function() { return currentlyOpenedForm.get() !== null; },
function() { currentlyOpenedForm.get().close(); } function() { currentlyOpenedForm.get().close(); }
); );

View file

@ -60,7 +60,7 @@ Template.editor.onRendered(function() {
}); });
}); });
EscapeActions.register(10, EscapeActions.register('textcomplete',
function() { return dropdownMenuIsOpened; }, function() { return dropdownMenuIsOpened; },
function() {} function() {}
); );

View file

@ -97,7 +97,7 @@ BlazeComponent.extendComponent({
} }
}).register('sidebar'); }).register('sidebar');
EscapeActions.register(40, EscapeActions.register('sidebarView',
function() { return Sidebar && Sidebar.getView() !== defaultView; }, function() { return Sidebar && Sidebar.getView() !== defaultView; },
function() { Sidebar.setView(defaultView); } function() { Sidebar.setView(defaultView); }
); );

View file

@ -35,15 +35,28 @@ Mousetrap.bind(['down', 'up'], function(evt, key) {
}); });
// Pressing `Escape` should close the last opened “element” and only the last // Pressing `Escape` should close the last opened “element” and only the last
// one. Components can register themself using a priority number (smaller is // one. Components can register themselves using a label a condition, and an
// closed first), a condition, and an action.This is used by Popup or // action. This is used by Popup or inlinedForm for instance. When we press
// inlinedForm for instance. When we press escape we execute the action which // escape we execute the action which have a condition is valid and his the the
// condition is valid with the highest priority. // highest in the label hierarchy.
EscapeActions = { EscapeActions = {
_actions: [], _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 }) // 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({ this._actions.push({
priority: priority, priority: priority,
condition: condition, condition: condition,
@ -60,9 +73,13 @@ EscapeActions = {
return topActiveAction && topActiveAction.action(); return topActiveAction && topActiveAction.action();
}, },
executeLowerThan: function(maxPriority) { executeLowerThan: function(label) {
maxPriority = maxPriority || Infinity; var maxPriority, currentAction;
var currentAction; if (! label)
maxPriority = Infinity;
else
maxPriority = this.hierarchy.indexOf(label);
for (var i = 0; i < this._actions.length; i++) { for (var i = 0; i < this._actions.length; i++) {
currentAction = this._actions[i]; currentAction = this._actions[i];
if (currentAction.priority > maxPriority) if (currentAction.priority > maxPriority)

View file

@ -204,4 +204,7 @@ $(document).on('click', function(evt) {
// Press escape to close the popup. // Press escape to close the popup.
var bindPopup = function(f) { return _.bind(f, 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)
);