2
0

swimlanes.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const { calculateIndex } = Utils;
  2. BlazeComponent.extendComponent({
  3. onRendered() {
  4. const boardComponent = this.parentComponent();
  5. const $listsDom = this.$('.js-lists');
  6. if (!Session.get('currentCard')) {
  7. boardComponent.scrollLeft();
  8. }
  9. // We want to animate the card details window closing. We rely on CSS
  10. // transition for the actual animation.
  11. $listsDom._uihooks = {
  12. removeElement(node) {
  13. const removeNode = _.once(() => {
  14. node.parentNode.removeChild(node);
  15. });
  16. if ($(node).hasClass('js-card-details')) {
  17. $(node).css({
  18. flexBasis: 0,
  19. padding: 0,
  20. });
  21. $listsDom.one(CSSEvents.transitionend, removeNode);
  22. } else {
  23. removeNode();
  24. }
  25. },
  26. };
  27. $listsDom.sortable({
  28. tolerance: 'pointer',
  29. helper: 'clone',
  30. handle: '.js-list-header',
  31. items: '.js-list:not(.js-list-composer)',
  32. placeholder: 'list placeholder',
  33. distance: 7,
  34. start(evt, ui) {
  35. ui.placeholder.height(ui.helper.height());
  36. EscapeActions.executeUpTo('popup-close');
  37. boardComponent.setIsDragging(true);
  38. },
  39. stop(evt, ui) {
  40. // To attribute the new index number, we need to get the DOM element
  41. // of the previous and the following card -- if any.
  42. const prevListDom = ui.item.prev('.js-list').get(0);
  43. const nextListDom = ui.item.next('.js-list').get(0);
  44. const sortIndex = calculateIndex(prevListDom, nextListDom, 1);
  45. $listsDom.sortable('cancel');
  46. const listDomElement = ui.item.get(0);
  47. const list = Blaze.getData(listDomElement);
  48. Lists.update(list._id, {
  49. $set: {
  50. sort: sortIndex.base,
  51. },
  52. });
  53. boardComponent.setIsDragging(false);
  54. },
  55. });
  56. function userIsMember() {
  57. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  58. }
  59. // Disable drag-dropping while in multi-selection mode, or if the current user
  60. // is not a board member
  61. boardComponent.autorun(() => {
  62. const $listDom = $listsDom;
  63. if ($listDom.data('sortable')) {
  64. $listsDom.sortable('option', 'disabled',
  65. MultiSelection.isActive() || !userIsMember());
  66. }
  67. });
  68. },
  69. onCreated() {
  70. this.draggingActive = new ReactiveVar(false);
  71. this._isDragging = false;
  72. this._lastDragPositionX = 0;
  73. },
  74. id() {
  75. return this._id;
  76. },
  77. currentCardIsInThisList(listId, swimlaneId) {
  78. const currentCard = Cards.findOne(Session.get('currentCard'));
  79. const currentBoardId = Session.get('currentBoard');
  80. const board = Boards.findOne(currentBoardId);
  81. if (board.view === 'board-view-lists')
  82. return currentCard && currentCard.listId === listId;
  83. else if (board.view === 'board-view-swimlanes')
  84. return currentCard && currentCard.listId === listId && currentCard.swimlaneId === swimlaneId;
  85. else
  86. return false;
  87. },
  88. events() {
  89. return [{
  90. // Click-and-drag action
  91. 'mousedown .board-canvas'(evt) {
  92. // Translating the board canvas using the click-and-drag action can
  93. // conflict with the build-in browser mechanism to select text. We
  94. // define a list of elements in which we disable the dragging because
  95. // the user will legitimately expect to be able to select some text with
  96. // his mouse.
  97. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  98. if ($(evt.target).closest(noDragInside.join(',')).length === 0 && this.$('.swimlane').prop('clientHeight') > evt.offsetY) {
  99. this._isDragging = true;
  100. this._lastDragPositionX = evt.clientX;
  101. }
  102. },
  103. 'mouseup'() {
  104. if (this._isDragging) {
  105. this._isDragging = false;
  106. }
  107. },
  108. 'mousemove'(evt) {
  109. if (this._isDragging) {
  110. // Update the canvas position
  111. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  112. this._lastDragPositionX = evt.clientX;
  113. // Disable browser text selection while dragging
  114. evt.stopPropagation();
  115. evt.preventDefault();
  116. // Don't close opened card or inlined form at the end of the
  117. // click-and-drag.
  118. EscapeActions.executeUpTo('popup-close');
  119. EscapeActions.preventNextClick();
  120. }
  121. },
  122. }];
  123. },
  124. }).register('swimlane');
  125. BlazeComponent.extendComponent({
  126. // Proxy
  127. open() {
  128. this.childComponents('inlinedForm')[0].open();
  129. },
  130. events() {
  131. return [{
  132. submit(evt) {
  133. evt.preventDefault();
  134. const titleInput = this.find('.list-name-input');
  135. const title = titleInput.value.trim();
  136. if (title) {
  137. Lists.insert({
  138. title,
  139. boardId: Session.get('currentBoard'),
  140. sort: $('.list').length,
  141. });
  142. titleInput.value = '';
  143. titleInput.focus();
  144. }
  145. },
  146. }];
  147. },
  148. }).register('addListForm');
  149. BlazeComponent.extendComponent({
  150. // Proxy
  151. open() {
  152. this.childComponents('inlinedForm')[0].open();
  153. },
  154. events() {
  155. return [{
  156. submit(evt) {
  157. evt.preventDefault();
  158. let titleInput = this.find('.list-name-input');
  159. if (titleInput) {
  160. const title = titleInput.value.trim();
  161. if (title) {
  162. Lists.insert({
  163. title,
  164. boardId: Session.get('currentBoard'),
  165. sort: $('.list').length,
  166. });
  167. titleInput.value = '';
  168. titleInput.focus();
  169. }
  170. } else {
  171. titleInput = this.find('.swimlane-name-input');
  172. const title = titleInput.value.trim();
  173. if (title) {
  174. Swimlanes.insert({
  175. title,
  176. boardId: Session.get('currentBoard'),
  177. sort: $('.swimlane').length,
  178. });
  179. titleInput.value = '';
  180. titleInput.focus();
  181. }
  182. }
  183. },
  184. }];
  185. },
  186. }).register('addListAndSwimlaneForm');
  187. Template.swimlane.helpers({
  188. canSeeAddList() {
  189. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  190. },
  191. });