aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/coffee/component-viewer/header.coffee
blob: b98d8a31bda01bb7b1bfd5da98386672db5e8dbf (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
define [
  'backbone.marionette'
  'templates/component-viewer'

  'component-viewer/header/basic-header'
  'component-viewer/header/issues-header'
  'component-viewer/header/coverage-header'
  'component-viewer/header/duplications-header'
  'component-viewer/header/scm-header'
  'component-viewer/header/tests-header'
  'component-viewer/header/more-actions'

  'common/handlebars-extensions'
], (
  Marionette
  Templates

  BasicHeaderView
  IssuesHeaderView
  CoverageHeaderView
  DuplicationsHeaderView
  SCMHeaderView
  TestsHeaderView
  MoreActionsView
) ->

  $ = jQuery

  API_FAVORITE = "#{baseUrl}/api/favourites"
  API_EXTENSION = "#{baseUrl}/resource/extension"
  BARS = [
    { scope: 'basic', view: BasicHeaderView }
    { scope: 'issues', view: IssuesHeaderView }
    { scope: 'coverage', view: CoverageHeaderView }
    { scope: 'duplications', view: DuplicationsHeaderView }
    { scope: 'scm', view: SCMHeaderView }
    { scope: 'tests', view: TestsHeaderView }
  ]


  class extends Marionette.Layout
    template: Templates['header']


    regions:
      barRegion: '.component-viewer-header-expanded-bar'


    ui:
      expandLinks: '.component-viewer-header-measures-expand'
      expandedBar: '.component-viewer-header-expanded-bar'
      spinnerBar: '.component-viewer-header-expanded-bar[data-scope=spinner]'
      unitTests: '.js-unit-test'


    events:
      'click .js-favorite': 'toggleFavorite'
      'click .js-actions': 'showMoreActions'
      'click .js-extension-close': 'closeExtension'
      'click .js-permalink': 'getPermalink'
      'click @ui.expandLinks': 'showExpandedBar'
      'click .js-toggle-issues': 'toggleIssues'
      'click .js-toggle-coverage': 'toggleCoverage'
      'click .js-toggle-duplications': 'toggleDuplications'
      'click .js-toggle-scm': 'toggleSCM'


    initialize: (options) ->
      options.main.settings.on 'change', => @changeSettings()
      @state = options.main.state
      @component = options.main.component
      @settings = options.main.component


    onRender: ->
      @delegateEvents()
      activeHeaderTab = @state.get 'activeHeaderTab'
      activeHeaderItem = @state.get 'activeHeaderItem'
      if activeHeaderTab
        if _.findWhere(BARS, scope: activeHeaderTab)?
          @enableBar(activeHeaderTab).done =>
            if activeHeaderItem
              @enableBarItem activeHeaderItem, @silentUpdate
        else
          @showExtension activeHeaderTab
      @silentUpdate = false


    toggleFavorite: ->
      component = @component
      if component.get 'fav'
        $.ajax
          url: "#{API_FAVORITE}/#{component.get 'key'}"
          type: 'DELETE'
        .done =>
          component.set 'fav', false
          @render()
      else
        $.ajax
          url: API_FAVORITE
          type: 'POST'
          data: key: component.get 'key'
        .done =>
          component.set 'fav', true
          @render()


    showMoreActions: (e) ->
      e.stopPropagation()
      $('body').click()
      view = new MoreActionsView main: @options.main
      view.render().$el.appendTo @$el


    showExtension: (key) ->
      bar = @ui.expandedBar
      bar.html('<i class="spinner spinner-margin"></i>').addClass 'active'
      @ui.expandLinks.removeClass 'active'
      $.get API_EXTENSION, id: @options.main.component.get('key'), tab: key, (r) =>
        bar.html r


    closeExtension: (e) ->
      e.preventDefault()
      @ui.expandedBar.html('').removeClass 'active'


    showBarSpinner: ->
      @ui.spinnerBar.addClass 'active'


    hideBarSpinner: ->
      @ui.spinnerBar.removeClass 'active'


    resetBars: ->
      @state.set 'activeHeaderTab', null
      @ui.expandLinks.removeClass 'active'
      @ui.expandedBar.removeClass 'active'
      @barRegion.reset()


    enableBar: (scope) ->
      @ui.expandedBar.html('<i class="spinner spinner-margin"></i>').addClass 'active'
      requests = []
      unless @state.get 'hasMeasures'
        requests.push @options.main.requestMeasures @options.main.key
      if @component.get('isUnitTest') && !@state.get('hasTests')
        requests.push @options.main.requestTests @options.main.key
      $.when.apply($, requests).done =>
        @state.set 'activeHeaderTab', scope
        bar = _.findWhere BARS, scope: scope
        @barRegion.show new bar.view
          main: @options.main, state: @state, component: @component, settings: @settings, source: @model, header: @
        @ui.expandedBar.addClass 'active'
        @ui.expandLinks.filter("[data-scope=#{scope}]").addClass 'active'
        activeHeaderItem = @state.get 'activeHeaderItem'
        if activeHeaderItem
          @$(activeHeaderItem).addClass 'active'


    enableBarItem: (item, silent = false) ->
      $item = @$(item)
      if $item.length > 0
        if silent then @$(item).addClass('active') else  @$(item).click()
      else
        @options.main.hideAllLines()


    showExpandedBar: (e) ->
      el = $(e.currentTarget)
      active = el.is '.active'
      @resetBars()
      unless active
        el.addClass 'active'
        scope = el.data 'scope'
        @enableBar scope


    changeSettings: ->
      @$('.js-toggle-issues').toggleClass 'active', @options.main.settings.get 'issues'
      @$('.js-toggle-coverage').toggleClass 'active', @options.main.settings.get 'coverage'
      @$('.js-toggle-duplications').toggleClass 'active', @options.main.settings.get 'duplications'
      @$('.js-toggle-scm').toggleClass 'active', @options.main.settings.get 'scm'


    toggleSetting: (e, show, hide) ->
      @showBlocks = []
      active = $(e.currentTarget).is '.active'
      if active
        hide.call @options.main, true
      else
        show.call @options.main, true


    toggleIssues: (e) -> @toggleSetting e, @options.main.showIssues, @options.main.hideIssues
    toggleCoverage: (e) -> @toggleSetting e, @options.main.showCoverage, @options.main.hideCoverage
    toggleDuplications: (e) -> @toggleSetting e, @options.main.showDuplications, @options.main.hideDuplications
    toggleSCM: (e) -> @toggleSetting e, @options.main.showSCM, @options.main.hideSCM
    toggleWorkspace: (e) -> @toggleSetting e, @options.main.showWorkspace, @options.main.hideWorkspace


    showTimeChangesSpinner: ->
      @$('.component-viewer-header-time-changes').html '<i class="spinner spinner-margin"></i>'


    filterLines: (e, methodName, extra) ->
      @$('.component-viewer-header-expanded-bar-section-list .active').removeClass 'active'
      $(e.currentTarget).addClass 'active'
      method = @options.main[methodName]
      method.call @options.main, extra


    serializeShowBlocks: ->
      blocks = @options.main.sourceView.showBlocks.map (b) -> "#{b.from},#{b.to}"
      blocks.join ';'


    getPermalink: ->
      params = []
      params.push key: 'component', value: @options.main.component.get 'key'
      settings = []
      _.map @options.main.settings.toJSON(), (v, k) -> settings.push k if v
      params.push key: 'settings', value: settings.join ','
      params.push key: 'blocks', value: @serializeShowBlocks()
      activeHeaderTab = @state.get 'activeHeaderTab'
      if activeHeaderTab
        params.push key: 'tab', value: activeHeaderTab
      activeHeaderItem = @state.get 'activeHeaderItem'
      if activeHeaderItem
        params.push key: 'item', value: activeHeaderItem
      highlightedLine = @options.main.sourceView.highlightedLine
      period = @state.get 'period'
      if period?
        params.push key: 'period', value: period.get 'key'
      if @options.main.currentIssue
        params.push key: 'currentIssue', value: @options.main.currentIssue
      if highlightedLine
        params.push key: 'line', value: highlightedLine
      hash = params.map((d) -> "#{d.key}=#{encodeURIComponent(d.value)}" ).join '&'

      windowParams = 'resizable=1,scrollbars=1,status=1'
      window.open "#{baseUrl}/component/index##{hash}", @options.main.component.get('name'), windowParams



    serializeData: ->
      component = @component.toJSON()
      if component.measures
        component.measures.maxIssues = Math.max(
          component.measures.fBlockerIssues || 0
          component.measures.fCriticalIssues || 0
          component.measures.fMajorIssues || 0
          component.measures.fMinorIssues || 0
          component.measures.fInfoIssues || 0
        )

      settings: @options.main.settings.toJSON()
      state: @state.toJSON()
      showSettings: @showSettings
      component: component
      currentIssue: @options.main.currentIssue