swimlanes.js 6.3 KB

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