swimlanes.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. const { calculateIndex } = Utils;
  3. function currentListIsInThisSwimlane(swimlaneId) {
  4. const currentList = Utils.getCurrentList();
  5. return (
  6. currentList &&
  7. (currentList.swimlaneId === swimlaneId || currentList.swimlaneId === '')
  8. );
  9. }
  10. function currentCardIsInThisList(listId, swimlaneId) {
  11. const currentCard = Utils.getCurrentCard();
  12. //const currentUser = ReactiveCache.getCurrentUser();
  13. if (
  14. //currentUser &&
  15. //currentUser.profile &&
  16. Utils.boardView() === 'board-view-swimlanes'
  17. )
  18. return (
  19. currentCard &&
  20. currentCard.listId === listId &&
  21. currentCard.swimlaneId === swimlaneId
  22. );
  23. else if (
  24. //currentUser &&
  25. //currentUser.profile &&
  26. Utils.boardView() === 'board-view-lists'
  27. )
  28. return (
  29. currentCard &&
  30. currentCard.listId === listId
  31. );
  32. // https://github.com/wekan/wekan/issues/1623
  33. // https://github.com/ChronikEwok/wekan/commit/cad9b20451bb6149bfb527a99b5001873b06c3de
  34. // TODO: In public board, if you would like to switch between List/Swimlane view, you could
  35. // 1) If there is no view cookie, save to cookie board-view-lists
  36. // board-view-lists / board-view-swimlanes / board-view-cal
  37. // 2) If public user changes clicks board-view-lists then change view and
  38. // then change view and save cookie with view value
  39. // without using currentuser above, because currentuser is null.
  40. }
  41. function initSortable(boardComponent, $listsDom) {
  42. // We want to animate the card details window closing. We rely on CSS
  43. // transition for the actual animation.
  44. $listsDom._uihooks = {
  45. removeElement(node) {
  46. const removeNode = _.once(() => {
  47. node.parentNode.removeChild(node);
  48. });
  49. if ($(node).hasClass('js-card-details')) {
  50. $(node).css({
  51. flexBasis: 0,
  52. padding: 0,
  53. });
  54. $listsDom.one(CSSEvents.transitionend, removeNode);
  55. } else {
  56. removeNode();
  57. }
  58. },
  59. };
  60. $listsDom.sortable({
  61. connectWith: '.js-swimlane, .js-lists',
  62. tolerance: 'pointer',
  63. helper: 'clone',
  64. items: '.js-list:not(.js-list-composer)',
  65. placeholder: 'js-list placeholder',
  66. distance: 7,
  67. start(evt, ui) {
  68. ui.placeholder.height(ui.helper.height());
  69. ui.placeholder.width(ui.helper.width());
  70. EscapeActions.executeUpTo('popup-close');
  71. boardComponent.setIsDragging(true);
  72. },
  73. stop(evt, ui) {
  74. // To attribute the new index number, we need to get the DOM element
  75. // of the previous and the following card -- if any.
  76. const prevListDom = ui.item.prev('.js-list').get(0);
  77. const nextListDom = ui.item.next('.js-list').get(0);
  78. const sortIndex = calculateIndex(prevListDom, nextListDom, 1);
  79. const listDomElement = ui.item.get(0);
  80. const list = Blaze.getData(listDomElement);
  81. // Detect if the list was dropped in a different swimlane
  82. const targetSwimlaneDom = ui.item.closest('.js-swimlane');
  83. let targetSwimlaneId = null;
  84. if (targetSwimlaneDom.length > 0) {
  85. // List was dropped in a swimlane
  86. targetSwimlaneId = targetSwimlaneDom.attr('id').replace('swimlane-', '');
  87. } else {
  88. // List was dropped in lists view (not swimlanes view)
  89. // In this case, assign to the default swimlane
  90. const currentBoard = ReactiveCache.getBoard(Session.get('currentBoard'));
  91. if (currentBoard) {
  92. const defaultSwimlane = currentBoard.getDefaultSwimline();
  93. if (defaultSwimlane) {
  94. targetSwimlaneId = defaultSwimlane._id;
  95. }
  96. }
  97. }
  98. // Get the original swimlane ID of the list (handle backward compatibility)
  99. const originalSwimlaneId = list.getEffectiveSwimlaneId ? list.getEffectiveSwimlaneId() : (list.swimlaneId || null);
  100. /*
  101. Reverted incomplete change list width,
  102. removed from below Lists.update:
  103. https://github.com/wekan/wekan/issues/4558
  104. $set: {
  105. width: list._id.width(),
  106. height: list._id.height(),
  107. */
  108. // Prepare update object
  109. const updateData = {
  110. sort: sortIndex.base,
  111. };
  112. // Check if the list was dropped in a different swimlane
  113. const isDifferentSwimlane = targetSwimlaneId && targetSwimlaneId !== originalSwimlaneId;
  114. // If the list was dropped in a different swimlane, update the swimlaneId
  115. if (isDifferentSwimlane) {
  116. updateData.swimlaneId = targetSwimlaneId;
  117. if (process.env.DEBUG === 'true') {
  118. console.log(`Moving list "${list.title}" from swimlane ${originalSwimlaneId} to swimlane ${targetSwimlaneId}`);
  119. }
  120. // Move all cards in the list to the new swimlane
  121. const cardsInList = ReactiveCache.getCards({
  122. listId: list._id,
  123. archived: false
  124. });
  125. cardsInList.forEach(card => {
  126. card.move(list.boardId, targetSwimlaneId, list._id);
  127. });
  128. if (process.env.DEBUG === 'true') {
  129. console.log(`Moved ${cardsInList.length} cards to swimlane ${targetSwimlaneId}`);
  130. }
  131. // Don't cancel the sortable when moving to a different swimlane
  132. // The DOM move should be allowed to complete
  133. } else {
  134. // If staying in the same swimlane, cancel the sortable to prevent DOM manipulation issues
  135. $listsDom.sortable('cancel');
  136. }
  137. Lists.update(list._id, {
  138. $set: updateData,
  139. });
  140. boardComponent.setIsDragging(false);
  141. },
  142. });
  143. boardComponent.autorun(() => {
  144. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  145. $listsDom.sortable({
  146. handle: '.js-list-handle',
  147. connectWith: '.js-swimlane, .js-lists',
  148. });
  149. } else {
  150. $listsDom.sortable({
  151. handle: '.js-list-header',
  152. connectWith: '.js-swimlane, .js-lists',
  153. });
  154. }
  155. const $listDom = $listsDom;
  156. if ($listDom.data('uiSortable') || $listDom.data('sortable')) {
  157. $listsDom.sortable(
  158. 'option',
  159. 'disabled',
  160. !ReactiveCache.getCurrentUser()?.isBoardAdmin(),
  161. );
  162. }
  163. });
  164. }
  165. BlazeComponent.extendComponent({
  166. onRendered() {
  167. const boardComponent = this.parentComponent();
  168. const $listsDom = this.$('.js-lists');
  169. if (!Utils.getCurrentCardId()) {
  170. boardComponent.scrollLeft();
  171. }
  172. initSortable(boardComponent, $listsDom);
  173. },
  174. onCreated() {
  175. this.draggingActive = new ReactiveVar(false);
  176. this._isDragging = false;
  177. this._lastDragPositionX = 0;
  178. },
  179. id() {
  180. return this._id;
  181. },
  182. currentCardIsInThisList(listId, swimlaneId) {
  183. return currentCardIsInThisList(listId, swimlaneId);
  184. },
  185. currentListIsInThisSwimlane(swimlaneId) {
  186. return currentListIsInThisSwimlane(swimlaneId);
  187. },
  188. visible(list) {
  189. if (list.archived) {
  190. // Show archived list only when filter archive is on
  191. if (!Filter.archive.isSelected()) {
  192. return false;
  193. }
  194. }
  195. if (Filter.lists._isActive()) {
  196. if (!list.title.match(Filter.lists.getRegexSelector())) {
  197. return false;
  198. }
  199. }
  200. if (Filter.hideEmpty.isSelected()) {
  201. // Check for cards in all swimlanes, not just the current one
  202. // This ensures lists with cards in other swimlanes are still visible
  203. const cards = list.cards();
  204. if (cards.length === 0) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. },
  210. events() {
  211. return [
  212. {
  213. // Click-and-drag action
  214. 'mousedown .board-canvas'(evt) {
  215. // Translating the board canvas using the click-and-drag action can
  216. // conflict with the build-in browser mechanism to select text. We
  217. // define a list of elements in which we disable the dragging because
  218. // the user will legitimately expect to be able to select some text with
  219. // his mouse.
  220. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  221. Utils.isTouchScreenOrShowDesktopDragHandles()
  222. ? ['.js-list-handle', '.js-swimlane-header-handle']
  223. : ['.js-list-header'],
  224. );
  225. if (
  226. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  227. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  228. ) {
  229. this._isDragging = true;
  230. this._lastDragPositionX = evt.clientX;
  231. }
  232. },
  233. mouseup() {
  234. if (this._isDragging) {
  235. this._isDragging = false;
  236. }
  237. },
  238. mousemove(evt) {
  239. if (this._isDragging) {
  240. // Update the canvas position
  241. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  242. this._lastDragPositionX = evt.clientX;
  243. // Disable browser text selection while dragging
  244. evt.stopPropagation();
  245. evt.preventDefault();
  246. // Don't close opened card or inlined form at the end of the
  247. // click-and-drag.
  248. EscapeActions.executeUpTo('popup-close');
  249. EscapeActions.preventNextClick();
  250. }
  251. },
  252. },
  253. ];
  254. },
  255. swimlaneHeight() {
  256. const user = ReactiveCache.getCurrentUser();
  257. const swimlane = Template.currentData();
  258. const height = user.getSwimlaneHeight(swimlane.boardId, swimlane._id);
  259. return height == -1 ? "auto" : (height + 5 + "px");
  260. },
  261. }).register('swimlane');
  262. BlazeComponent.extendComponent({
  263. onCreated() {
  264. this.currentBoard = Utils.getCurrentBoard();
  265. this.isListTemplatesSwimlane =
  266. this.currentBoard.isTemplatesBoard() &&
  267. this.currentData().isListTemplatesSwimlane();
  268. this.currentSwimlane = this.currentData();
  269. },
  270. // Proxy
  271. open() {
  272. this.childComponents('inlinedForm')[0].open();
  273. },
  274. events() {
  275. return [
  276. {
  277. submit(evt) {
  278. evt.preventDefault();
  279. const titleInput = this.find('.list-name-input');
  280. const title = titleInput?.value.trim();
  281. if (!title) return;
  282. let sortIndex = 0;
  283. const lastList = this.currentBoard.getLastList();
  284. const boardId = Utils.getCurrentBoardId();
  285. const positionInput = this.find('.list-position-input');
  286. if (positionInput) {
  287. const positionId = positionInput.value.trim();
  288. const selectedList = ReactiveCache.getList({ boardId, _id: positionId, archived: false });
  289. if (selectedList) {
  290. sortIndex = selectedList.sort + 1;
  291. } else {
  292. sortIndex = Utils.calculateIndexData(lastList, null).base;
  293. }
  294. } else {
  295. sortIndex = Utils.calculateIndexData(lastList, null).base;
  296. }
  297. Lists.insert({
  298. title,
  299. boardId: Session.get('currentBoard'),
  300. sort: sortIndex,
  301. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  302. swimlaneId: this.currentSwimlane._id, // Always set swimlaneId for per-swimlane list titles
  303. });
  304. titleInput.value = '';
  305. titleInput.focus();
  306. }
  307. },
  308. {
  309. 'click .js-list-template': Popup.open('searchElement'),
  310. },
  311. ];
  312. },
  313. }).register('addListForm');
  314. Template.swimlane.helpers({
  315. canSeeAddList() {
  316. return ReactiveCache.getCurrentUser().isBoardAdmin();
  317. },
  318. });
  319. BlazeComponent.extendComponent({
  320. currentCardIsInThisList(listId, swimlaneId) {
  321. return currentCardIsInThisList(listId, swimlaneId);
  322. },
  323. visible(list) {
  324. if (list.archived) {
  325. // Show archived list only when filter archive is on
  326. if (!Filter.archive.isSelected()) {
  327. return false;
  328. }
  329. }
  330. if (Filter.lists._isActive()) {
  331. if (!list.title.match(Filter.lists.getRegexSelector())) {
  332. return false;
  333. }
  334. }
  335. if (Filter.hideEmpty.isSelected()) {
  336. // Check for cards in all swimlanes, not just the current one
  337. // This ensures lists with cards in other swimlanes are still visible
  338. const cards = list.cards();
  339. if (cards.length === 0) {
  340. return false;
  341. }
  342. }
  343. return true;
  344. },
  345. onRendered() {
  346. const boardComponent = this.parentComponent();
  347. const $listsDom = this.$('.js-lists');
  348. if (!Utils.getCurrentCardId()) {
  349. boardComponent.scrollLeft();
  350. }
  351. initSortable(boardComponent, $listsDom);
  352. },
  353. }).register('listsGroup');
  354. class MoveSwimlaneComponent extends BlazeComponent {
  355. serverMethod = 'moveSwimlane';
  356. onCreated() {
  357. this.currentSwimlane = this.currentData();
  358. }
  359. board() {
  360. return Utils.getCurrentBoard();
  361. }
  362. toBoardsSelector() {
  363. return {
  364. archived: false,
  365. 'members.userId': Meteor.userId(),
  366. type: 'board',
  367. _id: { $ne: this.board()._id },
  368. };
  369. }
  370. toBoards() {
  371. const ret = ReactiveCache.getBoards(this.toBoardsSelector(), { sort: { title: 1 } });
  372. return ret;
  373. }
  374. events() {
  375. return [
  376. {
  377. 'click .js-done'() {
  378. const bSelect = $('.js-select-boards')[0];
  379. let boardId;
  380. if (bSelect) {
  381. boardId = bSelect.options[bSelect.selectedIndex].value;
  382. Meteor.call(this.serverMethod, this.currentSwimlane._id, boardId);
  383. }
  384. Popup.back();
  385. },
  386. },
  387. ];
  388. }
  389. }
  390. MoveSwimlaneComponent.register('moveSwimlanePopup');
  391. (class extends MoveSwimlaneComponent {
  392. serverMethod = 'copySwimlane';
  393. toBoardsSelector() {
  394. const selector = super.toBoardsSelector();
  395. delete selector._id;
  396. return selector;
  397. }
  398. }.register('copySwimlanePopup'));