boardBody.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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() && Utils.getCurrentCardId(true);
  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. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  173. $swimlanesDom.sortable({
  174. handle: '.js-swimlane-header-handle',
  175. });
  176. } else {
  177. $swimlanesDom.sortable({
  178. handle: '.swimlane-header',
  179. });
  180. }
  181. // Disable drag-dropping if the current user is not a board member
  182. //$swimlanesDom.sortable('option', 'disabled', !userIsMember());
  183. $swimlanesDom.sortable(
  184. 'option',
  185. 'disabled',
  186. !Meteor.user() || !Meteor.user().isBoardAdmin(),
  187. );
  188. });
  189. function userIsMember() {
  190. return (
  191. Meteor.user() &&
  192. Meteor.user().isBoardMember() &&
  193. !Meteor.user().isCommentOnly()
  194. );
  195. }
  196. // If there is no data in the board (ie, no lists) we autofocus the list
  197. // creation form by clicking on the corresponding element.
  198. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  199. if (userIsMember() && currentBoard.lists().count() === 0) {
  200. boardComponent.openNewListForm();
  201. }
  202. },
  203. notDisplayThisBoard(){
  204. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  205. let currentBoard = Boards.findOne(Session.get('currentBoard'));
  206. if(allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue && currentBoard.permission == 'public'){
  207. return true;
  208. }
  209. return false;
  210. },
  211. isViewSwimlanes() {
  212. currentUser = Meteor.user();
  213. if (currentUser) {
  214. return (currentUser.profile || {}).boardView === 'board-view-swimlanes';
  215. } else {
  216. return (
  217. window.localStorage.getItem('boardView') === 'board-view-swimlanes'
  218. );
  219. }
  220. },
  221. isViewLists() {
  222. currentUser = Meteor.user();
  223. if (currentUser) {
  224. return (currentUser.profile || {}).boardView === 'board-view-lists';
  225. } else {
  226. return window.localStorage.getItem('boardView') === 'board-view-lists';
  227. }
  228. },
  229. isViewCalendar() {
  230. currentUser = Meteor.user();
  231. if (currentUser) {
  232. return (currentUser.profile || {}).boardView === 'board-view-cal';
  233. } else {
  234. return window.localStorage.getItem('boardView') === 'board-view-cal';
  235. }
  236. },
  237. openNewListForm() {
  238. if (this.isViewSwimlanes()) {
  239. this.childComponents('swimlane')[0]
  240. .childComponents('addListAndSwimlaneForm')[0]
  241. .open();
  242. } else if (this.isViewLists()) {
  243. this.childComponents('listsGroup')[0]
  244. .childComponents('addListForm')[0]
  245. .open();
  246. }
  247. },
  248. events() {
  249. return [
  250. {
  251. // XXX The board-overlay div should probably be moved to the parent
  252. // component.
  253. mouseup() {
  254. if (this._isDragging) {
  255. this._isDragging = false;
  256. }
  257. },
  258. },
  259. ];
  260. },
  261. // XXX Flow components allow us to avoid creating these two setter methods by
  262. // exposing a public API to modify the component state. We need to investigate
  263. // best practices here.
  264. setIsDragging(bool) {
  265. this.draggingActive.set(bool);
  266. },
  267. scrollLeft(position = 0) {
  268. const swimlanes = this.$('.js-swimlanes');
  269. swimlanes &&
  270. swimlanes.animate({
  271. scrollLeft: position,
  272. });
  273. },
  274. scrollTop(position = 0) {
  275. const swimlanes = this.$('.js-swimlanes');
  276. swimlanes &&
  277. swimlanes.animate({
  278. scrollTop: position,
  279. });
  280. },
  281. }).register('boardBody');
  282. BlazeComponent.extendComponent({
  283. onRendered() {
  284. this.autorun(function() {
  285. $('#calendar-view').fullCalendar('refetchEvents');
  286. });
  287. },
  288. calendarOptions() {
  289. return {
  290. id: 'calendar-view',
  291. defaultView: 'agendaDay',
  292. editable: true,
  293. timezone: 'local',
  294. weekNumbers: true,
  295. header: {
  296. left: 'title today prev,next',
  297. center:
  298. 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,listMonth',
  299. right: '',
  300. },
  301. // height: 'parent', nope, doesn't work as the parent might be small
  302. height: 'auto',
  303. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  304. navLinks: true,
  305. nowIndicator: true,
  306. businessHours: {
  307. // days of week. an array of zero-based day of week integers (0=Sunday)
  308. dow: [1, 2, 3, 4, 5], // Monday - Friday
  309. start: '8:00',
  310. end: '18:00',
  311. },
  312. locale: TAPi18n.getLanguage(),
  313. events(start, end, timezone, callback) {
  314. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  315. const events = [];
  316. const pushEvent = function(card, title, start, end, extraCls) {
  317. start = start || card.startAt;
  318. end = end || card.endAt;
  319. title = title || card.title;
  320. const className =
  321. (extraCls ? `${extraCls} ` : '') +
  322. (card.color ? `calendar-event-${card.color}` : '');
  323. events.push({
  324. id: card._id,
  325. title,
  326. start,
  327. end: end || card.endAt,
  328. allDay:
  329. Math.abs(end.getTime() - start.getTime()) / 1000 === 24 * 3600,
  330. url: FlowRouter.path('card', {
  331. boardId: currentBoard._id,
  332. slug: currentBoard.slug,
  333. cardId: card._id,
  334. }),
  335. className,
  336. });
  337. };
  338. currentBoard
  339. .cardsInInterval(start.toDate(), end.toDate())
  340. .forEach(function(card) {
  341. pushEvent(card);
  342. });
  343. currentBoard
  344. .cardsDueInBetween(start.toDate(), end.toDate())
  345. .forEach(function(card) {
  346. pushEvent(
  347. card,
  348. `${card.title} ${TAPi18n.__('card-due')}`,
  349. card.dueAt,
  350. new Date(card.dueAt.getTime() + 36e5),
  351. );
  352. });
  353. events.sort(function(first, second) {
  354. return first.id > second.id ? 1 : -1;
  355. });
  356. callback(events);
  357. },
  358. eventResize(event, delta, revertFunc) {
  359. let isOk = false;
  360. const card = Cards.findOne(event.id);
  361. if (card) {
  362. card.setEnd(event.end.toDate());
  363. isOk = true;
  364. }
  365. if (!isOk) {
  366. revertFunc();
  367. }
  368. },
  369. eventDrop(event, delta, revertFunc) {
  370. let isOk = false;
  371. const card = Cards.findOne(event.id);
  372. if (card) {
  373. // TODO: add a flag for allDay events
  374. if (!event.allDay) {
  375. card.setStart(event.start.toDate());
  376. card.setEnd(event.end.toDate());
  377. isOk = true;
  378. }
  379. }
  380. if (!isOk) {
  381. revertFunc();
  382. }
  383. },
  384. };
  385. },
  386. isViewCalendar() {
  387. currentUser = Meteor.user();
  388. if (currentUser) {
  389. return (currentUser.profile || {}).boardView === 'board-view-cal';
  390. } else {
  391. return window.localStorage.getItem('boardView') === 'board-view-cal';
  392. }
  393. },
  394. }).register('calendarView');