boardBody.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import { TAPi18n } from '/imports/i18n';
  2. const subManager = new SubsManager();
  3. const { calculateIndex } = Utils;
  4. const swimlaneWhileSortingHeight = 150;
  5. BlazeComponent.extendComponent({
  6. onCreated() {
  7. this.isBoardReady = new ReactiveVar(false);
  8. // The pattern we use to manually handle data loading is described here:
  9. // https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
  10. // XXX The boardId should be readed from some sort the component "props",
  11. // unfortunatly, Blaze doesn't have this notion.
  12. this.autorun(() => {
  13. const currentBoardId = Session.get('currentBoard');
  14. if (!currentBoardId) 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() && Utils.getCurrentCardId(true);
  25. },
  26. goHome() {
  27. FlowRouter.go('home');
  28. },
  29. }).register('board');
  30. BlazeComponent.extendComponent({
  31. onCreated() {
  32. Meteor.subscribe('tableVisibilityModeSettings');
  33. this.showOverlay = new ReactiveVar(false);
  34. this.draggingActive = new ReactiveVar(false);
  35. this._isDragging = false;
  36. // Used to set the overlay
  37. this.mouseHasEnterCardDetails = false;
  38. // fix swimlanes sort field if there are null values
  39. const currentBoardData = Boards.findOne(Session.get('currentBoard'));
  40. const nullSortSwimlanes = currentBoardData.nullSortSwimlanes();
  41. if (nullSortSwimlanes.count() > 0) {
  42. const swimlanes = currentBoardData.swimlanes();
  43. let count = 0;
  44. swimlanes.forEach(s => {
  45. Swimlanes.update(s._id, {
  46. $set: {
  47. sort: count,
  48. },
  49. });
  50. count += 1;
  51. });
  52. }
  53. // fix lists sort field if there are null values
  54. const nullSortLists = currentBoardData.nullSortLists();
  55. if (nullSortLists.count() > 0) {
  56. const lists = currentBoardData.lists();
  57. let count = 0;
  58. lists.forEach(l => {
  59. Lists.update(l._id, {
  60. $set: {
  61. sort: count,
  62. },
  63. });
  64. count += 1;
  65. });
  66. }
  67. },
  68. onRendered() {
  69. const boardComponent = this;
  70. const $swimlanesDom = boardComponent.$('.js-swimlanes');
  71. $swimlanesDom.sortable({
  72. tolerance: 'pointer',
  73. appendTo: '.board-canvas',
  74. helper(evt, item) {
  75. const helper = $(`<div class="swimlane"
  76. style="flex-direction: column;
  77. height: ${swimlaneWhileSortingHeight}px;
  78. width: $(boardComponent.width)px;
  79. overflow: hidden;"/>`);
  80. helper.append(item.clone());
  81. // Also grab the list of lists of cards
  82. const list = item.next();
  83. helper.append(list.clone());
  84. return helper;
  85. },
  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
  102. .siblings('.js-swimlane')
  103. .css('height', `${swimlaneWhileSortingHeight - 26}px`);
  104. // set the new scroll height after the resize and insertion of
  105. // the placeholder. We want the element under the cursor to stay
  106. // at the same place on the screen
  107. ui.item.parent().get(0).scrollTop =
  108. ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  109. },
  110. beforeStop(evt, ui) {
  111. const parentOffset = ui.item.parent().offset();
  112. const siblings = ui.item.siblings('.js-swimlane');
  113. siblings.css('height', '');
  114. // compute the new scroll height after the resize and removal of
  115. // the placeholder
  116. const scrollTop =
  117. ui.placeholder.get(0).offsetTop + parentOffset.top - evt.pageY;
  118. // then reset the original view of the swimlane
  119. siblings.removeClass('moving-swimlane');
  120. // and apply the computed scrollheight
  121. ui.item.parent().get(0).scrollTop = scrollTop;
  122. },
  123. stop(evt, ui) {
  124. // To attribute the new index number, we need to get the DOM element
  125. // of the previous and the following card -- if any.
  126. const prevSwimlaneDom = ui.item.prevAll('.js-swimlane').get(0);
  127. const nextSwimlaneDom = ui.item.nextAll('.js-swimlane').get(0);
  128. const sortIndex = calculateIndex(prevSwimlaneDom, nextSwimlaneDom, 1);
  129. $swimlanesDom.sortable('cancel');
  130. const swimlaneDomElement = ui.item.get(0);
  131. const swimlane = Blaze.getData(swimlaneDomElement);
  132. Swimlanes.update(swimlane._id, {
  133. $set: {
  134. sort: sortIndex.base,
  135. },
  136. });
  137. boardComponent.setIsDragging(false);
  138. },
  139. sort(evt, ui) {
  140. // get the mouse position in the sortable
  141. const parentOffset = ui.item.parent().offset();
  142. const cursorY =
  143. evt.pageY - parentOffset.top + ui.item.parent().scrollTop();
  144. // compute the intended index of the placeholder (we need to skip the
  145. // slots between the headers and the list of cards)
  146. const newplaceholderIndex = Math.floor(
  147. cursorY / swimlaneWhileSortingHeight,
  148. );
  149. let destPlaceholderIndex = (newplaceholderIndex + 1) * 2;
  150. // if we are scrolling far away from the bottom of the list
  151. if (destPlaceholderIndex >= ui.item.parent().get(0).childElementCount) {
  152. destPlaceholderIndex = ui.item.parent().get(0).childElementCount - 1;
  153. }
  154. // update the placeholder position in the DOM tree
  155. if (destPlaceholderIndex !== ui.placeholder.index()) {
  156. if (destPlaceholderIndex < boardComponent.origPlaceholderIndex) {
  157. ui.placeholder.insertBefore(
  158. ui.placeholder
  159. .siblings()
  160. .slice(destPlaceholderIndex - 2, destPlaceholderIndex - 1),
  161. );
  162. } else {
  163. ui.placeholder.insertAfter(
  164. ui.placeholder
  165. .siblings()
  166. .slice(destPlaceholderIndex - 1, destPlaceholderIndex),
  167. );
  168. }
  169. }
  170. },
  171. });
  172. this.autorun(() => {
  173. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  174. $swimlanesDom.sortable({
  175. handle: '.js-swimlane-header-handle',
  176. });
  177. } else {
  178. $swimlanesDom.sortable({
  179. handle: '.swimlane-header',
  180. });
  181. }
  182. // Disable drag-dropping if the current user is not a board member
  183. //$swimlanesDom.sortable('option', 'disabled', !userIsMember());
  184. $swimlanesDom.sortable(
  185. 'option',
  186. 'disabled',
  187. !Meteor.user() || !Meteor.user().isBoardAdmin(),
  188. );
  189. });
  190. function userIsMember() {
  191. return (
  192. Meteor.user() &&
  193. Meteor.user().isBoardMember() &&
  194. !Meteor.user().isCommentOnly()
  195. );
  196. }
  197. // If there is no data in the board (ie, no lists) we autofocus the list
  198. // creation form by clicking on the corresponding element.
  199. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  200. if (userIsMember() && currentBoard.lists().count() === 0) {
  201. boardComponent.openNewListForm();
  202. }
  203. },
  204. notDisplayThisBoard() {
  205. let allowPrivateVisibilityOnly = TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly');
  206. let currentBoard = Boards.findOne(Session.get('currentBoard'));
  207. if (allowPrivateVisibilityOnly !== undefined && allowPrivateVisibilityOnly.booleanValue && currentBoard.permission == 'public') {
  208. return true;
  209. }
  210. return false;
  211. },
  212. isViewSwimlanes() {
  213. currentUser = Meteor.user();
  214. if (currentUser) {
  215. return (currentUser.profile || {}).boardView === 'board-view-swimlanes';
  216. } else {
  217. return (
  218. window.localStorage.getItem('boardView') === 'board-view-swimlanes'
  219. );
  220. }
  221. },
  222. isViewLists() {
  223. currentUser = Meteor.user();
  224. if (currentUser) {
  225. return (currentUser.profile || {}).boardView === 'board-view-lists';
  226. } else {
  227. return window.localStorage.getItem('boardView') === 'board-view-lists';
  228. }
  229. },
  230. isViewCalendar() {
  231. currentUser = Meteor.user();
  232. if (currentUser) {
  233. return (currentUser.profile || {}).boardView === 'board-view-cal';
  234. } else {
  235. return window.localStorage.getItem('boardView') === 'board-view-cal';
  236. }
  237. },
  238. openNewListForm() {
  239. if (this.isViewSwimlanes()) {
  240. // The form had been removed in 416b17062e57f215206e93a85b02ef9eb1ab4902
  241. // this.childComponents('swimlane')[0]
  242. // .childComponents('addListAndSwimlaneForm')[0]
  243. // .open();
  244. } else if (this.isViewLists()) {
  245. this.childComponents('listsGroup')[0]
  246. .childComponents('addListForm')[0]
  247. .open();
  248. }
  249. },
  250. events() {
  251. return [
  252. {
  253. // XXX The board-overlay div should probably be moved to the parent
  254. // component.
  255. mouseup() {
  256. if (this._isDragging) {
  257. this._isDragging = false;
  258. }
  259. },
  260. },
  261. ];
  262. },
  263. // XXX Flow components allow us to avoid creating these two setter methods by
  264. // exposing a public API to modify the component state. We need to investigate
  265. // best practices here.
  266. setIsDragging(bool) {
  267. this.draggingActive.set(bool);
  268. },
  269. scrollLeft(position = 0) {
  270. const swimlanes = this.$('.js-swimlanes');
  271. swimlanes &&
  272. swimlanes.animate({
  273. scrollLeft: position,
  274. });
  275. },
  276. scrollTop(position = 0) {
  277. const swimlanes = this.$('.js-swimlanes');
  278. swimlanes &&
  279. swimlanes.animate({
  280. scrollTop: position,
  281. });
  282. },
  283. }).register('boardBody');
  284. BlazeComponent.extendComponent({
  285. onRendered() {
  286. this.autorun(function () {
  287. $('#calendar-view').fullCalendar('refetchEvents');
  288. });
  289. },
  290. calendarOptions() {
  291. return {
  292. id: 'calendar-view',
  293. defaultView: 'agendaDay',
  294. editable: true,
  295. selecatble: true,
  296. timezone: 'local',
  297. weekNumbers: true,
  298. header: {
  299. left: 'title today prev,next',
  300. center:
  301. 'agendaDay,listDay,timelineDay agendaWeek,listWeek,timelineWeek month,listMonth',
  302. right: '',
  303. },
  304. // height: 'parent', nope, doesn't work as the parent might be small
  305. height: 'auto',
  306. /* TODO: lists as resources: https://fullcalendar.io/docs/vertical-resource-view */
  307. navLinks: true,
  308. nowIndicator: true,
  309. businessHours: {
  310. // days of week. an array of zero-based day of week integers (0=Sunday)
  311. dow: [1, 2, 3, 4, 5], // Monday - Friday
  312. start: '8:00',
  313. end: '18:00',
  314. },
  315. locale: TAPi18n.getLanguage(),
  316. events(start, end, timezone, callback) {
  317. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  318. const events = [];
  319. const pushEvent = function (card, title, start, end, extraCls) {
  320. start = start || card.startAt;
  321. end = end || card.endAt;
  322. title = title || card.title;
  323. const className =
  324. (extraCls ? `${extraCls} ` : '') +
  325. (card.color ? `calendar-event-${card.color}` : '');
  326. events.push({
  327. id: card._id,
  328. title,
  329. start,
  330. end: end || card.endAt,
  331. allDay:
  332. Math.abs(end.getTime() - start.getTime()) / 1000 === 24 * 3600,
  333. url: FlowRouter.path('card', {
  334. boardId: currentBoard._id,
  335. slug: currentBoard.slug,
  336. cardId: card._id,
  337. }),
  338. className,
  339. });
  340. };
  341. currentBoard
  342. .cardsInInterval(start.toDate(), end.toDate())
  343. .forEach(function (card) {
  344. pushEvent(card);
  345. });
  346. currentBoard
  347. .cardsDueInBetween(start.toDate(), end.toDate())
  348. .forEach(function (card) {
  349. pushEvent(
  350. card,
  351. `${card.title} ${TAPi18n.__('card-due')}`,
  352. card.dueAt,
  353. new Date(card.dueAt.getTime() + 36e5),
  354. );
  355. });
  356. events.sort(function (first, second) {
  357. return first.id > second.id ? 1 : -1;
  358. });
  359. callback(events);
  360. },
  361. eventResize(event, delta, revertFunc) {
  362. let isOk = false;
  363. const card = Cards.findOne(event.id);
  364. if (card) {
  365. card.setEnd(event.end.toDate());
  366. isOk = true;
  367. }
  368. if (!isOk) {
  369. revertFunc();
  370. }
  371. },
  372. eventDrop(event, delta, revertFunc) {
  373. let isOk = false;
  374. const card = Cards.findOne(event.id);
  375. if (card) {
  376. // TODO: add a flag for allDay events
  377. if (!event.allDay) {
  378. // https://github.com/wekan/wekan/issues/2917#issuecomment-1236753962
  379. //card.setStart(event.start.toDate());
  380. //card.setEnd(event.end.toDate());
  381. card.setDue(event.start.toDate());
  382. isOk = true;
  383. }
  384. }
  385. if (!isOk) {
  386. revertFunc();
  387. }
  388. },
  389. select: function(startDate) {
  390. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  391. const currentUser = Meteor.user();
  392. const $modal = $(`
  393. <div class="modal fade" tabindex="-1" role="dialog">
  394. <div class="modal-dialog justify-content-center align-items-center" role="document">
  395. <div class="modal-content">
  396. <div class="modal-header">
  397. <h5 class="modal-title">Card Title</h5>
  398. </div>
  399. <div class="modal-body text-center">
  400. <input type="text" class="form-control" id="card-title-input" placeholder="Card Title">
  401. </div>
  402. <div class="modal-footer">
  403. <button type="button" class="btn btn-primary" id="create-card-button">Create Card</button>
  404. <button type="button" class="btn btn-secondary" data-dismiss="modal"Cancel</button>
  405. </div>
  406. </div>
  407. </div>
  408. </div>
  409. `);
  410. $modal.modal('show');
  411. $modal.find('#create-card-button').click(function() {
  412. const myTitle = $modal.find('#card-title-input').val();
  413. if (myTitle) {
  414. const firstList = currentBoard.draggableLists().fetch()[0];
  415. const firstSwimlane = currentBoard.swimlanes().fetch()[0];
  416. Meteor.call('createCardWithDueDate', currentBoard._id, firstList._id, myTitle, startDate.toDate(), firstSwimlane._id, function(error, result) {
  417. if (error) {
  418. console.log(error);
  419. } else {
  420. console.log("Card Created", result);
  421. }
  422. });
  423. $modal.modal('hide');
  424. }
  425. });
  426. },
  427. };
  428. },
  429. isViewCalendar() {
  430. currentUser = Meteor.user();
  431. if (currentUser) {
  432. return (currentUser.profile || {}).boardView === 'board-view-cal';
  433. } else {
  434. return window.localStorage.getItem('boardView') === 'board-view-cal';
  435. }
  436. },
  437. }).register('calendarView');