cardDetails.js 38 KB

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