cardDetails.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. 'mouseenter .js-card-details' () {
  131. this.parentComponent().parentComponent().showOverlay.set(true);
  132. this.parentComponent().parentComponent().mouseHasEnterCardDetails = true;
  133. },
  134. 'click #toggleButton'() {
  135. Meteor.call('toggleSystemMessages');
  136. },
  137. }];
  138. },
  139. }).register('cardDetails');
  140. // We extends the normal InlinedForm component to support UnsavedEdits draft
  141. // feature.
  142. (class extends InlinedForm {
  143. _getUnsavedEditKey() {
  144. return {
  145. fieldName: 'cardDescription',
  146. // XXX Recovering the currentCard identifier form a session variable is
  147. // fragile because this variable may change for instance if the route
  148. // change. We should use some component props instead.
  149. docId: Session.get('currentCard'),
  150. };
  151. }
  152. close(isReset = false) {
  153. if (this.isOpen.get() && !isReset) {
  154. const draft = this.getValue().trim();
  155. if (draft !== Cards.findOne(Session.get('currentCard')).description) {
  156. UnsavedEdits.set(this._getUnsavedEditKey(), this.getValue());
  157. }
  158. }
  159. super.close();
  160. }
  161. reset() {
  162. UnsavedEdits.reset(this._getUnsavedEditKey());
  163. this.close(true);
  164. }
  165. events() {
  166. const parentEvents = InlinedForm.prototype.events()[0];
  167. return [{
  168. ...parentEvents,
  169. 'click .js-close-inlined-form': this.reset,
  170. }];
  171. }
  172. }).register('inlinedCardDescription');
  173. Template.cardDetailsActionsPopup.helpers({
  174. isWatching() {
  175. return this.findWatcher(Meteor.userId());
  176. },
  177. canModifyCard() {
  178. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  179. },
  180. });
  181. Template.cardDetailsActionsPopup.events({
  182. 'click .js-members': Popup.open('cardMembers'),
  183. 'click .js-labels': Popup.open('cardLabels'),
  184. 'click .js-attachments': Popup.open('cardAttachments'),
  185. 'click .js-start-date': Popup.open('editCardStartDate'),
  186. 'click .js-due-date': Popup.open('editCardDueDate'),
  187. 'click .js-spent-time': Popup.open('editCardSpentTime'),
  188. 'click .js-move-card': Popup.open('moveCard'),
  189. 'click .js-copy-card': Popup.open('copyCard'),
  190. 'click .js-copy-checklist-cards': Popup.open('copyChecklistToManyCards'),
  191. 'click .js-move-card-to-top' (evt) {
  192. evt.preventDefault();
  193. const minOrder = _.min(this.list().cards(this.swimlaneId).map((c) => c.sort));
  194. this.move(this.swimlaneId, this.listId, minOrder - 1);
  195. },
  196. 'click .js-move-card-to-bottom' (evt) {
  197. evt.preventDefault();
  198. const maxOrder = _.max(this.list().cards(this.swimlaneId).map((c) => c.sort));
  199. this.move(this.swimlaneId, this.listId, maxOrder + 1);
  200. },
  201. 'click .js-archive' (evt) {
  202. evt.preventDefault();
  203. this.archive();
  204. Popup.close();
  205. },
  206. 'click .js-more': Popup.open('cardMore'),
  207. 'click .js-toggle-watch-card' () {
  208. const currentCard = this;
  209. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  210. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  211. if (!err && ret) Popup.close();
  212. });
  213. },
  214. });
  215. Template.editCardTitleForm.onRendered(function () {
  216. autosize(this.$('.js-edit-card-title'));
  217. });
  218. Template.editCardTitleForm.events({
  219. 'keydown .js-edit-card-title' (evt) {
  220. // If enter key was pressed, submit the data
  221. // Unless the shift key is also being pressed
  222. if (evt.keyCode === 13 && !evt.shiftKey) {
  223. $('.js-submit-edit-card-title-form').click();
  224. }
  225. },
  226. });
  227. Template.moveCardPopup.events({
  228. 'click .js-done' () {
  229. // XXX We should *not* get the currentCard from the global state, but
  230. // instead from a “component” state.
  231. const card = Cards.findOne(Session.get('currentCard'));
  232. const lSelect = $('.js-select-lists')[0];
  233. const newListId = lSelect.options[lSelect.selectedIndex].value;
  234. const slSelect = $('.js-select-swimlanes')[0];
  235. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  236. card.move(card.swimlaneId, newListId, 0);
  237. Popup.close();
  238. },
  239. });
  240. BlazeComponent.extendComponent({
  241. onCreated() {
  242. subManager.subscribe('board', Session.get('currentBoard'));
  243. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  244. },
  245. boards() {
  246. const boards = Boards.find({
  247. archived: false,
  248. 'members.userId': Meteor.userId(),
  249. }, {
  250. sort: ['title'],
  251. });
  252. return boards;
  253. },
  254. swimlanes() {
  255. const board = Boards.findOne(this.selectedBoardId.get());
  256. return board.swimlanes();
  257. },
  258. aBoardLists() {
  259. const board = Boards.findOne(this.selectedBoardId.get());
  260. return board.lists();
  261. },
  262. events() {
  263. return [{
  264. 'change .js-select-boards'(evt) {
  265. this.selectedBoardId.set($(evt.currentTarget).val());
  266. subManager.subscribe('board', this.selectedBoardId.get());
  267. },
  268. }];
  269. },
  270. }).register('boardsAndLists');
  271. Template.copyCardPopup.events({
  272. 'click .js-done'() {
  273. const card = Cards.findOne(Session.get('currentCard'));
  274. const oldId = card._id;
  275. card._id = null;
  276. const lSelect = $('.js-select-lists')[0];
  277. card.listId = lSelect.options[lSelect.selectedIndex].value;
  278. const slSelect = $('.js-select-swimlanes')[0];
  279. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  280. const bSelect = $('.js-select-boards')[0];
  281. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  282. const textarea = $('#copy-card-title');
  283. const title = textarea.val().trim();
  284. // insert new card to the bottom of new list
  285. card.sort = Lists.findOne(card.listId).cards().count();
  286. if (title) {
  287. card.title = title;
  288. card.coverId = '';
  289. const _id = Cards.insert(card);
  290. // In case the filter is active we need to add the newly inserted card in
  291. // the list of exceptions -- cards that are not filtered. Otherwise the
  292. // card will disappear instantly.
  293. // See https://github.com/wekan/wekan/issues/80
  294. Filter.addException(_id);
  295. // copy checklists
  296. let cursor = Checklists.find({cardId: oldId});
  297. cursor.forEach(function() {
  298. 'use strict';
  299. const checklist = arguments[0];
  300. checklist.cardId = _id;
  301. checklist._id = null;
  302. Checklists.insert(checklist);
  303. });
  304. // copy card comments
  305. cursor = CardComments.find({cardId: oldId});
  306. cursor.forEach(function () {
  307. 'use strict';
  308. const comment = arguments[0];
  309. comment.cardId = _id;
  310. comment._id = null;
  311. CardComments.insert(comment);
  312. });
  313. Popup.close();
  314. }
  315. },
  316. });
  317. Template.copyChecklistToManyCardsPopup.events({
  318. 'click .js-select-list' (evt) {
  319. const card = Cards.findOne(Session.get('currentCard'));
  320. const oldId = card._id;
  321. card._id = null;
  322. card.listId = this._id;
  323. const list = Lists.findOne(card.listId);
  324. card.boardId = list.boardId;
  325. const textarea = $(evt.currentTarget).parents('.content').find('textarea');
  326. const titleEntry = textarea.val().trim();
  327. // insert new card to the bottom of new list
  328. card.sort = Lists.findOne(this._id).cards().count();
  329. if (titleEntry) {
  330. const titleList = JSON.parse(titleEntry);
  331. for (let i = 0; i < titleList.length; i++){
  332. const obj = titleList[i];
  333. card.title = obj.title;
  334. card.description = obj.description;
  335. card.coverId = '';
  336. const _id = Cards.insert(card);
  337. // In case the filter is active we need to add the newly inserted card in
  338. // the list of exceptions -- cards that are not filtered. Otherwise the
  339. // card will disappear instantly.
  340. // See https://github.com/wekan/wekan/issues/80
  341. Filter.addException(_id);
  342. // copy checklists
  343. let cursor = Checklists.find({cardId: oldId});
  344. cursor.forEach(function() {
  345. 'use strict';
  346. const checklist = arguments[0];
  347. checklist.cardId = _id;
  348. checklist._id = null;
  349. Checklists.insert(checklist);
  350. });
  351. // copy card comments
  352. cursor = CardComments.find({cardId: oldId});
  353. cursor.forEach(function () {
  354. 'use strict';
  355. const comment = arguments[0];
  356. comment.cardId = _id;
  357. comment._id = null;
  358. CardComments.insert(comment);
  359. });
  360. }
  361. Popup.close();
  362. }
  363. },
  364. });
  365. Template.cardMorePopup.events({
  366. 'click .js-copy-card-link-to-clipboard' () {
  367. // Clipboard code from:
  368. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  369. const StringToCopyElement = document.getElementById('cardURL');
  370. StringToCopyElement.select();
  371. if (document.execCommand('copy')) {
  372. StringToCopyElement.blur();
  373. } else {
  374. document.getElementById('cardURL').selectionStart = 0;
  375. document.getElementById('cardURL').selectionEnd = 999;
  376. document.execCommand('copy');
  377. if (window.getSelection) {
  378. if (window.getSelection().empty) { // Chrome
  379. window.getSelection().empty();
  380. } else if (window.getSelection().removeAllRanges) { // Firefox
  381. window.getSelection().removeAllRanges();
  382. }
  383. } else if (document.selection) { // IE?
  384. document.selection.empty();
  385. }
  386. }
  387. },
  388. 'click .js-delete': Popup.afterConfirm('cardDelete', function () {
  389. Popup.close();
  390. Cards.remove(this._id);
  391. Utils.goBoardId(this.boardId);
  392. }),
  393. });
  394. // Close the card details pane by pressing escape
  395. EscapeActions.register('detailsPane',
  396. () => {
  397. Utils.goBoardId(Session.get('currentBoard'));
  398. },
  399. () => {
  400. return !Session.equals('currentCard', null);
  401. }, {
  402. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  403. }
  404. );