summaryrefslogtreecommitdiffstats
path: root/client/lib/keyboard.js
blob: 8b105c2800e59ab3914d683423d1c831ca0b548c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// XXX Pressing `?` should display a list of all shortcuts available.
//
// 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).

Mousetrap.bind('w', function() {
  Sidebar.toogle();
});

Mousetrap.bind('q', function() {
  var currentBoardId = Session.get('currentBoard');
  var currentUserId = Meteor.userId();
  if (currentBoardId && currentUserId) {
    Filter.members.toogle(currentUserId);
  }
});

Mousetrap.bind('x', function() {
  if (Filter.isActive()) {
    Filter.reset();
  }
});

Mousetrap.bind(['down', 'up'], function(evt, key) {
  if (! Session.get('currentCard')) {
    return;
  }

  var nextFunc = (key === 'down' ? 'next' : 'prev');
  var nextCard = $('.js-minicard.is-selected')[nextFunc]('.js-minicard').get(0);
  if (nextCard) {
    var nextCardId = Blaze.getData(nextCard)._id;
    Utils.goCardId(nextCardId);
  }
});

// Pressing `Escape` should close the last opened “element” and only the last
// 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: [],

  // Executed in order
  hierarchy: [
    'textcomplete',
    'popup',
    'inlinedForm',
    'multiselection-disable',
    'sidebarView',
    'detailsPane',
    'multiselection-reset'
  ],

  register: function(label, action, condition) {
    if (_.isUndefined(condition))
      condition = function() { return true; };

    // 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,
      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(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)
        return;
      if (!! currentAction.condition())
        currentAction.action();
    }
  },

  executeAll: function() {
    return this.executeLowerThan();
  }
};

Mousetrap.bind('esc', function() {
  EscapeActions.executeLowest();
});