boardBody.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. handle: '.js-swimlane-header',
  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. // ugly touch event hotfix
  172. enableClickOnTouch('.js-swimlane:not(.placeholder)');
  173. function userIsMember() {
  174. return (
  175. Meteor.user() &&
  176. Meteor.user().isBoardMember() &&
  177. !Meteor.user().isCommentOnly()
  178. );
  179. }
  180. // If there is no data in the board (ie, no lists) we autofocus the list
  181. // creation form by clicking on the corresponding element.
  182. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  183. if (userIsMember() && currentBoard.lists().count() === 0) {
  184. boardComponent.openNewListForm();
  185. }
  186. },
  187. isViewSwimlanes() {
  188. const currentUser = Meteor.user();
  189. if (!currentUser) return false;
  190. return (currentUser.profile || {}).boardView === 'board-view-swimlanes';
  191. },
  192. isViewLists() {
  193. const currentUser = Meteor.user();
  194. if (!currentUser) return true;
  195. return (currentUser.profile || {}).boardView === 'board-view-lists';
  196. },
  197. isViewCalendar() {
  198. const currentUser = Meteor.user();
  199. if (!currentUser) return false;
  200. return (currentUser.profile || {}).boardView === 'board-view-cal';
  201. },
  202. openNewListForm() {
  203. if (this.isViewSwimlanes()) {
  204. this.childComponents('swimlane')[0]
  205. .childComponents('addListAndSwimlaneForm')[0]
  206. .open();
  207. } else if (this.isViewLists()) {
  208. this.childComponents('listsGroup')[0]
  209. .childComponents('addListForm')[0]
  210. .open();
  211. }
  212. },
  213. events() {
  214. return [
  215. {
  216. // XXX The board-overlay div should probably be moved to the parent
  217. // component.
  218. 'mouseenter .board-overlay'() {
  219. if (this.mouseHasEnterCardDetails) {
  220. this.showOverlay.set(false);
  221. }
  222. },
  223. mouseup() {
  224. if (this._isDragging) {
  225. this._isDragging = false;
  226. }
  227. },
  228. },
  229. ];
  230. },
  231. // XXX Flow components allow us to avoid creating these two setter methods by
  232. // exposing a public API to modify the component state. We need to investigate
  233. // best practices here.
  234. setIsDragging(bool) {
  235. this.draggingActive.set(bool);
  236. },
  237. scrollLeft(position = 0) {
  238. const swimlanes = this.$('.js-swimlanes');
  239. swimlanes &&
  240. swimlanes.animate({
  241. scrollLeft: position,
  242. });
  243. },
  244. scrollTop(position = 0) {
  245. const swimlanes = this.$('.js-swimlanes');
  246. swimlanes &&
  247. swimlanes.animate({
  248. scrollTop: position,
  249. });
  250. },
  251. }).register('boardBody');
  252. BlazeComponent.extendComponent({
  253. onRendered() {
  254. this.autorun(function() {
  255. $('#calendar-view').fullCalendar('refetchEvents');
  256. });
  257. },
  258. calendarOptions() {
  259. return {
  260. id: 'calendar-view',
  261. defaultView: 'agendaDay',
  262. editable: true,
  263. timezone: 'local',
  264. header: {
  265. left: 'title today prev,next',
  266. center:
  267. 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,timelineMonth timelineYear',
  268. right: '',
  269. },
  270. // height: 'parent', nope, doesn't work as the parent might be small
  271. height: 'auto',
  272. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  273. navLinks: true,
  274. nowIndicator: true,
  275. businessHours: {
  276. // days of week. an array of zero-based day of week integers (0=Sunday)
  277. dow: [1, 2, 3, 4, 5], // Monday - Friday
  278. start: '8:00',
  279. end: '18:00',
  280. },
  281. locale: TAPi18n.getLanguage(),
  282. events(start, end, timezone, callback) {
  283. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  284. const events = [];
  285. currentBoard
  286. .cardsInInterval(start.toDate(), end.toDate())
  287. .forEach(function(card) {
  288. events.push({
  289. id: card._id,
  290. title: card.title,
  291. start: card.startAt,
  292. end: card.endAt,
  293. allDay:
  294. Math.abs(card.endAt.getTime() - card.startAt.getTime()) /
  295. 1000 ===
  296. 24 * 3600,
  297. url: FlowRouter.url('card', {
  298. boardId: currentBoard._id,
  299. slug: currentBoard.slug,
  300. cardId: card._id,
  301. }),
  302. });
  303. });
  304. callback(events);
  305. },
  306. eventResize(event, delta, revertFunc) {
  307. let isOk = false;
  308. const card = Cards.findOne(event.id);
  309. if (card) {
  310. card.setEnd(event.end.toDate());
  311. isOk = true;
  312. }
  313. if (!isOk) {
  314. revertFunc();
  315. }
  316. },
  317. eventDrop(event, delta, revertFunc) {
  318. let isOk = false;
  319. const card = Cards.findOne(event.id);
  320. if (card) {
  321. // TODO: add a flag for allDay events
  322. if (!event.allDay) {
  323. card.setStart(event.start.toDate());
  324. card.setEnd(event.end.toDate());
  325. isOk = true;
  326. }
  327. }
  328. if (!isOk) {
  329. revertFunc();
  330. }
  331. },
  332. };
  333. },
  334. isViewCalendar() {
  335. const currentUser = Meteor.user();
  336. if (!currentUser) return false;
  337. return (currentUser.profile || {}).boardView === 'board-view-cal';
  338. },
  339. }).register('calendarView');