comments.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const commentFormIsOpen = new ReactiveVar(false);
  2. BlazeComponent.extendComponent({
  3. onDestroyed() {
  4. commentFormIsOpen.set(false);
  5. },
  6. commentFormIsOpen() {
  7. return commentFormIsOpen.get();
  8. },
  9. getInput() {
  10. return this.$('.js-new-comment-input');
  11. },
  12. events() {
  13. return [{
  14. 'click .js-new-comment:not(.focus)'() {
  15. commentFormIsOpen.set(true);
  16. },
  17. 'submit .js-new-comment-form'(evt) {
  18. const input = this.getInput();
  19. const text = input.val().trim();
  20. if (text) {
  21. CardComments.insert({
  22. text,
  23. boardId: this.currentData().boardId,
  24. cardId: this.currentData()._id,
  25. });
  26. resetCommentInput(input);
  27. Tracker.flush();
  28. autosize.update(input);
  29. }
  30. evt.preventDefault();
  31. },
  32. // Pressing Ctrl+Enter should submit the form
  33. 'keydown form textarea'(evt) {
  34. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  35. this.find('button[type=submit]').click();
  36. }
  37. },
  38. }];
  39. },
  40. }).register('commentForm');
  41. // XXX This should be a static method of the `commentForm` component
  42. function resetCommentInput(input) {
  43. input.val('');
  44. input.blur();
  45. commentFormIsOpen.set(false);
  46. }
  47. // XXX This should handled a `onUpdated` callback of the `commentForm` component
  48. // but since this callback doesn't exists, and `onRendered` is not called if the
  49. // data is not destroyed and recreated, we simulate the desired callback using
  50. // Tracker.autorun to register the component dependencies, and re-run when these
  51. // dependencies are invalidated. A better component API would remove this hack.
  52. Tracker.autorun(() => {
  53. Session.get('currentCard');
  54. Tracker.afterFlush(() => {
  55. autosize.update($('.js-new-comment-input'));
  56. });
  57. });
  58. EscapeActions.register('inlinedForm',
  59. () => {
  60. const draftKey = {
  61. fieldName: 'cardComment',
  62. docId: Session.get('currentCard'),
  63. };
  64. const commentInput = $('.js-new-comment-input');
  65. const draft = commentInput.val().trim();
  66. if (draft) {
  67. UnsavedEdits.set(draftKey, draft);
  68. } else {
  69. UnsavedEdits.reset(draftKey);
  70. }
  71. resetCommentInput(commentInput);
  72. },
  73. () => { return commentFormIsOpen.get(); }, {
  74. noClickEscapeOn: '.js-new-comment',
  75. }
  76. );