swimlanes.js 9.4 KB

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