swimlanes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 lastList = this.currentBoard.getLastList();
  234. const titleInput = this.find('.list-name-input');
  235. const title = titleInput.value.trim();
  236. let sortIndex = 0
  237. if (lastList) {
  238. const positionInput = this.find('.list-position-input');
  239. const position = positionInput.value.trim();
  240. const ret = ReactiveCache.getList({ boardId: Utils.getCurrentBoardId(), _id: position, archived: false })
  241. sortIndex = parseInt(JSON.stringify(ret['sort']))
  242. sortIndex = sortIndex+1
  243. } else {
  244. sortIndex = Utils.calculateIndexData(lastList, null).base;
  245. }
  246. if (title) {
  247. Lists.insert({
  248. title,
  249. boardId: Session.get('currentBoard'),
  250. sort: sortIndex,
  251. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  252. swimlaneId: this.currentBoard.isTemplatesBoard()
  253. ? this.currentSwimlane._id
  254. : '',
  255. });
  256. titleInput.value = '';
  257. titleInput.focus();
  258. }
  259. },
  260. 'click .js-list-template': Popup.open('searchElement'),
  261. },
  262. ];
  263. },
  264. }).register('addListForm');
  265. Template.swimlane.helpers({
  266. canSeeAddList() {
  267. return ReactiveCache.getCurrentUser().isBoardAdmin();
  268. },
  269. });
  270. BlazeComponent.extendComponent({
  271. currentCardIsInThisList(listId, swimlaneId) {
  272. return currentCardIsInThisList(listId, swimlaneId);
  273. },
  274. visible(list) {
  275. if (list.archived) {
  276. // Show archived list only when filter archive is on
  277. if (!Filter.archive.isSelected()) {
  278. return false;
  279. }
  280. }
  281. if (Filter.lists._isActive()) {
  282. if (!list.title.match(Filter.lists.getRegexSelector())) {
  283. return false;
  284. }
  285. }
  286. if (Filter.hideEmpty.isSelected()) {
  287. const swimlaneId = this.parentComponent()
  288. .parentComponent()
  289. .data()._id;
  290. const cards = list.cards(swimlaneId);
  291. if (cards.length === 0) {
  292. return false;
  293. }
  294. }
  295. return true;
  296. },
  297. onRendered() {
  298. const boardComponent = this.parentComponent();
  299. const $listsDom = this.$('.js-lists');
  300. if (!Utils.getCurrentCardId()) {
  301. boardComponent.scrollLeft();
  302. }
  303. initSortable(boardComponent, $listsDom);
  304. },
  305. }).register('listsGroup');
  306. class MoveSwimlaneComponent extends BlazeComponent {
  307. serverMethod = 'moveSwimlane';
  308. onCreated() {
  309. this.currentSwimlane = this.currentData();
  310. }
  311. board() {
  312. return Utils.getCurrentBoard();
  313. }
  314. toBoardsSelector() {
  315. return {
  316. archived: false,
  317. 'members.userId': Meteor.userId(),
  318. type: 'board',
  319. _id: { $ne: this.board()._id },
  320. };
  321. }
  322. toBoards() {
  323. const ret = ReactiveCache.getBoards(this.toBoardsSelector(), { sort: { title: 1 } });
  324. return ret;
  325. }
  326. events() {
  327. return [
  328. {
  329. 'click .js-done'() {
  330. const bSelect = $('.js-select-boards')[0];
  331. let boardId;
  332. if (bSelect) {
  333. boardId = bSelect.options[bSelect.selectedIndex].value;
  334. Meteor.call(this.serverMethod, this.currentSwimlane._id, boardId);
  335. }
  336. Popup.back();
  337. },
  338. },
  339. ];
  340. }
  341. }
  342. MoveSwimlaneComponent.register('moveSwimlanePopup');
  343. (class extends MoveSwimlaneComponent {
  344. serverMethod = 'copySwimlane';
  345. toBoardsSelector() {
  346. const selector = super.toBoardsSelector();
  347. delete selector._id;
  348. return selector;
  349. }
  350. }.register('copySwimlanePopup'));