boardBody.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. // Always reset dragscroll on view switch
  176. dragscroll.reset();
  177. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  178. $swimlanesDom.sortable({
  179. handle: '.js-swimlane-header-handle',
  180. });
  181. } else {
  182. $swimlanesDom.sortable({
  183. handle: '.swimlane-header',
  184. });
  185. }
  186. // Disable drag-dropping if the current user is not a board member
  187. $swimlanesDom.sortable(
  188. 'option',
  189. 'disabled',
  190. !ReactiveCache.getCurrentUser()?.isBoardAdmin(),
  191. );
  192. });
  193. // If there is no data in the board (ie, no lists) we autofocus the list
  194. // creation form by clicking on the corresponding element.
  195. const currentBoard = Utils.getCurrentBoard();
  196. if (Utils.canModifyBoard() && currentBoard.lists().length === 0) {
  197. boardComponent.openNewListForm();
  198. }
  199. dragscroll.reset();
  200. Utils.setBackgroundImage();
  201. },
  202. notDisplayThisBoard() {
  203. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  204. let currentBoard = Utils.getCurrentBoard();
  205. if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue && currentBoard.permission == 'public') {
  206. return true;
  207. }
  208. return false;
  209. },
  210. isViewSwimlanes() {
  211. const currentUser = ReactiveCache.getCurrentUser();
  212. if (currentUser) {
  213. return (currentUser.profile || {}).boardView === 'board-view-swimlanes';
  214. } else {
  215. return (
  216. window.localStorage.getItem('boardView') === 'board-view-swimlanes'
  217. );
  218. }
  219. },
  220. isViewLists() {
  221. const currentUser = ReactiveCache.getCurrentUser();
  222. if (currentUser) {
  223. return (currentUser.profile || {}).boardView === 'board-view-lists';
  224. } else {
  225. return window.localStorage.getItem('boardView') === 'board-view-lists';
  226. }
  227. },
  228. isViewCalendar() {
  229. const currentUser = ReactiveCache.getCurrentUser();
  230. if (currentUser) {
  231. return (currentUser.profile || {}).boardView === 'board-view-cal';
  232. } else {
  233. return window.localStorage.getItem('boardView') === 'board-view-cal';
  234. }
  235. },
  236. openNewListForm() {
  237. if (this.isViewSwimlanes()) {
  238. // The form had been removed in 416b17062e57f215206e93a85b02ef9eb1ab4902
  239. // this.childComponents('swimlane')[0]
  240. // .childComponents('addListAndSwimlaneForm')[0]
  241. // .open();
  242. } else if (this.isViewLists()) {
  243. this.childComponents('listsGroup')[0]
  244. .childComponents('addListForm')[0]
  245. .open();
  246. }
  247. },
  248. events() {
  249. return [
  250. {
  251. // XXX The board-overlay div should probably be moved to the parent
  252. // component.
  253. mouseup() {
  254. if (this._isDragging) {
  255. this._isDragging = false;
  256. }
  257. },
  258. },
  259. ];
  260. },
  261. // XXX Flow components allow us to avoid creating these two setter methods by
  262. // exposing a public API to modify the component state. We need to investigate
  263. // best practices here.
  264. setIsDragging(bool) {
  265. this.draggingActive.set(bool);
  266. },
  267. scrollLeft(position = 0) {
  268. const swimlanes = this.$('.js-swimlanes');
  269. swimlanes &&
  270. swimlanes.animate({
  271. scrollLeft: position,
  272. });
  273. },
  274. scrollTop(position = 0) {
  275. const swimlanes = this.$('.js-swimlanes');
  276. swimlanes &&
  277. swimlanes.animate({
  278. scrollTop: position,
  279. });
  280. },
  281. }).register('boardBody');
  282. BlazeComponent.extendComponent({
  283. onRendered() {
  284. this.autorun(function () {
  285. $('#calendar-view').fullCalendar('refetchEvents');
  286. });
  287. },
  288. calendarOptions() {
  289. return {
  290. id: 'calendar-view',
  291. defaultView: 'month',
  292. editable: true,
  293. selectable: true,
  294. timezone: 'local',
  295. weekNumbers: true,
  296. header: {
  297. left: 'title today prev,next',
  298. center:
  299. 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,listMonth',
  300. right: '',
  301. },
  302. // height: 'parent', nope, doesn't work as the parent might be small
  303. height: 'auto',
  304. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  305. navLinks: true,
  306. nowIndicator: true,
  307. businessHours: {
  308. // days of week. an array of zero-based day of week integers (0=Sunday)
  309. dow: [1, 2, 3, 4, 5], // Monday - Friday
  310. start: '8:00',
  311. end: '18:00',
  312. },
  313. locale: TAPi18n.getLanguage(),
  314. events(start, end, timezone, callback) {
  315. const currentBoard = Utils.getCurrentBoard();
  316. const events = [];
  317. const pushEvent = function (card, title, start, end, extraCls) {
  318. start = start || card.startAt;
  319. end = end || card.endAt;
  320. title = title || card.title;
  321. const className =
  322. (extraCls ? `${extraCls} ` : '') +
  323. (card.color ? `calendar-event-${card.color}` : '');
  324. events.push({
  325. id: card._id,
  326. title,
  327. start,
  328. end: end || card.endAt,
  329. allDay:
  330. Math.abs(end.getTime() - start.getTime()) / 1000 === 24 * 3600,
  331. url: FlowRouter.path('card', {
  332. boardId: currentBoard._id,
  333. slug: currentBoard.slug,
  334. cardId: card._id,
  335. }),
  336. className,
  337. });
  338. };
  339. currentBoard
  340. .cardsInInterval(start.toDate(), end.toDate())
  341. .forEach(function (card) {
  342. pushEvent(card);
  343. });
  344. currentBoard
  345. .cardsDueInBetween(start.toDate(), end.toDate())
  346. .forEach(function (card) {
  347. pushEvent(
  348. card,
  349. `${card.title} ${TAPi18n.__('card-due')}`,
  350. card.dueAt,
  351. new Date(card.dueAt.getTime() + 36e5),
  352. );
  353. });
  354. events.sort(function (first, second) {
  355. return first.id > second.id ? 1 : -1;
  356. });
  357. callback(events);
  358. },
  359. eventResize(event, delta, revertFunc) {
  360. let isOk = false;
  361. const card = ReactiveCache.getCard(event.id);
  362. if (card) {
  363. card.setEnd(event.end.toDate());
  364. isOk = true;
  365. }
  366. if (!isOk) {
  367. revertFunc();
  368. }
  369. },
  370. eventDrop(event, delta, revertFunc) {
  371. let isOk = false;
  372. const card = ReactiveCache.getCard(event.id);
  373. if (card) {
  374. // TODO: add a flag for allDay events
  375. if (!event.allDay) {
  376. // https://github.com/wekan/wekan/issues/2917#issuecomment-1236753962
  377. //card.setStart(event.start.toDate());
  378. //card.setEnd(event.end.toDate());
  379. card.setDue(event.start.toDate());
  380. isOk = true;
  381. }
  382. }
  383. if (!isOk) {
  384. revertFunc();
  385. }
  386. },
  387. select: function (startDate) {
  388. const currentBoard = Utils.getCurrentBoard();
  389. const currentUser = ReactiveCache.getCurrentUser();
  390. const modalElement = document.createElement('div');
  391. modalElement.classList.add('modal', 'fade');
  392. modalElement.setAttribute('tabindex', '-1');
  393. modalElement.setAttribute('role', 'dialog');
  394. modalElement.innerHTML = `
  395. <div class="modal-dialog justify-content-center align-items-center" role="document">
  396. <div class="modal-content">
  397. <div class="modal-header">
  398. <h5 class="modal-title">${TAPi18n.__('r-create-card')}</h5>
  399. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  400. <span aria-hidden="true">&times;</span>
  401. </button>
  402. </div>
  403. <div class="modal-body text-center">
  404. <input type="text" class="form-control" id="card-title-input" placeholder="">
  405. </div>
  406. <div class="modal-footer">
  407. <button type="button" class="btn btn-primary" id="create-card-button">${TAPi18n.__('add-card')}</button>
  408. </div>
  409. </div>
  410. </div>
  411. `;
  412. const createCardButton = modalElement.querySelector('#create-card-button');
  413. createCardButton.addEventListener('click', function () {
  414. const myTitle = modalElement.querySelector('#card-title-input').value;
  415. if (myTitle) {
  416. const firstList = currentBoard.draggableLists()[0];
  417. const firstSwimlane = currentBoard.swimlanes()[0];
  418. Meteor.call('createCardWithDueDate', currentBoard._id, firstList._id, myTitle, startDate.toDate(), firstSwimlane._id, function(error, result) {
  419. if (error) {
  420. console.log(error);
  421. } else {
  422. console.log("Card Created", result);
  423. }
  424. });
  425. closeModal();
  426. }
  427. });
  428. document.body.appendChild(modalElement);
  429. const openModal = function() {
  430. modalElement.style.display = 'flex';
  431. };
  432. const closeModal = function() {
  433. modalElement.style.display = 'none';
  434. };
  435. const closeButton = modalElement.querySelector('[data-dismiss="modal"]');
  436. closeButton.addEventListener('click', closeModal);
  437. openModal();
  438. }
  439. };
  440. },
  441. isViewCalendar() {
  442. const currentUser = ReactiveCache.getCurrentUser();
  443. if (currentUser) {
  444. return (currentUser.profile || {}).boardView === 'board-view-cal';
  445. } else {
  446. return window.localStorage.getItem('boardView') === 'board-view-cal';
  447. }
  448. },
  449. }).register('calendarView');