boardBody.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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)
  14. return;
  15. const handle = subManager.subscribe('board', currentBoardId, false);
  16. Tracker.nonreactive(() => {
  17. Tracker.autorun(() => {
  18. this.isBoardReady.set(handle.ready());
  19. });
  20. });
  21. });
  22. },
  23. onlyShowCurrentCard() {
  24. return Utils.isMiniScreen() && Session.get('currentCard');
  25. },
  26. goHome() {
  27. FlowRouter.go('home');
  28. },
  29. }).register('board');
  30. BlazeComponent.extendComponent({
  31. onCreated() {
  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. handle: '.js-swimlane-header',
  86. items: '.swimlane:not(.placeholder)',
  87. placeholder: 'swimlane placeholder',
  88. distance: 7,
  89. start(evt, ui) {
  90. const listDom = ui.placeholder.next('.js-swimlane');
  91. const parentOffset = ui.item.parent().offset();
  92. ui.placeholder.height(ui.helper.height());
  93. EscapeActions.executeUpTo('popup-close');
  94. listDom.addClass('moving-swimlane');
  95. boardComponent.setIsDragging(true);
  96. ui.placeholder.insertAfter(ui.placeholder.next());
  97. boardComponent.origPlaceholderIndex = ui.placeholder.index();
  98. // resize all swimlanes + headers to be a total of 150 px per row
  99. // this could be achieved by setIsDragging(true) but we want immediate
  100. // result
  101. ui.item.siblings('.js-swimlane').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 = ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  106. },
  107. beforeStop(evt, ui) {
  108. const parentOffset = ui.item.parent().offset();
  109. const siblings = ui.item.siblings('.js-swimlane');
  110. siblings.css('height', '');
  111. // compute the new scroll height after the resize and removal of
  112. // the placeholder
  113. const scrollTop = ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  114. // then reset the original view of the swimlane
  115. siblings.removeClass('moving-swimlane');
  116. // and apply the computed scrollheight
  117. ui.item.parent().get(0).scrollTop = scrollTop;
  118. },
  119. stop(evt, ui) {
  120. // To attribute the new index number, we need to get the DOM element
  121. // of the previous and the following card -- if any.
  122. const prevSwimlaneDom = ui.item.prevAll('.js-swimlane').get(0);
  123. const nextSwimlaneDom = ui.item.nextAll('.js-swimlane').get(0);
  124. const sortIndex = calculateIndex(prevSwimlaneDom, nextSwimlaneDom, 1);
  125. $swimlanesDom.sortable('cancel');
  126. const swimlaneDomElement = ui.item.get(0);
  127. const swimlane = Blaze.getData(swimlaneDomElement);
  128. Swimlanes.update(swimlane._id, {
  129. $set: {
  130. sort: sortIndex.base,
  131. },
  132. });
  133. boardComponent.setIsDragging(false);
  134. },
  135. sort(evt, ui) {
  136. // get the mouse position in the sortable
  137. const parentOffset = ui.item.parent().offset();
  138. const cursorY = evt.pageY - parentOffset.top + ui.item.parent().scrollTop();
  139. // compute the intended index of the placeholder (we need to skip the
  140. // slots between the headers and the list of cards)
  141. const newplaceholderIndex = Math.floor(cursorY / swimlaneWhileSortingHeight);
  142. let destPlaceholderIndex = (newplaceholderIndex + 1) * 2;
  143. // if we are scrolling far away from the bottom of the list
  144. if (destPlaceholderIndex >= ui.item.parent().get(0).childElementCount) {
  145. destPlaceholderIndex = ui.item.parent().get(0).childElementCount - 1;
  146. }
  147. // update the placeholder position in the DOM tree
  148. if (destPlaceholderIndex !== ui.placeholder.index()) {
  149. if (destPlaceholderIndex < boardComponent.origPlaceholderIndex) {
  150. ui.placeholder.insertBefore(ui.placeholder.siblings().slice(destPlaceholderIndex - 2, destPlaceholderIndex - 1));
  151. } else {
  152. ui.placeholder.insertAfter(ui.placeholder.siblings().slice(destPlaceholderIndex - 1, destPlaceholderIndex));
  153. }
  154. }
  155. },
  156. });
  157. // ugly touch event hotfix
  158. enableClickOnTouch('.js-swimlane:not(.placeholder)');
  159. function userIsMember() {
  160. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  161. }
  162. // If there is no data in the board (ie, no lists) we autofocus the list
  163. // creation form by clicking on the corresponding element.
  164. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  165. if (userIsMember() && currentBoard.lists().count() === 0) {
  166. boardComponent.openNewListForm();
  167. }
  168. },
  169. isViewSwimlanes() {
  170. const currentUser = Meteor.user();
  171. if (!currentUser) return false;
  172. return ((currentUser.profile || {}).boardView === 'board-view-swimlanes');
  173. },
  174. isViewLists() {
  175. const currentUser = Meteor.user();
  176. if (!currentUser) return true;
  177. return ((currentUser.profile || {}).boardView === 'board-view-lists');
  178. },
  179. isViewCalendar() {
  180. const currentUser = Meteor.user();
  181. if (!currentUser) return false;
  182. return ((currentUser.profile || {}).boardView === 'board-view-cal');
  183. },
  184. openNewListForm() {
  185. if (this.isViewSwimlanes()) {
  186. this.childComponents('swimlane')[0]
  187. .childComponents('addListAndSwimlaneForm')[0].open();
  188. } else if (this.isViewLists()) {
  189. this.childComponents('listsGroup')[0]
  190. .childComponents('addListForm')[0].open();
  191. }
  192. },
  193. events() {
  194. return [{
  195. // XXX The board-overlay div should probably be moved to the parent
  196. // component.
  197. 'mouseenter .board-overlay'() {
  198. if (this.mouseHasEnterCardDetails) {
  199. this.showOverlay.set(false);
  200. }
  201. },
  202. 'mouseup'() {
  203. if (this._isDragging) {
  204. this._isDragging = false;
  205. }
  206. },
  207. }];
  208. },
  209. // XXX Flow components allow us to avoid creating these two setter methods by
  210. // exposing a public API to modify the component state. We need to investigate
  211. // best practices here.
  212. setIsDragging(bool) {
  213. this.draggingActive.set(bool);
  214. },
  215. scrollLeft(position = 0) {
  216. const swimlanes = this.$('.js-swimlanes');
  217. swimlanes && swimlanes.animate({
  218. scrollLeft: position,
  219. });
  220. },
  221. scrollTop(position = 0) {
  222. const swimlanes = this.$('.js-swimlanes');
  223. swimlanes && swimlanes.animate({
  224. scrollTop: position,
  225. });
  226. },
  227. }).register('boardBody');
  228. BlazeComponent.extendComponent({
  229. onRendered() {
  230. this.autorun(function(){
  231. $('#calendar-view').fullCalendar('refetchEvents');
  232. });
  233. },
  234. calendarOptions() {
  235. return {
  236. id: 'calendar-view',
  237. defaultView: 'agendaDay',
  238. editable: true,
  239. timezone: 'local',
  240. header: {
  241. left: 'title today prev,next',
  242. center: 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,timelineMonth timelineYear',
  243. right: '',
  244. },
  245. // height: 'parent', nope, doesn't work as the parent might be small
  246. height: 'auto',
  247. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  248. navLinks: true,
  249. nowIndicator: true,
  250. businessHours: {
  251. // days of week. an array of zero-based day of week integers (0=Sunday)
  252. dow: [ 1, 2, 3, 4, 5 ], // Monday - Friday
  253. start: '8:00',
  254. end: '18:00',
  255. },
  256. locale: TAPi18n.getLanguage(),
  257. events(start, end, timezone, callback) {
  258. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  259. const events = [];
  260. currentBoard.cardsInInterval(start.toDate(), end.toDate()).forEach(function(card){
  261. events.push({
  262. id: card._id,
  263. title: card.title,
  264. start: card.startAt,
  265. end: card.endAt,
  266. allDay: Math.abs(card.endAt.getTime() - card.startAt.getTime()) / 1000 === 24*3600,
  267. url: FlowRouter.url('card', {
  268. boardId: currentBoard._id,
  269. slug: currentBoard.slug,
  270. cardId: card._id,
  271. }),
  272. });
  273. });
  274. callback(events);
  275. },
  276. eventResize(event, delta, revertFunc) {
  277. let isOk = false;
  278. const card = Cards.findOne(event.id);
  279. if (card) {
  280. card.setEnd(event.end.toDate());
  281. isOk = true;
  282. }
  283. if (!isOk) {
  284. revertFunc();
  285. }
  286. },
  287. eventDrop(event, delta, revertFunc) {
  288. let isOk = false;
  289. const card = Cards.findOne(event.id);
  290. if (card) {
  291. // TODO: add a flag for allDay events
  292. if (!event.allDay) {
  293. card.setStart(event.start.toDate());
  294. card.setEnd(event.end.toDate());
  295. isOk = true;
  296. }
  297. }
  298. if (!isOk) {
  299. revertFunc();
  300. }
  301. },
  302. };
  303. },
  304. isViewCalendar() {
  305. const currentUser = Meteor.user();
  306. if (!currentUser) return false;
  307. return ((currentUser.profile || {}).boardView === 'board-view-cal');
  308. },
  309. }).register('calendarView');