comments.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. const commentFormIsOpen = new ReactiveVar(false);
  3. BlazeComponent.extendComponent({
  4. onDestroyed() {
  5. commentFormIsOpen.set(false);
  6. $('.note-popover').hide();
  7. },
  8. commentFormIsOpen() {
  9. return commentFormIsOpen.get();
  10. },
  11. getInput() {
  12. return this.$('.js-new-comment-input');
  13. },
  14. events() {
  15. return [
  16. {
  17. 'submit .js-new-comment-form'(evt) {
  18. const input = this.getInput();
  19. const text = input.val().trim();
  20. const card = this.currentData();
  21. let boardId = card.boardId;
  22. let cardId = card._id;
  23. if (card.isLinkedCard()) {
  24. boardId = ReactiveCache.getCard(card.linkedId).boardId;
  25. cardId = card.linkedId;
  26. } else if (card.isLinkedBoard()) {
  27. boardId = card.linkedId;
  28. }
  29. if (text) {
  30. CardComments.insert({
  31. text,
  32. boardId,
  33. cardId,
  34. });
  35. resetCommentInput(input);
  36. Tracker.flush();
  37. autosize.update(input);
  38. input.trigger('submitted');
  39. }
  40. evt.preventDefault();
  41. },
  42. // Pressing Ctrl+Enter should submit the form
  43. 'keydown form textarea'(evt) {
  44. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  45. this.find('button[type=submit]').click();
  46. }
  47. },
  48. },
  49. ];
  50. },
  51. }).register('commentForm');
  52. BlazeComponent.extendComponent({
  53. getComments() {
  54. const ret = this.data().comments();
  55. return ret;
  56. },
  57. }).register("comments");
  58. BlazeComponent.extendComponent({
  59. events() {
  60. return [
  61. {
  62. 'click .js-delete-comment': Popup.afterConfirm('deleteComment', () => {
  63. const commentId = this.data()._id;
  64. CardComments.remove(commentId);
  65. Popup.back();
  66. }),
  67. 'submit .js-edit-comment'(evt) {
  68. evt.preventDefault();
  69. const commentText = this.currentComponent()
  70. .getValue()
  71. .trim();
  72. const commentId = this.data()._id;
  73. if (commentText) {
  74. CardComments.update(commentId, {
  75. $set: {
  76. text: commentText,
  77. },
  78. });
  79. }
  80. },
  81. },
  82. ];
  83. },
  84. }).register("comment");
  85. // XXX This should be a static method of the `commentForm` component
  86. function resetCommentInput(input) {
  87. input.val(''); // without manually trigger, input event won't be fired
  88. input.blur();
  89. commentFormIsOpen.set(false);
  90. }
  91. // XXX This should handled a `onUpdated` callback of the `commentForm` component
  92. // but since this callback doesn't exists, and `onRendered` is not called if the
  93. // data is not destroyed and recreated, we simulate the desired callback using
  94. // Tracker.autorun to register the component dependencies, and re-run when these
  95. // dependencies are invalidated. A better component API would remove this hack.
  96. Tracker.autorun(() => {
  97. Utils.getCurrentCardId();
  98. Tracker.afterFlush(() => {
  99. autosize.update($('.js-new-comment-input'));
  100. });
  101. });
  102. EscapeActions.register(
  103. 'inlinedForm',
  104. () => {
  105. const draftKey = {
  106. fieldName: 'cardComment',
  107. docId: Utils.getCurrentCardId(),
  108. };
  109. const commentInput = $('.js-new-comment-input');
  110. const draft = commentInput.val().trim();
  111. if (draft) {
  112. UnsavedEdits.set(draftKey, draft);
  113. } else {
  114. UnsavedEdits.reset(draftKey);
  115. }
  116. resetCommentInput(commentInput);
  117. },
  118. () => {
  119. return commentFormIsOpen.get();
  120. },
  121. {
  122. noClickEscapeOn: '.js-new-comment',
  123. },
  124. );