cardDetails.js 28 KB

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