boardBody.js 16 KB

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