boardBody.js 15 KB

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