boardBody.js 7.9 KB

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