cardDetails.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. const subManager = new SubsManager();
  2. const { calculateIndexData, enableClickOnTouch } = Utils;
  3. let cardColors;
  4. Meteor.startup(() => {
  5. cardColors = Cards.simpleSchema()._schema.color.allowedValues;
  6. });
  7. BlazeComponent.extendComponent({
  8. mixins() {
  9. return [Mixins.InfiniteScrolling, Mixins.PerfectScrollbar];
  10. },
  11. calculateNextPeak() {
  12. const cardElement = this.find('.js-card-details');
  13. if (cardElement) {
  14. const altitude = cardElement.scrollHeight;
  15. this.callFirstWith(this, 'setNextPeak', altitude);
  16. }
  17. },
  18. reachNextPeak() {
  19. const activitiesComponent = this.childComponents('activities')[0];
  20. activitiesComponent.loadNextPage();
  21. },
  22. onCreated() {
  23. this.currentBoard = Boards.findOne(Session.get('currentBoard'));
  24. this.isLoaded = new ReactiveVar(false);
  25. const boardBody = this.parentComponent().parentComponent();
  26. //in Miniview parent is Board, not BoardBody.
  27. if (boardBody !== null) {
  28. boardBody.showOverlay.set(true);
  29. boardBody.mouseHasEnterCardDetails = false;
  30. }
  31. this.calculateNextPeak();
  32. Meteor.subscribe('unsaved-edits');
  33. },
  34. isWatching() {
  35. const card = this.currentData();
  36. return card.findWatcher(Meteor.userId());
  37. },
  38. hiddenSystemMessages() {
  39. return Meteor.user().hasHiddenSystemMessages();
  40. },
  41. canModifyCard() {
  42. return (
  43. Meteor.user() &&
  44. Meteor.user().isBoardMember() &&
  45. !Meteor.user().isCommentOnly()
  46. );
  47. },
  48. scrollParentContainer() {
  49. const cardPanelWidth = 510;
  50. const bodyBoardComponent = this.parentComponent().parentComponent();
  51. //On Mobile View Parent is Board, Not Board Body. I cant see how this funciton should work then.
  52. if (bodyBoardComponent === null) return;
  53. const $cardView = this.$(this.firstNode());
  54. const $cardContainer = bodyBoardComponent.$('.js-swimlanes');
  55. const cardContainerScroll = $cardContainer.scrollLeft();
  56. const cardContainerWidth = $cardContainer.width();
  57. const cardViewStart = $cardView.offset().left;
  58. const cardViewEnd = cardViewStart + cardPanelWidth;
  59. let offset = false;
  60. if (cardViewStart < 0) {
  61. offset = cardViewStart;
  62. } else if (cardViewEnd > cardContainerWidth) {
  63. offset = cardViewEnd - cardContainerWidth;
  64. }
  65. if (offset) {
  66. bodyBoardComponent.scrollLeft(cardContainerScroll + offset);
  67. }
  68. //Scroll top
  69. const cardViewStartTop = $cardView.offset().top;
  70. const cardContainerScrollTop = $cardContainer.scrollTop();
  71. let topOffset = false;
  72. if (cardViewStartTop !== 100) {
  73. topOffset = cardViewStartTop - 100;
  74. }
  75. if (topOffset !== false) {
  76. bodyBoardComponent.scrollTop(cardContainerScrollTop + topOffset);
  77. }
  78. },
  79. presentParentTask() {
  80. let result = this.currentBoard.presentParentTask;
  81. if (result === null || result === undefined) {
  82. result = 'no-parent';
  83. }
  84. return result;
  85. },
  86. linkForCard() {
  87. const card = this.currentData();
  88. let result = '#';
  89. if (card) {
  90. const board = Boards.findOne(card.boardId);
  91. if (board) {
  92. result = FlowRouter.url('card', {
  93. boardId: card.boardId,
  94. slug: board.slug,
  95. cardId: card._id,
  96. });
  97. }
  98. }
  99. return result;
  100. },
  101. onRendered() {
  102. if (Meteor.settings.public.CARD_OPENED_WEBHOOK_ENABLED) {
  103. // Send Webhook but not create Activities records ---
  104. const card = this.currentData();
  105. const userId = Meteor.userId();
  106. const params = {
  107. userId,
  108. cardId: card._id,
  109. boardId: card.boardId,
  110. listId: card.listId,
  111. user: Meteor.user().username,
  112. url: '',
  113. };
  114. const integrations = Integrations.find({
  115. boardId: { $in: [card.boardId, Integrations.Const.GLOBAL_WEBHOOK_ID] },
  116. enabled: true,
  117. activities: { $in: ['CardDetailsRendered', 'all'] },
  118. }).fetch();
  119. if (integrations.length > 0) {
  120. integrations.forEach(integration => {
  121. Meteor.call(
  122. 'outgoingWebhooks',
  123. integration,
  124. 'CardSelected',
  125. params,
  126. () => {
  127. return;
  128. },
  129. );
  130. });
  131. }
  132. //-------------
  133. }
  134. if (!Utils.isMiniScreen()) {
  135. Meteor.setTimeout(() => {
  136. $('.card-details').mCustomScrollbar({
  137. theme: 'minimal-dark',
  138. setWidth: false,
  139. setLeft: 0,
  140. scrollbarPosition: 'outside',
  141. mouseWheel: true,
  142. });
  143. this.scrollParentContainer();
  144. }, 500);
  145. }
  146. const $checklistsDom = this.$('.card-checklist-items');
  147. $checklistsDom.sortable({
  148. tolerance: 'pointer',
  149. helper: 'clone',
  150. handle: '.checklist-title',
  151. items: '.js-checklist',
  152. placeholder: 'checklist placeholder',
  153. distance: 7,
  154. start(evt, ui) {
  155. ui.placeholder.height(ui.helper.height());
  156. EscapeActions.executeUpTo('popup-close');
  157. },
  158. stop(evt, ui) {
  159. let prevChecklist = ui.item.prev('.js-checklist').get(0);
  160. if (prevChecklist) {
  161. prevChecklist = Blaze.getData(prevChecklist).checklist;
  162. }
  163. let nextChecklist = ui.item.next('.js-checklist').get(0);
  164. if (nextChecklist) {
  165. nextChecklist = Blaze.getData(nextChecklist).checklist;
  166. }
  167. const sortIndex = calculateIndexData(prevChecklist, nextChecklist, 1);
  168. $checklistsDom.sortable('cancel');
  169. const checklist = Blaze.getData(ui.item.get(0)).checklist;
  170. Checklists.update(checklist._id, {
  171. $set: {
  172. sort: sortIndex.base,
  173. },
  174. });
  175. },
  176. });
  177. // ugly touch event hotfix
  178. enableClickOnTouch('.card-checklist-items .js-checklist');
  179. const $subtasksDom = this.$('.card-subtasks-items');
  180. $subtasksDom.sortable({
  181. tolerance: 'pointer',
  182. helper: 'clone',
  183. handle: '.subtask-title',
  184. items: '.js-subtasks',
  185. placeholder: 'subtasks placeholder',
  186. distance: 7,
  187. start(evt, ui) {
  188. ui.placeholder.height(ui.helper.height());
  189. EscapeActions.executeUpTo('popup-close');
  190. },
  191. stop(evt, ui) {
  192. let prevChecklist = ui.item.prev('.js-subtasks').get(0);
  193. if (prevChecklist) {
  194. prevChecklist = Blaze.getData(prevChecklist).subtask;
  195. }
  196. let nextChecklist = ui.item.next('.js-subtasks').get(0);
  197. if (nextChecklist) {
  198. nextChecklist = Blaze.getData(nextChecklist).subtask;
  199. }
  200. const sortIndex = calculateIndexData(prevChecklist, nextChecklist, 1);
  201. $subtasksDom.sortable('cancel');
  202. const subtask = Blaze.getData(ui.item.get(0)).subtask;
  203. Subtasks.update(subtask._id, {
  204. $set: {
  205. subtaskSort: sortIndex.base,
  206. },
  207. });
  208. },
  209. });
  210. // ugly touch event hotfix
  211. enableClickOnTouch('.card-subtasks-items .js-subtasks');
  212. function userIsMember() {
  213. return Meteor.user() && Meteor.user().isBoardMember();
  214. }
  215. // Disable sorting if the current user is not a board member
  216. this.autorun(() => {
  217. if ($checklistsDom.data('sortable')) {
  218. $checklistsDom.sortable('option', 'disabled', !userIsMember());
  219. }
  220. if ($subtasksDom.data('sortable')) {
  221. $subtasksDom.sortable('option', 'disabled', !userIsMember());
  222. }
  223. });
  224. },
  225. onDestroyed() {
  226. const parentComponent = this.parentComponent().parentComponent();
  227. //on mobile view parent is Board, not board body.
  228. if (parentComponent === null) return;
  229. parentComponent.showOverlay.set(false);
  230. },
  231. events() {
  232. const events = {
  233. [`${CSSEvents.transitionend} .js-card-details`]() {
  234. this.isLoaded.set(true);
  235. },
  236. [`${CSSEvents.animationend} .js-card-details`]() {
  237. this.isLoaded.set(true);
  238. },
  239. };
  240. return [
  241. {
  242. ...events,
  243. 'click .js-close-card-details'() {
  244. Utils.goBoardId(this.data().boardId);
  245. },
  246. 'click .js-open-card-details-menu': Popup.open('cardDetailsActions'),
  247. 'submit .js-card-description'(event) {
  248. event.preventDefault();
  249. const description = this.currentComponent().getValue();
  250. this.data().setDescription(description);
  251. },
  252. 'submit .js-card-details-title'(event) {
  253. event.preventDefault();
  254. const title = this.currentComponent()
  255. .getValue()
  256. .trim();
  257. if (title) {
  258. this.data().setTitle(title);
  259. }
  260. },
  261. 'submit .js-card-details-assigner'(event) {
  262. event.preventDefault();
  263. const assigner = this.currentComponent()
  264. .getValue()
  265. .trim();
  266. if (assigner) {
  267. this.data().setAssignedBy(assigner);
  268. }
  269. },
  270. 'submit .js-card-details-requester'(event) {
  271. event.preventDefault();
  272. const requester = this.currentComponent()
  273. .getValue()
  274. .trim();
  275. if (requester) {
  276. this.data().setRequestedBy(requester);
  277. }
  278. },
  279. 'click .js-member': Popup.open('cardMember'),
  280. 'click .js-add-members': Popup.open('cardMembers'),
  281. 'click .js-assignee': Popup.open('cardAssignee'),
  282. 'click .js-add-assignees': Popup.open('cardAssignees'),
  283. 'click .js-add-labels': Popup.open('cardLabels'),
  284. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  285. 'click .js-start-date': Popup.open('editCardStartDate'),
  286. 'click .js-due-date': Popup.open('editCardDueDate'),
  287. 'click .js-end-date': Popup.open('editCardEndDate'),
  288. 'mouseenter .js-card-details'() {
  289. const parentComponent = this.parentComponent().parentComponent();
  290. //on mobile view parent is Board, not BoardBody.
  291. if (parentComponent === null) return;
  292. parentComponent.showOverlay.set(true);
  293. parentComponent.mouseHasEnterCardDetails = true;
  294. },
  295. 'mousedown .js-card-details'() {
  296. Session.set('cardDetailsIsDragging', false);
  297. Session.set('cardDetailsIsMouseDown', true);
  298. },
  299. 'mousemove .js-card-details'() {
  300. if (Session.get('cardDetailsIsMouseDown')) {
  301. Session.set('cardDetailsIsDragging', true);
  302. }
  303. },
  304. 'mouseup .js-card-details'() {
  305. Session.set('cardDetailsIsDragging', false);
  306. Session.set('cardDetailsIsMouseDown', false);
  307. },
  308. 'click #toggleButton'() {
  309. Meteor.call('toggleSystemMessages');
  310. },
  311. },
  312. ];
  313. },
  314. }).register('cardDetails');
  315. Template.cardDetails.helpers({
  316. userData() {
  317. // We need to handle a special case for the search results provided by the
  318. // `matteodem:easy-search` package. Since these results gets published in a
  319. // separate collection, and not in the standard Meteor.Users collection as
  320. // expected, we use a component parameter ("property") to distinguish the
  321. // two cases.
  322. const userCollection = this.esSearch ? ESSearchResults : Users;
  323. return userCollection.findOne(this.userId, {
  324. fields: {
  325. profile: 1,
  326. username: 1,
  327. },
  328. });
  329. },
  330. memberType() {
  331. const user = Users.findOne(this.userId);
  332. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  333. },
  334. presenceStatusClassName() {
  335. const user = Users.findOne(this.userId);
  336. const userPresence = presences.findOne({ userId: this.userId });
  337. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  338. else if (!userPresence) return 'disconnected';
  339. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  340. return 'active';
  341. else return 'idle';
  342. },
  343. });
  344. Template.userAvatarAssigneeInitials.helpers({
  345. initials() {
  346. const user = Users.findOne(this.userId);
  347. return user && user.getInitials();
  348. },
  349. viewPortWidth() {
  350. const user = Users.findOne(this.userId);
  351. return ((user && user.getInitials().length) || 1) * 12;
  352. },
  353. });
  354. // We extends the normal InlinedForm component to support UnsavedEdits draft
  355. // feature.
  356. (class extends InlinedForm {
  357. _getUnsavedEditKey() {
  358. return {
  359. fieldName: 'cardDescription',
  360. // XXX Recovering the currentCard identifier form a session variable is
  361. // fragile because this variable may change for instance if the route
  362. // change. We should use some component props instead.
  363. docId: Session.get('currentCard'),
  364. };
  365. }
  366. close(isReset = false) {
  367. if (this.isOpen.get() && !isReset) {
  368. const draft = this.getValue().trim();
  369. if (
  370. draft !== Cards.findOne(Session.get('currentCard')).getDescription()
  371. ) {
  372. UnsavedEdits.set(this._getUnsavedEditKey(), this.getValue());
  373. }
  374. }
  375. super.close();
  376. }
  377. reset() {
  378. UnsavedEdits.reset(this._getUnsavedEditKey());
  379. this.close(true);
  380. }
  381. events() {
  382. const parentEvents = InlinedForm.prototype.events()[0];
  383. return [
  384. {
  385. ...parentEvents,
  386. 'click .js-close-inlined-form': this.reset,
  387. },
  388. ];
  389. }
  390. }.register('inlinedCardDescription'));
  391. Template.cardDetailsActionsPopup.helpers({
  392. isWatching() {
  393. return this.findWatcher(Meteor.userId());
  394. },
  395. canModifyCard() {
  396. return (
  397. Meteor.user() &&
  398. Meteor.user().isBoardMember() &&
  399. !Meteor.user().isCommentOnly()
  400. );
  401. },
  402. });
  403. Template.cardDetailsActionsPopup.events({
  404. 'click .js-members': Popup.open('cardMembers'),
  405. 'click .js-assignees': Popup.open('cardAssignees'),
  406. 'click .js-labels': Popup.open('cardLabels'),
  407. 'click .js-attachments': Popup.open('cardAttachments'),
  408. 'click .js-custom-fields': Popup.open('cardCustomFields'),
  409. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  410. 'click .js-start-date': Popup.open('editCardStartDate'),
  411. 'click .js-due-date': Popup.open('editCardDueDate'),
  412. 'click .js-end-date': Popup.open('editCardEndDate'),
  413. 'click .js-spent-time': Popup.open('editCardSpentTime'),
  414. 'click .js-move-card': Popup.open('moveCard'),
  415. 'click .js-copy-card': Popup.open('copyCard'),
  416. 'click .js-copy-checklist-cards': Popup.open('copyChecklistToManyCards'),
  417. 'click .js-set-card-color': Popup.open('setCardColor'),
  418. 'click .js-move-card-to-top'(event) {
  419. event.preventDefault();
  420. const minOrder = _.min(
  421. this.list()
  422. .cards(this.swimlaneId)
  423. .map(c => c.sort),
  424. );
  425. this.move(this.boardId, this.swimlaneId, this.listId, minOrder - 1);
  426. },
  427. 'click .js-move-card-to-bottom'(event) {
  428. event.preventDefault();
  429. const maxOrder = _.max(
  430. this.list()
  431. .cards(this.swimlaneId)
  432. .map(c => c.sort),
  433. );
  434. this.move(this.boardId, this.swimlaneId, this.listId, maxOrder + 1);
  435. },
  436. 'click .js-archive'(event) {
  437. event.preventDefault();
  438. this.archive();
  439. Popup.close();
  440. },
  441. 'click .js-more': Popup.open('cardMore'),
  442. 'click .js-toggle-watch-card'() {
  443. const currentCard = this;
  444. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  445. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  446. if (!err && ret) Popup.close();
  447. });
  448. },
  449. });
  450. Template.editCardTitleForm.onRendered(function() {
  451. autosize(this.$('.js-edit-card-title'));
  452. });
  453. Template.editCardTitleForm.events({
  454. 'keydown .js-edit-card-title'(event) {
  455. // If enter key was pressed, submit the data
  456. // Unless the shift key is also being pressed
  457. if (event.keyCode === 13 && !event.shiftKey) {
  458. $('.js-submit-edit-card-title-form').click();
  459. }
  460. },
  461. });
  462. Template.editCardRequesterForm.onRendered(function() {
  463. autosize(this.$('.js-edit-card-requester'));
  464. });
  465. Template.editCardRequesterForm.events({
  466. 'keydown .js-edit-card-requester'(event) {
  467. // If enter key was pressed, submit the data
  468. if (event.keyCode === 13) {
  469. $('.js-submit-edit-card-requester-form').click();
  470. }
  471. },
  472. });
  473. Template.editCardAssignerForm.onRendered(function() {
  474. autosize(this.$('.js-edit-card-assigner'));
  475. });
  476. Template.editCardAssignerForm.events({
  477. 'keydown .js-edit-card-assigner'(event) {
  478. // If enter key was pressed, submit the data
  479. if (event.keyCode === 13) {
  480. $('.js-submit-edit-card-assigner-form').click();
  481. }
  482. },
  483. });
  484. Template.moveCardPopup.events({
  485. 'click .js-done'() {
  486. // XXX We should *not* get the currentCard from the global state, but
  487. // instead from a “component” state.
  488. const card = Cards.findOne(Session.get('currentCard'));
  489. const bSelect = $('.js-select-boards')[0];
  490. const boardId = bSelect.options[bSelect.selectedIndex].value;
  491. const lSelect = $('.js-select-lists')[0];
  492. const listId = lSelect.options[lSelect.selectedIndex].value;
  493. const slSelect = $('.js-select-swimlanes')[0];
  494. const swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  495. card.move(boardId, swimlaneId, listId, 0);
  496. Popup.close();
  497. },
  498. });
  499. BlazeComponent.extendComponent({
  500. onCreated() {
  501. subManager.subscribe('board', Session.get('currentBoard'), false);
  502. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  503. },
  504. boards() {
  505. const boards = Boards.find(
  506. {
  507. archived: false,
  508. 'members.userId': Meteor.userId(),
  509. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  510. },
  511. {
  512. sort: ['title'],
  513. },
  514. );
  515. return boards;
  516. },
  517. swimlanes() {
  518. const board = Boards.findOne(this.selectedBoardId.get());
  519. return board.swimlanes();
  520. },
  521. aBoardLists() {
  522. const board = Boards.findOne(this.selectedBoardId.get());
  523. return board.lists();
  524. },
  525. events() {
  526. return [
  527. {
  528. 'change .js-select-boards'(event) {
  529. this.selectedBoardId.set($(event.currentTarget).val());
  530. subManager.subscribe('board', this.selectedBoardId.get(), false);
  531. },
  532. },
  533. ];
  534. },
  535. }).register('boardsAndLists');
  536. Template.copyCardPopup.events({
  537. 'click .js-done'() {
  538. const card = Cards.findOne(Session.get('currentCard'));
  539. const lSelect = $('.js-select-lists')[0];
  540. listId = lSelect.options[lSelect.selectedIndex].value;
  541. const slSelect = $('.js-select-swimlanes')[0];
  542. const swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  543. const bSelect = $('.js-select-boards')[0];
  544. const boardId = bSelect.options[bSelect.selectedIndex].value;
  545. const textarea = $('#copy-card-title');
  546. const title = textarea.val().trim();
  547. // insert new card to the bottom of new list
  548. card.sort = Lists.findOne(card.listId)
  549. .cards()
  550. .count();
  551. if (title) {
  552. card.title = title;
  553. card.coverId = '';
  554. const _id = card.copy(boardId, swimlaneId, listId);
  555. // In case the filter is active we need to add the newly inserted card in
  556. // the list of exceptions -- cards that are not filtered. Otherwise the
  557. // card will disappear instantly.
  558. // See https://github.com/wekan/wekan/issues/80
  559. Filter.addException(_id);
  560. Popup.close();
  561. }
  562. },
  563. });
  564. Template.copyChecklistToManyCardsPopup.events({
  565. 'click .js-done'() {
  566. const card = Cards.findOne(Session.get('currentCard'));
  567. const oldId = card._id;
  568. card._id = null;
  569. const lSelect = $('.js-select-lists')[0];
  570. card.listId = lSelect.options[lSelect.selectedIndex].value;
  571. const slSelect = $('.js-select-swimlanes')[0];
  572. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  573. const bSelect = $('.js-select-boards')[0];
  574. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  575. const textarea = $('#copy-card-title');
  576. const titleEntry = textarea.val().trim();
  577. // insert new card to the bottom of new list
  578. card.sort = Lists.findOne(card.listId)
  579. .cards()
  580. .count();
  581. if (titleEntry) {
  582. const titleList = JSON.parse(titleEntry);
  583. for (let i = 0; i < titleList.length; i++) {
  584. const obj = titleList[i];
  585. card.title = obj.title;
  586. card.description = obj.description;
  587. card.coverId = '';
  588. const _id = Cards.insert(card);
  589. // In case the filter is active we need to add the newly inserted card in
  590. // the list of exceptions -- cards that are not filtered. Otherwise the
  591. // card will disappear instantly.
  592. // See https://github.com/wekan/wekan/issues/80
  593. Filter.addException(_id);
  594. // copy checklists
  595. Checklists.find({ cardId: oldId }).forEach(ch => {
  596. ch.copy(_id);
  597. });
  598. // copy subtasks
  599. cursor = Cards.find({ parentId: oldId });
  600. cursor.forEach(function() {
  601. 'use strict';
  602. const subtask = arguments[0];
  603. subtask.parentId = _id;
  604. subtask._id = null;
  605. /* const newSubtaskId = */ Cards.insert(subtask);
  606. });
  607. // copy card comments
  608. CardComments.find({ cardId: oldId }).forEach(cmt => {
  609. cmt.copy(_id);
  610. });
  611. }
  612. Popup.close();
  613. }
  614. },
  615. });
  616. BlazeComponent.extendComponent({
  617. onCreated() {
  618. this.currentCard = this.currentData();
  619. this.currentColor = new ReactiveVar(this.currentCard.color);
  620. },
  621. colors() {
  622. return cardColors.map(color => ({ color, name: '' }));
  623. },
  624. isSelected(color) {
  625. if (this.currentColor.get() === null) {
  626. return color === 'white';
  627. }
  628. return this.currentColor.get() === color;
  629. },
  630. events() {
  631. return [
  632. {
  633. 'click .js-palette-color'() {
  634. this.currentColor.set(this.currentData().color);
  635. },
  636. 'click .js-submit'() {
  637. this.currentCard.setColor(this.currentColor.get());
  638. Popup.close();
  639. },
  640. 'click .js-remove-color'() {
  641. this.currentCard.setColor(null);
  642. Popup.close();
  643. },
  644. },
  645. ];
  646. },
  647. }).register('setCardColorPopup');
  648. BlazeComponent.extendComponent({
  649. onCreated() {
  650. this.currentCard = this.currentData();
  651. this.parentBoard = new ReactiveVar(null);
  652. this.parentCard = this.currentCard.parentCard();
  653. if (this.parentCard) {
  654. const list = $('.js-field-parent-card');
  655. list.val(this.parentCard._id);
  656. this.parentBoard.set(this.parentCard.board()._id);
  657. } else {
  658. this.parentBoard.set(null);
  659. }
  660. },
  661. boards() {
  662. const boards = Boards.find(
  663. {
  664. archived: false,
  665. 'members.userId': Meteor.userId(),
  666. _id: {
  667. $ne: Meteor.user().getTemplatesBoardId(),
  668. },
  669. },
  670. {
  671. sort: ['title'],
  672. },
  673. );
  674. return boards;
  675. },
  676. cards() {
  677. const currentId = Session.get('currentCard');
  678. if (this.parentBoard.get()) {
  679. return Cards.find({
  680. boardId: this.parentBoard.get(),
  681. _id: { $ne: currentId },
  682. });
  683. } else {
  684. return [];
  685. }
  686. },
  687. isParentBoard() {
  688. const board = this.currentData();
  689. if (this.parentBoard.get()) {
  690. return board._id === this.parentBoard.get();
  691. }
  692. return false;
  693. },
  694. isParentCard() {
  695. const card = this.currentData();
  696. if (this.parentCard) {
  697. return card._id === this.parentCard;
  698. }
  699. return false;
  700. },
  701. setParentCardId(cardId) {
  702. if (cardId) {
  703. this.parentCard = Cards.findOne(cardId);
  704. } else {
  705. this.parentCard = null;
  706. }
  707. this.currentCard.setParentId(cardId);
  708. },
  709. events() {
  710. return [
  711. {
  712. 'click .js-copy-card-link-to-clipboard'() {
  713. // Clipboard code from:
  714. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  715. const StringToCopyElement = document.getElementById('cardURL');
  716. StringToCopyElement.select();
  717. if (document.execCommand('copy')) {
  718. StringToCopyElement.blur();
  719. } else {
  720. document.getElementById('cardURL').selectionStart = 0;
  721. document.getElementById('cardURL').selectionEnd = 999;
  722. document.execCommand('copy');
  723. if (window.getSelection) {
  724. if (window.getSelection().empty) {
  725. // Chrome
  726. window.getSelection().empty();
  727. } else if (window.getSelection().removeAllRanges) {
  728. // Firefox
  729. window.getSelection().removeAllRanges();
  730. }
  731. } else if (document.selection) {
  732. // IE?
  733. document.selection.empty();
  734. }
  735. }
  736. },
  737. 'click .js-delete': Popup.afterConfirm('cardDelete', function() {
  738. Popup.close();
  739. Cards.remove(this._id);
  740. Utils.goBoardId(this.boardId);
  741. }),
  742. 'change .js-field-parent-board'(event) {
  743. const selection = $(event.currentTarget).val();
  744. const list = $('.js-field-parent-card');
  745. if (selection === 'none') {
  746. this.parentBoard.set(null);
  747. } else {
  748. subManager.subscribe('board', $(event.currentTarget).val(), false);
  749. this.parentBoard.set(selection);
  750. list.prop('disabled', false);
  751. }
  752. this.setParentCardId(null);
  753. },
  754. 'change .js-field-parent-card'(event) {
  755. const selection = $(event.currentTarget).val();
  756. this.setParentCardId(selection);
  757. },
  758. },
  759. ];
  760. },
  761. }).register('cardMorePopup');
  762. // Close the card details pane by pressing escape
  763. EscapeActions.register(
  764. 'detailsPane',
  765. () => {
  766. if (Session.get('cardDetailsIsDragging')) {
  767. // Reset dragging status as the mouse landed outside the cardDetails template area and this will prevent a mousedown event from firing
  768. Session.set('cardDetailsIsDragging', false);
  769. Session.set('cardDetailsIsMouseDown', false);
  770. } else {
  771. // Prevent close card when the user is selecting text and moves the mouse cursor outside the card detail area
  772. Utils.goBoardId(Session.get('currentBoard'));
  773. }
  774. },
  775. () => {
  776. return !Session.equals('currentCard', null);
  777. },
  778. {
  779. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  780. },
  781. );
  782. Template.cardAssigneesPopup.events({
  783. 'click .js-select-assignee'(event) {
  784. const card = Cards.findOne(Session.get('currentCard'));
  785. const assigneeId = this.userId;
  786. card.toggleAssignee(assigneeId);
  787. event.preventDefault();
  788. },
  789. });
  790. Template.cardAssigneesPopup.helpers({
  791. isCardAssignee() {
  792. const card = Template.parentData();
  793. const cardAssignees = card.getAssignees();
  794. return _.contains(cardAssignees, this.userId);
  795. },
  796. user() {
  797. return Users.findOne(this.userId);
  798. },
  799. });
  800. Template.cardAssigneePopup.helpers({
  801. userData() {
  802. // We need to handle a special case for the search results provided by the
  803. // `matteodem:easy-search` package. Since these results gets published in a
  804. // separate collection, and not in the standard Meteor.Users collection as
  805. // expected, we use a component parameter ("property") to distinguish the
  806. // two cases.
  807. const userCollection = this.esSearch ? ESSearchResults : Users;
  808. return userCollection.findOne(this.userId, {
  809. fields: {
  810. profile: 1,
  811. username: 1,
  812. },
  813. });
  814. },
  815. memberType() {
  816. const user = Users.findOne(this.userId);
  817. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  818. },
  819. presenceStatusClassName() {
  820. const user = Users.findOne(this.userId);
  821. const userPresence = presences.findOne({ userId: this.userId });
  822. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  823. else if (!userPresence) return 'disconnected';
  824. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  825. return 'active';
  826. else return 'idle';
  827. },
  828. isCardAssignee() {
  829. const card = Template.parentData();
  830. const cardAssignees = card.getAssignees();
  831. return _.contains(cardAssignees, this.userId);
  832. },
  833. user() {
  834. return Users.findOne(this.userId);
  835. },
  836. });
  837. Template.cardAssigneePopup.events({
  838. 'click .js-remove-assignee'() {
  839. Cards.findOne(this.cardId).unassignAssignee(this.userId);
  840. Popup.close();
  841. },
  842. 'click .js-edit-profile': Popup.open('editProfile'),
  843. });