cardDetails.js 32 KB

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