swimlanes.js 10 KB

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