cardDetails.js 35 KB

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