swimlanes.js 6.6 KB

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