cardDetails.js 34 KB

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