aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/coding-rules/rule/custom-rule-creation-view.js
blob: efa3a520e14d9ec8dbdb1faadb9f67a9cda22b0e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
define([
  'common/modal-form',
  'templates/coding-rules'
], function (ModalFormView) {

  var $ = jQuery;

  return ModalFormView.extend({
    template: Templates['coding-rules-custom-rule-creation'],

    ui: function () {
      return _.extend(ModalFormView.prototype.ui.apply(this, arguments), {
        customRuleCreationKey: '#coding-rules-custom-rule-creation-key',
        customRuleCreationName: '#coding-rules-custom-rule-creation-name',
        customRuleCreationHtmlDescription: '#coding-rules-custom-rule-creation-html-description',
        customRuleCreationSeverity: '#coding-rules-custom-rule-creation-severity',
        customRuleCreationStatus: '#coding-rules-custom-rule-creation-status',
        customRuleCreationParameters: '[name]',
        customRuleCreationCreate: '#coding-rules-custom-rule-creation-create',
        customRuleCreationReactivate: '#coding-rules-custom-rule-creation-reactivate',
        modalFoot: '.modal-foot'
      });
    },

    events: function () {
      return _.extend(ModalFormView.prototype.events.apply(this, arguments), {
        'input @ui.customRuleCreationName': 'generateKey',
        'keydown @ui.customRuleCreationName': 'generateKey',
        'keyup @ui.customRuleCreationName': 'generateKey',

        'input @ui.customRuleCreationKey': 'flagKey',
        'keydown @ui.customRuleCreationKey': 'flagKey',
        'keyup @ui.customRuleCreationKey': 'flagKey',

        'click #coding-rules-custom-rule-creation-cancel': 'close',
        'click @ui.customRuleCreationCreate': 'create',
        'click @ui.customRuleCreationReactivate': 'reactivate'
      });
    },

    generateKey: function () {
      if (!this.keyModifiedByUser && this.ui.customRuleCreationKey) {
        var generatedKey = this.ui.customRuleCreationName.val().latinize().replace(/[^A-Za-z0-9]/g, '_');
        this.ui.customRuleCreationKey.val(generatedKey);
      }
    },

    flagKey: function () {
      this.keyModifiedByUser = true;
      // Cannot use @ui.customRuleCreationReactivate.hide() directly since it was not there at initial render
      jQuery(this.ui.customRuleCreationReactivate.selector).hide();
    },

    onRender: function () {
      ModalFormView.prototype.onRender.apply(this, arguments);

      this.keyModifiedByUser = false;

      var format = function (state) {
            if (!state.id) {
              return state.text;
            } else {
              return '<i class="icon-severity-' + state.id.toLowerCase() + '"></i> ' + state.text;
            }
          },
          severity = (this.model && this.model.get('severity')) || this.options.templateRule.get('severity'),
          status = (this.model && this.model.get('status')) || this.options.templateRule.get('status');

      this.ui.customRuleCreationSeverity.val(severity);
      this.ui.customRuleCreationSeverity.select2({
        width: '250px',
        minimumResultsForSearch: 999,
        formatResult: format,
        formatSelection: format
      });

      this.ui.customRuleCreationStatus.val(status);
      this.ui.customRuleCreationStatus.select2({
        width: '250px',
        minimumResultsForSearch: 999
      });
    },

    create: function (e) {
      e.preventDefault();
      var action = (this.model && this.model.has('key')) ? 'update' : 'create',
          options = {
            name: this.ui.customRuleCreationName.val(),
            markdown_description: this.ui.customRuleCreationHtmlDescription.val(),
            severity: this.ui.customRuleCreationSeverity.val(),
            status: this.ui.customRuleCreationStatus.val()
          };
      if (this.model && this.model.has('key')) {
        options.key = this.model.get('key');
      } else {
        _.extend(options, {
          template_key: this.options.templateRule.get('key'),
          custom_key: this.ui.customRuleCreationKey.val(),
          prevent_reactivation: true
        });
      }
      var params = this.ui.customRuleCreationParameters.map(function () {
        var node = $(this),
            value = node.val();
        if (!value && action === 'create') {
          value = node.prop('placeholder') || '';
        }
        return {
          key: node.prop('name'),
          value: value
        };
      }).get();
      options.params = params.map(function (param) {
        return param.key + '=' + window.csvEscape(param.value);
      }).join(';');
      this.sendRequest(action, options);
    },

    reactivate: function () {
      var options = {
            name: this.existingRule.name,
            markdown_description: this.existingRule.mdDesc,
            severity: this.existingRule.severity,
            status: this.existingRule.status,
            template_key: this.existingRule.templateKey,
            custom_key: this.ui.customRuleCreationKey.val(),
            prevent_reactivation: false
          },
          params = this.existingRule.params;
      options.params = params.map(function (param) {
        return param.key + '=' + param.defaultValue;
      }).join(';');
      this.sendRequest('create', options);
    },

    sendRequest: function (action, options) {
      this.$('.modal-error').hide();
      this.$('.modal-warning').hide();
      var that = this,
          url = baseUrl + '/api/rules/' + action;
      return $.post(url, options).done(function () {
        that.options.app.controller.showDetails(that.options.templateRule);
        that.close();
      }).fail(function (jqXHR) {
        if (jqXHR.status === 409) {
          that.existingRule = jqXHR.responseJSON.rule;
          that.$('.modal-warning').show();
          that.ui.modalFoot.html(Templates['coding-rules-custom-rule-reactivation']());
        } else {
          that.showErrors(jqXHR.responseJSON.errors, jqXHR.responseJSON.warnings);
        }
      });
    },

    serializeData: function () {
      var params = {};
      if (this.options.templateRule) {
        params = this.options.templateRule.get('params');
      } else if (this.model && this.model.has('params')) {
        params = this.model.get('params').map(function (p) {
          _.extend(p, { value: p.defaultValue });
        });
      }

      var statuses = ['READY', 'BETA', 'DEPRECATED'].map(function (status) {
        return {
          id: status,
          text: t('rules.status', status.toLowerCase())
        };
      });

      return _.extend(ModalFormView.prototype.serializeData.apply(this, arguments), {
        change: this.model && this.model.has('key'),
        params: params,
        severities: ['BLOCKER', 'CRITICAL', 'MAJOR', 'MINOR', 'INFO'],
        statuses: statuses
      });
    }
  });

});