swimlanes.js 9.7 KB

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