boardBody.js 15 KB

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