swimlanes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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: '.board-canvas',
  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. $listsDom.sortable('cancel');
  80. const listDomElement = ui.item.get(0);
  81. const list = Blaze.getData(listDomElement);
  82. /*
  83. Reverted incomplete change list width,
  84. removed from below Lists.update:
  85. https://github.com/wekan/wekan/issues/4558
  86. $set: {
  87. width: list._id.width(),
  88. height: list._id.height(),
  89. */
  90. Lists.update(list._id, {
  91. $set: {
  92. sort: sortIndex.base,
  93. },
  94. });
  95. boardComponent.setIsDragging(false);
  96. },
  97. });
  98. boardComponent.autorun(() => {
  99. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  100. $listsDom.sortable({
  101. handle: '.js-list-handle',
  102. });
  103. } else {
  104. $listsDom.sortable({
  105. handle: '.js-list-header',
  106. });
  107. }
  108. const $listDom = $listsDom;
  109. if ($listDom.data('uiSortable') || $listDom.data('sortable')) {
  110. $listsDom.sortable(
  111. 'option',
  112. 'disabled',
  113. !ReactiveCache.getCurrentUser()?.isBoardAdmin(),
  114. );
  115. }
  116. });
  117. }
  118. BlazeComponent.extendComponent({
  119. onRendered() {
  120. const boardComponent = this.parentComponent();
  121. const $listsDom = this.$('.js-lists');
  122. if (!Utils.getCurrentCardId()) {
  123. boardComponent.scrollLeft();
  124. }
  125. initSortable(boardComponent, $listsDom);
  126. },
  127. onCreated() {
  128. this.draggingActive = new ReactiveVar(false);
  129. this._isDragging = false;
  130. this._lastDragPositionX = 0;
  131. },
  132. id() {
  133. return this._id;
  134. },
  135. currentCardIsInThisList(listId, swimlaneId) {
  136. return currentCardIsInThisList(listId, swimlaneId);
  137. },
  138. currentListIsInThisSwimlane(swimlaneId) {
  139. return currentListIsInThisSwimlane(swimlaneId);
  140. },
  141. visible(list) {
  142. if (list.archived) {
  143. // Show archived list only when filter archive is on
  144. if (!Filter.archive.isSelected()) {
  145. return false;
  146. }
  147. }
  148. if (Filter.lists._isActive()) {
  149. if (!list.title.match(Filter.lists.getRegexSelector())) {
  150. return false;
  151. }
  152. }
  153. if (Filter.hideEmpty.isSelected()) {
  154. const swimlaneId = this.parentComponent()
  155. .parentComponent()
  156. .data()._id;
  157. const cards = list.cards(swimlaneId);
  158. if (cards.length === 0) {
  159. return false;
  160. }
  161. }
  162. return true;
  163. },
  164. events() {
  165. return [
  166. {
  167. // Click-and-drag action
  168. 'mousedown .board-canvas'(evt) {
  169. // Translating the board canvas using the click-and-drag action can
  170. // conflict with the build-in browser mechanism to select text. We
  171. // define a list of elements in which we disable the dragging because
  172. // the user will legitimately expect to be able to select some text with
  173. // his mouse.
  174. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  175. Utils.isTouchScreenOrShowDesktopDragHandles()
  176. ? ['.js-list-handle', '.js-swimlane-header-handle']
  177. : ['.js-list-header'],
  178. );
  179. if (
  180. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  181. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  182. ) {
  183. this._isDragging = true;
  184. this._lastDragPositionX = evt.clientX;
  185. }
  186. },
  187. mouseup() {
  188. if (this._isDragging) {
  189. this._isDragging = false;
  190. }
  191. },
  192. mousemove(evt) {
  193. if (this._isDragging) {
  194. // Update the canvas position
  195. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  196. this._lastDragPositionX = evt.clientX;
  197. // Disable browser text selection while dragging
  198. evt.stopPropagation();
  199. evt.preventDefault();
  200. // Don't close opened card or inlined form at the end of the
  201. // click-and-drag.
  202. EscapeActions.executeUpTo('popup-close');
  203. EscapeActions.preventNextClick();
  204. }
  205. },
  206. },
  207. ];
  208. },
  209. swimlaneHeight() {
  210. const user = ReactiveCache.getCurrentUser();
  211. const swimlane = Template.currentData();
  212. const height = user.getSwimlaneHeight(swimlane.boardId, swimlane._id);
  213. return height == -1 ? "auto" : (height + "px");
  214. },
  215. }).register('swimlane');
  216. BlazeComponent.extendComponent({
  217. onCreated() {
  218. this.currentBoard = Utils.getCurrentBoard();
  219. this.isListTemplatesSwimlane =
  220. this.currentBoard.isTemplatesBoard() &&
  221. this.currentData().isListTemplatesSwimlane();
  222. this.currentSwimlane = this.currentData();
  223. },
  224. // Proxy
  225. open() {
  226. this.childComponents('inlinedForm')[0].open();
  227. },
  228. events() {
  229. return [
  230. {
  231. submit(evt) {
  232. evt.preventDefault();
  233. const titleInput = this.find('.list-name-input');
  234. const title = titleInput?.value.trim();
  235. if (!title) return;
  236. let sortIndex = 0;
  237. const lastList = this.currentBoard.getLastList();
  238. const boardId = Utils.getCurrentBoardId();
  239. const positionInput = this.find('.list-position-input');
  240. if (positionInput) {
  241. const positionId = positionInput.value.trim();
  242. const selectedList = ReactiveCache.getList({ boardId, _id: positionId, archived: false });
  243. if (selectedList) {
  244. sortIndex = selectedList.sort + 1;
  245. } else {
  246. sortIndex = Utils.calculateIndexData(lastList, null).base;
  247. }
  248. } else {
  249. sortIndex = Utils.calculateIndexData(lastList, null).base;
  250. }
  251. Lists.insert({
  252. title,
  253. boardId: Session.get('currentBoard'),
  254. sort: sortIndex,
  255. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  256. swimlaneId: this.currentBoard.isTemplatesBoard() ? this.currentSwimlane._id : '',
  257. });
  258. titleInput.value = '';
  259. titleInput.focus();
  260. }
  261. },
  262. {
  263. 'click .js-list-template': Popup.open('searchElement'),
  264. },
  265. ];
  266. },
  267. }).register('addListForm');
  268. Template.swimlane.helpers({
  269. canSeeAddList() {
  270. return ReactiveCache.getCurrentUser().isBoardAdmin();
  271. },
  272. });
  273. BlazeComponent.extendComponent({
  274. currentCardIsInThisList(listId, swimlaneId) {
  275. return currentCardIsInThisList(listId, swimlaneId);
  276. },
  277. visible(list) {
  278. if (list.archived) {
  279. // Show archived list only when filter archive is on
  280. if (!Filter.archive.isSelected()) {
  281. return false;
  282. }
  283. }
  284. if (Filter.lists._isActive()) {
  285. if (!list.title.match(Filter.lists.getRegexSelector())) {
  286. return false;
  287. }
  288. }
  289. if (Filter.hideEmpty.isSelected()) {
  290. const swimlaneId = this.parentComponent()
  291. .parentComponent()
  292. .data()._id;
  293. const cards = list.cards(swimlaneId);
  294. if (cards.length === 0) {
  295. return false;
  296. }
  297. }
  298. return true;
  299. },
  300. onRendered() {
  301. const boardComponent = this.parentComponent();
  302. const $listsDom = this.$('.js-lists');
  303. if (!Utils.getCurrentCardId()) {
  304. boardComponent.scrollLeft();
  305. }
  306. initSortable(boardComponent, $listsDom);
  307. },
  308. }).register('listsGroup');
  309. class MoveSwimlaneComponent extends BlazeComponent {
  310. serverMethod = 'moveSwimlane';
  311. onCreated() {
  312. this.currentSwimlane = this.currentData();
  313. }
  314. board() {
  315. return Utils.getCurrentBoard();
  316. }
  317. toBoardsSelector() {
  318. return {
  319. archived: false,
  320. 'members.userId': Meteor.userId(),
  321. type: 'board',
  322. _id: { $ne: this.board()._id },
  323. };
  324. }
  325. toBoards() {
  326. const ret = ReactiveCache.getBoards(this.toBoardsSelector(), { sort: { title: 1 } });
  327. return ret;
  328. }
  329. events() {
  330. return [
  331. {
  332. 'click .js-done'() {
  333. const bSelect = $('.js-select-boards')[0];
  334. let boardId;
  335. if (bSelect) {
  336. boardId = bSelect.options[bSelect.selectedIndex].value;
  337. Meteor.call(this.serverMethod, this.currentSwimlane._id, boardId);
  338. }
  339. Popup.back();
  340. },
  341. },
  342. ];
  343. }
  344. }
  345. MoveSwimlaneComponent.register('moveSwimlanePopup');
  346. (class extends MoveSwimlaneComponent {
  347. serverMethod = 'copySwimlane';
  348. toBoardsSelector() {
  349. const selector = super.toBoardsSelector();
  350. delete selector._id;
  351. return selector;
  352. }
  353. }.register('copySwimlanePopup'));