aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/issue/views/comment-form-view.js
blob: ee2f1c91e87ed550e5a7e233c4bcb1cc80edb4bc (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import $ from 'jquery';
import _ from 'underscore';
import PopupView from 'components/common/popup';
import '../templates';

export default PopupView.extend({
  className: 'bubble-popup issue-comment-bubble-popup',
  template: Templates['comment-form'],

  ui: {
    textarea: '.issue-comment-form-text textarea',
    cancelButton: '.js-issue-comment-cancel',
    submitButton: '.js-issue-comment-submit'
  },

  events: {
    'click': 'onClick',
    'keydown @ui.textarea': 'onKeydown',
    'keyup @ui.textarea': 'toggleSubmit',
    'click @ui.cancelButton': 'cancel',
    'click @ui.submitButton': 'submit'
  },

  onRender: function () {
    var that = this;
    PopupView.prototype.onRender.apply(this, arguments);
    setTimeout(function () {
      that.ui.textarea.focus();
    }, 100);
  },

  toggleSubmit: function () {
    this.ui.submitButton.prop('disabled', this.ui.textarea.val().length === 0);
  },

  onClick: function (e) {
    e.stopPropagation();
  },

  onKeydown: function (e) {
    if (e.keyCode === 27) {
      this.destroy();
    }
  },

  cancel: function () {
    this.options.detailView.updateAfterAction(false);
  },

  disableForm: function () {
    this.$(':input').prop('disabled', true);
  },

  enableForm: function () {
    this.$(':input').prop('disabled', false);
  },

  submit: function () {
    var that = this;
    var text = this.ui.textarea.val(),
        update = this.model && this.model.has('key'),
        method = update ? 'edit_comment' : 'add_comment',
        url = baseUrl + '/api/issues/' + method,
        data = { text: text };
    if (update) {
      data.key = this.model.get('key');
    } else {
      data.issue = this.options.issue.id;
    }
    this.disableForm();
    this.options.detailView.disableControls();
    return $.post(url, data)
        .done(function () {
          that.options.detailView.updateAfterAction(true);
        }).fail(function () {
          that.enableForm();
          that.options.detailView.enableControls();
        });
  },

  serializeData: function () {
    var options = _.defaults(this.options.additionalOptions, { fromTransition: false });
    return _.extend(PopupView.prototype.serializeData.apply(this, arguments), {
      options: options
    });
  }
});