swimlanes.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 = Cards.findOne(Session.get('currentCard'));
  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. tolerance: 'pointer',
  53. helper: 'clone',
  54. items: '.js-list:not(.js-list-composer)',
  55. placeholder: 'list placeholder',
  56. distance: 7,
  57. start(evt, ui) {
  58. ui.placeholder.height(ui.helper.height());
  59. EscapeActions.executeUpTo('popup-close');
  60. boardComponent.setIsDragging(true);
  61. },
  62. stop(evt, ui) {
  63. // To attribute the new index number, we need to get the DOM element
  64. // of the previous and the following card -- if any.
  65. const prevListDom = ui.item.prev('.js-list').get(0);
  66. const nextListDom = ui.item.next('.js-list').get(0);
  67. const sortIndex = calculateIndex(prevListDom, nextListDom, 1);
  68. $listsDom.sortable('cancel');
  69. const listDomElement = ui.item.get(0);
  70. const list = Blaze.getData(listDomElement);
  71. Lists.update(list._id, {
  72. $set: {
  73. sort: sortIndex.base,
  74. },
  75. });
  76. boardComponent.setIsDragging(false);
  77. },
  78. });
  79. function userIsMember() {
  80. return (
  81. Meteor.user() &&
  82. Meteor.user().isBoardMember() &&
  83. !Meteor.user().isCommentOnly() &&
  84. !Meteor.user().isWorker()
  85. );
  86. }
  87. boardComponent.autorun(() => {
  88. let showDesktopDragHandles = false;
  89. currentUser = Meteor.user();
  90. if (currentUser) {
  91. showDesktopDragHandles = (currentUser.profile || {})
  92. .showDesktopDragHandles;
  93. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  94. showDesktopDragHandles = true;
  95. } else {
  96. showDesktopDragHandles = false;
  97. }
  98. if (Utils.isMiniScreen() || showDesktopDragHandles) {
  99. $listsDom.sortable({
  100. handle: '.js-list-handle',
  101. });
  102. } else if (!Utils.isMiniScreen() && !showDesktopDragHandles) {
  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. // Not disable drag-dropping while in multi-selection mode
  115. // MultiSelection.isActive() || !userIsMember(),
  116. );
  117. }
  118. });
  119. }
  120. BlazeComponent.extendComponent({
  121. onRendered() {
  122. const boardComponent = this.parentComponent();
  123. const $listsDom = this.$('.js-lists');
  124. if (!Session.get('currentCard')) {
  125. boardComponent.scrollLeft();
  126. }
  127. initSortable(boardComponent, $listsDom);
  128. },
  129. onCreated() {
  130. this.draggingActive = new ReactiveVar(false);
  131. this._isDragging = false;
  132. this._lastDragPositionX = 0;
  133. },
  134. id() {
  135. return this._id;
  136. },
  137. currentCardIsInThisList(listId, swimlaneId) {
  138. return currentCardIsInThisList(listId, swimlaneId);
  139. },
  140. currentListIsInThisSwimlane(swimlaneId) {
  141. return currentListIsInThisSwimlane(swimlaneId);
  142. },
  143. events() {
  144. return [
  145. {
  146. // Click-and-drag action
  147. 'mousedown .board-canvas'(evt) {
  148. // Translating the board canvas using the click-and-drag action can
  149. // conflict with the build-in browser mechanism to select text. We
  150. // define a list of elements in which we disable the dragging because
  151. // the user will legitimately expect to be able to select some text with
  152. // his mouse.
  153. let showDesktopDragHandles = false;
  154. currentUser = Meteor.user();
  155. if (currentUser) {
  156. showDesktopDragHandles = (currentUser.profile || {})
  157. .showDesktopDragHandles;
  158. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  159. showDesktopDragHandles = true;
  160. } else {
  161. showDesktopDragHandles = false;
  162. }
  163. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  164. Utils.isMiniScreen() || showDesktopDragHandles
  165. ? ['.js-list-handle', '.js-swimlane-header-handle']
  166. : ['.js-list-header'],
  167. );
  168. if (
  169. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  170. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  171. ) {
  172. this._isDragging = true;
  173. this._lastDragPositionX = evt.clientX;
  174. }
  175. },
  176. mouseup() {
  177. if (this._isDragging) {
  178. this._isDragging = false;
  179. }
  180. },
  181. mousemove(evt) {
  182. if (this._isDragging) {
  183. // Update the canvas position
  184. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  185. this._lastDragPositionX = evt.clientX;
  186. // Disable browser text selection while dragging
  187. evt.stopPropagation();
  188. evt.preventDefault();
  189. // Don't close opened card or inlined form at the end of the
  190. // click-and-drag.
  191. EscapeActions.executeUpTo('popup-close');
  192. EscapeActions.preventNextClick();
  193. }
  194. },
  195. },
  196. ];
  197. },
  198. }).register('swimlane');
  199. BlazeComponent.extendComponent({
  200. onCreated() {
  201. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  202. this.isListTemplatesSwimlane =
  203. this.currentBoard.isTemplatesBoard() &&
  204. this.currentData().isListTemplatesSwimlane();
  205. this.currentSwimlane = this.currentData();
  206. },
  207. // Proxy
  208. open() {
  209. this.childComponents('inlinedForm')[0].open();
  210. },
  211. events() {
  212. return [
  213. {
  214. submit(evt) {
  215. evt.preventDefault();
  216. const titleInput = this.find('.list-name-input');
  217. const title = titleInput.value.trim();
  218. if (title) {
  219. Lists.insert({
  220. title,
  221. boardId: Session.get('currentBoard'),
  222. sort: $('.list').length,
  223. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  224. swimlaneId: this.currentBoard.isTemplatesBoard()
  225. ? this.currentSwimlane._id
  226. : '',
  227. });
  228. titleInput.value = '';
  229. titleInput.focus();
  230. }
  231. },
  232. 'click .js-list-template': Popup.open('searchElement'),
  233. },
  234. ];
  235. },
  236. }).register('addListForm');
  237. Template.swimlane.helpers({
  238. showDesktopDragHandles() {
  239. currentUser = Meteor.user();
  240. if (currentUser) {
  241. return (currentUser.profile || {}).showDesktopDragHandles;
  242. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  243. return true;
  244. } else {
  245. return false;
  246. }
  247. },
  248. canSeeAddList() {
  249. return (
  250. Meteor.user() &&
  251. Meteor.user().isBoardMember() &&
  252. !Meteor.user().isCommentOnly() &&
  253. !Meteor.user().isWorker()
  254. );
  255. },
  256. });
  257. BlazeComponent.extendComponent({
  258. currentCardIsInThisList(listId, swimlaneId) {
  259. return currentCardIsInThisList(listId, swimlaneId);
  260. },
  261. visible(list) {
  262. if (list.archived) {
  263. // Show archived list only when filter archive is on or archive is selected
  264. if (!(Filter.archive.isSelected() || archivedRequested)) {
  265. return false;
  266. }
  267. }
  268. if (Filter.lists._isActive()) {
  269. if (!list.title.match(Filter.lists.getRegexSelector())) {
  270. return false;
  271. }
  272. }
  273. if (Filter.hideEmpty.isSelected()) {
  274. const swimlaneId = this.parentComponent()
  275. .parentComponent()
  276. .data()._id;
  277. const cards = list.cards(swimlaneId);
  278. if (cards.count() === 0) {
  279. return false;
  280. }
  281. }
  282. return true;
  283. },
  284. onRendered() {
  285. const boardComponent = this.parentComponent();
  286. const $listsDom = this.$('.js-lists');
  287. if (!Session.get('currentCard')) {
  288. boardComponent.scrollLeft();
  289. }
  290. initSortable(boardComponent, $listsDom);
  291. },
  292. }).register('listsGroup');