cardDetails.js 34 KB

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