popup.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. window.Popup = new class {
  2. constructor() {
  3. // The template we use to render popups
  4. this.template = Template.popup;
  5. // We only want to display one popup at a time and we keep the view object
  6. // in this `Popup._current` variable. If there is no popup currently opened
  7. // the value is `null`.
  8. this._current = null;
  9. // It's possible to open a sub-popup B from a popup A. In that case we keep
  10. // the data of popup A so we can return back to it. Every time we open a new
  11. // popup the stack grows, every time we go back the stack decrease, and if
  12. // we close the popup the stack is reseted to the empty stack [].
  13. this._stack = [];
  14. // We invalidate this internal dependency every time the top of the stack
  15. // has changed and we want to re-render a popup with the new top-stack data.
  16. this._dep = new Tracker.Dependency();
  17. }
  18. /// This function returns a callback that can be used in an event map:
  19. /// Template.tplName.events({
  20. /// 'click .elementClass': Popup.open("popupName"),
  21. /// });
  22. /// The popup inherit the data context of its parent.
  23. open(name) {
  24. const self = this;
  25. const popupName = `${name}Popup`;
  26. function clickFromPopup(evt) {
  27. return $(evt.target).closest('.js-pop-over').length !== 0;
  28. }
  29. return function(evt) {
  30. // If a popup is already opened, clicking again on the opener element
  31. // should close it -- and interrupt the current `open` function.
  32. if (self.isOpen()) {
  33. const previousOpenerElement = self._getTopStack().openerElement;
  34. if (previousOpenerElement === evt.currentTarget) {
  35. self.close();
  36. return;
  37. } else {
  38. $(previousOpenerElement).removeClass('is-active');
  39. }
  40. }
  41. // We determine the `openerElement` (the DOM element that is being clicked
  42. // and the one we take in reference to position the popup) from the event
  43. // if the popup has no parent, or from the parent `openerElement` if it
  44. // has one. This allows us to position a sub-popup exactly at the same
  45. // position than its parent.
  46. let openerElement;
  47. if (clickFromPopup(evt)) {
  48. openerElement = self._getTopStack().openerElement;
  49. } else {
  50. self._stack = [];
  51. openerElement = evt.currentTarget;
  52. }
  53. $(openerElement).addClass('is-active');
  54. evt.preventDefault();
  55. // We push our popup data to the stack. The top of the stack is always
  56. // used as the data source for our current popup.
  57. self._stack.push({
  58. popupName,
  59. openerElement,
  60. hasPopupParent: clickFromPopup(evt),
  61. title: self._getTitle(popupName),
  62. depth: self._stack.length,
  63. offset: self._getOffset(openerElement),
  64. dataContext: this.currentData && this.currentData() || this,
  65. });
  66. // If there are no popup currently opened we use the Blaze API to render
  67. // one into the DOM. We use a reactive function as the data parameter that
  68. // return the complete along with its top element and depends on our
  69. // internal dependency that is being invalidated every time the top
  70. // element of the stack has changed and we want to update the popup.
  71. //
  72. // Otherwise if there is already a popup open we just need to invalidate
  73. // our internal dependency, and since we just changed the top element of
  74. // our internal stack, the popup will be updated with the new data.
  75. if (!self.isOpen()) {
  76. console.log(self.template)
  77. self.current = Blaze.renderWithData(self.template, () => {
  78. self._dep.depend();
  79. return { ...self._getTopStack(), stack: self._stack };
  80. }, document.body);
  81. } else {
  82. self._dep.changed();
  83. }
  84. };
  85. }
  86. /// This function returns a callback that can be used in an event map:
  87. /// Template.tplName.events({
  88. /// 'click .elementClass': Popup.afterConfirm("popupName", function() {
  89. /// // What to do after the user has confirmed the action
  90. /// }),
  91. /// });
  92. afterConfirm(name, action) {
  93. const self = this;
  94. return function(evt, tpl) {
  95. const context = this.currentData && this.currentData() || this;
  96. context.__afterConfirmAction = action;
  97. self.open(name).call(context, evt, tpl);
  98. };
  99. }
  100. /// The public reactive state of the popup.
  101. isOpen() {
  102. this._dep.changed();
  103. return Boolean(this.current);
  104. }
  105. /// In case the popup was opened from a parent popup we can get back to it
  106. /// with this `Popup.back()` function. You can go back several steps at once
  107. /// by providing a number to this function, e.g. `Popup.back(2)`. In this case
  108. /// intermediate popup won't even be rendered on the DOM. If the number of
  109. /// steps back is greater than the popup stack size, the popup will be closed.
  110. back(n = 1) {
  111. if (this._stack.length > n) {
  112. _.times(n, () => this._stack.pop());
  113. this._dep.changed();
  114. } else {
  115. this.close();
  116. }
  117. }
  118. /// Close the current opened popup.
  119. close() {
  120. if (this.isOpen()) {
  121. Blaze.remove(this.current);
  122. this.current = null;
  123. const openerElement = this._getTopStack().openerElement;
  124. $(openerElement).removeClass('is-active');
  125. this._stack = [];
  126. }
  127. }
  128. getOpenerComponent() {
  129. const { openerElement } = Template.parentData(4);
  130. return BlazeComponent.getComponentForElement(openerElement);
  131. }
  132. // An utility fonction that returns the top element of the internal stack
  133. _getTopStack() {
  134. return this._stack[this._stack.length - 1];
  135. }
  136. // We automatically calculate the popup offset from the reference element
  137. // position and dimensions. We also reactively use the window dimensions to
  138. // ensure that the popup is always visible on the screen.
  139. _getOffset(element) {
  140. const $element = $(element);
  141. return () => {
  142. Utils.windowResizeDep.depend();
  143. if(Utils.isMiniScreen()) return { left:0, top:0 };
  144. const offset = $element.offset();
  145. const popupWidth = 300 + 15;
  146. return {
  147. left: Math.min(offset.left, $(window).width() - popupWidth),
  148. top: offset.top + $element.outerHeight(),
  149. };
  150. };
  151. }
  152. // We get the title from the translation files. Instead of returning the
  153. // result, we return a function that compute the result and since `TAPi18n.__`
  154. // is a reactive data source, the title will be changed reactively.
  155. _getTitle(popupName) {
  156. return () => {
  157. const translationKey = `${popupName}-title`;
  158. // XXX There is no public API to check if there is an available
  159. // translation for a given key. So we try to translate the key and if the
  160. // translation output equals the key input we deduce that no translation
  161. // was available and returns `false`. There is a (small) risk a false
  162. // positives.
  163. const title = TAPi18n.__(translationKey);
  164. // when popup showed as full of small screen, we need a default header to clearly see [X] button
  165. const defaultTitle = Utils.isMiniScreen() ? 'Wekan' : false;
  166. return title !== translationKey ? title : defaultTitle;
  167. };
  168. }
  169. }();
  170. // We close a potential opened popup on any left click on the document, or go
  171. // one step back by pressing escape.
  172. const escapeActions = ['back', 'close'];
  173. escapeActions.forEach((actionName) => {
  174. EscapeActions.register(`popup-${actionName}`,
  175. () => Popup[actionName](),
  176. () => Popup.isOpen(),
  177. {
  178. noClickEscapeOn: '.js-pop-over',
  179. enabledOnClick: actionName === 'close',
  180. }
  181. );
  182. });