boardBody.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. template: function() {
  4. return 'board';
  5. },
  6. onCreated: function() {
  7. var self = this;
  8. self.draggingActive = new ReactiveVar(false);
  9. self.showOverlay = new ReactiveVar(false);
  10. self.isBoardReady = new ReactiveVar(false);
  11. // The pattern we use to manually handle data loading is described here:
  12. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  13. // XXX The boardId should be readed from some sort the component "props",
  14. // unfortunatly, Blaze doesn't have this notion.
  15. self.autorun(function() {
  16. var handle = subManager.subscribe('board', Session.get('currentBoard'));
  17. self.isBoardReady.set(handle.ready());
  18. });
  19. self._isDragging = false;
  20. self._lastDragPositionX = 0;
  21. // Used to set the overlay
  22. self.mouseHasEnterCardDetails = false;
  23. },
  24. openNewListForm: function() {
  25. this.componentChildren('addListForm')[0].open();
  26. },
  27. // XXX Flow components allow us to avoid creating these two setter methods by
  28. // exposing a public API to modify the component state. We need to investigate
  29. // best practices here.
  30. setIsDragging: function(bool) {
  31. this.draggingActive.set(bool);
  32. },
  33. scrollLeft: function(position) {
  34. position = position || 0;
  35. var $container = $(this.listsDom);
  36. var containerWidth = $container.width();
  37. var currentScrollPosition = $container.scrollLeft();
  38. if (position < currentScrollPosition) {
  39. $container.animate({
  40. scrollLeft: position
  41. });
  42. } else if (position > currentScrollPosition + containerWidth) {
  43. $container.animate({
  44. scrollLeft: Math.max(0, position - containerWidth)
  45. });
  46. }
  47. },
  48. currentCardIsInThisList: function() {
  49. var currentCard = Cards.findOne(Session.get('currentCard'));
  50. var listId = this.currentData()._id;
  51. return currentCard && currentCard.listId === listId;
  52. },
  53. events: function() {
  54. return [{
  55. // XXX The board-overlay div should probably be moved to the parent
  56. // component.
  57. 'mouseenter .board-overlay': function() {
  58. if (this.mouseHasEnterCardDetails) {
  59. this.showOverlay.set(false);
  60. }
  61. },
  62. // Click-and-drag action
  63. 'mousedown .board-canvas': function(evt) {
  64. if ($(evt.target).closest('a,.js-list-header').length === 0) {
  65. this._isDragging = true;
  66. this._lastDragPositionX = evt.clientX;
  67. }
  68. },
  69. 'mouseup': function(evt) {
  70. if (this._isDragging) {
  71. this._isDragging = false;
  72. }
  73. },
  74. 'mousemove': function(evt) {
  75. if (this._isDragging) {
  76. // Update the canvas position
  77. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  78. this._lastDragPositionX = evt.clientX;
  79. // Disable browser text selection while dragging
  80. evt.stopPropagation();
  81. evt.preventDefault();
  82. // Don't close opened card or inlined form at the end of the
  83. // click-and-drag.
  84. EscapeActions.executeUpTo('popup-close');
  85. EscapeActions.preventNextClick();
  86. }
  87. }
  88. }];
  89. }
  90. }).register('board');
  91. Template.boardBody.onRendered(function() {
  92. var self = BlazeComponent.getComponentForElement(this.firstNode);
  93. self.scrollLeft();
  94. self.listsDom = this.find('.js-lists');
  95. // We want to animate the card details window closing. We rely on CSS
  96. // transition for the actual animation.
  97. self.listsDom._uihooks = {
  98. removeElement: function(node) {
  99. var removeNode = _.once(function() {
  100. node.parentNode.removeChild(node);
  101. });
  102. if ($(node).hasClass('js-card-details')) {
  103. $(node).css({
  104. flexBasis: 0,
  105. padding: 0
  106. });
  107. $(self.listsDom).one(CSSEvents.transitionend, removeNode);
  108. } else {
  109. removeNode();
  110. }
  111. }
  112. };
  113. if (! Meteor.user() || ! Meteor.user().isBoardMember())
  114. return;
  115. self.$(self.listsDom).sortable({
  116. tolerance: 'pointer',
  117. helper: 'clone',
  118. handle: '.js-list-header',
  119. items: '.js-list:not(.js-list-composer)',
  120. placeholder: 'list placeholder',
  121. distance: 7,
  122. start: function(evt, ui) {
  123. ui.placeholder.height(ui.helper.height());
  124. Popup.close();
  125. },
  126. stop: function() {
  127. self.$('.js-lists').find('.js-list:not(.js-list-composer)').each(
  128. function(i, list) {
  129. var data = Blaze.getData(list);
  130. Lists.update(data._id, {
  131. $set: {
  132. sort: i
  133. }
  134. });
  135. }
  136. );
  137. }
  138. });
  139. // Disable drag-dropping while in multi-selection mode
  140. self.autorun(function() {
  141. self.$(self.listsDom).sortable('option', 'disabled',
  142. MultiSelection.isActive());
  143. });
  144. // If there is no data in the board (ie, no lists) we autofocus the list
  145. // creation form by clicking on the corresponding element.
  146. var currentBoard = Boards.findOne(Session.get('currentBoard'));
  147. if (currentBoard.lists().count() === 0) {
  148. self.openNewListForm();
  149. }
  150. });
  151. BlazeComponent.extendComponent({
  152. template: function() {
  153. return 'addListForm';
  154. },
  155. // Proxy
  156. open: function() {
  157. this.componentChildren('inlinedForm')[0].open();
  158. },
  159. events: function() {
  160. return [{
  161. submit: function(evt) {
  162. evt.preventDefault();
  163. var title = this.find('.list-name-input');
  164. if ($.trim(title.value)) {
  165. Lists.insert({
  166. title: title.value,
  167. boardId: Session.get('currentBoard'),
  168. sort: $('.list').length
  169. });
  170. title.value = '';
  171. }
  172. }
  173. }];
  174. }
  175. }).register('addListForm');