boardBody.js 5.7 KB

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