swimlanes.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. );
  90. }
  91. boardComponent.autorun(() => {
  92. let showDesktopDragHandles = false;
  93. currentUser = Meteor.user();
  94. if (currentUser) {
  95. showDesktopDragHandles = (currentUser.profile || {})
  96. .showDesktopDragHandles;
  97. } else {
  98. if (cookies.has('showDesktopDragHandles')) {
  99. showDesktopDragHandles = true;
  100. } else {
  101. showDesktopDragHandles = false;
  102. }
  103. }
  104. if (!Utils.isMiniScreen() && showDesktopDragHandles) {
  105. $listsDom.sortable({
  106. handle: '.js-list-handle',
  107. });
  108. } else {
  109. $listsDom.sortable({
  110. handle: '.js-list-header',
  111. });
  112. }
  113. const $listDom = $listsDom;
  114. if ($listDom.data('sortable')) {
  115. $listsDom.sortable(
  116. 'option',
  117. 'disabled',
  118. // Disable drag-dropping when user is not member
  119. !userIsMember(),
  120. // Not disable drag-dropping while in multi-selection mode
  121. // MultiSelection.isActive() || !userIsMember(),
  122. );
  123. }
  124. });
  125. }
  126. BlazeComponent.extendComponent({
  127. onRendered() {
  128. const boardComponent = this.parentComponent();
  129. const $listsDom = this.$('.js-lists');
  130. if (!Session.get('currentCard')) {
  131. boardComponent.scrollLeft();
  132. }
  133. initSortable(boardComponent, $listsDom);
  134. },
  135. onCreated() {
  136. this.draggingActive = new ReactiveVar(false);
  137. this._isDragging = false;
  138. this._lastDragPositionX = 0;
  139. },
  140. id() {
  141. return this._id;
  142. },
  143. currentCardIsInThisList(listId, swimlaneId) {
  144. return currentCardIsInThisList(listId, swimlaneId);
  145. },
  146. currentListIsInThisSwimlane(swimlaneId) {
  147. return currentListIsInThisSwimlane(swimlaneId);
  148. },
  149. events() {
  150. return [
  151. {
  152. // Click-and-drag action
  153. 'mousedown .board-canvas'(evt) {
  154. // Translating the board canvas using the click-and-drag action can
  155. // conflict with the build-in browser mechanism to select text. We
  156. // define a list of elements in which we disable the dragging because
  157. // the user will legitimately expect to be able to select some text with
  158. // his mouse.
  159. let showDesktopDragHandles = false;
  160. currentUser = Meteor.user();
  161. if (currentUser) {
  162. showDesktopDragHandles = (currentUser.profile || {})
  163. .showDesktopDragHandles;
  164. } else {
  165. if (cookies.has('showDesktopDragHandles')) {
  166. showDesktopDragHandles = true;
  167. } else {
  168. showDesktopDragHandles = false;
  169. }
  170. }
  171. const noDragInside = ['a', 'input', 'textarea', 'p'].concat(
  172. Utils.isMiniScreen() ||
  173. (!Utils.isMiniScreen() && showDesktopDragHandles)
  174. ? ['.js-list-handle', '.js-swimlane-header-handle']
  175. : ['.js-list-header'],
  176. );
  177. if (
  178. $(evt.target).closest(noDragInside.join(',')).length === 0 &&
  179. this.$('.swimlane').prop('clientHeight') > evt.offsetY
  180. ) {
  181. this._isDragging = true;
  182. this._lastDragPositionX = evt.clientX;
  183. }
  184. },
  185. mouseup() {
  186. if (this._isDragging) {
  187. this._isDragging = false;
  188. }
  189. },
  190. mousemove(evt) {
  191. if (this._isDragging) {
  192. // Update the canvas position
  193. this.listsDom.scrollLeft -= evt.clientX - this._lastDragPositionX;
  194. this._lastDragPositionX = evt.clientX;
  195. // Disable browser text selection while dragging
  196. evt.stopPropagation();
  197. evt.preventDefault();
  198. // Don't close opened card or inlined form at the end of the
  199. // click-and-drag.
  200. EscapeActions.executeUpTo('popup-close');
  201. EscapeActions.preventNextClick();
  202. }
  203. },
  204. },
  205. ];
  206. },
  207. }).register('swimlane');
  208. BlazeComponent.extendComponent({
  209. onCreated() {
  210. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  211. this.isListTemplatesSwimlane =
  212. this.currentBoard.isTemplatesBoard() &&
  213. this.currentData().isListTemplatesSwimlane();
  214. this.currentSwimlane = this.currentData();
  215. },
  216. // Proxy
  217. open() {
  218. this.childComponents('inlinedForm')[0].open();
  219. },
  220. events() {
  221. return [
  222. {
  223. submit(evt) {
  224. evt.preventDefault();
  225. const titleInput = this.find('.list-name-input');
  226. const title = titleInput.value.trim();
  227. if (title) {
  228. Lists.insert({
  229. title,
  230. boardId: Session.get('currentBoard'),
  231. sort: $('.list').length,
  232. type: this.isListTemplatesSwimlane ? 'template-list' : 'list',
  233. swimlaneId: this.currentBoard.isTemplatesBoard()
  234. ? this.currentSwimlane._id
  235. : '',
  236. });
  237. titleInput.value = '';
  238. titleInput.focus();
  239. }
  240. },
  241. 'click .js-list-template': Popup.open('searchElement'),
  242. },
  243. ];
  244. },
  245. }).register('addListForm');
  246. Template.swimlane.helpers({
  247. showDesktopDragHandles() {
  248. currentUser = Meteor.user();
  249. if (currentUser) {
  250. return (currentUser.profile || {}).showDesktopDragHandles;
  251. } else {
  252. if (cookies.has('showDesktopDragHandles')) {
  253. return true;
  254. } else {
  255. return false;
  256. }
  257. }
  258. },
  259. canSeeAddList() {
  260. return (
  261. Meteor.user() &&
  262. Meteor.user().isBoardMember() &&
  263. !Meteor.user().isCommentOnly()
  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');