cardDetails.js 16 KB

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