boardBody.js 5.6 KB

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