cardDetails.js 9.8 KB

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