boardBody.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import dragscroll from '@wekanteam/dragscroll';
  4. import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
  5. const subManager = new SubsManager();
  6. const { calculateIndex } = Utils;
  7. const swimlaneWhileSortingHeight = 150;
  8. BlazeComponent.extendComponent({
  9. onCreated() {
  10. this.isBoardReady = new ReactiveVar(false);
  11. // The pattern we use to manually handle data loading is described here:
  12. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  13. // XXX The boardId should be readed from some sort the component "props",
  14. // unfortunatly, Blaze doesn't have this notion.
  15. this.autorun(() => {
  16. const currentBoardId = Session.get('currentBoard');
  17. if (!currentBoardId) return;
  18. const handle = subManager.subscribe('board', currentBoardId, false);
  19. Tracker.nonreactive(() => {
  20. Tracker.autorun(() => {
  21. this.isBoardReady.set(handle.ready());
  22. });
  23. });
  24. });
  25. },
  26. onlyShowCurrentCard() {
  27. return Utils.isMiniScreen() && Utils.getCurrentCardId(true);
  28. },
  29. goHome() {
  30. FlowRouter.go('home');
  31. },
  32. }).register('board');
  33. BlazeComponent.extendComponent({
  34. onCreated() {
  35. Meteor.subscribe('tableVisibilityModeSettings');
  36. this.showOverlay = new ReactiveVar(false);
  37. this.draggingActive = new ReactiveVar(false);
  38. this._isDragging = false;
  39. // Used to set the overlay
  40. this.mouseHasEnterCardDetails = false;
  41. // fix swimlanes sort field if there are null values
  42. const currentBoardData = Utils.getCurrentBoard();
  43. const nullSortSwimlanes = currentBoardData.nullSortSwimlanes();
  44. if (nullSortSwimlanes.length > 0) {
  45. const swimlanes = currentBoardData.swimlanes();
  46. let count = 0;
  47. swimlanes.forEach(s => {
  48. Swimlanes.update(s._id, {
  49. $set: {
  50. sort: count,
  51. },
  52. });
  53. count += 1;
  54. });
  55. }
  56. // fix lists sort field if there are null values
  57. const nullSortLists = currentBoardData.nullSortLists();
  58. if (nullSortLists.length > 0) {
  59. const lists = currentBoardData.lists();
  60. let count = 0;
  61. lists.forEach(l => {
  62. Lists.update(l._id, {
  63. $set: {
  64. sort: count,
  65. },
  66. });
  67. count += 1;
  68. });
  69. }
  70. },
  71. onRendered() {
  72. const boardComponent = this;
  73. const $swimlanesDom = boardComponent.$('.js-swimlanes');
  74. $swimlanesDom.sortable({
  75. tolerance: 'pointer',
  76. appendTo: '.board-canvas',
  77. helper(evt, item) {
  78. const helper = $(`<div class="swimlane"
  79. style="flex-direction: column;
  80. height: ${swimlaneWhileSortingHeight}px;
  81. width: $(boardComponent.width)px;
  82. overflow: hidden;"/>`);
  83. helper.append(item.clone());
  84. // Also grab the list of lists of cards
  85. const list = item.next();
  86. helper.append(list.clone());
  87. return helper;
  88. },
  89. items: '.swimlane:not(.placeholder)',
  90. placeholder: 'swimlane placeholder',
  91. distance: 7,
  92. start(evt, ui) {
  93. const listDom = ui.placeholder.next('.js-swimlane');
  94. const parentOffset = ui.item.parent().offset();
  95. ui.placeholder.height(ui.helper.height());
  96. EscapeActions.executeUpTo('popup-close');
  97. listDom.addClass('moving-swimlane');
  98. boardComponent.setIsDragging(true);
  99. ui.placeholder.insertAfter(ui.placeholder.next());
  100. boardComponent.origPlaceholderIndex = ui.placeholder.index();
  101. // resize all swimlanes + headers to be a total of 150 px per row
  102. // this could be achieved by setIsDragging(true) but we want immediate
  103. // result
  104. ui.item
  105. .siblings('.js-swimlane')
  106. .css('height', `${swimlaneWhileSortingHeight - 26}px`);
  107. // set the new scroll height after the resize and insertion of
  108. // the placeholder. We want the element under the cursor to stay
  109. // at the same place on the screen
  110. ui.item.parent().get(0).scrollTop =
  111. ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  112. },
  113. beforeStop(evt, ui) {
  114. const parentOffset = ui.item.parent().offset();
  115. const siblings = ui.item.siblings('.js-swimlane');
  116. siblings.css('height', '');
  117. // compute the new scroll height after the resize and removal of
  118. // the placeholder
  119. const scrollTop =
  120. ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  121. // then reset the original view of the swimlane
  122. siblings.removeClass('moving-swimlane');
  123. // and apply the computed scrollheight
  124. ui.item.parent().get(0).scrollTop = scrollTop;
  125. },
  126. stop(evt, ui) {
  127. // To attribute the new index number, we need to get the DOM element
  128. // of the previous and the following card -- if any.
  129. const prevSwimlaneDom = ui.item.prevAll('.js-swimlane').get(0);
  130. const nextSwimlaneDom = ui.item.nextAll('.js-swimlane').get(0);
  131. const sortIndex = calculateIndex(prevSwimlaneDom, nextSwimlaneDom, 1);
  132. $swimlanesDom.sortable('cancel');
  133. const swimlaneDomElement = ui.item.get(0);
  134. const swimlane = Blaze.getData(swimlaneDomElement);
  135. Swimlanes.update(swimlane._id, {
  136. $set: {
  137. sort: sortIndex.base,
  138. },
  139. });
  140. boardComponent.setIsDragging(false);
  141. },
  142. sort(evt, ui) {
  143. // get the mouse position in the sortable
  144. const parentOffset = ui.item.parent().offset();
  145. const cursorY =
  146. evt.pageY - parentOffset.top + ui.item.parent().scrollTop();
  147. // compute the intended index of the placeholder (we need to skip the
  148. // slots between the headers and the list of cards)
  149. const newplaceholderIndex = Math.floor(
  150. cursorY / swimlaneWhileSortingHeight,
  151. );
  152. let destPlaceholderIndex = (newplaceholderIndex + 1) * 2;
  153. // if we are scrolling far away from the bottom of the list
  154. if (destPlaceholderIndex >= ui.item.parent().get(0).childElementCount) {
  155. destPlaceholderIndex = ui.item.parent().get(0).childElementCount - 1;
  156. }
  157. // update the placeholder position in the DOM tree
  158. if (destPlaceholderIndex !== ui.placeholder.index()) {
  159. if (destPlaceholderIndex < boardComponent.origPlaceholderIndex) {
  160. ui.placeholder.insertBefore(
  161. ui.placeholder
  162. .siblings()
  163. .slice(destPlaceholderIndex - 2, destPlaceholderIndex - 1),
  164. );
  165. } else {
  166. ui.placeholder.insertAfter(
  167. ui.placeholder
  168. .siblings()
  169. .slice(destPlaceholderIndex - 1, destPlaceholderIndex),
  170. );
  171. }
  172. }
  173. },
  174. });
  175. this.autorun(() => {
  176. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  177. $swimlanesDom.sortable({
  178. handle: '.js-swimlane-header-handle',
  179. });
  180. } else {
  181. $swimlanesDom.sortable({
  182. handle: '.swimlane-header',
  183. });
  184. }
  185. // Disable drag-dropping if the current user is not a board member
  186. $swimlanesDom.sortable(
  187. 'option',
  188. 'disabled',
  189. !ReactiveCache.getCurrentUser()?.isBoardAdmin(),
  190. );
  191. });
  192. // If there is no data in the board (ie, no lists) we autofocus the list
  193. // creation form by clicking on the corresponding element.
  194. const currentBoard = Utils.getCurrentBoard();
  195. if (Utils.canModifyBoard() && currentBoard.lists().length === 0) {
  196. boardComponent.openNewListForm();
  197. }
  198. dragscroll.reset();
  199. Utils.setBackgroundImage();
  200. },
  201. notDisplayThisBoard() {
  202. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  203. let currentBoard = Utils.getCurrentBoard();
  204. if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue && currentBoard.permission == 'public') {
  205. return true;
  206. }
  207. return false;
  208. },
  209. isViewSwimlanes() {
  210. const currentUser = ReactiveCache.getCurrentUser();
  211. if (currentUser) {
  212. return (currentUser.profile || {}).boardView === 'board-view-swimlanes';
  213. } else {
  214. return (
  215. window.localStorage.getItem('boardView') === 'board-view-swimlanes'
  216. );
  217. }
  218. },
  219. isViewLists() {
  220. const currentUser = ReactiveCache.getCurrentUser();
  221. if (currentUser) {
  222. return (currentUser.profile || {}).boardView === 'board-view-lists';
  223. } else {
  224. return window.localStorage.getItem('boardView') === 'board-view-lists';
  225. }
  226. },
  227. isViewCalendar() {
  228. const currentUser = ReactiveCache.getCurrentUser();
  229. if (currentUser) {
  230. return (currentUser.profile || {}).boardView === 'board-view-cal';
  231. } else {
  232. return window.localStorage.getItem('boardView') === 'board-view-cal';
  233. }
  234. },
  235. openNewListForm() {
  236. if (this.isViewSwimlanes()) {
  237. // The form had been removed in 416b17062e57f215206e93a85b02ef9eb1ab4902
  238. // this.childComponents('swimlane')[0]
  239. // .childComponents('addListAndSwimlaneForm')[0]
  240. // .open();
  241. } else if (this.isViewLists()) {
  242. this.childComponents('listsGroup')[0]
  243. .childComponents('addListForm')[0]
  244. .open();
  245. }
  246. },
  247. events() {
  248. return [
  249. {
  250. // XXX The board-overlay div should probably be moved to the parent
  251. // component.
  252. mouseup() {
  253. if (this._isDragging) {
  254. this._isDragging = false;
  255. }
  256. },
  257. },
  258. ];
  259. },
  260. // XXX Flow components allow us to avoid creating these two setter methods by
  261. // exposing a public API to modify the component state. We need to investigate
  262. // best practices here.
  263. setIsDragging(bool) {
  264. this.draggingActive.set(bool);
  265. },
  266. scrollLeft(position = 0) {
  267. const swimlanes = this.$('.js-swimlanes');
  268. swimlanes &&
  269. swimlanes.animate({
  270. scrollLeft: position,
  271. });
  272. },
  273. scrollTop(position = 0) {
  274. const swimlanes = this.$('.js-swimlanes');
  275. swimlanes &&
  276. swimlanes.animate({
  277. scrollTop: position,
  278. });
  279. },
  280. }).register('boardBody');
  281. BlazeComponent.extendComponent({
  282. onRendered() {
  283. this.autorun(function () {
  284. $('#calendar-view').fullCalendar('refetchEvents');
  285. });
  286. },
  287. calendarOptions() {
  288. return {
  289. id: 'calendar-view',
  290. defaultView: 'month',
  291. editable: true,
  292. selectable: true,
  293. timezone: 'local',
  294. weekNumbers: true,
  295. header: {
  296. left: 'title today prev,next',
  297. center:
  298. 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,listMonth',
  299. right: '',
  300. },
  301. // height: 'parent', nope, doesn't work as the parent might be small
  302. height: 'auto',
  303. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  304. navLinks: true,
  305. nowIndicator: true,
  306. businessHours: {
  307. // days of week. an array of zero-based day of week integers (0=Sunday)
  308. dow: [1, 2, 3, 4, 5], // Monday - Friday
  309. start: '8:00',
  310. end: '18:00',
  311. },
  312. locale: TAPi18n.getLanguage(),
  313. events(start, end, timezone, callback) {
  314. const currentBoard = Utils.getCurrentBoard();
  315. const events = [];
  316. const pushEvent = function (card, title, start, end, extraCls) {
  317. start = start || card.startAt;
  318. end = end || card.endAt;
  319. title = title || card.title;
  320. const className =
  321. (extraCls ? `${extraCls} ` : '') +
  322. (card.color ? `calendar-event-${card.color}` : '');
  323. events.push({
  324. id: card._id,
  325. title,
  326. start,
  327. end: end || card.endAt,
  328. allDay:
  329. Math.abs(end.getTime() - start.getTime()) / 1000 === 24 * 3600,
  330. url: FlowRouter.path('card', {
  331. boardId: currentBoard._id,
  332. slug: currentBoard.slug,
  333. cardId: card._id,
  334. }),
  335. className,
  336. });
  337. };
  338. currentBoard
  339. .cardsInInterval(start.toDate(), end.toDate())
  340. .forEach(function (card) {
  341. pushEvent(card);
  342. });
  343. currentBoard
  344. .cardsDueInBetween(start.toDate(), end.toDate())
  345. .forEach(function (card) {
  346. pushEvent(
  347. card,
  348. `${card.title} ${TAPi18n.__('card-due')}`,
  349. card.dueAt,
  350. new Date(card.dueAt.getTime() + 36e5),
  351. );
  352. });
  353. events.sort(function (first, second) {
  354. return first.id > second.id ? 1 : -1;
  355. });
  356. callback(events);
  357. },
  358. eventResize(event, delta, revertFunc) {
  359. let isOk = false;
  360. const card = ReactiveCache.getCard(event.id);
  361. if (card) {
  362. card.setEnd(event.end.toDate());
  363. isOk = true;
  364. }
  365. if (!isOk) {
  366. revertFunc();
  367. }
  368. },
  369. eventDrop(event, delta, revertFunc) {
  370. let isOk = false;
  371. const card = ReactiveCache.getCard(event.id);
  372. if (card) {
  373. // TODO: add a flag for allDay events
  374. if (!event.allDay) {
  375. // https://github.com/wekan/wekan/issues/2917#issuecomment-1236753962
  376. //card.setStart(event.start.toDate());
  377. //card.setEnd(event.end.toDate());
  378. card.setDue(event.start.toDate());
  379. isOk = true;
  380. }
  381. }
  382. if (!isOk) {
  383. revertFunc();
  384. }
  385. },
  386. select: function (startDate) {
  387. const currentBoard = Utils.getCurrentBoard();
  388. const currentUser = ReactiveCache.getCurrentUser();
  389. const modalElement = document.createElement('div');
  390. modalElement.classList.add('modal', 'fade');
  391. modalElement.setAttribute('tabindex', '-1');
  392. modalElement.setAttribute('role', 'dialog');
  393. modalElement.innerHTML = `
  394. <div class="modal-dialog justify-content-center align-items-center" role="document">
  395. <div class="modal-content">
  396. <div class="modal-header">
  397. <h5 class="modal-title">${TAPi18n.__('r-create-card')}</h5>
  398. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  399. <span aria-hidden="true">&times;</span>
  400. </button>
  401. </div>
  402. <div class="modal-body text-center">
  403. <input type="text" class="form-control" id="card-title-input" placeholder="">
  404. </div>
  405. <div class="modal-footer">
  406. <button type="button" class="btn btn-primary" id="create-card-button">${TAPi18n.__('add-card')}</button>
  407. </div>
  408. </div>
  409. </div>
  410. `;
  411. const createCardButton = modalElement.querySelector('#create-card-button');
  412. createCardButton.addEventListener('click', function () {
  413. const myTitle = modalElement.querySelector('#card-title-input').value;
  414. if (myTitle) {
  415. const firstList = currentBoard.draggableLists()[0];
  416. const firstSwimlane = currentBoard.swimlanes()[0];
  417. Meteor.call('createCardWithDueDate', currentBoard._id, firstList._id, myTitle, startDate.toDate(), firstSwimlane._id, function(error, result) {
  418. if (error) {
  419. console.log(error);
  420. } else {
  421. console.log("Card Created", result);
  422. }
  423. });
  424. closeModal();
  425. }
  426. });
  427. document.body.appendChild(modalElement);
  428. const openModal = function() {
  429. modalElement.style.display = 'flex';
  430. };
  431. const closeModal = function() {
  432. modalElement.style.display = 'none';
  433. };
  434. const closeButton = modalElement.querySelector('[data-dismiss="modal"]');
  435. closeButton.addEventListener('click', closeModal);
  436. openModal();
  437. }
  438. };
  439. },
  440. isViewCalendar() {
  441. const currentUser = ReactiveCache.getCurrentUser();
  442. if (currentUser) {
  443. return (currentUser.profile || {}).boardView === 'board-view-cal';
  444. } else {
  445. return window.localStorage.getItem('boardView') === 'board-view-cal';
  446. }
  447. },
  448. }).register('calendarView');