aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/coding-rules/rule/profile-activation-view.js
blob: a9c0bee72d09976455ef059fc9403e59a2d8b67d (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
define([
  'common/modals',
  'templates/coding-rules'
], function (Modal) {

  var $ = jQuery;

  return Modal.extend({
    template: Templates['coding-rules-profile-activation'],

    ui: {
      qualityProfileSelect: '#coding-rules-quality-profile-activation-select',
      qualityProfileSeverity: '#coding-rules-quality-profile-activation-severity',
      qualityProfileActivate: '#coding-rules-quality-profile-activation-activate',
      qualityProfileParameters: '[name]'
    },

    events: function () {
      return _.extend(Modal.prototype.events.apply(this, arguments), {
        'click @ui.qualityProfileActivate': 'activate'
      });
    },

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

      this.ui.qualityProfileSelect.select2({
        width: '250px',
        minimumResultsForSearch: 5
      });

      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.rule.get('severity');
      this.ui.qualityProfileSeverity.val(severity);
      this.ui.qualityProfileSeverity.select2({
        width: '250px',
        minimumResultsForSearch: 999,
        formatResult: format,
        formatSelection: format
      });
    },

    activate: function (e) {
      e.preventDefault();
      var that = this,
          p = window.process.addBackgroundProcess(),
          profileKey = this.ui.qualityProfileSelect.val(),
          params = this.ui.qualityProfileParameters.map(function () {
            return {
              key: $(this).prop('name'),
              value: $(this).val() || $(this).prop('placeholder') || ''
            };
          }).get(),
          paramsHash = (params.map(function (param) {
            return param.key + '=' + window.csvEscape(param.value);
          })).join(';');

      if (this.model) {
        profileKey = this.model.get('qProfile');
        if (!profileKey) {
          profileKey = this.model.get('key');
        }
      }

      var severity = this.ui.qualityProfileSeverity.val(),
          ruleKey = this.options.rule.get('key');

      this.close();

      return jQuery.ajax({
        type: 'POST',
        url: baseUrl + '/api/qualityprofiles/activate_rule',
        data: {
          profile_key: profileKey,
          rule_key: ruleKey,
          severity: severity,
          params: paramsHash
        }
      }).done(function () {
        that.options.app.controller.showDetails(that.options.rule);
        window.process.finishBackgroundProcess(p);
      }).fail(function () {
        that.options.app.controller.showDetails(that.options.rule);
        window.process.failBackgroundProcess(p);
      });
    },

    getAvailableQualityProfiles: function (lang) {
      var activeQualityProfiles = this.collection,
          inactiveProfiles = _.reject(this.options.app.qualityProfiles, function (profile) {
            return activeQualityProfiles.findWhere({ key: profile.key });
          });
      return _.filter(inactiveProfiles, function (profile) {
        return profile.lang === lang;
      });
    },

    serializeData: function () {
      var params = this.options.rule.get('params');
      if (this.model != null) {
        var modelParams = this.model.get('params');
        if (_.isArray(modelParams)) {
          params = params.map(function (p) {
            var parentParam = _.findWhere(modelParams, { key: p.key });
            if (parentParam != null) {
              _.extend(p, { value: parentParam.value });
            }
            return p;
          });
        }
      }

      var availableProfiles = this.getAvailableQualityProfiles(this.options.rule.get('lang'));

      return _.extend(Modal.prototype.serializeData.apply(this, arguments), {
        change: this.model && this.model.has('severity'),
        params: params,
        qualityProfiles: availableProfiles,
        severities: ['BLOCKER', 'CRITICAL', 'MAJOR', 'MINOR', 'INFO'],
        saveEnabled: !_.isEmpty(availableProfiles) || (this.model && this.model.get('qProfile')),
        isCustomRule: (this.model && this.model.has('templateKey')) || this.options.rule.has('templateKey')
      });
    }
  });

});