escapeActions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(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. const noClickEscapeOn = options.noClickEscapeOn;
  30. this._actions = _.sortBy(
  31. [
  32. ...this._actions,
  33. {
  34. priority,
  35. condition,
  36. action,
  37. noClickEscapeOn,
  38. enabledOnClick,
  39. },
  40. ],
  41. action => action.priority,
  42. );
  43. },
  44. executeLowest() {
  45. return this._execute({
  46. multipleActions: false,
  47. });
  48. },
  49. executeAll() {
  50. return this._execute({
  51. multipleActions: true,
  52. });
  53. },
  54. executeUpTo(maxLabel) {
  55. return this._execute({
  56. maxLabel,
  57. multipleActions: true,
  58. });
  59. },
  60. clickExecute(target, maxLabel) {
  61. if (this._nextclickPrevented) {
  62. this._nextclickPrevented = false;
  63. return false;
  64. } else {
  65. return this._execute({
  66. maxLabel,
  67. multipleActions: false,
  68. isClick: true,
  69. clickTarget: target,
  70. });
  71. }
  72. },
  73. preventNextClick() {
  74. this._nextclickPrevented = true;
  75. },
  76. _stopClick(action, clickTarget) {
  77. if (!_.isString(action.noClickEscapeOn)) return false;
  78. else return $(clickTarget).closest(action.noClickEscapeOn).length > 0;
  79. },
  80. _execute(options) {
  81. const maxLabel = options.maxLabel;
  82. const multipleActions = options.multipleActions;
  83. const isClick = Boolean(options.isClick);
  84. const clickTarget = options.clickTarget;
  85. let executedAtLeastOne = false;
  86. let maxPriority;
  87. if (!maxLabel) maxPriority = Infinity;
  88. else maxPriority = this.hierarchy.indexOf(maxLabel);
  89. for (const currentAction of this._actions) {
  90. if (currentAction.priority > maxPriority) return executedAtLeastOne;
  91. if (isClick && this._stopClick(currentAction, clickTarget))
  92. return executedAtLeastOne;
  93. const isEnabled = currentAction.enabledOnClick || !isClick;
  94. if (isEnabled && currentAction.condition()) {
  95. currentAction.action();
  96. executedAtLeastOne = true;
  97. if (!multipleActions) return executedAtLeastOne;
  98. }
  99. }
  100. return executedAtLeastOne;
  101. },
  102. };
  103. // Pressing escape to execute one escape action. We use `bindGloabal` vecause
  104. // the shortcut sould work on textarea and inputs as well.
  105. Mousetrap.bindGlobal('esc', () => {
  106. EscapeActions.executeLowest();
  107. Sidebar.hide();
  108. });
  109. // On a left click on the document, we try to exectute one escape action (eg,
  110. // close the popup). We don't execute any action if the user has clicked on a
  111. // link or a button.
  112. $(document).on('click', evt => {
  113. if (
  114. evt.button === 0 &&
  115. $(evt.target).closest('a,button,.is-editable').length === 0
  116. ) {
  117. EscapeActions.clickExecute(evt.target, 'multiselection');
  118. }
  119. });
  120. $(document).on('click', 'a[href=\\#]', evt => {
  121. evt.preventDefault();
  122. });