aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/coffee/coding-rules/views/coding-rules-custom-rule-creation-view.coffee
blob: 04be236be4707c3ca40681db445803c80dc1387e (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
define [
  'backbone.marionette',
  'templates/coding-rules'
], (
  Marionette,
  Templates
) ->

  class CodingRulesCustomRuleCreationView extends Marionette.ItemView
    className: 'modal'
    template: Templates['coding-rules-custom-rule-creation']


    ui:
      customRuleCreationKey: '#coding-rules-custom-rule-creation-key'
      customRuleCreationName: '#coding-rules-custom-rule-creation-name'
      customRuleCreationHtmlDescription: '#coding-rules-custom-rule-creation-html-description'
      customRuleCreationSeverity: '#coding-rules-custom-rule-creation-severity'
      customRuleCreationStatus: '#coding-rules-custom-rule-creation-status'
      customRuleCreationParameters: '[name]'
      customRuleCreationCreate: '#coding-rules-custom-rule-creation-create'


    events:
      'input @ui.customRuleCreationName': 'generateKey'
      'keydown @ui.customRuleCreationName': 'generateKey'
      'keyup @ui.customRuleCreationName': 'generateKey'

      'input @ui.customRuleCreationKey': 'flagKey'
      'keydown @ui.customRuleCreationKey': 'flagKey'
      'keyup @ui.customRuleCreationKey': 'flagKey'

      'click #coding-rules-custom-rule-creation-cancel': 'hide'
      'click @ui.customRuleCreationCreate': 'create'


    generateKey: ->
      unless @keyModifiedByUser
        if @ui.customRuleCreationKey
          generatedKey = @ui.customRuleCreationName.val().latinize().replace(/[^A-Za-z0-9]/g, '_')
          @ui.customRuleCreationKey.val generatedKey

    flagKey: ->
      @keyModifiedByUser = true


    create: ->
      action = 'create'
      if @model and @model.has 'key'
        action = 'update'

      postData =
        name: @ui.customRuleCreationName.val()
        html_description: @ui.customRuleCreationHtmlDescription.val()
        severity: @ui.customRuleCreationSeverity.val()
        status: @ui.customRuleCreationStatus.val()

      if @model && @model.has 'key'
        postData.key = @model.get 'key'
      else
        postData.template_key = @templateRule.get 'key'
        postData.custom_key = @ui.customRuleCreationKey.val()

      params = @ui.customRuleCreationParameters.map(->
        key: jQuery(@).prop('name'), value: jQuery(@).val() || jQuery(@).prop('placeholder') || '').get()

      postData.params = (params.map (param) -> param.key + '=' + param.value).join(';')

      origFooter = @$('.modal-foot').html()
      @$('.modal-foot').html '<i class="spinner"></i>'

      jQuery.ajax
        type: 'POST'
        url: "#{baseUrl}/api/rules/" + action
        data: postData
      .done (r) =>
          @options.app.showRule r.rule.key
          @hide()
      .fail =>
          @$('.modal-foot').html origFooter


    onRender: ->
      @$el.dialog
        dialogClass: 'no-close',
        width: '600px',
        draggable: false,
        autoOpen: false,
        modal: true,
        minHeight: 50,
        resizable: false,
        title: null

      @keyModifiedByUser = false

      format = (state) ->
        return state.text unless state.id
        "<i class='icon-severity-#{state.id.toLowerCase()}'></i> #{state.text}"

      severity = (@model && @model.get 'severity') || @templateRule.get 'severity'
      @ui.customRuleCreationSeverity.val severity
      @ui.customRuleCreationSeverity.select2
        width: '250px'
        minimumResultsForSearch: 999
        formatResult: format
        formatSelection: format

      status = (@model && @model.get 'status') || @templateRule.get 'status'
      @ui.customRuleCreationStatus.val status
      @ui.customRuleCreationStatus.select2
        width: '250px'
        minimumResultsForSearch: 999


    show: ->
      @render()
      @$el.dialog 'open'


    hide: ->
      @$el.dialog 'close'


    serializeData: ->
      params = {}
      if @templateRule
        params = @templateRule.get 'params'
      else if @model
        params = @model.get('params').map (p) ->
          _.extend p, value: p.defaultValue

      _.extend super,
        change: @model && @model.has 'key'
        params: params
        severities: ['BLOCKER', 'CRITICAL', 'MAJOR', 'MINOR', 'INFO']
        statuses: _.map @options.app.statuses, (value, key) ->
          id: key
          text: value