cardDetails.js 15 KB

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