aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/coffee/issue/manual-issue-view.coffee
blob: cf570fefc420b2c43b8fd95f7e1aea3f451c0129 (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
define [
  'backbone.marionette'
  'templates/issue'
], (
  Marionette
  Templates
) ->

  $ = jQuery
  API_ISSUE = "#{baseUrl}/api/issues/show"
  API_ADD_MANUAL_ISSUE = "#{baseUrl}/api/issues/create"


  class extends Marionette.ItemView
    template: Templates['manual-issue']


    events:
      'submit .js-manual-issue-form': 'formSubmit'
      'click .js-cancel': 'cancel'


    onRender: ->
      @delegateEvents()
      @$('[name=rule]').select2
        width: '250px'
        minimumResultsForSearch: 10
      @$('[name=rule]').select2 'open'
      if key?
        @key = key.getScope()
        key.setScope ''


    onClose: ->
      key.setScope @key if key? && @key?


    showSpinner: ->
      @$('.js-submit').hide()
      @$('.js-spinner').show()


    hideSpinner: ->
      @$('.js-submit').show()
      @$('.js-spinner').hide()


    validateFields: ->
      message = @$('[name=message]')
      unless message.val()
        message.addClass('invalid').focus()
        return false
      return true


    formSubmit: (e) ->
      e.preventDefault()
      return unless @validateFields()
      @showSpinner()
      data = $(e.currentTarget).serialize()
      $.post API_ADD_MANUAL_ISSUE, data
        .done (r) =>
          @addIssue r.issue.key
        .fail (r) =>
          @hideSpinner()
          if r.responseJSON?.errors?
            @showError _.pluck(r.responseJSON.errors, 'msg').join '. '


    addIssue: (key) ->
      $.get API_ISSUE, key: key, (r) =>
        @trigger 'add', r.issue
        @close()


    showError: (msg) ->
      @$('.code-issue-errors').removeClass('hidden').text msg


    cancel: (e) ->
      e.preventDefault()
      @close()


    serializeData: ->
      _.extend super,
        line: @options.line
        component: @options.component
        rules: _.sortBy @options.rules, 'name'