swimlanes.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. const swimlaneId = this.parentComponent()
  202. .parentComponent()
  203. .data()._id;
  204. const cards = list.cards(swimlaneId);
  205. if (cards.length === 0) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. },
  211. events() {
  212. return [
  213. {
  214. // Click-and-drag action
  215. 'mousedown .board-canvas'(evt) {
  216. // Translating the board canvas using the click-and-drag action can
  217. // conflict with the build-in browser mechanism to select text. We
  218. // define a list of elements in which we disable the dragging because
  219. // the user will legitimately expect to be able to select some text with
  220. // his mouse.
  221. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  222. Utils.isTouchScreenOrShowDesktopDragHandles()
  223. ? ['.js-list-handle', '.js-swimlane-header-handle']
  224. : ['.js-list-header'],
  225. );
  226. if (
  227. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  228. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  229. ) {
  230. this._isDragging = true;
  231. this._lastDragPositionX = evt.clientX;
  232. }
  233. },
  234. mouseup() {
  235. if (this._isDragging) {
  236. this._isDragging = false;
  237. }
  238. },
  239. mousemove(evt) {
  240. if (this._isDragging) {
  241. // Update the canvas position
  242. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  243. this._lastDragPositionX = evt.clientX;
  244. // Disable browser text selection while dragging
  245. evt.stopPropagation();
  246. evt.preventDefault();
  247. // Don't close opened card or inlined form at the end of the
  248. // click-and-drag.
  249. EscapeActions.executeUpTo('popup-close');
  250. EscapeActions.preventNextClick();
  251. }
  252. },
  253. },
  254. ];
  255. },
  256. swimlaneHeight() {
  257. const user = ReactiveCache.getCurrentUser();
  258. const swimlane = Template.currentData();
  259. const height = user.getSwimlaneHeight(swimlane.boardId, swimlane._id);
  260. return height == -1 ? "auto" : (height + 5 + "px");
  261. },
  262. }).register('swimlane');
  263. BlazeComponent.extendComponent({
  264. onCreated() {
  265. this.currentBoard = Utils.getCurrentBoard();
  266. this.isListTemplatesSwimlane =
  267. this.currentBoard.isTemplatesBoard() &&
  268. this.currentData().isListTemplatesSwimlane();
  269. this.currentSwimlane = this.currentData();
  270. },
  271. // Proxy
  272. open() {
  273. this.childComponents('inlinedForm')[0].open();
  274. },
  275. events() {
  276. return [
  277. {
  278. submit(evt) {
  279. evt.preventDefault();
  280. const titleInput = this.find('.list-name-input');
  281. const title = titleInput?.value.trim();
  282. if (!title) return;
  283. let sortIndex = 0;
  284. const lastList = this.currentBoard.getLastList();
  285. const boardId = Utils.getCurrentBoardId();
  286. const positionInput = this.find('.list-position-input');
  287. if (positionInput) {
  288. const positionId = positionInput.value.trim();
  289. const selectedList = ReactiveCache.getList({ boardId, _id: positionId, archived: false });
  290. if (selectedList) {
  291. sortIndex = selectedList.sort + 1;
  292. } else {
  293. sortIndex = Utils.calculateIndexData(lastList, null).base;
  294. }
  295. } else {
  296. sortIndex = Utils.calculateIndexData(lastList, null).base;
  297. }
  298. Lists.insert({
  299. title,
  300. boardId: Session.get('currentBoard'),
  301. sort: sortIndex,
  302. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  303. swimlaneId: this.currentSwimlane._id, // Always set swimlaneId for per-swimlane list titles
  304. });
  305. titleInput.value = '';
  306. titleInput.focus();
  307. }
  308. },
  309. {
  310. 'click .js-list-template': Popup.open('searchElement'),
  311. },
  312. ];
  313. },
  314. }).register('addListForm');
  315. Template.swimlane.helpers({
  316. canSeeAddList() {
  317. return ReactiveCache.getCurrentUser().isBoardAdmin();
  318. },
  319. });
  320. BlazeComponent.extendComponent({
  321. currentCardIsInThisList(listId, swimlaneId) {
  322. return currentCardIsInThisList(listId, swimlaneId);
  323. },
  324. visible(list) {
  325. if (list.archived) {
  326. // Show archived list only when filter archive is on
  327. if (!Filter.archive.isSelected()) {
  328. return false;
  329. }
  330. }
  331. if (Filter.lists._isActive()) {
  332. if (!list.title.match(Filter.lists.getRegexSelector())) {
  333. return false;
  334. }
  335. }
  336. if (Filter.hideEmpty.isSelected()) {
  337. const swimlaneId = this.parentComponent()
  338. .parentComponent()
  339. .data()._id;
  340. const cards = list.cards(swimlaneId);
  341. if (cards.length === 0) {
  342. return false;
  343. }
  344. }
  345. return true;
  346. },
  347. onRendered() {
  348. const boardComponent = this.parentComponent();
  349. const $listsDom = this.$('.js-lists');
  350. if (!Utils.getCurrentCardId()) {
  351. boardComponent.scrollLeft();
  352. }
  353. initSortable(boardComponent, $listsDom);
  354. },
  355. }).register('listsGroup');
  356. class MoveSwimlaneComponent extends BlazeComponent {
  357. serverMethod = 'moveSwimlane';
  358. onCreated() {
  359. this.currentSwimlane = this.currentData();
  360. }
  361. board() {
  362. return Utils.getCurrentBoard();
  363. }
  364. toBoardsSelector() {
  365. return {
  366. archived: false,
  367. 'members.userId': Meteor.userId(),
  368. type: 'board',
  369. _id: { $ne: this.board()._id },
  370. };
  371. }
  372. toBoards() {
  373. const ret = ReactiveCache.getBoards(this.toBoardsSelector(), { sort: { title: 1 } });
  374. return ret;
  375. }
  376. events() {
  377. return [
  378. {
  379. 'click .js-done'() {
  380. const bSelect = $('.js-select-boards')[0];
  381. let boardId;
  382. if (bSelect) {
  383. boardId = bSelect.options[bSelect.selectedIndex].value;
  384. Meteor.call(this.serverMethod, this.currentSwimlane._id, boardId);
  385. }
  386. Popup.back();
  387. },
  388. },
  389. ];
  390. }
  391. }
  392. MoveSwimlaneComponent.register('moveSwimlanePopup');
  393. (class extends MoveSwimlaneComponent {
  394. serverMethod = 'copySwimlane';
  395. toBoardsSelector() {
  396. const selector = super.toBoardsSelector();
  397. delete selector._id;
  398. return selector;
  399. }
  400. }.register('copySwimlanePopup'));