swimlanes.js 6.4 KB

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