aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/quality-profiles/profile-details-view.js
blob: 10fc51c4a309f536044de5bec47ac7f96f6b1f0b (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
import $ from 'jquery';
import _ from 'underscore';
import Marionette from 'backbone.marionette';
import ChangeProfileParentView from './change-profile-parent-view';
import ProfileChangelogView from './profile-changelog-view';
import ProfileComparisonView from './profile-comparison-view';
import '../../components/common/select-list';
import './helpers';
import Template from './templates/quality-profiles-profile-details.hbs';

export default Marionette.LayoutView.extend({
  template: Template,

  regions: {
    changelogRegion: '#quality-profile-changelog',
    comparisonRegion: '#quality-profile-comparison'
  },

  modelEvents: {
    'change': 'onChange',
    'flashChangelog': 'flashChangelog'
  },

  events: {
    'click .js-profile': 'onProfileClick',
    'click #quality-profile-change-parent': 'onChangeParentClick'
  },

  onRender: function () {
    if (!this.model.get('isDefault')) {
      this.initProjectsSelect();
    }
    this.changelogRegion.show(new ProfileChangelogView({ model: this.model }));
    this.comparisonRegion.show(new ProfileComparisonView({ model: this.model }));
    if (this.options.anchor === 'changelog') {
      this.scrollToChangelog();
      this.flashChangelog();
    }
    if (this.options.anchor === 'comparison') {
      this.scrollToComparison();
    }
    this.$('#quality-profile-changelog-form input')
        .datepicker({
          dateFormat: 'yy-mm-dd',
          changeMonth: true,
          changeYear: true
        });
  },

  onChange: function () {
    var changed = Object.keys(this.model.changedAttributes());
    if (!(changed.length === 1 && changed[0] === 'projectCount')) {
      this.render();
    }
  },

  initProjectsSelect: function () {
    var key = this.model.get('key');
    this.projectsSelectList = new window.SelectList({
      el: this.$('#quality-profile-projects-list'),
      width: '100%',
      height: 200,
      readOnly: !this.options.canWrite,
      focusSearch: false,
      format: function (item) {
        return item.name;
      },
      searchUrl: baseUrl + '/api/qualityprofiles/projects?key=' + encodeURIComponent(key),
      selectUrl: baseUrl + '/api/qualityprofiles/add_project',
      deselectUrl: baseUrl + '/api/qualityprofiles/remove_project',
      extra: {
        profileKey: key
      },
      selectParameter: 'projectUuid',
      selectParameterValue: 'uuid',
      labels: {
        selected: window.t('quality_gates.projects.with'),
        deselected: window.t('quality_gates.projects.without'),
        all: window.t('quality_gates.projects.all'),
        noResults: window.t('quality_gates.projects.noResults')
      },
      tooltips: {
        select: window.t('quality_profiles.projects.select_hint'),
        deselect: window.t('quality_profiles.projects.deselect_hint')
      }
    });
    this.listenTo(this.projectsSelectList.collection, 'change:selected', this.onProjectsChange);
  },

  onProfileClick: function (e) {
    var key = $(e.currentTarget).data('key'),
        profile = this.model.collection.get(key);
    if (profile != null) {
      e.preventDefault();
      this.model.collection.trigger('select', profile);
    }
  },

  onChangeParentClick: function (e) {
    e.preventDefault();
    this.changeParent();
  },

  onProjectsChange: function () {
    this.model.collection.updateForLanguage(this.model.get('language'));
  },

  changeParent: function () {
    new ChangeProfileParentView({
      model: this.model
    }).render();
  },

  scrollTo: function (selector) {
    var el = this.$(selector),
        parent = el.scrollParent();
    var elOffset = el.offset(),
        parentOffset = parent.offset();
    if (parent.is(document)) {
      parentOffset = { top: 0 };
    }
    if (elOffset != null && parentOffset != null) {
      var scrollTop = elOffset.top - parentOffset.top - 53;
      parent.scrollTop(scrollTop);
    }
  },

  scrollToChangelog: function () {
    this.scrollTo('#quality-profile-changelog');
  },

  scrollToComparison: function () {
    this.scrollTo('#quality-profile-comparison');
  },

  getExporters: function () {
    var language = this.model.get('language');
    return this.options.exporters.filter(function (exporter) {
      return exporter.languages.indexOf(language) !== -1;
    });
  },

  flashChangelog: function () {
    var changelogEl = this.$(this.changelogRegion.el);
    changelogEl.addClass('flash in');
    setTimeout(function () {
      changelogEl.removeClass('in');
    }, 2000);
  },

  serializeData: function () {
    var key = this.model.get('key'),
        rulesSearchUrl = '/coding_rules#qprofile=' + encodeURIComponent(key) + '|activation=true';
    return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), {
      rulesSearchUrl: rulesSearchUrl,
      canWrite: this.options.canWrite,
      exporters: this.getExporters()
    });
  }
});