summaryrefslogtreecommitdiffstats
path: root/client/lib/keyboard.js
diff options
context:
space:
mode:
authorMaxime Quandalle <maxime@quandalle.com>2015-05-26 20:30:01 +0200
committerMaxime Quandalle <maxime@quandalle.com>2015-05-26 20:34:56 +0200
commit40c2411f2a1ce0bbd177f377828f9d6700112b06 (patch)
treebf1f7ab8d94fe3e0edfcde817961d6954c11af4d /client/lib/keyboard.js
parent1b4fcc67f4ec94ed53a2f86ad6889e551f00815e (diff)
downloadwekan-40c2411f2a1ce0bbd177f377828f9d6700112b06.tar.gz
wekan-40c2411f2a1ce0bbd177f377828f9d6700112b06.tar.bz2
wekan-40c2411f2a1ce0bbd177f377828f9d6700112b06.zip
Implement a new system to handle "escape actions"
The new EscapeActions object decide what to do when the user press the Escape key (such as closing a opened popup or inlined form). This commit also re-introduced the sidebar current view as a sidebar component local state.
Diffstat (limited to 'client/lib/keyboard.js')
-rw-r--r--client/lib/keyboard.js58
1 files changed, 43 insertions, 15 deletions
diff --git a/client/lib/keyboard.js b/client/lib/keyboard.js
index 8601e623..723d498b 100644
--- a/client/lib/keyboard.js
+++ b/client/lib/keyboard.js
@@ -3,21 +3,6 @@
// XXX There is no reason to define these shortcuts globally, they should be
// attached to a template (most of them will go in the `board` template).
-// Pressing `Escape` should close the last opened “element” and only the last
-// one -- curently we handle popups and the card detailed view of the sidebar.
-Mousetrap.bind('esc', function() {
- if (currentlyOpenedForm.get() !== null) {
- currentlyOpenedForm.get().close();
-
- } else if (Popup.isOpen()) {
- Popup.back();
-
- // XXX We should have a higher level API
- } else if (Session.get('currentCard')) {
- Utils.goBoardId(Session.get('currentBoard'));
- }
-});
-
Mousetrap.bind('w', function() {
Sidebar.toogle();
});
@@ -48,3 +33,46 @@ Mousetrap.bind(['down', 'up'], function(evt, key) {
Utils.goCardId(nextCardId);
}
});
+
+// 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.
+EscapeActions = {
+ _actions: [],
+
+ register: function(priority, condition, action) {
+ // XXX Rewrite this with ES6: .push({ priority, condition, action })
+ this._actions.push({
+ priority: priority,
+ condition: condition,
+ action: action
+ });
+ // XXX Rewrite this with ES6: => function
+ this._actions = _.sortBy(this._actions, function(a) { return a.priority; });
+ },
+
+ executeLowest: function() {
+ var topActiveAction = _.find(this._actions, function(a) {
+ return !! a.condition();
+ });
+ return topActiveAction && topActiveAction.action();
+ },
+
+ executeLowerThan: function(maxPriority) {
+ maxPriority = maxPriority || Infinity;
+ var currentAction;
+ for (var i = 0; i < this._actions.length; i++) {
+ currentAction = this._actions[i];
+ if (currentAction.priority > maxPriority)
+ return;
+ if (!! currentAction.condition())
+ currentAction.action();
+ }
+ }
+};
+
+Mousetrap.bind('esc', function() {
+ EscapeActions.executeLowest();
+});