keyboard.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // XXX Pressing `?` should display a list of all shortcuts available.
  2. //
  3. // XXX There is no reason to define these shortcuts globally, they should be
  4. // attached to a template (most of them will go in the `board` template).
  5. Mousetrap.bind('w', function() {
  6. Sidebar.toogle();
  7. });
  8. Mousetrap.bind('q', function() {
  9. var currentBoardId = Session.get('currentBoard');
  10. var currentUserId = Meteor.userId();
  11. if (currentBoardId && currentUserId) {
  12. Filter.members.toogle(currentUserId);
  13. }
  14. });
  15. Mousetrap.bind('x', function() {
  16. if (Filter.isActive()) {
  17. Filter.reset();
  18. }
  19. });
  20. Mousetrap.bind(['down', 'up'], function(evt, key) {
  21. if (! Session.get('currentCard')) {
  22. return;
  23. }
  24. var nextFunc = (key === 'down' ? 'next' : 'prev');
  25. var nextCard = $('.js-minicard.is-selected')[nextFunc]('.js-minicard').get(0);
  26. if (nextCard) {
  27. var nextCardId = Blaze.getData(nextCard)._id;
  28. Utils.goCardId(nextCardId);
  29. }
  30. });
  31. // Pressing `Escape` should close the last opened “element” and only the last
  32. // one. Components can register themselves using a label a condition, and an
  33. // action. This is used by Popup or inlinedForm for instance. When we press
  34. // escape we execute the action which have a condition is valid and his the the
  35. // highest in the label hierarchy.
  36. EscapeActions = {
  37. _actions: [],
  38. // Executed in order
  39. hierarchy: [
  40. 'textcomplete',
  41. 'popup',
  42. 'inlinedForm',
  43. 'sidebarView',
  44. 'detailedPane'
  45. ],
  46. register: function(label, condition, action) {
  47. // XXX Rewrite this with ES6: .push({ priority, condition, action })
  48. var priority = this.hierarchy.indexOf(label);
  49. if (priority === -1) {
  50. throw Error('You must define the label in the EscapeActions hierarchy');
  51. }
  52. this._actions.push({
  53. priority: priority,
  54. condition: condition,
  55. action: action
  56. });
  57. // XXX Rewrite this with ES6: => function
  58. this._actions = _.sortBy(this._actions, function(a) { return a.priority; });
  59. },
  60. executeLowest: function() {
  61. var topActiveAction = _.find(this._actions, function(a) {
  62. return !! a.condition();
  63. });
  64. return topActiveAction && topActiveAction.action();
  65. },
  66. executeLowerThan: function(label) {
  67. var maxPriority, currentAction;
  68. if (! label)
  69. maxPriority = Infinity;
  70. else
  71. maxPriority = this.hierarchy.indexOf(label);
  72. for (var i = 0; i < this._actions.length; i++) {
  73. currentAction = this._actions[i];
  74. if (currentAction.priority > maxPriority)
  75. return;
  76. if (!! currentAction.condition())
  77. currentAction.action();
  78. }
  79. }
  80. };
  81. Mousetrap.bind('esc', function() {
  82. EscapeActions.executeLowest();
  83. });