cardDetails.js 35 KB

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