boardBody.js 14 KB

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