cardDetails.js 12 KB

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