swimlanes.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 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. function userIsMember() {
  63. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  64. }
  65. // Disable drag-dropping while in multi-selection mode, or if the current user
  66. // is not a board member
  67. boardComponent.autorun(() => {
  68. const $listDom = $listsDom;
  69. if ($listDom.data('sortable')) {
  70. $listsDom.sortable('option', 'disabled',
  71. MultiSelection.isActive() || !userIsMember());
  72. }
  73. });
  74. }
  75. BlazeComponent.extendComponent({
  76. onRendered() {
  77. const boardComponent = this.parentComponent();
  78. const $listsDom = this.$('.js-lists');
  79. if (!Session.get('currentCard')) {
  80. boardComponent.scrollLeft();
  81. }
  82. initSortable(boardComponent, $listsDom);
  83. },
  84. onCreated() {
  85. this.draggingActive = new ReactiveVar(false);
  86. this._isDragging = false;
  87. this._lastDragPositionX = 0;
  88. },
  89. id() {
  90. return this._id;
  91. },
  92. currentCardIsInThisList(listId, swimlaneId) {
  93. return currentCardIsInThisList(listId, swimlaneId);
  94. },
  95. events() {
  96. return [{
  97. // Click-and-drag action
  98. 'mousedown .board-canvas'(evt) {
  99. // Translating the board canvas using the click-and-drag action can
  100. // conflict with the build-in browser mechanism to select text. We
  101. // define a list of elements in which we disable the dragging because
  102. // the user will legitimately expect to be able to select some text with
  103. // his mouse.
  104. const noDragInside = ['a', 'input', 'textarea', 'p', '.js-list-header'];
  105. if ($(evt.target).closest(noDragInside.join(',')).length === 0 && this.$('.swimlane').prop('clientHeight') > evt.offsetY) {
  106. this._isDragging = true;
  107. this._lastDragPositionX = evt.clientX;
  108. }
  109. },
  110. 'mouseup'() {
  111. if (this._isDragging) {
  112. this._isDragging = false;
  113. }
  114. },
  115. 'mousemove'(evt) {
  116. if (this._isDragging) {
  117. // Update the canvas position
  118. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  119. this._lastDragPositionX = evt.clientX;
  120. // Disable browser text selection while dragging
  121. evt.stopPropagation();
  122. evt.preventDefault();
  123. // Don't close opened card or inlined form at the end of the
  124. // click-and-drag.
  125. EscapeActions.executeUpTo('popup-close');
  126. EscapeActions.preventNextClick();
  127. }
  128. },
  129. }];
  130. },
  131. }).register('swimlane');
  132. BlazeComponent.extendComponent({
  133. // Proxy
  134. open() {
  135. this.childComponents('inlinedForm')[0].open();
  136. },
  137. events() {
  138. return [{
  139. submit(evt) {
  140. evt.preventDefault();
  141. const titleInput = this.find('.list-name-input');
  142. const title = titleInput.value.trim();
  143. if (title) {
  144. Lists.insert({
  145. title,
  146. boardId: Session.get('currentBoard'),
  147. sort: $('.list').length,
  148. });
  149. titleInput.value = '';
  150. titleInput.focus();
  151. }
  152. },
  153. }];
  154. },
  155. }).register('addListForm');
  156. BlazeComponent.extendComponent({
  157. // Proxy
  158. open() {
  159. this.childComponents('inlinedForm')[0].open();
  160. },
  161. events() {
  162. return [{
  163. submit(evt) {
  164. evt.preventDefault();
  165. let titleInput = this.find('.list-name-input');
  166. if (titleInput) {
  167. const title = titleInput.value.trim();
  168. if (title) {
  169. Lists.insert({
  170. title,
  171. boardId: Session.get('currentBoard'),
  172. sort: $('.list').length,
  173. });
  174. titleInput.value = '';
  175. titleInput.focus();
  176. }
  177. } else {
  178. titleInput = this.find('.swimlane-name-input');
  179. const title = titleInput.value.trim();
  180. if (title) {
  181. Swimlanes.insert({
  182. title,
  183. boardId: Session.get('currentBoard'),
  184. sort: $('.swimlane').length,
  185. });
  186. titleInput.value = '';
  187. titleInput.focus();
  188. }
  189. }
  190. },
  191. }];
  192. },
  193. }).register('addListAndSwimlaneForm');
  194. Template.swimlane.helpers({
  195. canSeeAddList() {
  196. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  197. },
  198. });
  199. BlazeComponent.extendComponent({
  200. currentCardIsInThisList(listId, swimlaneId) {
  201. return currentCardIsInThisList(listId, swimlaneId);
  202. },
  203. onRendered() {
  204. const boardComponent = this.parentComponent();
  205. const $listsDom = this.$('.js-lists');
  206. if (!Session.get('currentCard')) {
  207. boardComponent.scrollLeft();
  208. }
  209. initSortable(boardComponent, $listsDom);
  210. },
  211. }).register('listsGroup');