cardDetails.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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-move-card-to-top' (evt) {
  150. evt.preventDefault();
  151. const minOrder = _.min(this.list().cards().map((c) => c.sort));
  152. this.move(this.listId, minOrder - 1);
  153. },
  154. 'click .js-move-card-to-bottom' (evt) {
  155. evt.preventDefault();
  156. const maxOrder = _.max(this.list().cards().map((c) => c.sort));
  157. this.move(this.listId, maxOrder + 1);
  158. },
  159. 'click .js-archive' (evt) {
  160. evt.preventDefault();
  161. this.archive();
  162. Popup.close();
  163. },
  164. 'click .js-more': Popup.open('cardMore'),
  165. 'click .js-toggle-watch-card' () {
  166. const currentCard = this;
  167. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  168. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  169. if (!err && ret) Popup.close();
  170. });
  171. },
  172. });
  173. Template.editCardTitleForm.onRendered(function () {
  174. autosize(this.$('.js-edit-card-title'));
  175. });
  176. Template.editCardTitleForm.events({
  177. 'keydown .js-edit-card-title' (evt) {
  178. // If enter key was pressed, submit the data
  179. // Unless the shift key is also being pressed
  180. if (evt.keyCode === 13 && !evt.shiftKey) {
  181. $('.js-submit-edit-card-title-form').click();
  182. }
  183. },
  184. });
  185. Template.moveCardPopup.events({
  186. 'click .js-select-list' () {
  187. // XXX We should *not* get the currentCard from the global state, but
  188. // instead from a “component” state.
  189. const card = Cards.findOne(Session.get('currentCard'));
  190. const newListId = this._id;
  191. card.move(newListId);
  192. Popup.close();
  193. },
  194. });
  195. BlazeComponent.extendComponent({
  196. onCreated() {
  197. this.selectedBoard = new ReactiveVar(Session.get('currentBoard'));
  198. },
  199. boards() {
  200. const boards = Boards.find({
  201. archived: false,
  202. 'members.userId': Meteor.userId(),
  203. }, {
  204. sort: ['title'],
  205. });
  206. return boards;
  207. },
  208. aBoardLists() {
  209. subManager.subscribe('board', this.selectedBoard.get());
  210. const board = Boards.findOne(this.selectedBoard.get());
  211. return board.lists();
  212. },
  213. events() {
  214. return [{
  215. 'change .js-select-boards'(evt) {
  216. this.selectedBoard.set($(evt.currentTarget).val());
  217. },
  218. }];
  219. },
  220. }).register('boardsAndLists');
  221. Template.copyCardPopup.events({
  222. 'click .js-select-list' (evt) {
  223. const card = Cards.findOne(Session.get('currentCard'));
  224. const oldId = card._id;
  225. card._id = null;
  226. card.listId = this._id;
  227. const list = Lists.findOne(card.listId);
  228. card.boardId = list.boardId;
  229. const textarea = $(evt.currentTarget).parents('.content').find('textarea');
  230. const title = textarea.val().trim();
  231. // insert new card to the bottom of new list
  232. card.sort = Lists.findOne(this._id).cards().count();
  233. if (title) {
  234. card.title = title;
  235. card.coverId = '';
  236. const _id = Cards.insert(card);
  237. // In case the filter is active we need to add the newly inserted card in
  238. // the list of exceptions -- cards that are not filtered. Otherwise the
  239. // card will disappear instantly.
  240. // See https://github.com/wekan/wekan/issues/80
  241. Filter.addException(_id);
  242. // copy checklists
  243. let cursor = Checklists.find({cardId: oldId});
  244. cursor.forEach(function() {
  245. 'use strict';
  246. const checklist = arguments[0];
  247. checklist.cardId = _id;
  248. checklist._id = null;
  249. Checklists.insert(checklist);
  250. });
  251. // copy card comments
  252. cursor = CardComments.find({cardId: oldId});
  253. cursor.forEach(function () {
  254. 'use strict';
  255. const comment = arguments[0];
  256. comment.cardId = _id;
  257. comment._id = null;
  258. CardComments.insert(comment);
  259. });
  260. Popup.close();
  261. }
  262. },
  263. });
  264. Template.cardMorePopup.events({
  265. 'click .js-copy-card-link-to-clipboard' () {
  266. // Clipboard code from:
  267. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  268. const StringToCopyElement = document.getElementById('cardURL');
  269. StringToCopyElement.select();
  270. if (document.execCommand('copy')) {
  271. StringToCopyElement.blur();
  272. } else {
  273. document.getElementById('cardURL').selectionStart = 0;
  274. document.getElementById('cardURL').selectionEnd = 999;
  275. document.execCommand('copy');
  276. if (window.getSelection) {
  277. if (window.getSelection().empty) { // Chrome
  278. window.getSelection().empty();
  279. } else if (window.getSelection().removeAllRanges) { // Firefox
  280. window.getSelection().removeAllRanges();
  281. }
  282. } else if (document.selection) { // IE?
  283. document.selection.empty();
  284. }
  285. }
  286. },
  287. 'click .js-delete': Popup.afterConfirm('cardDelete', function () {
  288. Popup.close();
  289. Cards.remove(this._id);
  290. Utils.goBoardId(this.boardId);
  291. }),
  292. });
  293. // Close the card details pane by pressing escape
  294. EscapeActions.register('detailsPane',
  295. () => {
  296. Utils.goBoardId(Session.get('currentBoard'));
  297. },
  298. () => {
  299. return !Session.equals('currentCard', null);
  300. }, {
  301. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  302. }
  303. );