boardBody.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.componentChildren('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. this.$('.js-lists').animate({
  41. scrollLeft: position,
  42. });
  43. },
  44. currentCardIsInThisList() {
  45. const currentCard = Cards.findOne(Session.get('currentCard'));
  46. const listId = this.currentData()._id;
  47. return currentCard && currentCard.listId === listId;
  48. },
  49. events() {
  50. return [{
  51. // XXX The board-overlay div should probably be moved to the parent
  52. // component.
  53. 'mouseenter .board-overlay'() {
  54. if (this.mouseHasEnterCardDetails) {
  55. this.showOverlay.set(false);
  56. }
  57. },
  58. // Click-and-drag action
  59. 'mousedown .board-canvas'(evt) {
  60. if ($(evt.target).closest('a,.js-list-header').length === 0) {
  61. this._isDragging = true;
  62. this._lastDragPositionX = evt.clientX;
  63. }
  64. },
  65. 'mouseup'() {
  66. if (this._isDragging) {
  67. this._isDragging = false;
  68. }
  69. },
  70. 'mousemove'(evt) {
  71. if (this._isDragging) {
  72. // Update the canvas position
  73. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  74. this._lastDragPositionX = evt.clientX;
  75. // Disable browser text selection while dragging
  76. evt.stopPropagation();
  77. evt.preventDefault();
  78. // Don't close opened card or inlined form at the end of the
  79. // click-and-drag.
  80. EscapeActions.executeUpTo('popup-close');
  81. EscapeActions.preventNextClick();
  82. }
  83. },
  84. }];
  85. },
  86. }).register('board');
  87. Template.boardBody.onRendered(function() {
  88. const self = BlazeComponent.getComponentForElement(this.firstNode);
  89. self.listsDom = this.find('.js-lists');
  90. if (!Session.get('currentCard')) {
  91. self.scrollLeft();
  92. }
  93. // We want to animate the card details window closing. We rely on CSS
  94. // transition for the actual animation.
  95. self.listsDom._uihooks = {
  96. removeElement(node) {
  97. const removeNode = _.once(() => {
  98. node.parentNode.removeChild(node);
  99. });
  100. if ($(node).hasClass('js-card-details')) {
  101. $(node).css({
  102. flexBasis: 0,
  103. padding: 0,
  104. });
  105. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  106. } else {
  107. removeNode();
  108. }
  109. },
  110. };
  111. if (!Meteor.user() || !Meteor.user().isBoardMember())
  112. return;
  113. self.$(self.listsDom).sortable({
  114. tolerance: 'pointer',
  115. helper: 'clone',
  116. handle: '.js-list-header',
  117. items: '.js-list:not(.js-list-composer)',
  118. placeholder: 'list placeholder',
  119. distance: 7,
  120. start(evt, ui) {
  121. ui.placeholder.height(ui.helper.height());
  122. Popup.close();
  123. },
  124. stop() {
  125. self.$('.js-lists').find('.js-list:not(.js-list-composer)').each(
  126. (i, list) => {
  127. const data = Blaze.getData(list);
  128. Lists.update(data._id, {
  129. $set: {
  130. sort: i,
  131. },
  132. });
  133. }
  134. );
  135. },
  136. });
  137. // Disable drag-dropping while in multi-selection mode
  138. self.autorun(() => {
  139. self.$(self.listsDom).sortable('option', 'disabled',
  140. MultiSelection.isActive());
  141. });
  142. // If there is no data in the board (ie, no lists) we autofocus the list
  143. // creation form by clicking on the corresponding element.
  144. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  145. if (currentBoard.lists().count() === 0) {
  146. self.openNewListForm();
  147. }
  148. });
  149. BlazeComponent.extendComponent({
  150. template() {
  151. return 'addListForm';
  152. },
  153. // Proxy
  154. open() {
  155. this.componentChildren('inlinedForm')[0].open();
  156. },
  157. events() {
  158. return [{
  159. submit(evt) {
  160. evt.preventDefault();
  161. const title = this.find('.list-name-input');
  162. if ($.trim(title.value)) {
  163. Lists.insert({
  164. title: title.value,
  165. boardId: Session.get('currentBoard'),
  166. sort: $('.list').length,
  167. });
  168. title.value = '';
  169. }
  170. },
  171. }];
  172. },
  173. }).register('addListForm');