cardDetails.js 34 KB

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