aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/coffee/coding-rules/app.coffee
blob: 1add838f708b9515e94a2f27e84a6af55b6c7800 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
requirejs.config
  baseUrl: "#{baseUrl}/js"

  paths:
    'backbone': 'third-party/backbone'
    'backbone.marionette': 'third-party/backbone.marionette'
    'handlebars': 'third-party/handlebars'
    'jquery.mockjax': 'third-party/jquery.mockjax'

  shim:
    'backbone.marionette':
      deps: ['backbone']
      exports: 'Marionette'
    'backbone':
      exports: 'Backbone'
    'handlebars':
      exports: 'Handlebars'


requirejs [
  'backbone', 'backbone.marionette',

  'coding-rules/layout',
  'coding-rules/router',

  # views
  'coding-rules/views/header-view',
  'coding-rules/views/actions-view',
  'coding-rules/views/filter-bar-view',
  'coding-rules/views/coding-rules-list-view',
  'coding-rules/views/coding-rules-detail-view',
  'coding-rules/views/coding-rules-bulk-change-view',
  'coding-rules/views/coding-rules-quality-profile-activation-view',
  'coding-rules/views/coding-rules-bulk-change-dropdown-view',
  'coding-rules/views/coding-rules-facets-view',
  'coding-rules/views/coding-rules-custom-rule-creation-view',

  # filters
  'navigator/filters/base-filters',
  'navigator/filters/choice-filters',
  'navigator/filters/string-filters',
  'navigator/filters/date-filter-view',
  'navigator/filters/read-only-filters',
  'coding-rules/views/filters/query-filter-view',
  'coding-rules/views/filters/quality-profile-filter-view',
  'coding-rules/views/filters/inheritance-filter-view',
  'coding-rules/views/filters/active-severities-filter-view',
  'coding-rules/views/filters/activation-filter-view',
  'coding-rules/views/filters/characteristic-filter-view',
  'coding-rules/views/filters/repository-filter-view',
  'coding-rules/views/filters/tag-filter-view',
  'coding-rules/views/filters/language-filter-view',

  'coding-rules/mockjax',
  'common/handlebars-extensions'
], (
  Backbone, Marionette,

  CodingRulesLayout,
  CodingRulesRouter,

  # views
  CodingRulesHeaderView,
  CodingRulesActionsView,
  CodingRulesFilterBarView,
  CodingRulesListView,
  CodingRulesDetailView,
  CodingRulesBulkChangeView,
  CodingRulesQualityProfileActivationView,
  CodingRulesBulkChangeDropdownView,
  CodingRulesFacetsView,
  CodingRulesCustomRuleCreationView,

  # filters
  BaseFilters,
  ChoiceFilters,
  StringFilterView,
  DateFilterView,
  ReadOnlyFilterView,
  QueryFilterView,
  QualityProfileFilterView,
  InheritanceFilterView,
  ActiveSeveritiesFilterView,
  ActivationFilterView,
  CharacteristicFilterView,
  RepositoryFilterView,
  TagFilterView,
  LanguageFilterView
) ->

  # Create a generic error handler for ajax requests
  jQuery.ajaxSetup
    error: (jqXHR) ->
      text = jqXHR.responseText
      errorBox = jQuery('.modal-error')
      if jqXHR.responseJSON?.errors?
        text = _.pluck(jqXHR.responseJSON.errors, 'msg').join '. '
      else
        text = t 'default_error_message'
      if errorBox.length > 0
        errorBox.show().text text
      else
        alert text


  # Add html class to mark the page as navigator page
  jQuery('html').addClass('navigator-page coding-rules-page');


  # Create an Application
  App = new Marionette.Application


  App.getQuery = (includeFacetsQuery = true) ->
    query = @filterBarView.getQuery()
    if includeFacetsQuery and @codingRulesFacetsView
      _.extend query, @codingRulesFacetsView.getQuery()
    query


  App.restoreSorting = (params) ->
    sort = _.findWhere(params, key: 'sort')
    asc = _.findWhere(params, key: 'asc')

    if (sort && asc)
      @codingRules.sorting =
        sort: sort.value
        asc: asc.value =='true'


  App.restoreDefaultSorting = ->
    params = []
    params.push(key: 'sort', value: 'createdAt')
    params.push(key: 'asc', value: false)
    @restoreSorting params


  App.storeQuery = (query, sorting) ->
    if sorting && sorting.sort
      _.extend query,
        s: sorting.sort
        asc: '' + sorting.asc
    queryString = _.map query, (v, k) -> "#{k}=#{encodeURIComponent(v)}"
    @router.navigate queryString.join('|'), replace: true



  App.fetchList = (firstPage, fromFacets = false) ->
    pristineQuery = @getQuery(false)
    query = @getQuery(fromFacets)

    fetchQuery = _.extend { p: @pageIndex, ps: 25, facets: not fromFacets }, query

    if @codingRules.sorting && @codingRules.sorting.sort
      _.extend fetchQuery,
          s: @codingRules.sorting.sort,
          asc: @codingRules.sorting.asc

    @storeQuery pristineQuery, @codingRules.sorting

    # Optimize requested fields
    _.extend fetchQuery, f: 'name,lang,status'

    if @codingRulesListView
      scrollOffset = jQuery('.navigator-results')[0].scrollTop
    else
      scrollOffset = 0

    @layout.showSpinner 'resultsRegion'
    @layout.showSpinner 'facetsRegion' unless fromFacets || !firstPage


    jQuery.ajax
      url: "#{baseUrl}/api/rules/search"
      data: fetchQuery
    .done (r) =>
      _.map(r.rules, (rule) ->
        rule.language = App.languages[rule.lang]
      )

      @codingRules.paging =
        total: r.total
        pageIndex: r.p
        pageSize: r.ps
        pages: 1 + (r.total / r.ps)

      if @codingRulesListView
        @codingRulesListView.close()

      if firstPage
        @codingRules.reset r.rules
        @codingRulesListView = new CodingRulesListView
          app: @
          collection: @codingRules
      else
        @codingRulesListView.unbindEvents()
        @codingRules.add r.rules

      @layout.resultsRegion.show @codingRulesListView


      if @codingRules.isEmpty()
        @layout.detailsRegion.reset()
      else if firstPage
        @codingRulesListView.selectFirst()
      else
        @codingRulesListView.selectCurrent()

      unless firstPage
        jQuery('.navigator-results')[0].scrollTop = scrollOffset

      unless fromFacets
        @codingRulesFacetsView = new CodingRulesFacetsView
          app: @
          collection: new Backbone.Collection r.facets, comparator: 'property'
        @layout.facetsRegion.show @codingRulesFacetsView

      @layout.onResize()



  App.facetLabel = (property, value) ->
    return value unless App.facetPropertyToLabels[property]
    App.facetPropertyToLabels[property](value)


  App.fetchFirstPage = (fromFacets = false) ->
    @pageIndex = 1
    App.fetchList true, fromFacets


  App.fetchNextPage = (fromFacets = true) ->
    if @pageIndex < @codingRules.paging.pages
      @pageIndex++
      App.fetchList false, fromFacets


  App.getQualityProfile = ->
    value = @qualityProfileFilter.get('value')
    if value? && value.length == 1 then value[0] else null


  App.getQualityProfilesForLanguage = (language_key) ->
    _.filter App.qualityProfiles, (p) => p.lang == language_key

  App.getQualityProfileByKey = (profile_key) ->
    _.findWhere App.qualityProfiles, key: profile_key


  App.getSubcharacteristicName = (name) ->
    (App.characteristics[name] || '').replace ': ', ' > '


  App.showRule = (ruleKey) ->
    App.layout.showSpinner 'detailsRegion'
    jQuery.ajax
      url: "#{baseUrl}/api/rules/show"
      data:
        key: ruleKey
        actives: true
    .done (r) =>
      rule = new Backbone.Model(r.rule)
      App.codingRulesQualityProfileActivationView.rule = rule
      App.detailView = new CodingRulesDetailView
        app: App
        model: rule
        actives: r.actives
      App.layout.detailsRegion.show App.detailView


  App.manualRepository = ->
    key: 'manual'
    name: 'Manual Rules'
    language: 'none'


  # Construct layout
  App.addInitializer ->
    @layout = new CodingRulesLayout app: @
    jQuery('#content').append @layout.render().el
    @layout.onResize()


  # Construct header
  App.addInitializer ->
    @codingRulesHeaderView = new CodingRulesHeaderView app: @
    @layout.headerRegion.show @codingRulesHeaderView


  # Define coding rules
  App.addInitializer ->
    @codingRules = new Backbone.Collection
    @restoreDefaultSorting()


  # Construct status bar
  App.addInitializer ->
    @codingRulesActionsView = new CodingRulesActionsView
      app: @
      collection: @codingRules
    @layout.actionsRegion.show @codingRulesActionsView


  # Construct bulk change views
  App.addInitializer ->
    @codingRulesBulkChangeView = new CodingRulesBulkChangeView app: @
    @codingRulesBulkChangeDropdownView = new CodingRulesBulkChangeDropdownView app: @


  # Construct quality profile activation view
  App.addInitializer ->
    @codingRulesQualityProfileActivationView = new CodingRulesQualityProfileActivationView app: @


  # Construct custom rule creation view
  App.addInitializer ->
    @codingRulesCustomRuleCreationView = new CodingRulesCustomRuleCreationView app: @


  # Define filters
  App.addInitializer ->
    @filters = new BaseFilters.Filters

    @queryFilter = new BaseFilters.Filter
      property: 'q'
      type: QueryFilterView
      size: 50
    @filters.add @queryFilter

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.severity'
      property: 'severities'
      type: ChoiceFilters.ChoiceFilterView
      optional: true
      choices:
        'BLOCKER': t 'severity.BLOCKER'
        'CRITICAL': t 'severity.CRITICAL'
        'MAJOR': t 'severity.MAJOR'
        'MINOR': t 'severity.MINOR'
        'INFO': t 'severity.INFO'
      choiceIcons:
        'BLOCKER': 'severity-blocker'
        'CRITICAL': 'severity-critical'
        'MAJOR': 'severity-major'
        'MINOR': 'severity-minor'
        'INFO': 'severity-info'

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.tag'
      property: 'tags'
      type: TagFilterView
      optional: true

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.characteristic'
      property: 'debt_characteristics'
      type: CharacteristicFilterView
      choices: @characteristics
      multiple: false
      optional: true

    @qualityProfileFilter = new BaseFilters.Filter
      name: t 'coding_rules.filters.quality_profile'
      property: 'qprofile'
      type: QualityProfileFilterView
      app: @
      choices: @qualityProfiles
      multiple: false
    @filters.add @qualityProfileFilter

    @activationFilter = new BaseFilters.Filter
      name: t 'coding_rules.filters.activation'
      property: 'activation'
      type: ActivationFilterView
      enabled: false
      optional: true
      multiple: false
      qualityProfileFilter: @qualityProfileFilter
      choices:
        true: t 'coding_rules.filters.activation.active'
        false: t 'coding_rules.filters.activation.inactive'
    @filters.add @activationFilter

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.active_severity'
      property: 'active_severities'
      type: ActiveSeveritiesFilterView
      enabled: false
      optional: true
      qualityProfileFilter: @qualityProfileFilter
      choices:
        'BLOCKER': t 'severity.BLOCKER'
        'CRITICAL': t 'severity.CRITICAL'
        'MAJOR': t 'severity.MAJOR'
        'MINOR': t 'severity.MINOR'
        'INFO': t 'severity.INFO'
      choiceIcons:
        'BLOCKER': 'severity-blocker'
        'CRITICAL': 'severity-critical'
        'MAJOR': 'severity-major'
        'MINOR': 'severity-minor'
        'INFO': 'severity-info'

    @languageFilter =  new BaseFilters.Filter
      name: t 'coding_rules.filters.language'
      property: 'languages'
      type: LanguageFilterView
      app: @
      choices: @languages
      optional: true
    @filters.add @languageFilter

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.availableSince'
      property: 'available_since'
      type: DateFilterView
      enabled: false
      optional: true

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.inheritance'
      property: 'inheritance'
      type: InheritanceFilterView
      enabled: false
      optional: true
      multiple: false
      qualityProfileFilter: @qualityProfileFilter
      choices:
        'NONE': t 'coding_rules.filters.inheritance.not_inherited'
        'INHERITED': t 'coding_rules.filters.inheritance.inherited'
        'OVERRIDES': t 'coding_rules.filters.inheritance.overriden'

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.repository'
      property: 'repositories'
      type: RepositoryFilterView
      enabled: false
      optional: true
      app: @
      choices: @repositories

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.status'
      property: 'statuses'
      type: ChoiceFilters.ChoiceFilterView
      enabled: false
      optional: true
      choices: @statuses

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.template'
      property: 'is_template'
      type: ChoiceFilters.ChoiceFilterView
      optional: true
      multiple: false
      choices:
        'true': t 'coding_rules.filters.template.is_template'
        'false': t 'coding_rules.filters.template.is_not_template'

    @filters.add new BaseFilters.Filter
      name: t 'coding_rules.filters.key'
      property: 'rule_key'
      type: ReadOnlyFilterView
      enabled: false
      optional: true


    @filterBarView = new CodingRulesFilterBarView
      app: @
      collection: @filters,
      extra: sort: '', asc: false
    @layout.filtersRegion.show @filterBarView


  # Start router
  App.addInitializer ->
    @router = new CodingRulesRouter app: @
    Backbone.history.start()


  # Call app before start the application
  appXHR = jQuery.ajax
    url: "#{baseUrl}/api/rules/app"
  .done (r) ->
    App.appState = new Backbone.Model
    App.state = new Backbone.Model
    App.canWrite = r.canWrite
    App.qualityProfiles = _.sortBy r.qualityprofiles, ['name', 'lang']
    App.languages = _.extend r.languages, none: 'None'
    _.map App.qualityProfiles, (profile) ->
      profile.language = App.languages[profile.lang]
    App.repositories = r.repositories
    App.repositories.push App.manualRepository()
    App.statuses = r.statuses
    App.characteristics = r.characteristics

    App.facetPropertyToLabels =
      'languages': (value) -> App.languages[value]
      'repositories': (value) ->
        repo = _.findWhere(App.repositories, key: value)
        other_repo_with_same_name = _.find(App.repositories, (repos) -> repos.name == repo.name && repos.key != repo.key)
        if other_repo_with_same_name
          repo.name + ' - ' + App.languages[repo.language]
        else
          repo.name

  # Message bundles
  l10nXHR = window.requestMessages()

  jQuery.when(l10nXHR, appXHR).done ->
      # Remove the initial spinner
      jQuery('#coding-rules-page-loader').remove()

      # Start the application
      App.start()