2
0

cardDetails.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. const subManager = new SubsManager();
  2. const { calculateIndexData } = Utils;
  3. BlazeComponent.extendComponent({
  4. mixins() {
  5. return [Mixins.InfiniteScrolling, Mixins.PerfectScrollbar];
  6. },
  7. calculateNextPeak() {
  8. const cardElement = this.find('.js-card-details');
  9. if (cardElement) {
  10. const altitude = cardElement.scrollHeight;
  11. this.callFirstWith(this, 'setNextPeak', altitude);
  12. }
  13. },
  14. reachNextPeak() {
  15. const activitiesComponent = this.childComponents('activities')[0];
  16. activitiesComponent.loadNextPage();
  17. },
  18. onCreated() {
  19. this.isLoaded = new ReactiveVar(false);
  20. const boardBody = this.parentComponent().parentComponent();
  21. //in Miniview parent is Board, not BoardBody.
  22. if (boardBody !== null){
  23. boardBody.showOverlay.set(true);
  24. boardBody.mouseHasEnterCardDetails = false;
  25. }
  26. this.calculateNextPeak();
  27. Meteor.subscribe('unsaved-edits');
  28. },
  29. isWatching() {
  30. const card = this.currentData();
  31. return card.findWatcher(Meteor.userId());
  32. },
  33. hiddenSystemMessages() {
  34. return Meteor.user().hasHiddenSystemMessages();
  35. },
  36. canModifyCard() {
  37. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  38. },
  39. scrollParentContainer() {
  40. const cardPanelWidth = 510;
  41. const bodyBoardComponent = this.parentComponent().parentComponent();
  42. //On Mobile View Parent is Board, Not Board Body. I cant see how this funciton should work then.
  43. if (bodyBoardComponent === null) return;
  44. const $cardView = this.$(this.firstNode());
  45. const $cardContainer = bodyBoardComponent.$('.js-swimlanes');
  46. const cardContainerScroll = $cardContainer.scrollLeft();
  47. const cardContainerWidth = $cardContainer.width();
  48. const cardViewStart = $cardView.offset().left;
  49. const cardViewEnd = cardViewStart + cardPanelWidth;
  50. let offset = false;
  51. if (cardViewStart < 0) {
  52. offset = cardViewStart;
  53. } else if (cardViewEnd > cardContainerWidth) {
  54. offset = cardViewEnd - cardContainerWidth;
  55. }
  56. if (offset) {
  57. bodyBoardComponent.scrollLeft(cardContainerScroll + offset);
  58. }
  59. },
  60. onRendered() {
  61. if (!Utils.isMiniScreen()) this.scrollParentContainer();
  62. const $checklistsDom = this.$('.card-checklist-items');
  63. $checklistsDom.sortable({
  64. tolerance: 'pointer',
  65. helper: 'clone',
  66. handle: '.checklist-title',
  67. items: '.js-checklist',
  68. placeholder: 'checklist placeholder',
  69. distance: 7,
  70. start(evt, ui) {
  71. ui.placeholder.height(ui.helper.height());
  72. EscapeActions.executeUpTo('popup-close');
  73. },
  74. stop(evt, ui) {
  75. let prevChecklist = ui.item.prev('.js-checklist').get(0);
  76. if (prevChecklist) {
  77. prevChecklist = Blaze.getData(prevChecklist).checklist;
  78. }
  79. let nextChecklist = ui.item.next('.js-checklist').get(0);
  80. if (nextChecklist) {
  81. nextChecklist = Blaze.getData(nextChecklist).checklist;
  82. }
  83. const sortIndex = calculateIndexData(prevChecklist, nextChecklist, 1);
  84. $checklistsDom.sortable('cancel');
  85. const checklist = Blaze.getData(ui.item.get(0)).checklist;
  86. Checklists.update(checklist._id, {
  87. $set: {
  88. sort: sortIndex.base,
  89. },
  90. });
  91. },
  92. });
  93. function userIsMember() {
  94. return Meteor.user() && Meteor.user().isBoardMember();
  95. }
  96. // Disable sorting if the current user is not a board member
  97. this.autorun(() => {
  98. if ($checklistsDom.data('sortable')) {
  99. $checklistsDom.sortable('option', 'disabled', !userIsMember());
  100. }
  101. });
  102. },
  103. onDestroyed() {
  104. const parentComponent = this.parentComponent().parentComponent();
  105. //on mobile view parent is Board, not board body.
  106. if (parentComponent === null) return;
  107. parentComponent.showOverlay.set(false);
  108. },
  109. events() {
  110. const events = {
  111. [`${CSSEvents.transitionend} .js-card-details`]() {
  112. this.isLoaded.set(true);
  113. },
  114. [`${CSSEvents.animationend} .js-card-details`]() {
  115. this.isLoaded.set(true);
  116. },
  117. };
  118. return [{
  119. ...events,
  120. 'click .js-close-card-details' () {
  121. Utils.goBoardId(this.data().boardId);
  122. },
  123. 'click .js-open-card-details-menu': Popup.open('cardDetailsActions'),
  124. 'submit .js-card-description' (evt) {
  125. evt.preventDefault();
  126. const description = this.currentComponent().getValue();
  127. this.data().setDescription(description);
  128. },
  129. 'submit .js-card-details-title' (evt) {
  130. evt.preventDefault();
  131. const title = this.currentComponent().getValue().trim();
  132. if (title) {
  133. this.data().setTitle(title);
  134. }
  135. },
  136. 'submit .js-card-details-assigner'(evt) {
  137. evt.preventDefault();
  138. const assigner = this.currentComponent().getValue().trim();
  139. if (assigner) {
  140. this.data().setAssignedBy(assigner);
  141. }
  142. },
  143. 'submit .js-card-details-requester'(evt) {
  144. evt.preventDefault();
  145. const requester = this.currentComponent().getValue().trim();
  146. if (requester) {
  147. this.data().setRequestedBy(requester);
  148. }
  149. },
  150. 'click .js-member': Popup.open('cardMember'),
  151. 'click .js-add-members': Popup.open('cardMembers'),
  152. 'click .js-add-labels': Popup.open('cardLabels'),
  153. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  154. 'click .js-start-date': Popup.open('editCardStartDate'),
  155. 'click .js-due-date': Popup.open('editCardDueDate'),
  156. 'click .js-end-date': Popup.open('editCardEndDate'),
  157. 'mouseenter .js-card-details' () {
  158. const parentComponent = this.parentComponent().parentComponent();
  159. //on mobile view parent is Board, not BoardBody.
  160. if (parentComponent === null) return;
  161. parentComponent.showOverlay.set(true);
  162. parentComponent.mouseHasEnterCardDetails = true;
  163. },
  164. 'click #toggleButton'() {
  165. Meteor.call('toggleSystemMessages');
  166. },
  167. }];
  168. },
  169. }).register('cardDetails');
  170. // We extends the normal InlinedForm component to support UnsavedEdits draft
  171. // feature.
  172. (class extends InlinedForm {
  173. _getUnsavedEditKey() {
  174. return {
  175. fieldName: 'cardDescription',
  176. // XXX Recovering the currentCard identifier form a session variable is
  177. // fragile because this variable may change for instance if the route
  178. // change. We should use some component props instead.
  179. docId: Session.get('currentCard'),
  180. };
  181. }
  182. close(isReset = false) {
  183. if (this.isOpen.get() && !isReset) {
  184. const draft = this.getValue().trim();
  185. if (draft !== Cards.findOne(Session.get('currentCard')).description) {
  186. UnsavedEdits.set(this._getUnsavedEditKey(), this.getValue());
  187. }
  188. }
  189. super.close();
  190. }
  191. reset() {
  192. UnsavedEdits.reset(this._getUnsavedEditKey());
  193. this.close(true);
  194. }
  195. events() {
  196. const parentEvents = InlinedForm.prototype.events()[0];
  197. return [{
  198. ...parentEvents,
  199. 'click .js-close-inlined-form': this.reset,
  200. }];
  201. }
  202. }).register('inlinedCardDescription');
  203. Template.cardDetailsActionsPopup.helpers({
  204. isWatching() {
  205. return this.findWatcher(Meteor.userId());
  206. },
  207. canModifyCard() {
  208. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  209. },
  210. });
  211. Template.cardDetailsActionsPopup.events({
  212. 'click .js-members': Popup.open('cardMembers'),
  213. 'click .js-labels': Popup.open('cardLabels'),
  214. 'click .js-attachments': Popup.open('cardAttachments'),
  215. 'click .js-custom-fields': Popup.open('cardCustomFields'),
  216. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  217. 'click .js-start-date': Popup.open('editCardStartDate'),
  218. 'click .js-due-date': Popup.open('editCardDueDate'),
  219. 'click .js-end-date': Popup.open('editCardEndDate'),
  220. 'click .js-spent-time': Popup.open('editCardSpentTime'),
  221. 'click .js-move-card': Popup.open('moveCard'),
  222. 'click .js-copy-card': Popup.open('copyCard'),
  223. 'click .js-copy-checklist-cards': Popup.open('copyChecklistToManyCards'),
  224. 'click .js-move-card-to-top' (evt) {
  225. evt.preventDefault();
  226. const minOrder = _.min(this.list().cards(this.swimlaneId).map((c) => c.sort));
  227. this.move(this.swimlaneId, this.listId, minOrder - 1);
  228. },
  229. 'click .js-move-card-to-bottom' (evt) {
  230. evt.preventDefault();
  231. const maxOrder = _.max(this.list().cards(this.swimlaneId).map((c) => c.sort));
  232. this.move(this.swimlaneId, this.listId, maxOrder + 1);
  233. },
  234. 'click .js-archive' (evt) {
  235. evt.preventDefault();
  236. this.archive();
  237. Popup.close();
  238. },
  239. 'click .js-more': Popup.open('cardMore'),
  240. 'click .js-toggle-watch-card' () {
  241. const currentCard = this;
  242. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  243. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  244. if (!err && ret) Popup.close();
  245. });
  246. },
  247. });
  248. Template.editCardTitleForm.onRendered(function () {
  249. autosize(this.$('.js-edit-card-title'));
  250. });
  251. Template.editCardTitleForm.events({
  252. 'keydown .js-edit-card-title' (evt) {
  253. // If enter key was pressed, submit the data
  254. // Unless the shift key is also being pressed
  255. if (evt.keyCode === 13 && !evt.shiftKey) {
  256. $('.js-submit-edit-card-title-form').click();
  257. }
  258. },
  259. });
  260. Template.editCardRequesterForm.onRendered(function() {
  261. autosize(this.$('.js-edit-card-requester'));
  262. });
  263. Template.editCardRequesterForm.events({
  264. 'keydown .js-edit-card-requester'(evt) {
  265. // If enter key was pressed, submit the data
  266. if (evt.keyCode === 13) {
  267. $('.js-submit-edit-card-requester-form').click();
  268. }
  269. },
  270. });
  271. Template.editCardAssignerForm.onRendered(function() {
  272. autosize(this.$('.js-edit-card-assigner'));
  273. });
  274. Template.editCardAssignerForm.events({
  275. 'keydown .js-edit-card-assigner'(evt) {
  276. // If enter key was pressed, submit the data
  277. if (evt.keyCode === 13) {
  278. $('.js-submit-edit-card-assigner-form').click();
  279. }
  280. },
  281. });
  282. Template.moveCardPopup.events({
  283. 'click .js-done' () {
  284. // XXX We should *not* get the currentCard from the global state, but
  285. // instead from a “component” state.
  286. const card = Cards.findOne(Session.get('currentCard'));
  287. const lSelect = $('.js-select-lists')[0];
  288. const newListId = lSelect.options[lSelect.selectedIndex].value;
  289. const slSelect = $('.js-select-swimlanes')[0];
  290. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  291. card.move(card.swimlaneId, newListId, 0);
  292. Popup.close();
  293. },
  294. });
  295. BlazeComponent.extendComponent({
  296. onCreated() {
  297. subManager.subscribe('board', Session.get('currentBoard'));
  298. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  299. },
  300. boards() {
  301. const boards = Boards.find({
  302. archived: false,
  303. 'members.userId': Meteor.userId(),
  304. }, {
  305. sort: ['title'],
  306. });
  307. return boards;
  308. },
  309. swimlanes() {
  310. const board = Boards.findOne(this.selectedBoardId.get());
  311. return board.swimlanes();
  312. },
  313. aBoardLists() {
  314. const board = Boards.findOne(this.selectedBoardId.get());
  315. return board.lists();
  316. },
  317. events() {
  318. return [{
  319. 'change .js-select-boards'(evt) {
  320. this.selectedBoardId.set($(evt.currentTarget).val());
  321. subManager.subscribe('board', this.selectedBoardId.get());
  322. },
  323. }];
  324. },
  325. }).register('boardsAndLists');
  326. Template.copyCardPopup.events({
  327. 'click .js-done'() {
  328. const card = Cards.findOne(Session.get('currentCard'));
  329. const oldId = card._id;
  330. card._id = null;
  331. const lSelect = $('.js-select-lists')[0];
  332. card.listId = lSelect.options[lSelect.selectedIndex].value;
  333. const slSelect = $('.js-select-swimlanes')[0];
  334. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  335. const bSelect = $('.js-select-boards')[0];
  336. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  337. const textarea = $('#copy-card-title');
  338. const title = textarea.val().trim();
  339. // insert new card to the bottom of new list
  340. card.sort = Lists.findOne(card.listId).cards().count();
  341. if (title) {
  342. card.title = title;
  343. card.coverId = '';
  344. const _id = Cards.insert(card);
  345. // In case the filter is active we need to add the newly inserted card in
  346. // the list of exceptions -- cards that are not filtered. Otherwise the
  347. // card will disappear instantly.
  348. // See https://github.com/wekan/wekan/issues/80
  349. Filter.addException(_id);
  350. // copy checklists
  351. let cursor = Checklists.find({cardId: oldId});
  352. cursor.forEach(function() {
  353. 'use strict';
  354. const checklist = arguments[0];
  355. const checklistId = checklist._id;
  356. checklist.cardId = _id;
  357. checklist._id = null;
  358. const newChecklistId = Checklists.insert(checklist);
  359. ChecklistItems.find({checklistId}).forEach(function(item) {
  360. item._id = null;
  361. item.checklistId = newChecklistId;
  362. item.cardId = _id;
  363. ChecklistItems.insert(item);
  364. });
  365. });
  366. // copy card comments
  367. cursor = CardComments.find({cardId: oldId});
  368. cursor.forEach(function () {
  369. 'use strict';
  370. const comment = arguments[0];
  371. comment.cardId = _id;
  372. comment._id = null;
  373. CardComments.insert(comment);
  374. });
  375. Popup.close();
  376. }
  377. },
  378. });
  379. Template.copyChecklistToManyCardsPopup.events({
  380. 'click .js-done' () {
  381. const card = Cards.findOne(Session.get('currentCard'));
  382. const oldId = card._id;
  383. card._id = null;
  384. const lSelect = $('.js-select-lists')[0];
  385. card.listId = lSelect.options[lSelect.selectedIndex].value;
  386. const slSelect = $('.js-select-swimlanes')[0];
  387. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  388. const bSelect = $('.js-select-boards')[0];
  389. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  390. const textarea = $('#copy-card-title');
  391. const titleEntry = textarea.val().trim();
  392. // insert new card to the bottom of new list
  393. card.sort = Lists.findOne(card.listId).cards().count();
  394. if (titleEntry) {
  395. const titleList = JSON.parse(titleEntry);
  396. for (let i = 0; i < titleList.length; i++){
  397. const obj = titleList[i];
  398. card.title = obj.title;
  399. card.description = obj.description;
  400. card.coverId = '';
  401. const _id = Cards.insert(card);
  402. // In case the filter is active we need to add the newly inserted card in
  403. // the list of exceptions -- cards that are not filtered. Otherwise the
  404. // card will disappear instantly.
  405. // See https://github.com/wekan/wekan/issues/80
  406. Filter.addException(_id);
  407. // copy checklists
  408. let cursor = Checklists.find({cardId: oldId});
  409. cursor.forEach(function() {
  410. 'use strict';
  411. const checklist = arguments[0];
  412. const checklistId = checklist._id;
  413. checklist.cardId = _id;
  414. checklist._id = null;
  415. const newChecklistId = Checklists.insert(checklist);
  416. ChecklistItems.find({checklistId}).forEach(function(item) {
  417. item._id = null;
  418. item.checklistId = newChecklistId;
  419. item.cardId = _id;
  420. ChecklistItems.insert(item);
  421. });
  422. });
  423. // copy card comments
  424. cursor = CardComments.find({cardId: oldId});
  425. cursor.forEach(function () {
  426. 'use strict';
  427. const comment = arguments[0];
  428. comment.cardId = _id;
  429. comment._id = null;
  430. CardComments.insert(comment);
  431. });
  432. }
  433. Popup.close();
  434. }
  435. },
  436. });
  437. Template.cardMorePopup.events({
  438. 'click .js-copy-card-link-to-clipboard' () {
  439. // Clipboard code from:
  440. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  441. const StringToCopyElement = document.getElementById('cardURL');
  442. StringToCopyElement.select();
  443. if (document.execCommand('copy')) {
  444. StringToCopyElement.blur();
  445. } else {
  446. document.getElementById('cardURL').selectionStart = 0;
  447. document.getElementById('cardURL').selectionEnd = 999;
  448. document.execCommand('copy');
  449. if (window.getSelection) {
  450. if (window.getSelection().empty) { // Chrome
  451. window.getSelection().empty();
  452. } else if (window.getSelection().removeAllRanges) { // Firefox
  453. window.getSelection().removeAllRanges();
  454. }
  455. } else if (document.selection) { // IE?
  456. document.selection.empty();
  457. }
  458. }
  459. },
  460. 'click .js-delete': Popup.afterConfirm('cardDelete', function () {
  461. Popup.close();
  462. Cards.remove(this._id);
  463. Utils.goBoardId(this.boardId);
  464. }),
  465. });
  466. // Close the card details pane by pressing escape
  467. EscapeActions.register('detailsPane',
  468. () => {
  469. Utils.goBoardId(Session.get('currentBoard'));
  470. },
  471. () => {
  472. return !Session.equals('currentCard', null);
  473. }, {
  474. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  475. }
  476. );