escapeActions.js 4.3 KB

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