boardBody.js 14 KB

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