swimlanes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. const { calculateIndex, enableClickOnTouch } = Utils;
  2. function currentListIsInThisSwimlane(swimlaneId) {
  3. const currentList = Lists.findOne(Session.get('currentList'));
  4. return (
  5. currentList &&
  6. (currentList.swimlaneId === swimlaneId || currentList.swimlaneId === '')
  7. );
  8. }
  9. function currentCardIsInThisList(listId, swimlaneId) {
  10. const currentCard = Cards.findOne(Session.get('currentCard'));
  11. const currentUser = Meteor.user();
  12. if (
  13. currentUser &&
  14. currentUser.profile &&
  15. Utils.boardView() === 'board-view-swimlanes'
  16. )
  17. return (
  18. currentCard &&
  19. currentCard.listId === listId &&
  20. currentCard.swimlaneId === swimlaneId
  21. );
  22. // Default view: board-view-lists
  23. else return currentCard && currentCard.listId === listId;
  24. // https://github.com/wekan/wekan/issues/1623
  25. // https://github.com/ChronikEwok/wekan/commit/cad9b20451bb6149bfb527a99b5001873b06c3de
  26. // TODO: In public board, if you would like to switch between List/Swimlane view, you could
  27. // 1) If there is no view cookie, save to cookie board-view-lists
  28. // board-view-lists / board-view-swimlanes / board-view-cal
  29. // 2) If public user changes clicks board-view-lists then change view and
  30. // then change view and save cookie with view value
  31. // without using currentuser above, because currentuser is null.
  32. }
  33. function initSortable(boardComponent, $listsDom) {
  34. // We want to animate the card details window closing. We rely on CSS
  35. // transition for the actual animation.
  36. $listsDom._uihooks = {
  37. removeElement(node) {
  38. const removeNode = _.once(() => {
  39. node.parentNode.removeChild(node);
  40. });
  41. if ($(node).hasClass('js-card-details')) {
  42. $(node).css({
  43. flexBasis: 0,
  44. padding: 0,
  45. });
  46. $listsDom.one(CSSEvents.transitionend, removeNode);
  47. } else {
  48. removeNode();
  49. }
  50. },
  51. };
  52. $listsDom.sortable({
  53. tolerance: 'pointer',
  54. helper: 'clone',
  55. items: '.js-list:not(.js-list-composer)',
  56. placeholder: 'list placeholder',
  57. distance: 7,
  58. start(evt, ui) {
  59. ui.placeholder.height(ui.helper.height());
  60. EscapeActions.executeUpTo('popup-close');
  61. boardComponent.setIsDragging(true);
  62. },
  63. stop(evt, ui) {
  64. // To attribute the new index number, we need to get the DOM element
  65. // of the previous and the following card -- if any.
  66. const prevListDom = ui.item.prev('.js-list').get(0);
  67. const nextListDom = ui.item.next('.js-list').get(0);
  68. const sortIndex = calculateIndex(prevListDom, nextListDom, 1);
  69. $listsDom.sortable('cancel');
  70. const listDomElement = ui.item.get(0);
  71. const list = Blaze.getData(listDomElement);
  72. Lists.update(list._id, {
  73. $set: {
  74. sort: sortIndex.base,
  75. },
  76. });
  77. boardComponent.setIsDragging(false);
  78. },
  79. });
  80. // ugly touch event hotfix
  81. enableClickOnTouch('.js-list:not(.js-list-composer)');
  82. function userIsMember() {
  83. return (
  84. Meteor.user() &&
  85. Meteor.user().isBoardMember() &&
  86. !Meteor.user().isCommentOnly()
  87. );
  88. }
  89. boardComponent.autorun(() => {
  90. import { Cookies } from 'meteor/ostrio:cookies';
  91. const cookies = new Cookies();
  92. let showDesktopDragHandles = false;
  93. currentUser = Meteor.user();
  94. if (currentUser) {
  95. showDesktopDragHandles = (currentUser.profile || {})
  96. .showDesktopDragHandles;
  97. } else {
  98. import { Cookies } from 'meteor/ostrio:cookies';
  99. const cookies = new Cookies();
  100. if (cookies.has('showDesktopDragHandles')) {
  101. showDesktopDragHandles = true;
  102. } else {
  103. showDesktopDragHandles = false;
  104. }
  105. }
  106. if (!Utils.isMiniScreen() && showDesktopDragHandles) {
  107. $listsDom.sortable({
  108. handle: '.js-list-handle',
  109. });
  110. } else {
  111. $listsDom.sortable({
  112. handle: '.js-list-header',
  113. });
  114. }
  115. const $listDom = $listsDom;
  116. if ($listDom.data('sortable')) {
  117. $listsDom.sortable(
  118. 'option',
  119. 'disabled',
  120. // Disable drag-dropping when user is not member
  121. !userIsMember(),
  122. // Not disable drag-dropping while in multi-selection mode
  123. // MultiSelection.isActive() || !userIsMember(),
  124. );
  125. }
  126. });
  127. }
  128. BlazeComponent.extendComponent({
  129. onRendered() {
  130. const boardComponent = this.parentComponent();
  131. const $listsDom = this.$('.js-lists');
  132. if (!Session.get('currentCard')) {
  133. boardComponent.scrollLeft();
  134. }
  135. initSortable(boardComponent, $listsDom);
  136. import { Cookies } from 'meteor/ostrio:cookies';
  137. const cookies = new Cookies();
  138. if (cookies.has('collapseSwimlane')) {
  139. // Minimize swimlanes start https://www.w3schools.com/howto/howto_js_accordion.asp
  140. const acc = document.getElementsByClassName('accordion');
  141. let i;
  142. for (i = 0; i < acc.length; i++) {
  143. acc[i].addEventListener('click', function() {
  144. this.classList.toggle('active');
  145. const panel = this.nextElementSibling;
  146. if (panel.style.maxHeight) {
  147. panel.style.maxHeight = null;
  148. } else {
  149. panel.style.maxHeight = `${panel.scrollHeight}px`;
  150. }
  151. });
  152. }
  153. // Minimize swimlanes end https://www.w3schools.com/howto/howto_js_accordion.asp
  154. }
  155. },
  156. onCreated() {
  157. this.draggingActive = new ReactiveVar(false);
  158. this._isDragging = false;
  159. this._lastDragPositionX = 0;
  160. },
  161. id() {
  162. return this._id;
  163. },
  164. currentCardIsInThisList(listId, swimlaneId) {
  165. return currentCardIsInThisList(listId, swimlaneId);
  166. },
  167. currentListIsInThisSwimlane(swimlaneId) {
  168. return currentListIsInThisSwimlane(swimlaneId);
  169. },
  170. events() {
  171. return [
  172. {
  173. // Click-and-drag action
  174. 'mousedown .board-canvas'(evt) {
  175. // Translating the board canvas using the click-and-drag action can
  176. // conflict with the build-in browser mechanism to select text. We
  177. // define a list of elements in which we disable the dragging because
  178. // the user will legitimately expect to be able to select some text with
  179. // his mouse.
  180. import { Cookies } from 'meteor/ostrio:cookies';
  181. const cookies = new Cookies();
  182. let showDesktopDragHandles = false;
  183. currentUser = Meteor.user();
  184. if (currentUser) {
  185. showDesktopDragHandles = (currentUser.profile || {})
  186. .showDesktopDragHandles;
  187. } else {
  188. import { Cookies } from 'meteor/ostrio:cookies';
  189. const cookies = new Cookies();
  190. if (cookies.has('showDesktopDragHandles')) {
  191. showDesktopDragHandles = true;
  192. } else {
  193. showDesktopDragHandles = false;
  194. }
  195. }
  196. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  197. Utils.isMiniScreen() ||
  198. (!Utils.isMiniScreen() && showDesktopDragHandles)
  199. ? ['.js-list-handle', '.js-swimlane-header-handle']
  200. : ['.js-list-header'],
  201. );
  202. if (
  203. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  204. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  205. ) {
  206. this._isDragging = true;
  207. this._lastDragPositionX = evt.clientX;
  208. }
  209. },
  210. mouseup() {
  211. if (this._isDragging) {
  212. this._isDragging = false;
  213. }
  214. },
  215. mousemove(evt) {
  216. if (this._isDragging) {
  217. // Update the canvas position
  218. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  219. this._lastDragPositionX = evt.clientX;
  220. // Disable browser text selection while dragging
  221. evt.stopPropagation();
  222. evt.preventDefault();
  223. // Don't close opened card or inlined form at the end of the
  224. // click-and-drag.
  225. EscapeActions.executeUpTo('popup-close');
  226. EscapeActions.preventNextClick();
  227. }
  228. },
  229. },
  230. ];
  231. },
  232. }).register('swimlane');
  233. BlazeComponent.extendComponent({
  234. onCreated() {
  235. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  236. this.isListTemplatesSwimlane =
  237. this.currentBoard.isTemplatesBoard() &&
  238. this.currentData().isListTemplatesSwimlane();
  239. this.currentSwimlane = this.currentData();
  240. },
  241. // Proxy
  242. open() {
  243. this.childComponents('inlinedForm')[0].open();
  244. },
  245. events() {
  246. return [
  247. {
  248. submit(evt) {
  249. evt.preventDefault();
  250. const titleInput = this.find('.list-name-input');
  251. const title = titleInput.value.trim();
  252. if (title) {
  253. Lists.insert({
  254. title,
  255. boardId: Session.get('currentBoard'),
  256. sort: $('.list').length,
  257. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  258. swimlaneId: this.currentBoard.isTemplatesBoard()
  259. ? this.currentSwimlane._id
  260. : '',
  261. });
  262. titleInput.value = '';
  263. titleInput.focus();
  264. }
  265. },
  266. 'click .js-list-template': Popup.open('searchElement'),
  267. },
  268. ];
  269. },
  270. }).register('addListForm');
  271. Template.swimlane.helpers({
  272. showDesktopDragHandles() {
  273. currentUser = Meteor.user();
  274. if (currentUser) {
  275. return (currentUser.profile || {}).showDesktopDragHandles;
  276. } else {
  277. import { Cookies } from 'meteor/ostrio:cookies';
  278. const cookies = new Cookies();
  279. if (cookies.has('showDesktopDragHandles')) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. },
  286. canSeeAddList() {
  287. return (
  288. Meteor.user() &&
  289. Meteor.user().isBoardMember() &&
  290. !Meteor.user().isCommentOnly()
  291. );
  292. },
  293. });
  294. BlazeComponent.extendComponent({
  295. currentCardIsInThisList(listId, swimlaneId) {
  296. return currentCardIsInThisList(listId, swimlaneId);
  297. },
  298. visible(list) {
  299. if (list.archived) {
  300. // Show archived list only when filter archive is on or archive is selected
  301. if (!(Filter.archive.isSelected() || archivedRequested)) {
  302. return false;
  303. }
  304. }
  305. if (Filter.lists._isActive()) {
  306. if (!list.title.match(Filter.lists.getRegexSelector())) {
  307. return false;
  308. }
  309. }
  310. if (Filter.hideEmpty.isSelected()) {
  311. const swimlaneId = this.parentComponent()
  312. .parentComponent()
  313. .data()._id;
  314. const cards = list.cards(swimlaneId);
  315. if (cards.count() === 0) {
  316. return false;
  317. }
  318. }
  319. return true;
  320. },
  321. onRendered() {
  322. const boardComponent = this.parentComponent();
  323. const $listsDom = this.$('.js-lists');
  324. if (!Session.get('currentCard')) {
  325. boardComponent.scrollLeft();
  326. }
  327. initSortable(boardComponent, $listsDom);
  328. },
  329. }).register('listsGroup');