cardDetails.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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-start-date': Popup.open('editCardStartDate'),
  191. 'click .js-due-date': Popup.open('editCardDueDate'),
  192. 'click .js-end-date': Popup.open('editCardEndDate'),
  193. 'click .js-spent-time': Popup.open('editCardSpentTime'),
  194. 'click .js-move-card': Popup.open('moveCard'),
  195. 'click .js-copy-card': Popup.open('copyCard'),
  196. 'click .js-copy-checklist-cards': Popup.open('copyChecklistToManyCards'),
  197. 'click .js-move-card-to-top' (evt) {
  198. evt.preventDefault();
  199. const minOrder = _.min(this.list().cards(this.swimlaneId).map((c) => c.sort));
  200. this.move(this.swimlaneId, this.listId, minOrder - 1);
  201. },
  202. 'click .js-move-card-to-bottom' (evt) {
  203. evt.preventDefault();
  204. const maxOrder = _.max(this.list().cards(this.swimlaneId).map((c) => c.sort));
  205. this.move(this.swimlaneId, this.listId, maxOrder + 1);
  206. },
  207. 'click .js-archive' (evt) {
  208. evt.preventDefault();
  209. this.archive();
  210. Popup.close();
  211. },
  212. 'click .js-more': Popup.open('cardMore'),
  213. 'click .js-toggle-watch-card' () {
  214. const currentCard = this;
  215. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  216. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  217. if (!err && ret) Popup.close();
  218. });
  219. },
  220. });
  221. Template.editCardTitleForm.onRendered(function () {
  222. autosize(this.$('.js-edit-card-title'));
  223. });
  224. Template.editCardTitleForm.events({
  225. 'keydown .js-edit-card-title' (evt) {
  226. // If enter key was pressed, submit the data
  227. // Unless the shift key is also being pressed
  228. if (evt.keyCode === 13 && !evt.shiftKey) {
  229. $('.js-submit-edit-card-title-form').click();
  230. }
  231. },
  232. });
  233. Template.moveCardPopup.events({
  234. 'click .js-done' () {
  235. // XXX We should *not* get the currentCard from the global state, but
  236. // instead from a “component” state.
  237. const card = Cards.findOne(Session.get('currentCard'));
  238. const lSelect = $('.js-select-lists')[0];
  239. const newListId = lSelect.options[lSelect.selectedIndex].value;
  240. const slSelect = $('.js-select-swimlanes')[0];
  241. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  242. card.move(card.swimlaneId, newListId, 0);
  243. Popup.close();
  244. },
  245. });
  246. BlazeComponent.extendComponent({
  247. onCreated() {
  248. subManager.subscribe('board', Session.get('currentBoard'));
  249. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  250. },
  251. boards() {
  252. const boards = Boards.find({
  253. archived: false,
  254. 'members.userId': Meteor.userId(),
  255. }, {
  256. sort: ['title'],
  257. });
  258. return boards;
  259. },
  260. swimlanes() {
  261. const board = Boards.findOne(this.selectedBoardId.get());
  262. return board.swimlanes();
  263. },
  264. aBoardLists() {
  265. const board = Boards.findOne(this.selectedBoardId.get());
  266. return board.lists();
  267. },
  268. events() {
  269. return [{
  270. 'change .js-select-boards'(evt) {
  271. this.selectedBoardId.set($(evt.currentTarget).val());
  272. subManager.subscribe('board', this.selectedBoardId.get());
  273. },
  274. }];
  275. },
  276. }).register('boardsAndLists');
  277. Template.copyCardPopup.events({
  278. 'click .js-done'() {
  279. const card = Cards.findOne(Session.get('currentCard'));
  280. const oldId = card._id;
  281. card._id = null;
  282. const lSelect = $('.js-select-lists')[0];
  283. card.listId = lSelect.options[lSelect.selectedIndex].value;
  284. const slSelect = $('.js-select-swimlanes')[0];
  285. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  286. const bSelect = $('.js-select-boards')[0];
  287. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  288. const textarea = $('#copy-card-title');
  289. const title = textarea.val().trim();
  290. // insert new card to the bottom of new list
  291. card.sort = Lists.findOne(card.listId).cards().count();
  292. if (title) {
  293. card.title = title;
  294. card.coverId = '';
  295. const _id = Cards.insert(card);
  296. // In case the filter is active we need to add the newly inserted card in
  297. // the list of exceptions -- cards that are not filtered. Otherwise the
  298. // card will disappear instantly.
  299. // See https://github.com/wekan/wekan/issues/80
  300. Filter.addException(_id);
  301. // copy checklists
  302. let cursor = Checklists.find({cardId: oldId});
  303. cursor.forEach(function() {
  304. 'use strict';
  305. const checklist = arguments[0];
  306. const checklistId = checklist._id;
  307. checklist.cardId = _id;
  308. checklist._id = null;
  309. const newChecklistId = Checklists.insert(checklist);
  310. ChecklistItems.find({checklistId: checklistId}).forEach(function(item) {
  311. item._id = null;
  312. item.checklistId = newChecklistId;
  313. item.cardId = _id;
  314. ChecklistItems.insert(item);
  315. });
  316. });
  317. // copy card comments
  318. cursor = CardComments.find({cardId: oldId});
  319. cursor.forEach(function () {
  320. 'use strict';
  321. const comment = arguments[0];
  322. comment.cardId = _id;
  323. comment._id = null;
  324. CardComments.insert(comment);
  325. });
  326. Popup.close();
  327. }
  328. },
  329. });
  330. Template.copyChecklistToManyCardsPopup.events({
  331. 'click .js-done' (evt) {
  332. const card = Cards.findOne(Session.get('currentCard'));
  333. const oldId = card._id;
  334. card._id = null;
  335. const lSelect = $('.js-select-lists')[0];
  336. card.listId = lSelect.options[lSelect.selectedIndex].value;
  337. const slSelect = $('.js-select-swimlanes')[0];
  338. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  339. const bSelect = $('.js-select-boards')[0];
  340. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  341. const textarea = $('#copy-card-title');
  342. const titleEntry = textarea.val().trim();
  343. // insert new card to the bottom of new list
  344. card.sort = Lists.findOne(card.listId).cards().count();
  345. if (titleEntry) {
  346. const titleList = JSON.parse(titleEntry);
  347. for (let i = 0; i < titleList.length; i++){
  348. const obj = titleList[i];
  349. card.title = obj.title;
  350. card.description = obj.description;
  351. card.coverId = '';
  352. const _id = Cards.insert(card);
  353. // In case the filter is active we need to add the newly inserted card in
  354. // the list of exceptions -- cards that are not filtered. Otherwise the
  355. // card will disappear instantly.
  356. // See https://github.com/wekan/wekan/issues/80
  357. Filter.addException(_id);
  358. // copy checklists
  359. let cursor = Checklists.find({cardId: oldId});
  360. cursor.forEach(function() {
  361. 'use strict';
  362. const checklist = arguments[0];
  363. const checklistId = checklist._id;
  364. checklist.cardId = _id;
  365. checklist._id = null;
  366. const newChecklistId = Checklists.insert(checklist);
  367. ChecklistItems.find({checklistId: checklistId}).forEach(function(item) {
  368. item._id = null;
  369. item.checklistId = newChecklistId;
  370. item.cardId = _id;
  371. ChecklistItems.insert(item);
  372. });
  373. });
  374. // copy card comments
  375. cursor = CardComments.find({cardId: oldId});
  376. cursor.forEach(function () {
  377. 'use strict';
  378. const comment = arguments[0];
  379. comment.cardId = _id;
  380. comment._id = null;
  381. CardComments.insert(comment);
  382. });
  383. }
  384. Popup.close();
  385. }
  386. },
  387. });
  388. Template.cardMorePopup.events({
  389. 'click .js-copy-card-link-to-clipboard' () {
  390. // Clipboard code from:
  391. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  392. const StringToCopyElement = document.getElementById('cardURL');
  393. StringToCopyElement.select();
  394. if (document.execCommand('copy')) {
  395. StringToCopyElement.blur();
  396. } else {
  397. document.getElementById('cardURL').selectionStart = 0;
  398. document.getElementById('cardURL').selectionEnd = 999;
  399. document.execCommand('copy');
  400. if (window.getSelection) {
  401. if (window.getSelection().empty) { // Chrome
  402. window.getSelection().empty();
  403. } else if (window.getSelection().removeAllRanges) { // Firefox
  404. window.getSelection().removeAllRanges();
  405. }
  406. } else if (document.selection) { // IE?
  407. document.selection.empty();
  408. }
  409. }
  410. },
  411. 'click .js-delete': Popup.afterConfirm('cardDelete', function () {
  412. Popup.close();
  413. Cards.remove(this._id);
  414. Utils.goBoardId(this.boardId);
  415. }),
  416. });
  417. // Close the card details pane by pressing escape
  418. EscapeActions.register('detailsPane',
  419. () => {
  420. Utils.goBoardId(Session.get('currentBoard'));
  421. },
  422. () => {
  423. return !Session.equals('currentCard', null);
  424. }, {
  425. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  426. }
  427. );