swimlanes.js 11 KB

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