boardBody.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. onCreated() {
  4. this.draggingActive = new ReactiveVar(false);
  5. this.showOverlay = new ReactiveVar(false);
  6. this.isBoardReady = new ReactiveVar(false);
  7. // The pattern we use to manually handle data loading is described here:
  8. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  9. // XXX The boardId should be readed from some sort the component "props",
  10. // unfortunatly, Blaze doesn't have this notion.
  11. this.autorun(() => {
  12. const currentBoardId = Session.get('currentBoard');
  13. if (!currentBoardId)
  14. return;
  15. const handle = subManager.subscribe('board', currentBoardId);
  16. Tracker.nonreactive(() => {
  17. Tracker.autorun(() => {
  18. this.isBoardReady.set(handle.ready());
  19. });
  20. });
  21. });
  22. this._isDragging = false;
  23. this._lastDragPositionX = 0;
  24. // Used to set the overlay
  25. this.mouseHasEnterCardDetails = false;
  26. },
  27. openNewListForm() {
  28. this.childComponents('addListForm')[0].open();
  29. },
  30. // XXX Flow components allow us to avoid creating these two setter methods by
  31. // exposing a public API to modify the component state. We need to investigate
  32. // best practices here.
  33. setIsDragging(bool) {
  34. this.draggingActive.set(bool);
  35. },
  36. scrollLeft(position = 0) {
  37. const lists = this.$('.js-lists');
  38. lists && lists.animate({
  39. scrollLeft: position,
  40. });
  41. },
  42. currentCardIsInThisList() {
  43. const currentCard = Cards.findOne(Session.get('currentCard'));
  44. const listId = this.currentData()._id;
  45. return currentCard && currentCard.listId === listId;
  46. },
  47. onlyShowCurrentCard() {
  48. return Utils.isMiniScreen() && Session.get('currentCard');
  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 && $('.lists').prop('clientHeight') > evt.offsetY) {
  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. $(self.listsDom).sortable({
  119. tolerance: 'pointer',
  120. helper: 'clone',
  121. handle: '.js-list-header',
  122. items: '.js-list:not(.js-list-composer)',
  123. placeholder: 'list placeholder',
  124. distance: 7,
  125. start(evt, ui) {
  126. ui.placeholder.height(ui.helper.height());
  127. Popup.close();
  128. },
  129. stop() {
  130. $(self.listsDom).find('.js-list:not(.js-list-composer)').each(
  131. (i, list) => {
  132. const data = Blaze.getData(list);
  133. Lists.update(data._id, {
  134. $set: {
  135. sort: i,
  136. },
  137. });
  138. }
  139. );
  140. },
  141. });
  142. function userIsMember() {
  143. return Meteor.user() && Meteor.user().isBoardMember();
  144. }
  145. // Disable drag-dropping while in multi-selection mode, or if the current user
  146. // is not a board member
  147. self.autorun(() => {
  148. const $listDom = $(self.listsDom);
  149. if ($listDom.data('sortable')) {
  150. $(self.listsDom).sortable('option', 'disabled',
  151. MultiSelection.isActive() || !userIsMember());
  152. }
  153. });
  154. // If there is no data in the board (ie, no lists) we autofocus the list
  155. // creation form by clicking on the corresponding element.
  156. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  157. if (userIsMember() && currentBoard.lists().count() === 0) {
  158. self.openNewListForm();
  159. }
  160. });
  161. BlazeComponent.extendComponent({
  162. // Proxy
  163. open() {
  164. this.childComponents('inlinedForm')[0].open();
  165. },
  166. events() {
  167. return [{
  168. submit(evt) {
  169. evt.preventDefault();
  170. const titleInput = this.find('.list-name-input');
  171. const title = titleInput.value.trim();
  172. if (title) {
  173. Lists.insert({
  174. title,
  175. boardId: Session.get('currentBoard'),
  176. sort: $('.list').length,
  177. });
  178. titleInput.value = '';
  179. titleInput.focus();
  180. }
  181. },
  182. }];
  183. },
  184. }).register('addListForm');
  185. Template.boardBody.helpers({
  186. canSeeAddList() {
  187. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  188. },
  189. });