summaryrefslogtreecommitdiffstats
path: root/client/components/activities/comments.js
blob: d41b86dd3e5b76287ec4ace1536fbd966beee41e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var commentFormIsOpen = new ReactiveVar(false);

Template.commentForm.helpers({
  commentFormIsOpen: function() {
    return commentFormIsOpen.get();
  }
});

Template.commentForm.events({
  'click .js-new-comment:not(.focus)': function() {
    commentFormIsOpen.set(true);
  },
  'submit .js-new-comment-form': function(evt, tpl) {
    var input = tpl.$('.js-new-comment-input');
    if ($.trim(input.val())) {
      CardComments.insert({
        boardId: this.boardId,
        cardId: this._id,
        text: input.val()
      });
      input.val('');
      input.blur();
      commentFormIsOpen.set(false);
      Tracker.flush();
      autosize.update(input);
    }
    evt.preventDefault();
  },
  // Pressing Ctrl+Enter should submit the form
  'keydown form textarea': function(evt, tpl) {
    if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
      tpl.find('button[type=submit]').click();
    }
  }
});

Template.commentForm.onDestroyed(function() {
  commentFormIsOpen.set(false);
});

EscapeActions.register('inlinedForm',
  function() {
    commentFormIsOpen.set(false);
    $('.js-new-comment-input').blur();
  },
  function() { return commentFormIsOpen.get(); }, {
    noClickEscapeOn: '.js-new-comment'
  }
);