cardDetails.js 8.8 KB

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