boardBody.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. const subManager = new SubsManager();
  2. const { calculateIndex, enableClickOnTouch } = Utils;
  3. BlazeComponent.extendComponent({
  4. onCreated() {
  5. this.isBoardReady = new ReactiveVar(false);
  6. // The pattern we use to manually handle data loading is described here:
  7. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  8. // XXX The boardId should be readed from some sort the component "props",
  9. // unfortunatly, Blaze doesn't have this notion.
  10. this.autorun(() => {
  11. const currentBoardId = Session.get('currentBoard');
  12. if (!currentBoardId)
  13. return;
  14. const handle = subManager.subscribe('board', currentBoardId);
  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. }).register('board');
  26. BlazeComponent.extendComponent({
  27. onCreated() {
  28. this.showOverlay = new ReactiveVar(false);
  29. this.draggingActive = new ReactiveVar(false);
  30. this._isDragging = false;
  31. // Used to set the overlay
  32. this.mouseHasEnterCardDetails = false;
  33. },
  34. onRendered() {
  35. const boardComponent = this;
  36. const $swimlanesDom = boardComponent.$('.js-swimlanes');
  37. $swimlanesDom.sortable({
  38. tolerance: 'pointer',
  39. appendTo: '.board-canvas',
  40. helper: 'clone',
  41. handle: '.js-swimlane-header',
  42. items: '.js-swimlane:not(.placeholder)',
  43. placeholder: 'swimlane placeholder',
  44. distance: 7,
  45. start(evt, ui) {
  46. ui.placeholder.height(ui.helper.height());
  47. EscapeActions.executeUpTo('popup-close');
  48. boardComponent.setIsDragging(true);
  49. },
  50. stop(evt, ui) {
  51. // To attribute the new index number, we need to get the DOM element
  52. // of the previous and the following card -- if any.
  53. const prevSwimlaneDom = ui.item.prev('.js-swimlane').get(0);
  54. const nextSwimlaneDom = ui.item.next('.js-swimlane').get(0);
  55. const sortIndex = calculateIndex(prevSwimlaneDom, nextSwimlaneDom, 1);
  56. $swimlanesDom.sortable('cancel');
  57. const swimlaneDomElement = ui.item.get(0);
  58. const swimlane = Blaze.getData(swimlaneDomElement);
  59. Swimlanes.update(swimlane._id, {
  60. $set: {
  61. sort: sortIndex.base,
  62. },
  63. });
  64. boardComponent.setIsDragging(false);
  65. },
  66. });
  67. // ugly touch event hotfix
  68. enableClickOnTouch('.js-swimlane:not(.placeholder)');
  69. function userIsMember() {
  70. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  71. }
  72. // If there is no data in the board (ie, no lists) we autofocus the list
  73. // creation form by clicking on the corresponding element.
  74. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  75. if (userIsMember() && currentBoard.lists().count() === 0) {
  76. boardComponent.openNewListForm();
  77. }
  78. },
  79. isViewSwimlanes() {
  80. const currentUser = Meteor.user();
  81. if (!currentUser) return false;
  82. return (currentUser.profile.boardView === 'board-view-swimlanes');
  83. },
  84. isViewLists() {
  85. const currentUser = Meteor.user();
  86. if (!currentUser) return true;
  87. return (currentUser.profile.boardView === 'board-view-lists');
  88. },
  89. isViewCalendar() {
  90. const currentUser = Meteor.user();
  91. if (!currentUser) return true;
  92. return (currentUser.profile.boardView === 'board-view-cal');
  93. },
  94. openNewListForm() {
  95. if (this.isViewSwimlanes()) {
  96. this.childComponents('swimlane')[0]
  97. .childComponents('addListAndSwimlaneForm')[0].open();
  98. } else if (this.isViewLists()) {
  99. this.childComponents('listsGroup')[0]
  100. .childComponents('addListForm')[0].open();
  101. }
  102. },
  103. events() {
  104. return [{
  105. // XXX The board-overlay div should probably be moved to the parent
  106. // component.
  107. 'mouseenter .board-overlay'() {
  108. if (this.mouseHasEnterCardDetails) {
  109. this.showOverlay.set(false);
  110. }
  111. },
  112. 'mouseup'() {
  113. if (this._isDragging) {
  114. this._isDragging = false;
  115. }
  116. },
  117. }];
  118. },
  119. // XXX Flow components allow us to avoid creating these two setter methods by
  120. // exposing a public API to modify the component state. We need to investigate
  121. // best practices here.
  122. setIsDragging(bool) {
  123. this.draggingActive.set(bool);
  124. },
  125. scrollLeft(position = 0) {
  126. const swimlanes = this.$('.js-swimlanes');
  127. swimlanes && swimlanes.animate({
  128. scrollLeft: position,
  129. });
  130. },
  131. scrollTop(position = 0) {
  132. const swimlanes = this.$('.js-swimlanes');
  133. swimlanes && swimlanes.animate({
  134. scrollTop: position,
  135. });
  136. },
  137. }).register('boardBody');
  138. BlazeComponent.extendComponent({
  139. onRendered() {
  140. this.autorun(function(){
  141. $('#calendar-view').fullCalendar('refetchEvents');
  142. });
  143. },
  144. calendarOptions() {
  145. return {
  146. id: 'calendar-view',
  147. defaultView: 'agendaDay',
  148. editable: true,
  149. timezone: 'local',
  150. header: {
  151. left: 'title today prev,next',
  152. center: 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,timelineMonth timelineYear',
  153. right: '',
  154. },
  155. // height: 'parent', nope, doesn't work as the parent might be small
  156. height: 'auto',
  157. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  158. navLinks: true,
  159. nowIndicator: true,
  160. businessHours: {
  161. // days of week. an array of zero-based day of week integers (0=Sunday)
  162. dow: [ 1, 2, 3, 4, 5 ], // Monday - Friday
  163. start: '8:00',
  164. end: '18:00',
  165. },
  166. locale: TAPi18n.getLanguage(),
  167. events(start, end, timezone, callback) {
  168. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  169. const events = [];
  170. currentBoard.cardsInInterval(start.toDate(), end.toDate()).forEach(function(card){
  171. events.push({
  172. id: card._id,
  173. title: card.title,
  174. start: card.startAt,
  175. end: card.endAt,
  176. allDay: Math.abs(card.endAt.getTime() - card.startAt.getTime()) / 1000 === 24*3600,
  177. url: FlowRouter.url('card', {
  178. boardId: currentBoard._id,
  179. slug: currentBoard.slug,
  180. cardId: card._id,
  181. }),
  182. });
  183. });
  184. callback(events);
  185. },
  186. eventResize(event, delta, revertFunc) {
  187. let isOk = false;
  188. const card = Cards.findOne(event.id);
  189. if (card) {
  190. card.setEnd(event.end.toDate());
  191. isOk = true;
  192. }
  193. if (!isOk) {
  194. revertFunc();
  195. }
  196. },
  197. eventDrop(event, delta, revertFunc) {
  198. let isOk = false;
  199. const card = Cards.findOne(event.id);
  200. if (card) {
  201. // TODO: add a flag for allDay events
  202. if (!event.allDay) {
  203. card.setStart(event.start.toDate());
  204. card.setEnd(event.end.toDate());
  205. isOk = true;
  206. }
  207. }
  208. if (!isOk) {
  209. revertFunc();
  210. }
  211. },
  212. };
  213. },
  214. }).register('calendarView');