boardBody.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. const subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. template() {
  4. return 'board';
  5. },
  6. onCreated() {
  7. this.draggingActive = new ReactiveVar(false);
  8. this.showOverlay = new ReactiveVar(false);
  9. this.isBoardReady = new ReactiveVar(false);
  10. // The pattern we use to manually handle data loading is described here:
  11. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  12. // XXX The boardId should be readed from some sort the component "props",
  13. // unfortunatly, Blaze doesn't have this notion.
  14. this.autorun(() => {
  15. const currentBoardId = Session.get('currentBoard');
  16. if (!currentBoardId)
  17. return;
  18. const handle = subManager.subscribe('board', currentBoardId);
  19. Tracker.nonreactive(() => {
  20. Tracker.autorun(() => {
  21. this.isBoardReady.set(handle.ready());
  22. });
  23. });
  24. });
  25. this._isDragging = false;
  26. this._lastDragPositionX = 0;
  27. // Used to set the overlay
  28. this.mouseHasEnterCardDetails = false;
  29. },
  30. openNewListForm() {
  31. this.childComponents('addListForm')[0].open();
  32. },
  33. // XXX Flow components allow us to avoid creating these two setter methods by
  34. // exposing a public API to modify the component state. We need to investigate
  35. // best practices here.
  36. setIsDragging(bool) {
  37. this.draggingActive.set(bool);
  38. },
  39. scrollLeft(position = 0) {
  40. const lists = this.$('.js-lists');
  41. lists && lists.animate({
  42. scrollLeft: position,
  43. });
  44. },
  45. currentCardIsInThisList() {
  46. const currentCard = Cards.findOne(Session.get('currentCard'));
  47. const listId = this.currentData()._id;
  48. return currentCard && currentCard.listId === listId;
  49. },
  50. events() {
  51. return [{
  52. // XXX The board-overlay div should probably be moved to the parent
  53. // component.
  54. 'mouseenter .board-overlay'() {
  55. if (this.mouseHasEnterCardDetails) {
  56. this.showOverlay.set(false);
  57. }
  58. },
  59. // Click-and-drag action
  60. 'mousedown .board-canvas'(evt) {
  61. // Translating the board canvas using the click-and-drag action can
  62. // conflict with the build-in browser mechanism to select text. We
  63. // define a list of elements in which we disable the dragging because
  64. // the user will legitimately expect to be able to select some text with
  65. // his mouse.
  66. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  67. if ($(evt.target).closest(noDragInside.join(',')).length === 0) {
  68. this._isDragging = true;
  69. this._lastDragPositionX = evt.clientX;
  70. }
  71. },
  72. 'mouseup'() {
  73. if (this._isDragging) {
  74. this._isDragging = false;
  75. }
  76. },
  77. 'mousemove'(evt) {
  78. if (this._isDragging) {
  79. // Update the canvas position
  80. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  81. this._lastDragPositionX = evt.clientX;
  82. // Disable browser text selection while dragging
  83. evt.stopPropagation();
  84. evt.preventDefault();
  85. // Don't close opened card or inlined form at the end of the
  86. // click-and-drag.
  87. EscapeActions.executeUpTo('popup-close');
  88. EscapeActions.preventNextClick();
  89. }
  90. },
  91. }];
  92. },
  93. }).register('board');
  94. Template.boardBody.onRendered(function() {
  95. const self = BlazeComponent.getComponentForElement(this.firstNode);
  96. self.listsDom = this.find('.js-lists');
  97. if (!Session.get('currentCard')) {
  98. self.scrollLeft();
  99. }
  100. // We want to animate the card details window closing. We rely on CSS
  101. // transition for the actual animation.
  102. self.listsDom._uihooks = {
  103. removeElement(node) {
  104. const removeNode = _.once(() => {
  105. node.parentNode.removeChild(node);
  106. });
  107. if ($(node).hasClass('js-card-details')) {
  108. $(node).css({
  109. flexBasis: 0,
  110. padding: 0,
  111. });
  112. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  113. } else {
  114. removeNode();
  115. }
  116. },
  117. };
  118. if (!Meteor.user() || !Meteor.user().isBoardMember())
  119. return;
  120. $(self.listsDom).sortable({
  121. tolerance: 'pointer',
  122. helper: 'clone',
  123. handle: '.js-list-header',
  124. items: '.js-list:not(.js-list-composer)',
  125. placeholder: 'list placeholder',
  126. distance: 7,
  127. start(evt, ui) {
  128. ui.placeholder.height(ui.helper.height());
  129. Popup.close();
  130. },
  131. stop() {
  132. $(self.listsDom).find('.js-list:not(.js-list-composer)').each(
  133. (i, list) => {
  134. const data = Blaze.getData(list);
  135. Lists.update(data._id, {
  136. $set: {
  137. sort: i,
  138. },
  139. });
  140. }
  141. );
  142. },
  143. });
  144. // Disable drag-dropping while in multi-selection mode
  145. self.autorun(() => {
  146. $(self.listsDom).sortable('option', 'disabled',
  147. MultiSelection.isActive());
  148. });
  149. // If there is no data in the board (ie, no lists) we autofocus the list
  150. // creation form by clicking on the corresponding element.
  151. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  152. if (currentBoard.lists().count() === 0) {
  153. self.openNewListForm();
  154. }
  155. });
  156. BlazeComponent.extendComponent({
  157. template() {
  158. return 'addListForm';
  159. },
  160. // Proxy
  161. open() {
  162. this.childComponents('inlinedForm')[0].open();
  163. },
  164. events() {
  165. return [{
  166. submit(evt) {
  167. evt.preventDefault();
  168. const titleInput = this.find('.list-name-input');
  169. const title = titleInput.value.trim();
  170. if (title) {
  171. Lists.insert({
  172. title,
  173. boardId: Session.get('currentBoard'),
  174. sort: $('.list').length,
  175. });
  176. titleInput.value = '';
  177. titleInput.focus();
  178. }
  179. },
  180. }];
  181. },
  182. }).register('addListForm');