boardBody.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. template: function() {
  4. return 'board';
  5. },
  6. onCreated: function() {
  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. let currentBoardId = Session.get('currentBoard');
  16. if (! currentBoardId)
  17. return;
  18. var 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: function() {
  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: function(bool) {
  37. this.draggingActive.set(bool);
  38. },
  39. scrollLeft: function(position) {
  40. position = position || 0;
  41. var $container = $(this.listsDom);
  42. var containerWidth = $container.width();
  43. var currentScrollPosition = $container.scrollLeft();
  44. if (position < currentScrollPosition) {
  45. $container.animate({
  46. scrollLeft: position
  47. });
  48. } else if (position > currentScrollPosition + containerWidth) {
  49. $container.animate({
  50. scrollLeft: Math.max(0, position - containerWidth)
  51. });
  52. }
  53. },
  54. currentCardIsInThisList: function() {
  55. var currentCard = Cards.findOne(Session.get('currentCard'));
  56. var listId = this.currentData()._id;
  57. return currentCard && currentCard.listId === listId;
  58. },
  59. events: function() {
  60. return [{
  61. // XXX The board-overlay div should probably be moved to the parent
  62. // component.
  63. 'mouseenter .board-overlay': function() {
  64. if (this.mouseHasEnterCardDetails) {
  65. this.showOverlay.set(false);
  66. }
  67. },
  68. // Click-and-drag action
  69. 'mousedown .board-canvas': function(evt) {
  70. if ($(evt.target).closest('a,.js-list-header').length === 0) {
  71. this._isDragging = true;
  72. this._lastDragPositionX = evt.clientX;
  73. }
  74. },
  75. 'mouseup': function(evt) {
  76. if (this._isDragging) {
  77. this._isDragging = false;
  78. }
  79. },
  80. 'mousemove': function(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. var self = BlazeComponent.getComponentForElement(this.firstNode);
  99. self.scrollLeft();
  100. self.listsDom = this.find('.js-lists');
  101. // We want to animate the card details window closing. We rely on CSS
  102. // transition for the actual animation.
  103. self.listsDom._uihooks = {
  104. removeElement: function(node) {
  105. var removeNode = _.once(function() {
  106. node.parentNode.removeChild(node);
  107. });
  108. if ($(node).hasClass('js-card-details')) {
  109. $(node).css({
  110. flexBasis: 0,
  111. padding: 0
  112. });
  113. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  114. } else {
  115. removeNode();
  116. }
  117. }
  118. };
  119. if (! Meteor.user() || ! Meteor.user().isBoardMember())
  120. return;
  121. self.$(self.listsDom).sortable({
  122. tolerance: 'pointer',
  123. helper: 'clone',
  124. handle: '.js-list-header',
  125. items: '.js-list:not(.js-list-composer)',
  126. placeholder: 'list placeholder',
  127. distance: 7,
  128. start: function(evt, ui) {
  129. ui.placeholder.height(ui.helper.height());
  130. Popup.close();
  131. },
  132. stop: function() {
  133. self.$('.js-lists').find('.js-list:not(.js-list-composer)').each(
  134. function(i, list) {
  135. var data = Blaze.getData(list);
  136. Lists.update(data._id, {
  137. $set: {
  138. sort: i
  139. }
  140. });
  141. }
  142. );
  143. }
  144. });
  145. // Disable drag-dropping while in multi-selection mode
  146. self.autorun(function() {
  147. self.$(self.listsDom).sortable('option', 'disabled',
  148. MultiSelection.isActive());
  149. });
  150. // If there is no data in the board (ie, no lists) we autofocus the list
  151. // creation form by clicking on the corresponding element.
  152. var currentBoard = Boards.findOne(Session.get('currentBoard'));
  153. if (currentBoard.lists().count() === 0) {
  154. self.openNewListForm();
  155. }
  156. });
  157. BlazeComponent.extendComponent({
  158. template: function() {
  159. return 'addListForm';
  160. },
  161. // Proxy
  162. open: function() {
  163. this.componentChildren('inlinedForm')[0].open();
  164. },
  165. events: function() {
  166. return [{
  167. submit: function(evt) {
  168. evt.preventDefault();
  169. var title = this.find('.list-name-input');
  170. if ($.trim(title.value)) {
  171. Lists.insert({
  172. title: title.value,
  173. boardId: Session.get('currentBoard'),
  174. sort: $('.list').length
  175. });
  176. title.value = '';
  177. }
  178. }
  179. }];
  180. }
  181. }).register('addListForm');