swimlanes.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. ).concat([
  225. '.js-list-resize-handle',
  226. '.js-swimlane-resize-handle'
  227. ]);
  228. const isResizeHandle = $(evt.target).closest('.js-list-resize-handle, .js-swimlane-resize-handle').length > 0;
  229. const isInNoDragArea = $(evt.target).closest(noDragInside.join(',')).length > 0;
  230. if (isResizeHandle) {
  231. console.log('Board drag prevented - resize handle clicked');
  232. return;
  233. }
  234. if (
  235. !isInNoDragArea &&
  236. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  237. ) {
  238. this._isDragging = true;
  239. this._lastDragPositionX = evt.clientX;
  240. }
  241. },
  242. mouseup() {
  243. if (this._isDragging) {
  244. this._isDragging = false;
  245. }
  246. },
  247. mousemove(evt) {
  248. if (this._isDragging) {
  249. // Update the canvas position
  250. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  251. this._lastDragPositionX = evt.clientX;
  252. // Disable browser text selection while dragging
  253. evt.stopPropagation();
  254. evt.preventDefault();
  255. // Don't close opened card or inlined form at the end of the
  256. // click-and-drag.
  257. EscapeActions.executeUpTo('popup-close');
  258. EscapeActions.preventNextClick();
  259. }
  260. },
  261. },
  262. ];
  263. },
  264. swimlaneHeight() {
  265. const user = ReactiveCache.getCurrentUser();
  266. const swimlane = Template.currentData();
  267. const height = user.getSwimlaneHeightFromStorage(swimlane.boardId, swimlane._id);
  268. return height == -1 ? "auto" : (height + 5 + "px");
  269. },
  270. onRendered() {
  271. // Initialize swimlane resize functionality immediately
  272. this.initializeSwimlaneResize();
  273. },
  274. initializeSwimlaneResize() {
  275. // Check if we're still in a valid template context
  276. if (!Template.currentData()) {
  277. console.warn('No current template data available for swimlane resize initialization');
  278. return;
  279. }
  280. const swimlane = Template.currentData();
  281. const $swimlane = $(`#swimlane-${swimlane._id}`);
  282. const $resizeHandle = $swimlane.find('.js-swimlane-resize-handle');
  283. // Check if elements exist
  284. if (!$swimlane.length || !$resizeHandle.length) {
  285. console.warn('Swimlane or resize handle not found, retrying in 100ms');
  286. Meteor.setTimeout(() => {
  287. if (!this.isDestroyed) {
  288. this.initializeSwimlaneResize();
  289. }
  290. }, 100);
  291. return;
  292. }
  293. if ($resizeHandle.length === 0) {
  294. return;
  295. }
  296. let isResizing = false;
  297. let startY = 0;
  298. let startHeight = 0;
  299. const minHeight = 100;
  300. const maxHeight = 2000;
  301. const startResize = (e) => {
  302. isResizing = true;
  303. startY = e.pageY || e.originalEvent.touches[0].pageY;
  304. startHeight = parseInt($swimlane.css('height')) || 300;
  305. $swimlane.addClass('swimlane-resizing');
  306. $('body').addClass('swimlane-resizing-active');
  307. $('body').css('user-select', 'none');
  308. e.preventDefault();
  309. e.stopPropagation();
  310. };
  311. const doResize = (e) => {
  312. if (!isResizing) {
  313. return;
  314. }
  315. const currentY = e.pageY || e.originalEvent.touches[0].pageY;
  316. const deltaY = currentY - startY;
  317. const newHeight = Math.max(minHeight, Math.min(maxHeight, startHeight + deltaY));
  318. // Apply the new height immediately for real-time feedback
  319. $swimlane[0].style.setProperty('--swimlane-height', `${newHeight}px`);
  320. $swimlane[0].style.setProperty('height', `${newHeight}px`);
  321. $swimlane[0].style.setProperty('min-height', `${newHeight}px`);
  322. $swimlane[0].style.setProperty('max-height', `${newHeight}px`);
  323. $swimlane[0].style.setProperty('flex', 'none');
  324. $swimlane[0].style.setProperty('flex-basis', 'auto');
  325. $swimlane[0].style.setProperty('flex-grow', '0');
  326. $swimlane[0].style.setProperty('flex-shrink', '0');
  327. e.preventDefault();
  328. e.stopPropagation();
  329. };
  330. const stopResize = (e) => {
  331. if (!isResizing) return;
  332. isResizing = false;
  333. // Calculate final height
  334. const currentY = e.pageY || e.originalEvent.touches[0].pageY;
  335. const deltaY = currentY - startY;
  336. const finalHeight = Math.max(minHeight, Math.min(maxHeight, startHeight + deltaY));
  337. // Ensure the final height is applied
  338. $swimlane[0].style.setProperty('--swimlane-height', `${finalHeight}px`);
  339. $swimlane[0].style.setProperty('height', `${finalHeight}px`);
  340. $swimlane[0].style.setProperty('min-height', `${finalHeight}px`);
  341. $swimlane[0].style.setProperty('max-height', `${finalHeight}px`);
  342. $swimlane[0].style.setProperty('flex', 'none');
  343. $swimlane[0].style.setProperty('flex-basis', 'auto');
  344. $swimlane[0].style.setProperty('flex-grow', '0');
  345. $swimlane[0].style.setProperty('flex-shrink', '0');
  346. // Remove visual feedback but keep the height
  347. $swimlane.removeClass('swimlane-resizing');
  348. $('body').removeClass('swimlane-resizing-active');
  349. $('body').css('user-select', '');
  350. // Save the new height using the existing system
  351. const boardId = swimlane.boardId;
  352. const swimlaneId = swimlane._id;
  353. if (process.env.DEBUG === 'true') {
  354. }
  355. // Use the new storage method that handles both logged-in and non-logged-in users
  356. Meteor.call('applySwimlaneHeightToStorage', boardId, swimlaneId, finalHeight, (error, result) => {
  357. if (error) {
  358. console.error('Error saving swimlane height:', error);
  359. } else {
  360. if (process.env.DEBUG === 'true') {
  361. }
  362. }
  363. });
  364. e.preventDefault();
  365. };
  366. // Mouse events
  367. $resizeHandle.on('mousedown', startResize);
  368. $(document).on('mousemove', doResize);
  369. $(document).on('mouseup', stopResize);
  370. // Touch events for mobile
  371. $resizeHandle.on('touchstart', startResize, { passive: false });
  372. $(document).on('touchmove', doResize, { passive: false });
  373. $(document).on('touchend', stopResize, { passive: false });
  374. // Prevent dragscroll interference
  375. $resizeHandle.on('mousedown', (e) => {
  376. e.stopPropagation();
  377. });
  378. },
  379. }).register('swimlane');
  380. BlazeComponent.extendComponent({
  381. onCreated() {
  382. this.currentBoard = Utils.getCurrentBoard();
  383. this.isListTemplatesSwimlane =
  384. this.currentBoard.isTemplatesBoard() &&
  385. this.currentData().isListTemplatesSwimlane();
  386. this.currentSwimlane = this.currentData();
  387. },
  388. // Proxy
  389. open() {
  390. this.childComponents('inlinedForm')[0].open();
  391. },
  392. events() {
  393. return [
  394. {
  395. submit(evt) {
  396. evt.preventDefault();
  397. const titleInput = this.find('.list-name-input');
  398. const title = titleInput?.value.trim();
  399. if (!title) return;
  400. let sortIndex = 0;
  401. const lastList = this.currentBoard.getLastList();
  402. const boardId = Utils.getCurrentBoardId();
  403. const positionInput = this.find('.list-position-input');
  404. if (positionInput) {
  405. const positionId = positionInput.value.trim();
  406. const selectedList = ReactiveCache.getList({ boardId, _id: positionId, archived: false });
  407. if (selectedList) {
  408. sortIndex = selectedList.sort + 1;
  409. } else {
  410. sortIndex = Utils.calculateIndexData(lastList, null).base;
  411. }
  412. } else {
  413. sortIndex = Utils.calculateIndexData(lastList, null).base;
  414. }
  415. Lists.insert({
  416. title,
  417. boardId: Session.get('currentBoard'),
  418. sort: sortIndex,
  419. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  420. swimlaneId: this.currentSwimlane._id, // Always set swimlaneId for per-swimlane list titles
  421. });
  422. titleInput.value = '';
  423. titleInput.focus();
  424. }
  425. },
  426. {
  427. 'click .js-list-template': Popup.open('searchElement'),
  428. },
  429. ];
  430. },
  431. }).register('addListForm');
  432. Template.swimlane.helpers({
  433. canSeeAddList() {
  434. return ReactiveCache.getCurrentUser().isBoardAdmin();
  435. },
  436. });
  437. BlazeComponent.extendComponent({
  438. currentCardIsInThisList(listId, swimlaneId) {
  439. return currentCardIsInThisList(listId, swimlaneId);
  440. },
  441. visible(list) {
  442. if (list.archived) {
  443. // Show archived list only when filter archive is on
  444. if (!Filter.archive.isSelected()) {
  445. return false;
  446. }
  447. }
  448. if (Filter.lists._isActive()) {
  449. if (!list.title.match(Filter.lists.getRegexSelector())) {
  450. return false;
  451. }
  452. }
  453. if (Filter.hideEmpty.isSelected()) {
  454. // Check for cards in all swimlanes, not just the current one
  455. // This ensures lists with cards in other swimlanes are still visible
  456. const cards = list.cards();
  457. if (cards.length === 0) {
  458. return false;
  459. }
  460. }
  461. return true;
  462. },
  463. onRendered() {
  464. const boardComponent = this.parentComponent();
  465. const $listsDom = this.$('.js-lists');
  466. if (!Utils.getCurrentCardId()) {
  467. boardComponent.scrollLeft();
  468. }
  469. initSortable(boardComponent, $listsDom);
  470. },
  471. }).register('listsGroup');
  472. class MoveSwimlaneComponent extends BlazeComponent {
  473. serverMethod = 'moveSwimlane';
  474. onCreated() {
  475. this.currentSwimlane = this.currentData();
  476. }
  477. board() {
  478. return Utils.getCurrentBoard();
  479. }
  480. toBoardsSelector() {
  481. return {
  482. archived: false,
  483. 'members.userId': Meteor.userId(),
  484. type: 'board',
  485. _id: { $ne: this.board()._id },
  486. };
  487. }
  488. toBoards() {
  489. const ret = ReactiveCache.getBoards(this.toBoardsSelector(), { sort: { title: 1 } });
  490. return ret;
  491. }
  492. events() {
  493. return [
  494. {
  495. 'click .js-done'() {
  496. const bSelect = $('.js-select-boards')[0];
  497. let boardId;
  498. if (bSelect) {
  499. boardId = bSelect.options[bSelect.selectedIndex].value;
  500. Meteor.call(this.serverMethod, this.currentSwimlane._id, boardId);
  501. }
  502. Popup.back();
  503. },
  504. },
  505. ];
  506. }
  507. }
  508. MoveSwimlaneComponent.register('moveSwimlanePopup');
  509. (class extends MoveSwimlaneComponent {
  510. serverMethod = 'copySwimlane';
  511. toBoardsSelector() {
  512. const selector = super.toBoardsSelector();
  513. delete selector._id;
  514. return selector;
  515. }
  516. }.register('copySwimlanePopup'));