cardDetails.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 $cardContainer = bodyBoardComponent.$('.js-lists');
  38. const $cardView = this.$(this.firstNode());
  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-many-card': Popup.open('copyManyCard'),
  150. 'click .js-move-card-to-top' (evt) {
  151. evt.preventDefault();
  152. const minOrder = _.min(this.list().cards().map((c) => c.sort));
  153. this.move(this.listId, minOrder - 1);
  154. },
  155. 'click .js-move-card-to-bottom' (evt) {
  156. evt.preventDefault();
  157. const maxOrder = _.max(this.list().cards().map((c) => c.sort));
  158. this.move(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-select-list' () {
  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 newListId = this._id;
  192. card.move(newListId);
  193. Popup.close();
  194. },
  195. });
  196. BlazeComponent.extendComponent({
  197. onCreated() {
  198. this.selectedBoard = new ReactiveVar(Session.get('currentBoard'));
  199. },
  200. boards() {
  201. const boards = Boards.find({
  202. archived: false,
  203. 'members.userId': Meteor.userId(),
  204. }, {
  205. sort: ['title'],
  206. });
  207. return boards;
  208. },
  209. aBoardLists() {
  210. subManager.subscribe('board', this.selectedBoard.get());
  211. const board = Boards.findOne(this.selectedBoard.get());
  212. return board.lists();
  213. },
  214. events() {
  215. return [{
  216. 'change .js-select-boards'(evt) {
  217. this.selectedBoard.set($(evt.currentTarget).val());
  218. },
  219. }];
  220. },
  221. }).register('boardsAndLists');
  222. Template.copyCardPopup.events({
  223. 'click .js-select-list' (evt) {
  224. const card = Cards.findOne(Session.get('currentCard'));
  225. const oldId = card._id;
  226. card._id = null;
  227. card.listId = this._id;
  228. const list = Lists.findOne(card.listId);
  229. card.boardId = list.boardId;
  230. const textarea = $(evt.currentTarget).parents('.content').find('textarea');
  231. const title = textarea.val().trim();
  232. // insert new card to the bottom of new list
  233. card.sort = Lists.findOne(this._id).cards().count();
  234. if (title) {
  235. card.title = title;
  236. card.coverId = '';
  237. const _id = Cards.insert(card);
  238. // In case the filter is active we need to add the newly inserted card in
  239. // the list of exceptions -- cards that are not filtered. Otherwise the
  240. // card will disappear instantly.
  241. // See https://github.com/wekan/wekan/issues/80
  242. Filter.addException(_id);
  243. // copy checklists
  244. let cursor = Checklists.find({cardId: oldId});
  245. cursor.forEach(function() {
  246. 'use strict';
  247. const checklist = arguments[0];
  248. checklist.cardId = _id;
  249. checklist._id = null;
  250. Checklists.insert(checklist);
  251. });
  252. // copy card comments
  253. cursor = CardComments.find({cardId: oldId});
  254. cursor.forEach(function () {
  255. 'use strict';
  256. const comment = arguments[0];
  257. comment.cardId = _id;
  258. comment._id = null;
  259. CardComments.insert(comment);
  260. });
  261. Popup.close();
  262. }
  263. },
  264. });
  265. Template.copyManyCardPopup.events({
  266. 'click .js-select-list' (evt) {
  267. const card = Cards.findOne(Session.get('currentCard'));
  268. const oldId = card._id;
  269. card._id = null;
  270. card.listId = this._id;
  271. const list = Lists.findOne(card.listId);
  272. card.boardId = list.boardId;
  273. const textarea = $(evt.currentTarget).parents('.content').find('textarea');
  274. const titleEntry = textarea.val().trim();
  275. // insert new card to the bottom of new list
  276. card.sort = Lists.findOne(this._id).cards().count();
  277. if (titleEntry) {
  278. var titleList;
  279. var titleList = JSON.parse(titleEntry);
  280. for (var i = 0; i < titleList.length; i++){
  281. var obj = titleList[i];
  282. card.title = obj.title;
  283. card.description = obj.description;
  284. card.coverId = '';
  285. const _id = Cards.insert(card);
  286. // In case the filter is active we need to add the newly inserted card in
  287. // the list of exceptions -- cards that are not filtered. Otherwise the
  288. // card will disappear instantly.
  289. // See https://github.com/wekan/wekan/issues/80
  290. Filter.addException(_id);
  291. // copy checklists
  292. let cursor = Checklists.find({cardId: oldId});
  293. cursor.forEach(function() {
  294. 'use strict';
  295. const checklist = arguments[0];
  296. checklist.cardId = _id;
  297. checklist._id = null;
  298. Checklists.insert(checklist);
  299. });
  300. // copy card comments
  301. cursor = CardComments.find({cardId: oldId});
  302. cursor.forEach(function () {
  303. 'use strict';
  304. const comment = arguments[0];
  305. comment.cardId = _id;
  306. comment._id = null;
  307. CardComments.insert(comment);
  308. });
  309. }
  310. Popup.close();
  311. }
  312. },
  313. });
  314. Template.cardMorePopup.events({
  315. 'click .js-copy-card-link-to-clipboard' () {
  316. // Clipboard code from:
  317. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  318. const StringToCopyElement = document.getElementById('cardURL');
  319. StringToCopyElement.select();
  320. if (document.execCommand('copy')) {
  321. StringToCopyElement.blur();
  322. } else {
  323. document.getElementById('cardURL').selectionStart = 0;
  324. document.getElementById('cardURL').selectionEnd = 999;
  325. document.execCommand('copy');
  326. if (window.getSelection) {
  327. if (window.getSelection().empty) { // Chrome
  328. window.getSelection().empty();
  329. } else if (window.getSelection().removeAllRanges) { // Firefox
  330. window.getSelection().removeAllRanges();
  331. }
  332. } else if (document.selection) { // IE?
  333. document.selection.empty();
  334. }
  335. }
  336. },
  337. 'click .js-delete': Popup.afterConfirm('cardDelete', function () {
  338. Popup.close();
  339. Cards.remove(this._id);
  340. Utils.goBoardId(this.boardId);
  341. }),
  342. });
  343. // Close the card details pane by pressing escape
  344. EscapeActions.register('detailsPane',
  345. () => {
  346. Utils.goBoardId(Session.get('currentBoard'));
  347. },
  348. () => {
  349. return !Session.equals('currentCard', null);
  350. }, {
  351. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  352. }
  353. );