aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/quality-gates/gate-condition-view.js
blob: 9ab319a78e7f35982f3ebd29e73c0ac264ff955d (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
90
91
92
93
94
95
96
97
98
99
100
import _ from 'underscore';
import Marionette from 'backbone.marionette';
import DeleteConditionView from './gate-conditions-delete-view';
import Template from './templates/quality-gate-detail-condition.hbs';

export default Marionette.ItemView.extend({
  tagName: 'tr',
  template: Template,

  modelEvents: {
    'change': 'render'
  },

  ui: {
    periodSelect: '[name=period]',
    operatorSelect: '[name=operator]',
    warningInput: '[name=warning]',
    errorInput: '[name=error]',
    actionsBox: '.quality-gate-condition-actions',
    updateButton: '.update-condition',
    deleteButton: '.delete-condition'
  },

  events: {
    'click @ui.updateButton': 'saveCondition',
    'click @ui.deleteButton': 'deleteCondition',
    'click .add-condition': 'saveCondition',
    'click .cancel-add-condition': 'cancelAddCondition',
    'keyup :input': 'enableUpdate',
    'change :input': 'enableUpdate'
  },

  onRender: function () {
    this.ui.warningInput.val(this.model.get('warning'));
    this.ui.errorInput.val(this.model.get('error'));

    this.ui.periodSelect.select2({
      allowClear: false,
      minimumResultsForSearch: 999
    });

    this.ui.operatorSelect.select2({
      allowClear: false,
      minimumResultsForSearch: 999
    });

    if (this.model.isNew()) {
      this.ui.periodSelect.select2('open');
    }
  },

  saveCondition: function () {
    var attrs = {
      gateId: this.model.isNew() ? this.options.gate.id : void 0,
      period: this.ui.periodSelect.val(),
      op: this.ui.operatorSelect.val(),
      warning: this.ui.warningInput.val(),
      error: this.ui.errorInput.val()
    };
    this.model.save(attrs, { wait: true });
  },

  deleteCondition: function () {
    new DeleteConditionView({
      model: this.model,
      metric: this.getMetric()
    }).render();
  },

  cancelAddCondition: function () {
    this.destroy();
  },

  enableUpdate: function () {
    this.ui.updateButton.prop('disabled', false);
  },

  getMetric: function () {
    var key = this.model.get('metric');
    return _.findWhere(this.options.metrics, { key: key });
  },

  isDiffMetric: function () {
    var key = this.model.get('metric');
    return key.indexOf('new_') === 0;
  },

  serializeData: function () {
    var period = _.findWhere(this.options.periods, { key: this.model.get('period') });
    return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), {
      canEdit: this.options.canEdit,
      periods: this.options.periods,
      periodText: period ? period.text : t('value'),
      metric: this.getMetric(),
      isDiffMetric: this.isDiffMetric()
    });
  }
});