cardDetails.js 9.0 KB

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