aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/coffee/coding-rules/views/coding-rules-detail-view.coffee
blob: 67776649b39d239e1c42bf11fb854333246b30c7 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
define [
  'backbone'
  'backbone.marionette'
  'coding-rules/views/coding-rules-detail-quality-profiles-view'
  'coding-rules/views/coding-rules-detail-quality-profile-view'
  'coding-rules/views/coding-rules-detail-custom-rules-view'
  'coding-rules/views/coding-rules-detail-custom-rule-view'
  'coding-rules/views/coding-rules-parameter-popup-view'
  'coding-rules/views/coding-rules-debt-popup-view'
  'templates/coding-rules'
], (
  Backbone
  Marionette
  CodingRulesDetailQualityProfilesView
  CodingRulesDetailQualityProfileView
  CodingRulesDetailCustomRulesView
  CodingRulesDetailCustomRuleView
  CodingRulesParameterPopupView
  CodingRulesDebtPopupView
  Templates
) ->

  class CodingRulesDetailView extends Marionette.Layout
    template: Templates['coding-rules-detail']


    regions:
      qualityProfilesRegion: '#coding-rules-detail-quality-profiles'
      customRulesRegion: '.coding-rules-detail-custom-rules-section'
      customRulesListRegion: '#coding-rules-detail-custom-rules'
      contextRegion: '.coding-rules-detail-context'


    ui:
      tagsChange: '.coding-rules-detail-tags-change'
      tagInput: '.coding-rules-detail-tag-input'
      tagsEdit: '.coding-rules-detail-tag-edit'
      tagsEditDone: '.coding-rules-detail-tag-edit-done'
      tagsList: '.coding-rules-detail-tag-list'

      subcharacteristic: '.coding-rules-subcharacteristic'

      descriptionExtra: '#coding-rules-detail-description-extra'
      extendDescriptionLink: '#coding-rules-detail-extend-description'
      extendDescriptionForm: '.coding-rules-detail-extend-description-form'
      extendDescriptionSubmit: '#coding-rules-detail-extend-description-submit'
      extendDescriptionRemove: '#coding-rules-detail-extend-description-remove'
      extendDescriptionText: '#coding-rules-detail-extend-description-text'
      extendDescriptionSpinner: '#coding-rules-detail-extend-description-spinner'
      cancelExtendDescription: '#coding-rules-detail-extend-description-cancel'

      activateQualityProfile: '#coding-rules-quality-profile-activate'
      activateContextQualityProfile: '.coding-rules-detail-quality-profile-activate'
      changeQualityProfile: '.coding-rules-detail-quality-profile-update'
      createCustomRule: '#coding-rules-custom-rules-create'
      changeCustomRule: '#coding-rules-detail-custom-rule-change'
      changeManualRule: '#coding-rules-detail-manual-rule-change'
      deleteCustomRule: '#coding-rules-detail-rule-delete'


    events:
      'click @ui.tagsChange': 'changeTags'
      'click @ui.tagsEditDone': 'editDone'

      'click @ui.extendDescriptionLink': 'showExtendDescriptionForm'
      'click @ui.cancelExtendDescription': 'hideExtendDescriptionForm'
      'click @ui.extendDescriptionSubmit': 'submitExtendDescription'
      'click @ui.extendDescriptionRemove': 'removeExtendedDescription'

      'click @ui.activateQualityProfile': 'activateQualityProfile'
      'click @ui.activateContextQualityProfile': 'activateContextQualityProfile'
      'click @ui.changeQualityProfile': 'changeQualityProfile'
      'click @ui.createCustomRule': 'createCustomRule'
      'click @ui.changeCustomRule': 'changeCustomRule'
      'click @ui.changeManualRule': 'changeManualRule'
      'click @ui.deleteCustomRule': 'deleteRule'

      'click .coding-rules-detail-parameter-name': 'toggleParameterDescription'
      'click .coding-rules-subcharacteristic': 'showDebtPopup'

    initialize: (options) ->
      super options

      if @model.get 'params'
        origParams = @model.get('params')
        @model.set 'params', _.sortBy(origParams, 'key')

      _.map options.actives, (active) =>
        _.extend active, options.app.getQualityProfileByKey active.qProfile
      qualityProfiles = new Backbone.Collection options.actives,
        comparator: 'name'
      @qualityProfilesView = new CodingRulesDetailQualityProfilesView
        app: @options.app
        collection: qualityProfiles
        rule: @model

      unless @model.get 'isTemplate'
        qualityProfileKey = @options.app.getQualityProfile()

        if qualityProfileKey
          @contextProfile = qualityProfiles.findWhere qProfile: qualityProfileKey
          unless @contextProfile
            @contextProfile = new Backbone.Model
              key: qualityProfileKey, name: @options.app.qualityProfileFilter.view.renderValue()
          @contextQualityProfileView = new CodingRulesDetailQualityProfileView
            app: @options.app
            model: @contextProfile
            rule: @model
            qualityProfiles: qualityProfiles

          @listenTo @contextProfile, 'destroy', @hideContext

    onRender: ->
      @$el.find('.open-modal').modal()

      if @model.get 'isTemplate'
        @$(@contextRegion.el).hide()

        if _.isEmpty(@options.actives)
          @$(@qualityProfilesRegion.el).hide()
        else
          @qualityProfilesRegion.show @qualityProfilesView

        @$(@customRulesRegion.el).show()
        customRulesOriginal = @$(@customRulesRegion.el).html()

        @$(@customRulesRegion.el).html '<i class="spinner"></i>'

        customRules = new Backbone.Collection()
        jQuery.ajax
          url: "#{baseUrl}/api/rules/search"
          data:
            template_key: @model.get 'key'
            f: 'name,severity,params'
        .done (r) =>
          customRules.add r.rules

          # Protect against element disappearing due to navigation
          if @customRulesRegion
            if customRules.isEmpty() and not @options.app.canWrite
              @$(@customRulesRegion.el).hide()
            else
              @customRulesView = new CodingRulesDetailCustomRulesView
                app: @options.app
                collection: customRules
                templateRule: @model
              @$(@customRulesRegion.el).html customRulesOriginal
              @customRulesListRegion.show @customRulesView

      else
        @$(@customRulesRegion.el).hide()
        @$(@qualityProfilesRegion.el).show()
        @qualityProfilesRegion.show @qualityProfilesView

        if @options.app.getQualityProfile()
          @$(@contextRegion.el).show()
          @contextRegion.show @contextQualityProfileView
        else
          @$(@contextRegion.el).hide()

      that = @
      jQuery.ajax
        url: "#{baseUrl}/api/rules/tags"
      .done (r) =>
        if @ui.tagInput.select2
          # Prevent synchronization issue with navigation
          @ui.tagInput.select2
            tags: _.difference (_.difference r.tags, that.model.get 'tags'), that.model.get 'sysTags'
            width: '300px'

      @ui.tagsEdit.hide()

      @ui.extendDescriptionForm.hide()
      @ui.extendDescriptionSpinner.hide()


    toggleParameterDescription: (e) ->
      jQuery(e.currentTarget).next('.coding-rules-detail-parameter-description').toggle();


    showDebtPopup: (e) ->
      e.stopPropagation()
      jQuery('body').click()
      popup = new CodingRulesDebtPopupView
        model: @model
        app: @options.app
        triggerEl: jQuery(e.currentTarget)
      popup.render()
      false


    hideContext: ->
      @contextRegion.reset()
      @$(@contextRegion.el).hide()


    changeTags: ->
      if @ui.tagsEdit.show
        @ui.tagsEdit.show()
      if @ui.tagsList.hide
        @ui.tagsList.hide()
      key.setScope 'tags'
      key 'escape', 'tags', => @cancelEdit()


    cancelEdit: ->
      key.unbind 'escape', 'tags'
      if @ui.tagsList.show
        @ui.tagsList.show()
      if @ui.tagInput.select2
        @ui.tagInput.select2 'close'
      if @ui.tagsEdit.hide
        @ui.tagsEdit.hide()


    editDone: ->
      @ui.tagsEdit.html '<i class="spinner"></i>'
      tags = @ui.tagInput.val()
      jQuery.ajax
        type: 'POST'
        url: "#{baseUrl}/api/rules/update"
        data:
          key: @model.get 'key'
          tags: tags
      .done (r) =>
          @model.set 'tags', r.rule.tags
          @cancelEdit()
      .always =>
        @render()


    showExtendDescriptionForm: ->
      @ui.descriptionExtra.hide()
      @ui.extendDescriptionForm.show()
      key.setScope 'extraDesc'
      key 'escape', 'extraDesc', => @hideExtendDescriptionForm()
      @ui.extendDescriptionText.focus()


    hideExtendDescriptionForm: ->
      key.unbind 'escape', 'extraDesc'
      @ui.descriptionExtra.show()
      @ui.extendDescriptionForm.hide()


    submitExtendDescription: ->
      @ui.extendDescriptionForm.hide()
      @ui.extendDescriptionSpinner.show()
      jQuery.ajax
        type: 'POST'
        url: "#{baseUrl}/api/rules/update"
        dataType: 'json'
        data:
          key: @model.get 'key'
          markdown_note: @ui.extendDescriptionText.val()
      .done (r) =>
        @model.set
          htmlNote: r.rule.htmlNote
          mdNote: r.rule.mdNote
        @render()


    removeExtendedDescription: ->
      confirmDialog
        html: t 'coding_rules.remove_extended_description.confirm'
        yesHandler: =>
          @ui.extendDescriptionText.val ''
          @submitExtendDescription()


    activateQualityProfile: ->
      @options.app.codingRulesQualityProfileActivationView.model = null
      @options.app.codingRulesQualityProfileActivationView.show()


    activateContextQualityProfile: ->
      @options.app.codingRulesQualityProfileActivationView.model = @contextProfile
      @options.app.codingRulesQualityProfileActivationView.show()

    createCustomRule: ->
      @options.app.codingRulesCustomRuleCreationView.templateRule = @model
      @options.app.codingRulesCustomRuleCreationView.model = new Backbone.Model()
      @options.app.codingRulesCustomRuleCreationView.show()


    changeCustomRule: ->
      @options.app.codingRulesCustomRuleCreationView.model = @model
      @options.app.codingRulesCustomRuleCreationView.show()


    changeManualRule: ->
      @options.app.codingRulesManualRuleCreationView.model = @model
      @options.app.codingRulesManualRuleCreationView.show()


    deleteRule: ->
      ruleType = if @model.has('templateKey') then 'custom' else 'manual'
      confirmDialog
        title: t 'delete'
        html: tp "coding_rules.delete.#{ruleType}.confirm", @model.get('name')
        yesHandler: =>
          jQuery.ajax
            type: 'POST'
            url: "#{baseUrl}/api/rules/delete"
            data:
              key: @model.get 'key'
          .done =>
            @options.app.fetchFirstPage()
          .fail =>
            @options.app.showRule @model.get('key')


    serializeData: ->
      contextQualityProfile = @options.app.getQualityProfile()
      repoKey = @model.get 'repo'
      isManual = (@options.app.manualRepository().key == repoKey)
      isCustom = (@model.has 'templateKey')

      qualityProfilesVisible = not isManual
      if qualityProfilesVisible
        if @model.get 'isTemplate'
          qualityProfilesVisible = (not _.isEmpty(@options.actives))
        else
          qualityProfilesVisible = (@options.app.canWrite or not _.isEmpty(@options.actives))


      _.extend super,
        contextQualityProfile: contextQualityProfile
        contextQualityProfileName: @options.app.qualityProfileFilter.view.renderValue()
        qualityProfile: @contextProfile
        language: @options.app.languages[@model.get 'lang']
        repository: _.find(@options.app.repositories, (repo) -> repo.key == repoKey).name
        isManual: isManual
        canWrite: @options.app.canWrite
        isEditable: (@options.app.canWrite and (isManual or isCustom))
        qualityProfilesVisible: qualityProfilesVisible
        subcharacteristic: @options.app.getSubcharacteristicName(@model.get 'debtSubChar')
        createdAt: new Date(@model.get 'createdAt')
        allTags: _.union @model.get('sysTags'), @model.get('tags')