summaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/coffee/issues/controller.coffee
blob: d937228a9e8c22691bf0047b8b6b51ab9f78e866 (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
define [
  'backbone.marionette'

  'issues/component-viewer/main'
  'issues/component-viewer/state'
], (
  Marionette

  ComponentViewer
  ComponentViewerState
) ->

  $ = jQuery
  EXTRA_FIELDS = 'actions,transitions,assigneeName,reporterName,actionPlanName'
  PAGE_SIZE = 50
  ALL_FACETS = ['severities', 'statuses', 'resolutions', 'projectUuids', 'moduleUuids', 'componentUuids', 'assignees', 'reporters', 'rules',
                'tags', 'languages', 'actionPlans', 'creationDate', 'issues']
  FACET_DATA_FIELDS = ['components', 'projects', 'users', 'rules', 'actionPlans', 'languages']
  FACETS_FROM_SERVER = ['severities', 'statuses', 'resolutions', 'actionPlans', 'projectUuids', 'rules', 'tags'
                        'assignees', 'reporters', 'componentUuids', 'languages']
  TRANSFORM = {
    'resolved': 'resolutions'
    'assigned': 'assignees'
    'planned': 'actionPlans'
    'createdAt': 'creationDate'
    'createdBefore': 'creationDate'
    'createdAfter': 'creationDate'
  }


  class extends Marionette.Controller

    initialize: (options) ->
      @listenTo options.app.state, 'change:query', @fetchIssues


    _issuesParameters: ->
      p: @options.app.state.get 'page'
      ps: PAGE_SIZE
      s: 'FILE_LINE'
      asc: true
      extra_fields: EXTRA_FIELDS
      facets: @_facetsFromServer().join()


    _allFacets: ->
      ALL_FACETS.map (facet) -> { property: facet }


    _enabledFacets: ->
      facets = @options.app.state.get 'facets'
      criteria = Object.keys @options.app.state.get 'query'
      facets = facets.concat criteria
      facets = facets.map (facet) ->
        if TRANSFORM[facet]? then TRANSFORM[facet] else facet
      facets.filter (facet) -> ALL_FACETS.indexOf(facet) != -1


    _facetsFromServer: ->
      facets = @_enabledFacets()
      facets.filter (facet) -> FACETS_FROM_SERVER.indexOf(facet) != -1


    fetchIssues: (firstPage = true) ->
      if firstPage
        @options.app.state.set { selectedIndex: 0, page: 1 }, { silent: true }
        @closeComponentViewer()

      data = @_issuesParameters()
      _.extend data, @options.app.state.get 'query'

      fetchIssuesProcess = window.process.addBackgroundProcess()
      $.get "#{baseUrl}/api/issues/search", data
      .done (r) =>
        issues = @options.app.issues.parseIssues r
        if firstPage
          @options.app.issues.reset issues
        else
          @options.app.issues.add issues
        @options.app.issues.setIndex()
        FACET_DATA_FIELDS.forEach (field) => @options.app.facets[field] = r[field]
        @options.app.facets.reset @_allFacets()
        @options.app.facets.add r.facets, merge: true
        @enableFacets @_enabledFacets()
        @options.app.state.set
          page: r.p
          pageSize: r.ps
          total: r.total
          maxResultsReached: r.p * r.ps >= r.total
        window.process.finishBackgroundProcess fetchIssuesProcess
      .fail ->
        window.process.failBackgroundProcess fetchIssuesProcess


    fetchNextPage: ->
      @options.app.state.nextPage()
      @fetchIssues false


    fetchFilters: ->
      $.get "#{baseUrl}/api/issue_filters/app", (r) =>
        @options.app.state.set
          canBulkChange: r.canBulkChange
          canManageFilters: r.canManageFilters
        @options.app.filters.reset r.favorites


    enableFacet: (id) ->
      facet = @options.app.facets.get id
      if facet.has('values') || FACETS_FROM_SERVER.indexOf(id) == -1
        facet.set enabled: true
      else
        p = window.process.addBackgroundProcess()
        @requestFacet(id)
        .done =>
          facet.set enabled: true
          window.process.finishBackgroundProcess p
        .fail ->
          window.process.failBackgroundProcess p


    disableFacet: (id) ->
      facet = @options.app.facets.get id
      facet.set enabled: false
      @options.app.facetsView.children.findByModel(facet).disable()


    toggleFacet: (id) ->
      facet = @options.app.facets.get id
      if facet.get('enabled') then @disableFacet(id) else @enableFacet(id)


    enableFacets: (facets) ->
      facets.forEach @enableFacet, @


    _mergeCollections: (a, b) ->
      collection = new Backbone.Collection a
      collection.add b, merge: true
      collection.toJSON()


    requestFacet: (id) ->
      facet = @options.app.facets.get id
      data = _.extend { facets: id, ps: 1 }, @options.app.state.get('query')
      $.get "#{baseUrl}/api/issues/search", data, (r) =>
        FACET_DATA_FIELDS.forEach (field) =>
          @options.app.facets[field] = @_mergeCollections @options.app.facets[field], r[field]
        facetData = _.findWhere r.facets, property: id
        facet.set facetData if facetData?


    newSearch: ->
      @options.app.state.unset 'filter'
      @options.app.state.setQuery resolved: 'false'


    applyFilter: (filter, ignoreQuery = false) ->
      unless ignoreQuery
        filterQuery = @parseQuery filter.get 'query'
        @options.app.state.setQuery filterQuery
      @options.app.state.set filter: filter, changed: false


    parseQuery: (query, separator = '|') ->
      q = {}
      (query || '').split(separator).forEach (t) ->
        tokens = t.split('=')
        if tokens[0] && tokens[1]?
          q[tokens[0]] = decodeURIComponent tokens[1]
      # Do not allow to modify the sorting
      delete q.asc
      delete q.s
      q


    getQuery: (separator = '|') ->
      filter = @options.app.state.get 'query'
      route = []
      _.map filter, (value, property) ->
        route.push "#{property}=#{encodeURIComponent value}"
      route.join separator


    getRoute: (separator = '|') ->
      filter = @options.app.state.get 'filter'
      query = @getQuery separator
      if filter?
        if @options.app.state.get('changed') && query.length > 0
          query = "id=#{filter.id}|#{query}"
        else
          query = "id=#{filter.id}"
      query


    _prepareComponent: (issue) ->
      key: issue.get 'component'
      name: issue.get 'componentLongName'
      qualifier: issue.get 'componentQualifier'
      project: issue.get 'project'
      projectName: issue.get 'projectLongName'


    showComponentViewer: (issue) ->
      @options.app.layout.workspaceComponentViewerRegion.reset()
      key.setScope 'componentViewer'
      @options.app.issuesView.unbindScrollEvents()
      @options.app.state.set 'component', @_prepareComponent(issue)
      @options.app.componentViewer = new ComponentViewer app: @options.app
      @options.app.layout.workspaceComponentViewerRegion.show @options.app.componentViewer
      @options.app.layout.showComponentViewer()
      @options.app.componentViewer.openFileByIssue issue


    closeComponentViewer: ->
      key.setScope 'list'
      # close all popups
      $('body').click()
      @options.app.state.unset 'component'
      @options.app.layout.workspaceComponentViewerRegion.reset()
      @options.app.layout.hideComponentViewer()
      @options.app.issuesView.bindScrollEvents()
      @options.app.issuesView.scrollToIssue()


    selectNextIssue: ->
      index = @options.app.state.get('selectedIndex') + 1
      if index < @options.app.issues.length
        @options.app.state.set selectedIndex: index
      else
        unless @options.app.state.get('maxResultsReached')
          @fetchNextPage().done =>
            @options.app.state.set selectedIndex: index


    selectPreviousIssue: ->
      index = @options.app.state.get('selectedIndex') - 1
      if index >= 0
        @options.app.state.set selectedIndex: index