swimlanes.js 6.6 KB

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