cardDetails.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. const subManager = new SubsManager();
  2. const { calculateIndexData, enableClickOnTouch } = 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. 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. );
  47. },
  48. scrollParentContainer() {
  49. const cardPanelWidth = 510;
  50. const bodyBoardComponent = this.parentComponent().parentComponent();
  51. //On Mobile View Parent is Board, Not Board Body. I cant see how this funciton should work then.
  52. if (bodyBoardComponent === null) return;
  53. const $cardView = this.$(this.firstNode());
  54. const $cardContainer = bodyBoardComponent.$('.js-swimlanes');
  55. const cardContainerScroll = $cardContainer.scrollLeft();
  56. const cardContainerWidth = $cardContainer.width();
  57. const cardViewStart = $cardView.offset().left;
  58. const cardViewEnd = cardViewStart + cardPanelWidth;
  59. let offset = false;
  60. if (cardViewStart < 0) {
  61. offset = cardViewStart;
  62. } else if (cardViewEnd > cardContainerWidth) {
  63. offset = cardViewEnd - cardContainerWidth;
  64. }
  65. if (offset) {
  66. bodyBoardComponent.scrollLeft(cardContainerScroll + offset);
  67. }
  68. //Scroll top
  69. const cardViewStartTop = $cardView.offset().top;
  70. const cardContainerScrollTop = $cardContainer.scrollTop();
  71. let topOffset = false;
  72. if (cardViewStartTop !== 100) {
  73. topOffset = cardViewStartTop - 100;
  74. }
  75. if (topOffset !== false) {
  76. bodyBoardComponent.scrollTop(cardContainerScrollTop + topOffset);
  77. }
  78. },
  79. presentParentTask() {
  80. let result = this.currentBoard.presentParentTask;
  81. if (result === null || result === undefined) {
  82. result = 'no-parent';
  83. }
  84. return result;
  85. },
  86. linkForCard() {
  87. const card = this.currentData();
  88. let result = '#';
  89. if (card) {
  90. const board = Boards.findOne(card.boardId);
  91. if (board) {
  92. result = FlowRouter.url('card', {
  93. boardId: card.boardId,
  94. slug: board.slug,
  95. cardId: card._id,
  96. });
  97. }
  98. }
  99. return result;
  100. },
  101. onRendered() {
  102. if (Meteor.settings.public.CARD_OPENED_WEBHOOK_ENABLED) {
  103. // Send Webhook but not create Activities records ---
  104. const card = this.currentData();
  105. const userId = Meteor.userId();
  106. //console.log(`userId: ${userId}`);
  107. //console.log(`cardId: ${card._id}`);
  108. //console.log(`boardId: ${card.boardId}`);
  109. //console.log(`listId: ${card.listId}`);
  110. //console.log(`swimlaneId: ${card.swimlaneId}`);
  111. const params = {
  112. userId,
  113. cardId: card._id,
  114. boardId: card.boardId,
  115. listId: card.listId,
  116. user: Meteor.user().username,
  117. url: '',
  118. };
  119. //console.log('looking for integrations...');
  120. const integrations = Integrations.find({
  121. boardId: card.boardId,
  122. type: 'outgoing-webhooks',
  123. enabled: true,
  124. activities: { $in: ['CardDetailsRendered', 'all'] },
  125. }).fetch();
  126. //console.log(`Investigation length: ${integrations.length}`);
  127. if (integrations.length > 0) {
  128. Meteor.call('outgoingWebhooks', integrations, 'CardSelected', params);
  129. }
  130. //-------------
  131. }
  132. if (!Utils.isMiniScreen()) {
  133. Meteor.setTimeout(() => {
  134. $('.card-details').mCustomScrollbar({
  135. theme: 'minimal-dark',
  136. setWidth: false,
  137. setLeft: 0,
  138. scrollbarPosition: 'outside',
  139. mouseWheel: true,
  140. });
  141. this.scrollParentContainer();
  142. }, 500);
  143. }
  144. const $checklistsDom = this.$('.card-checklist-items');
  145. $checklistsDom.sortable({
  146. tolerance: 'pointer',
  147. helper: 'clone',
  148. handle: '.checklist-title',
  149. items: '.js-checklist',
  150. placeholder: 'checklist placeholder',
  151. distance: 7,
  152. start(evt, ui) {
  153. ui.placeholder.height(ui.helper.height());
  154. EscapeActions.executeUpTo('popup-close');
  155. },
  156. stop(evt, ui) {
  157. let prevChecklist = ui.item.prev('.js-checklist').get(0);
  158. if (prevChecklist) {
  159. prevChecklist = Blaze.getData(prevChecklist).checklist;
  160. }
  161. let nextChecklist = ui.item.next('.js-checklist').get(0);
  162. if (nextChecklist) {
  163. nextChecklist = Blaze.getData(nextChecklist).checklist;
  164. }
  165. const sortIndex = calculateIndexData(prevChecklist, nextChecklist, 1);
  166. $checklistsDom.sortable('cancel');
  167. const checklist = Blaze.getData(ui.item.get(0)).checklist;
  168. Checklists.update(checklist._id, {
  169. $set: {
  170. sort: sortIndex.base,
  171. },
  172. });
  173. },
  174. });
  175. // ugly touch event hotfix
  176. enableClickOnTouch('.card-checklist-items .js-checklist');
  177. const $subtasksDom = this.$('.card-subtasks-items');
  178. $subtasksDom.sortable({
  179. tolerance: 'pointer',
  180. helper: 'clone',
  181. handle: '.subtask-title',
  182. items: '.js-subtasks',
  183. placeholder: 'subtasks placeholder',
  184. distance: 7,
  185. start(evt, ui) {
  186. ui.placeholder.height(ui.helper.height());
  187. EscapeActions.executeUpTo('popup-close');
  188. },
  189. stop(evt, ui) {
  190. let prevChecklist = ui.item.prev('.js-subtasks').get(0);
  191. if (prevChecklist) {
  192. prevChecklist = Blaze.getData(prevChecklist).subtask;
  193. }
  194. let nextChecklist = ui.item.next('.js-subtasks').get(0);
  195. if (nextChecklist) {
  196. nextChecklist = Blaze.getData(nextChecklist).subtask;
  197. }
  198. const sortIndex = calculateIndexData(prevChecklist, nextChecklist, 1);
  199. $subtasksDom.sortable('cancel');
  200. const subtask = Blaze.getData(ui.item.get(0)).subtask;
  201. Subtasks.update(subtask._id, {
  202. $set: {
  203. subtaskSort: sortIndex.base,
  204. },
  205. });
  206. },
  207. });
  208. // ugly touch event hotfix
  209. enableClickOnTouch('.card-subtasks-items .js-subtasks');
  210. function userIsMember() {
  211. return Meteor.user() && Meteor.user().isBoardMember();
  212. }
  213. // Disable sorting if the current user is not a board member
  214. this.autorun(() => {
  215. if ($checklistsDom.data('sortable')) {
  216. $checklistsDom.sortable('option', 'disabled', !userIsMember());
  217. }
  218. if ($subtasksDom.data('sortable')) {
  219. $subtasksDom.sortable('option', 'disabled', !userIsMember());
  220. }
  221. });
  222. },
  223. onDestroyed() {
  224. const parentComponent = this.parentComponent().parentComponent();
  225. //on mobile view parent is Board, not board body.
  226. if (parentComponent === null) return;
  227. parentComponent.showOverlay.set(false);
  228. },
  229. events() {
  230. const events = {
  231. [`${CSSEvents.transitionend} .js-card-details`]() {
  232. this.isLoaded.set(true);
  233. },
  234. [`${CSSEvents.animationend} .js-card-details`]() {
  235. this.isLoaded.set(true);
  236. },
  237. };
  238. return [
  239. {
  240. ...events,
  241. 'click .js-close-card-details'() {
  242. Utils.goBoardId(this.data().boardId);
  243. },
  244. 'click .js-open-card-details-menu': Popup.open('cardDetailsActions'),
  245. 'submit .js-card-description'(event) {
  246. event.preventDefault();
  247. const description = this.currentComponent().getValue();
  248. this.data().setDescription(description);
  249. },
  250. 'submit .js-card-details-title'(event) {
  251. event.preventDefault();
  252. const title = this.currentComponent()
  253. .getValue()
  254. .trim();
  255. if (title) {
  256. this.data().setTitle(title);
  257. }
  258. },
  259. 'submit .js-card-details-assigner'(event) {
  260. event.preventDefault();
  261. const assigner = this.currentComponent()
  262. .getValue()
  263. .trim();
  264. if (assigner) {
  265. this.data().setAssignedBy(assigner);
  266. }
  267. },
  268. 'submit .js-card-details-requester'(event) {
  269. event.preventDefault();
  270. const requester = this.currentComponent()
  271. .getValue()
  272. .trim();
  273. if (requester) {
  274. this.data().setRequestedBy(requester);
  275. }
  276. },
  277. 'click .js-member': Popup.open('cardMember'),
  278. 'click .js-add-members': Popup.open('cardMembers'),
  279. 'click .js-assignee': Popup.open('cardAssignee'),
  280. 'click .js-add-assignees': Popup.open('cardAssignees'),
  281. 'click .js-add-labels': Popup.open('cardLabels'),
  282. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  283. 'click .js-start-date': Popup.open('editCardStartDate'),
  284. 'click .js-due-date': Popup.open('editCardDueDate'),
  285. 'click .js-end-date': Popup.open('editCardEndDate'),
  286. 'mouseenter .js-card-details'() {
  287. const parentComponent = this.parentComponent().parentComponent();
  288. //on mobile view parent is Board, not BoardBody.
  289. if (parentComponent === null) return;
  290. parentComponent.showOverlay.set(true);
  291. parentComponent.mouseHasEnterCardDetails = true;
  292. },
  293. 'mousedown .js-card-details'() {
  294. Session.set('cardDetailsIsDragging', false);
  295. Session.set('cardDetailsIsMouseDown', true);
  296. },
  297. 'mousemove .js-card-details'() {
  298. if (Session.get('cardDetailsIsMouseDown')) {
  299. Session.set('cardDetailsIsDragging', true);
  300. }
  301. },
  302. 'mouseup .js-card-details'() {
  303. Session.set('cardDetailsIsDragging', false);
  304. Session.set('cardDetailsIsMouseDown', false);
  305. },
  306. 'click #toggleButton'() {
  307. Meteor.call('toggleSystemMessages');
  308. },
  309. },
  310. ];
  311. },
  312. }).register('cardDetails');
  313. Template.cardDetails.helpers({
  314. userData() {
  315. // We need to handle a special case for the search results provided by the
  316. // `matteodem:easy-search` package. Since these results gets published in a
  317. // separate collection, and not in the standard Meteor.Users collection as
  318. // expected, we use a component parameter ("property") to distinguish the
  319. // two cases.
  320. const userCollection = this.esSearch ? ESSearchResults : Users;
  321. return userCollection.findOne(this.userId, {
  322. fields: {
  323. profile: 1,
  324. username: 1,
  325. },
  326. });
  327. },
  328. memberType() {
  329. const user = Users.findOne(this.userId);
  330. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  331. },
  332. presenceStatusClassName() {
  333. const user = Users.findOne(this.userId);
  334. const userPresence = presences.findOne({ userId: this.userId });
  335. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  336. else if (!userPresence) return 'disconnected';
  337. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  338. return 'active';
  339. else return 'idle';
  340. },
  341. });
  342. Template.userAvatarAssigneeInitials.helpers({
  343. initials() {
  344. const user = Users.findOne(this.userId);
  345. return user && user.getInitials();
  346. },
  347. viewPortWidth() {
  348. const user = Users.findOne(this.userId);
  349. return ((user && user.getInitials().length) || 1) * 12;
  350. },
  351. });
  352. // We extends the normal InlinedForm component to support UnsavedEdits draft
  353. // feature.
  354. (class extends InlinedForm {
  355. _getUnsavedEditKey() {
  356. return {
  357. fieldName: 'cardDescription',
  358. // XXX Recovering the currentCard identifier form a session variable is
  359. // fragile because this variable may change for instance if the route
  360. // change. We should use some component props instead.
  361. docId: Session.get('currentCard'),
  362. };
  363. }
  364. close(isReset = false) {
  365. if (this.isOpen.get() && !isReset) {
  366. const draft = this.getValue().trim();
  367. if (
  368. draft !== Cards.findOne(Session.get('currentCard')).getDescription()
  369. ) {
  370. UnsavedEdits.set(this._getUnsavedEditKey(), this.getValue());
  371. }
  372. }
  373. super.close();
  374. }
  375. reset() {
  376. UnsavedEdits.reset(this._getUnsavedEditKey());
  377. this.close(true);
  378. }
  379. events() {
  380. const parentEvents = InlinedForm.prototype.events()[0];
  381. return [
  382. {
  383. ...parentEvents,
  384. 'click .js-close-inlined-form': this.reset,
  385. },
  386. ];
  387. }
  388. }.register('inlinedCardDescription'));
  389. Template.cardDetailsActionsPopup.helpers({
  390. isWatching() {
  391. return this.findWatcher(Meteor.userId());
  392. },
  393. canModifyCard() {
  394. return (
  395. Meteor.user() &&
  396. Meteor.user().isBoardMember() &&
  397. !Meteor.user().isCommentOnly()
  398. );
  399. },
  400. });
  401. Template.cardDetailsActionsPopup.events({
  402. 'click .js-members': Popup.open('cardMembers'),
  403. 'click .js-assignees': Popup.open('cardAssignees'),
  404. 'click .js-labels': Popup.open('cardLabels'),
  405. 'click .js-attachments': Popup.open('cardAttachments'),
  406. 'click .js-custom-fields': Popup.open('cardCustomFields'),
  407. 'click .js-received-date': Popup.open('editCardReceivedDate'),
  408. 'click .js-start-date': Popup.open('editCardStartDate'),
  409. 'click .js-due-date': Popup.open('editCardDueDate'),
  410. 'click .js-end-date': Popup.open('editCardEndDate'),
  411. 'click .js-spent-time': Popup.open('editCardSpentTime'),
  412. 'click .js-move-card': Popup.open('moveCard'),
  413. 'click .js-copy-card': Popup.open('copyCard'),
  414. 'click .js-copy-checklist-cards': Popup.open('copyChecklistToManyCards'),
  415. 'click .js-set-card-color': Popup.open('setCardColor'),
  416. 'click .js-move-card-to-top'(event) {
  417. event.preventDefault();
  418. const minOrder = _.min(
  419. this.list()
  420. .cards(this.swimlaneId)
  421. .map(c => c.sort),
  422. );
  423. this.move(this.boardId, this.swimlaneId, this.listId, minOrder - 1);
  424. },
  425. 'click .js-move-card-to-bottom'(event) {
  426. event.preventDefault();
  427. const maxOrder = _.max(
  428. this.list()
  429. .cards(this.swimlaneId)
  430. .map(c => c.sort),
  431. );
  432. this.move(this.boardId, this.swimlaneId, this.listId, maxOrder + 1);
  433. },
  434. 'click .js-archive'(event) {
  435. event.preventDefault();
  436. this.archive();
  437. Popup.close();
  438. },
  439. 'click .js-more': Popup.open('cardMore'),
  440. 'click .js-toggle-watch-card'() {
  441. const currentCard = this;
  442. const level = currentCard.findWatcher(Meteor.userId()) ? null : 'watching';
  443. Meteor.call('watch', 'card', currentCard._id, level, (err, ret) => {
  444. if (!err && ret) Popup.close();
  445. });
  446. },
  447. });
  448. Template.editCardTitleForm.onRendered(function() {
  449. autosize(this.$('.js-edit-card-title'));
  450. });
  451. Template.editCardTitleForm.events({
  452. 'keydown .js-edit-card-title'(event) {
  453. // If enter key was pressed, submit the data
  454. // Unless the shift key is also being pressed
  455. if (event.keyCode === 13 && !event.shiftKey) {
  456. $('.js-submit-edit-card-title-form').click();
  457. }
  458. },
  459. });
  460. Template.editCardRequesterForm.onRendered(function() {
  461. autosize(this.$('.js-edit-card-requester'));
  462. });
  463. Template.editCardRequesterForm.events({
  464. 'keydown .js-edit-card-requester'(event) {
  465. // If enter key was pressed, submit the data
  466. if (event.keyCode === 13) {
  467. $('.js-submit-edit-card-requester-form').click();
  468. }
  469. },
  470. });
  471. Template.editCardAssignerForm.onRendered(function() {
  472. autosize(this.$('.js-edit-card-assigner'));
  473. });
  474. Template.editCardAssignerForm.events({
  475. 'keydown .js-edit-card-assigner'(event) {
  476. // If enter key was pressed, submit the data
  477. if (event.keyCode === 13) {
  478. $('.js-submit-edit-card-assigner-form').click();
  479. }
  480. },
  481. });
  482. Template.moveCardPopup.events({
  483. 'click .js-done'() {
  484. // XXX We should *not* get the currentCard from the global state, but
  485. // instead from a “component” state.
  486. const card = Cards.findOne(Session.get('currentCard'));
  487. const bSelect = $('.js-select-boards')[0];
  488. const boardId = bSelect.options[bSelect.selectedIndex].value;
  489. const lSelect = $('.js-select-lists')[0];
  490. const listId = lSelect.options[lSelect.selectedIndex].value;
  491. const slSelect = $('.js-select-swimlanes')[0];
  492. const swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  493. card.move(boardId, swimlaneId, listId, 0);
  494. Popup.close();
  495. },
  496. });
  497. BlazeComponent.extendComponent({
  498. onCreated() {
  499. subManager.subscribe('board', Session.get('currentBoard'), false);
  500. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  501. },
  502. boards() {
  503. const boards = Boards.find(
  504. {
  505. archived: false,
  506. 'members.userId': Meteor.userId(),
  507. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  508. },
  509. {
  510. sort: ['title'],
  511. },
  512. );
  513. return boards;
  514. },
  515. swimlanes() {
  516. const board = Boards.findOne(this.selectedBoardId.get());
  517. return board.swimlanes();
  518. },
  519. aBoardLists() {
  520. const board = Boards.findOne(this.selectedBoardId.get());
  521. return board.lists();
  522. },
  523. events() {
  524. return [
  525. {
  526. 'change .js-select-boards'(event) {
  527. this.selectedBoardId.set($(event.currentTarget).val());
  528. subManager.subscribe('board', this.selectedBoardId.get(), false);
  529. },
  530. },
  531. ];
  532. },
  533. }).register('boardsAndLists');
  534. Template.copyCardPopup.events({
  535. 'click .js-done'() {
  536. const card = Cards.findOne(Session.get('currentCard'));
  537. const lSelect = $('.js-select-lists')[0];
  538. listId = lSelect.options[lSelect.selectedIndex].value;
  539. const slSelect = $('.js-select-swimlanes')[0];
  540. const swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  541. const bSelect = $('.js-select-boards')[0];
  542. const boardId = bSelect.options[bSelect.selectedIndex].value;
  543. const textarea = $('#copy-card-title');
  544. const title = textarea.val().trim();
  545. // insert new card to the bottom of new list
  546. card.sort = Lists.findOne(card.listId)
  547. .cards()
  548. .count();
  549. if (title) {
  550. card.title = title;
  551. card.coverId = '';
  552. const _id = card.copy(boardId, swimlaneId, listId);
  553. // In case the filter is active we need to add the newly inserted card in
  554. // the list of exceptions -- cards that are not filtered. Otherwise the
  555. // card will disappear instantly.
  556. // See https://github.com/wekan/wekan/issues/80
  557. Filter.addException(_id);
  558. Popup.close();
  559. }
  560. },
  561. });
  562. Template.copyChecklistToManyCardsPopup.events({
  563. 'click .js-done'() {
  564. const card = Cards.findOne(Session.get('currentCard'));
  565. const oldId = card._id;
  566. card._id = null;
  567. const lSelect = $('.js-select-lists')[0];
  568. card.listId = lSelect.options[lSelect.selectedIndex].value;
  569. const slSelect = $('.js-select-swimlanes')[0];
  570. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  571. const bSelect = $('.js-select-boards')[0];
  572. card.boardId = bSelect.options[bSelect.selectedIndex].value;
  573. const textarea = $('#copy-card-title');
  574. const titleEntry = textarea.val().trim();
  575. // insert new card to the bottom of new list
  576. card.sort = Lists.findOne(card.listId)
  577. .cards()
  578. .count();
  579. if (titleEntry) {
  580. const titleList = JSON.parse(titleEntry);
  581. for (let i = 0; i < titleList.length; i++) {
  582. const obj = titleList[i];
  583. card.title = obj.title;
  584. card.description = obj.description;
  585. card.coverId = '';
  586. const _id = Cards.insert(card);
  587. // In case the filter is active we need to add the newly inserted card in
  588. // the list of exceptions -- cards that are not filtered. Otherwise the
  589. // card will disappear instantly.
  590. // See https://github.com/wekan/wekan/issues/80
  591. Filter.addException(_id);
  592. // copy checklists
  593. Checklists.find({ cardId: oldId }).forEach(ch => {
  594. ch.copy(_id);
  595. });
  596. // copy subtasks
  597. cursor = Cards.find({ parentId: oldId });
  598. cursor.forEach(function() {
  599. 'use strict';
  600. const subtask = arguments[0];
  601. subtask.parentId = _id;
  602. subtask._id = null;
  603. /* const newSubtaskId = */ Cards.insert(subtask);
  604. });
  605. // copy card comments
  606. CardComments.find({ cardId: oldId }).forEach(cmt => {
  607. cmt.copy(_id);
  608. });
  609. }
  610. Popup.close();
  611. }
  612. },
  613. });
  614. BlazeComponent.extendComponent({
  615. onCreated() {
  616. this.currentCard = this.currentData();
  617. this.currentColor = new ReactiveVar(this.currentCard.color);
  618. },
  619. colors() {
  620. return cardColors.map(color => ({ color, name: '' }));
  621. },
  622. isSelected(color) {
  623. if (this.currentColor.get() === null) {
  624. return color === 'white';
  625. }
  626. return this.currentColor.get() === color;
  627. },
  628. events() {
  629. return [
  630. {
  631. 'click .js-palette-color'() {
  632. this.currentColor.set(this.currentData().color);
  633. },
  634. 'click .js-submit'() {
  635. this.currentCard.setColor(this.currentColor.get());
  636. Popup.close();
  637. },
  638. 'click .js-remove-color'() {
  639. this.currentCard.setColor(null);
  640. Popup.close();
  641. },
  642. },
  643. ];
  644. },
  645. }).register('setCardColorPopup');
  646. BlazeComponent.extendComponent({
  647. onCreated() {
  648. this.currentCard = this.currentData();
  649. this.parentBoard = new ReactiveVar(null);
  650. this.parentCard = this.currentCard.parentCard();
  651. if (this.parentCard) {
  652. const list = $('.js-field-parent-card');
  653. list.val(this.parentCard._id);
  654. this.parentBoard.set(this.parentCard.board()._id);
  655. } else {
  656. this.parentBoard.set(null);
  657. }
  658. },
  659. boards() {
  660. const boards = Boards.find(
  661. {
  662. archived: false,
  663. 'members.userId': Meteor.userId(),
  664. _id: {
  665. $ne: Meteor.user().getTemplatesBoardId(),
  666. },
  667. },
  668. {
  669. sort: ['title'],
  670. },
  671. );
  672. return boards;
  673. },
  674. cards() {
  675. const currentId = Session.get('currentCard');
  676. if (this.parentBoard.get()) {
  677. return Cards.find({
  678. boardId: this.parentBoard.get(),
  679. _id: { $ne: currentId },
  680. });
  681. } else {
  682. return [];
  683. }
  684. },
  685. isParentBoard() {
  686. const board = this.currentData();
  687. if (this.parentBoard.get()) {
  688. return board._id === this.parentBoard.get();
  689. }
  690. return false;
  691. },
  692. isParentCard() {
  693. const card = this.currentData();
  694. if (this.parentCard) {
  695. return card._id === this.parentCard;
  696. }
  697. return false;
  698. },
  699. setParentCardId(cardId) {
  700. if (cardId) {
  701. this.parentCard = Cards.findOne(cardId);
  702. } else {
  703. this.parentCard = null;
  704. }
  705. this.currentCard.setParentId(cardId);
  706. },
  707. events() {
  708. return [
  709. {
  710. 'click .js-copy-card-link-to-clipboard'() {
  711. // Clipboard code from:
  712. // https://stackoverflow.com/questions/6300213/copy-selected-text-to-the-clipboard-without-using-flash-must-be-cross-browser
  713. const StringToCopyElement = document.getElementById('cardURL');
  714. StringToCopyElement.select();
  715. if (document.execCommand('copy')) {
  716. StringToCopyElement.blur();
  717. } else {
  718. document.getElementById('cardURL').selectionStart = 0;
  719. document.getElementById('cardURL').selectionEnd = 999;
  720. document.execCommand('copy');
  721. if (window.getSelection) {
  722. if (window.getSelection().empty) {
  723. // Chrome
  724. window.getSelection().empty();
  725. } else if (window.getSelection().removeAllRanges) {
  726. // Firefox
  727. window.getSelection().removeAllRanges();
  728. }
  729. } else if (document.selection) {
  730. // IE?
  731. document.selection.empty();
  732. }
  733. }
  734. },
  735. 'click .js-delete': Popup.afterConfirm('cardDelete', function() {
  736. Popup.close();
  737. Cards.remove(this._id);
  738. Utils.goBoardId(this.boardId);
  739. }),
  740. 'change .js-field-parent-board'(event) {
  741. const selection = $(event.currentTarget).val();
  742. const list = $('.js-field-parent-card');
  743. if (selection === 'none') {
  744. this.parentBoard.set(null);
  745. } else {
  746. subManager.subscribe('board', $(event.currentTarget).val(), false);
  747. this.parentBoard.set(selection);
  748. list.prop('disabled', false);
  749. }
  750. this.setParentCardId(null);
  751. },
  752. 'change .js-field-parent-card'(event) {
  753. const selection = $(event.currentTarget).val();
  754. this.setParentCardId(selection);
  755. },
  756. },
  757. ];
  758. },
  759. }).register('cardMorePopup');
  760. // Close the card details pane by pressing escape
  761. EscapeActions.register(
  762. 'detailsPane',
  763. () => {
  764. if (Session.get('cardDetailsIsDragging')) {
  765. // Reset dragging status as the mouse landed outside the cardDetails template area and this will prevent a mousedown event from firing
  766. Session.set('cardDetailsIsDragging', false);
  767. Session.set('cardDetailsIsMouseDown', false);
  768. } else {
  769. // Prevent close card when the user is selecting text and moves the mouse cursor outside the card detail area
  770. Utils.goBoardId(Session.get('currentBoard'));
  771. }
  772. },
  773. () => {
  774. return !Session.equals('currentCard', null);
  775. },
  776. {
  777. noClickEscapeOn: '.js-card-details,.board-sidebar,#header',
  778. },
  779. );
  780. Template.cardAssigneesPopup.events({
  781. 'click .js-select-assignee'(event) {
  782. const card = Cards.findOne(Session.get('currentCard'));
  783. const assigneeId = this.userId;
  784. card.toggleAssignee(assigneeId);
  785. event.preventDefault();
  786. },
  787. });
  788. Template.cardAssigneesPopup.helpers({
  789. isCardAssignee() {
  790. const card = Template.parentData();
  791. const cardAssignees = card.getAssignees();
  792. return _.contains(cardAssignees, this.userId);
  793. },
  794. user() {
  795. return Users.findOne(this.userId);
  796. },
  797. });
  798. Template.cardAssigneePopup.helpers({
  799. userData() {
  800. // We need to handle a special case for the search results provided by the
  801. // `matteodem:easy-search` package. Since these results gets published in a
  802. // separate collection, and not in the standard Meteor.Users collection as
  803. // expected, we use a component parameter ("property") to distinguish the
  804. // two cases.
  805. const userCollection = this.esSearch ? ESSearchResults : Users;
  806. return userCollection.findOne(this.userId, {
  807. fields: {
  808. profile: 1,
  809. username: 1,
  810. },
  811. });
  812. },
  813. memberType() {
  814. const user = Users.findOne(this.userId);
  815. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  816. },
  817. presenceStatusClassName() {
  818. const user = Users.findOne(this.userId);
  819. const userPresence = presences.findOne({ userId: this.userId });
  820. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  821. else if (!userPresence) return 'disconnected';
  822. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  823. return 'active';
  824. else return 'idle';
  825. },
  826. isCardAssignee() {
  827. const card = Template.parentData();
  828. const cardAssignees = card.getAssignees();
  829. return _.contains(cardAssignees, this.userId);
  830. },
  831. user() {
  832. return Users.findOne(this.userId);
  833. },
  834. });
  835. Template.cardAssigneePopup.events({
  836. 'click .js-remove-assignee'() {
  837. Cards.findOne(this.cardId).unassignAssignee(this.userId);
  838. Popup.close();
  839. },
  840. 'click .js-edit-profile': Popup.open('editProfile'),
  841. });