boardBody.js 4.6 KB

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