comments.js 2.3 KB

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