aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/coding-rules/controller.js
blob: f22cfbb19a3822d0e897a034c8f3746eac515275 (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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import $ from 'jquery';
import _ from 'underscore';
import Controller from '../../components/navigator/controller';
import Rule from './models/rule';
import RuleDetailsView from './rule-details-view';

export default Controller.extend({
  pageSize: 200,
  ruleFields: [
    'name', 'lang', 'langName', 'sysTags', 'tags', 'status', 'severity'
  ],

  _searchParameters () {
    const fields = this.ruleFields.slice();
    const profile = this.app.state.get('query').qprofile;
    if (profile != null) {
      fields.push('actives');
      fields.push('params');
      fields.push('isTemplate');
      fields.push('severity');
    }
    const params = {
      p: this.app.state.get('page'),
      ps: this.pageSize,
      facets: this._facetsFromServer().join(),
      f: fields.join()
    };
    if (this.app.state.get('query').q == null) {
      _.extend(params, { s: 'name', asc: true });
    }
    return params;
  },

  fetchList (firstPage) {
    firstPage = firstPage == null ? true : firstPage;
    if (firstPage) {
      this.app.state.set({ selectedIndex: 0, page: 1 }, { silent: true });
    }

    this.hideDetails(firstPage);

    const that = this;
    const url = window.baseUrl + '/api/rules/search';
    const options = _.extend(this._searchParameters(), this.app.state.get('query'));
    return $.get(url, options).done(r => {
      const rules = that.app.list.parseRules(r);
      if (firstPage) {
        that.app.list.reset(rules);
      } else {
        that.app.list.add(rules);
      }
      that.app.list.setIndex();
      that.app.list.addExtraAttributes(that.app.languages, that.app.repositories);
      that.app.facets.reset(that._allFacets());
      that.app.facets.add(r.facets, { merge: true });
      that.enableFacets(that._enabledFacets());
      that.app.state.set({
        page: r.p,
        pageSize: r.ps,
        total: r.total,
        maxResultsReached: r.p * r.ps >= r.total
      });
      if (firstPage && that.isRulePermalink()) {
        that.showDetails(that.app.list.first());
      }
    });
  },

  isRulePermalink () {
    const query = this.app.state.get('query');
    return query.rule_key != null && this.app.list.length === 1;
  },

  requestFacet (id) {
    const url = window.baseUrl + '/api/rules/search';
    const facet = this.app.facets.get(id);
    const options = _.extend({ facets: id, ps: 1 }, this.app.state.get('query'));
    return $.get(url, options).done(r => {
      const facetData = _.findWhere(r.facets, { property: id });
      if (facetData) {
        facet.set(facetData);
      }
    });
  },

  parseQuery () {
    const q = Controller.prototype.parseQuery.apply(this, arguments);
    delete q.asc;
    delete q.s;
    return q;
  },

  getRuleDetails (rule) {
    const that = this;
    const url = window.baseUrl + '/api/rules/show';
    const options = {
      key: rule.id,
      actives: true
    };
    return $.get(url, options).done(data => {
      rule.set(data.rule);
      rule.addExtraAttributes(that.app.repositories);
    });
  },

  showDetails (rule) {
    const that = this;
    const ruleModel = typeof rule === 'string' ? new Rule({ key: rule }) : rule;
    this.app.layout.workspaceDetailsRegion.reset();
    this.getRuleDetails(ruleModel).done(data => {
      key.setScope('details');
      that.app.workspaceListView.unbindScrollEvents();
      that.app.state.set({ rule: ruleModel });
      that.app.workspaceDetailsView = new RuleDetailsView({
        app: that.app,
        model: ruleModel,
        actives: data.actives
      });
      that.app.layout.showDetails();
      that.app.layout.workspaceDetailsRegion.show(that.app.workspaceDetailsView);
    });
  },

  showDetailsForSelected () {
    const rule = this.app.list.at(this.app.state.get('selectedIndex'));
    this.showDetails(rule);
  },

  hideDetails (firstPage) {
    key.setScope('list');
    this.app.state.unset('rule');
    this.app.layout.workspaceDetailsRegion.reset();
    this.app.layout.hideDetails();
    this.app.workspaceListView.bindScrollEvents();
    if (firstPage) {
      this.app.workspaceListView.scrollTo();
    }
  },

  activateCurrent () {
    if (this.app.layout.detailsShow()) {
      this.app.workspaceDetailsView.$('#coding-rules-quality-profile-activate').click();
    } else {
      const rule = this.app.list.at(this.app.state.get('selectedIndex'));
      const ruleView = this.app.workspaceListView.children.findByModel(rule);
      ruleView.$('.coding-rules-detail-quality-profile-activate').click();
    }
  },

  deactivateCurrent () {
    if (!this.app.layout.detailsShow()) {
      const rule = this.app.list.at(this.app.state.get('selectedIndex'));
      const ruleView = this.app.workspaceListView.children.findByModel(rule);
      ruleView.$('.coding-rules-detail-quality-profile-deactivate').click();
    }
  }

});