escapeActions.js 4.3 KB

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