boardBody.js 5.8 KB

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