boardBody.js 11 KB

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