boardBody.js 18 KB

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