swimlanes.js 5.7 KB

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