escapeActions.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Pressing `Escape` should close the last opened “element” and only the last
  2. // one. Components can register themselves using a label a condition, and an
  3. // action. This is used by Popup or inlinedForm for instance. When we press
  4. // escape we execute the action which have a valid condition and his the highest
  5. // in the label hierarchy.
  6. EscapeActions = {
  7. _nextclickPrevented: false,
  8. _actions: [],
  9. // Executed in order
  10. hierarchy: [
  11. 'textcomplete',
  12. 'popup-back',
  13. 'popup-close',
  14. 'modalWindow',
  15. 'inlinedForm',
  16. 'detailsPane',
  17. 'multiselection',
  18. 'sidebarView'
  19. ],
  20. register: function(label, action, condition = () => true, options = {}) {
  21. const priority = this.hierarchy.indexOf(label);
  22. if (priority === -1) {
  23. throw Error('You must define the label in the EscapeActions hierarchy');
  24. }
  25. let enabledOnClick = options.enabledOnClick;
  26. if (_.isUndefined(enabledOnClick)) {
  27. enabledOnClick = true;
  28. }
  29. let noClickEscapeOn = options.noClickEscapeOn;
  30. this._actions[priority] = {
  31. priority,
  32. condition,
  33. action,
  34. noClickEscapeOn,
  35. enabledOnClick
  36. };
  37. },
  38. executeLowest: function() {
  39. return this._execute({
  40. multipleAction: false
  41. });
  42. },
  43. executeAll: function() {
  44. return this._execute({
  45. multipleActions: true
  46. });
  47. },
  48. executeUpTo: function(maxLabel) {
  49. return this._execute({
  50. maxLabel: maxLabel,
  51. multipleActions: true
  52. });
  53. },
  54. clickExecute: function(target, maxLabel) {
  55. if (this._nextclickPrevented) {
  56. this._nextclickPrevented = false;
  57. } else {
  58. return this._execute({
  59. maxLabel: maxLabel,
  60. multipleActions: false,
  61. isClick: true,
  62. clickTarget: target
  63. });
  64. }
  65. },
  66. preventNextClick: function() {
  67. this._nextclickPrevented = true;
  68. },
  69. _stopClick: function(action, clickTarget) {
  70. if (! _.isString(action.noClickEscapeOn))
  71. return false;
  72. else
  73. return $(clickTarget).closest(action.noClickEscapeOn).length > 0;
  74. },
  75. _execute: function(options) {
  76. const maxLabel = options.maxLabel;
  77. const multipleActions = options.multipleActions;
  78. const isClick = !! options.isClick;
  79. const clickTarget = options.clickTarget;
  80. let executedAtLeastOne = false;
  81. let maxPriority;
  82. if (! maxLabel)
  83. maxPriority = Infinity;
  84. else
  85. maxPriority = this.hierarchy.indexOf(maxLabel);
  86. for (let i = 0; i < this._actions.length; i++) {
  87. let currentAction = this._actions[i];
  88. if (currentAction.priority > maxPriority)
  89. return executedAtLeastOne;
  90. if (isClick && this._stopClick(currentAction, clickTarget))
  91. return executedAtLeastOne;
  92. let isEnabled = currentAction.enabledOnClick || ! isClick;
  93. if (isEnabled && currentAction.condition()) {
  94. currentAction.action();
  95. executedAtLeastOne = true;
  96. if (! multipleActions)
  97. return executedAtLeastOne;
  98. }
  99. }
  100. return executedAtLeastOne;
  101. }
  102. };
  103. // MouseTrap plugin bindGlobal plugin. Adds a bindGlobal method to Mousetrap
  104. // that allows you to bind specific keyboard shortcuts that will still work
  105. // inside a text input field.
  106. //
  107. // usage:
  108. // Mousetrap.bindGlobal('ctrl+s', _saveChanges);
  109. //
  110. // source:
  111. // https://github.com/ccampbell/mousetrap/tree/master/plugins/global-bind
  112. var _globalCallbacks = {};
  113. var _originalStopCallback = Mousetrap.stopCallback;
  114. Mousetrap.stopCallback = function(e, element, combo, sequence) {
  115. var self = this;
  116. if (self.paused) {
  117. return true;
  118. }
  119. if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
  120. return false;
  121. }
  122. return _originalStopCallback.call(self, e, element, combo);
  123. };
  124. Mousetrap.bindGlobal = function(keys, callback, action) {
  125. var self = this;
  126. self.bind(keys, callback, action);
  127. if (keys instanceof Array) {
  128. for (var i = 0; i < keys.length; i++) {
  129. _globalCallbacks[keys[i]] = true;
  130. }
  131. return;
  132. }
  133. _globalCallbacks[keys] = true;
  134. };
  135. // Pressing escape to execute one escape action. We use `bindGloabal` vecause
  136. // the shortcut sould work on textarea and inputs as well.
  137. Mousetrap.bindGlobal('esc', function() {
  138. EscapeActions.executeLowest();
  139. });
  140. // On a left click on the document, we try to exectute one escape action (eg,
  141. // close the popup). We don't execute any action if the user has clicked on a
  142. // link or a button.
  143. $(document).on('click', function(evt) {
  144. if (evt.button === 0 &&
  145. $(evt.target).closest('a,button,.is-editable').length === 0) {
  146. EscapeActions.clickExecute(evt.target, 'multiselection');
  147. }
  148. });