]> source.dussan.org Git - sonarqube.git/blob
785075e167faea98c105ac71c349d5c4077adced
[sonarqube.git] /
1 define [
2   'backbone.marionette',
3   'templates/coding-rules'
4 ], (
5   Marionette,
6   Templates
7 ) ->
8
9   class CodingRulesQualityProfileActivationView extends Marionette.ItemView
10     className: 'modal coding-rules-modal'
11     template: Templates['coding-rules-quality-profile-activation']
12
13
14     ui:
15       qualityProfileSelect: '#coding-rules-quality-profile-activation-select'
16       qualityProfileSeverity: '#coding-rules-quality-profile-activation-severity'
17       qualityProfileActivate: '#coding-rules-quality-profile-activation-activate'
18       qualityProfileParameters: '[name]'
19
20
21     events:
22       'click #coding-rules-quality-profile-activation-cancel': 'hide'
23       'click @ui.qualityProfileActivate': 'activate'
24
25
26     activate: ->
27       profileKey = @ui.qualityProfileSelect.val()
28       params = @ui.qualityProfileParameters.map(->
29         key: jQuery(@).prop('name'), value: jQuery(@).val() || jQuery(@).prop('placeholder') || '').get()
30
31       paramsHash = (params.map (param) -> param.key + '=' + param.value).join(';')
32
33       if @model
34         profileKey = @model.get('qProfile')
35         unless profileKey
36           profileKey = @model.get('key')
37       severity = @ui.qualityProfileSeverity.val()
38
39       origFooter = @$('.modal-foot').html()
40       @$('.modal-foot').html '<i class="spinner"></i>'
41
42       ruleKey = @rule.get('key')
43       jQuery.ajax
44         type: 'POST'
45         url: "#{baseUrl}/api/qualityprofiles/activate_rule"
46         data:
47           profile_key: profileKey
48           rule_key: ruleKey
49           severity: severity
50           params: paramsHash
51       .done =>
52           @options.app.showRule ruleKey
53           @hide()
54       .fail =>
55           @$('.modal-foot').html origFooter
56
57
58     onRender: ->
59       @$el.dialog
60         dialogClass: 'no-close',
61         width: '600px',
62         draggable: false,
63         autoOpen: false,
64         modal: true,
65         minHeight: 50,
66         resizable: false,
67         title: null
68
69       @ui.qualityProfileSelect.select2
70         width: '250px'
71         minimumResultsForSearch: 5
72
73       format = (state) ->
74         return state.text unless state.id
75         "<i class='icon-severity-#{state.id.toLowerCase()}'></i> #{state.text}"
76
77       severity = (@model && @model.get 'severity') || @rule.get 'severity'
78       @ui.qualityProfileSeverity.val severity
79       @ui.qualityProfileSeverity.select2
80         width: '250px'
81         minimumResultsForSearch: 999
82         formatResult: format
83         formatSelection: format
84
85
86     show: ->
87       @render()
88       @$el.dialog 'open'
89
90
91     hide: ->
92       @$el.dialog 'close'
93
94
95     getAvailableQualityProfiles: (lang) ->
96       activeQualityProfiles =  @options.app.detailView.qualityProfilesView.collection
97       inactiveProfiles = _.reject @options.app.qualityProfiles, (profile) =>
98         activeQualityProfiles.findWhere key: profile.key
99       _.filter inactiveProfiles, (profile) =>
100         profile.lang == lang
101
102
103     serializeData: ->
104       params = @rule.get 'params'
105       if @model
106         modelParams = @model.get 'params'
107         if modelParams
108           params = params.map (p) ->
109             parentParam = _.findWhere(modelParams, key: p.key)
110             if parentParam
111               _.extend p, value: _.findWhere(modelParams, key: p.key).value
112             else
113               p
114
115       availableProfiles = @getAvailableQualityProfiles(@rule.get 'lang')
116
117       _.extend super,
118         rule: @rule.toJSON()
119         change: @model && @model.has 'severity'
120         params: params
121         qualityProfiles: availableProfiles
122         severities: ['BLOCKER', 'CRITICAL', 'MAJOR', 'MINOR', 'INFO']
123         saveEnabled: not _.isEmpty(availableProfiles) or (@model and @model.get('qProfile'))
124         isCustomRule: (@model and @model.has('templateKey')) or @rule.has 'templateKey'