swimlanes.js 10 KB

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