aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/coffee/quality-gate/app.coffee
blob: f3de49b69febf6ff1594337fe2006b024e598c14 (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
requirejs.config
  baseUrl: "#{baseUrl}/js"

  paths:
    'jquery': 'third-party/jquery'
    'backbone': 'third-party/backbone'
    'backbone.marionette': 'third-party/backbone.marionette'
    'handlebars': 'third-party/handlebars'
    'moment': 'third-party/moment'
    'select-list': 'common/select-list'

  shim:
    'backbone.marionette':
      deps: ['backbone']
      exports: 'Marionette'
    'backbone':
      exports: 'Backbone'
    'handlebars':
      exports: 'Handlebars'
    'moment':
      exports: 'moment'
    'select-list':
      exports: 'SelectList'


requirejs [
  'backbone', 'backbone.marionette', 'handlebars',
  'quality-gate/collections/quality-gates',
  'quality-gate/views/quality-gate-sidebar-list-view',
  'quality-gate/views/quality-gate-actions-view',
  'quality-gate/views/quality-gate-edit-view',
  'quality-gate/router',
  'quality-gate/layout',
  'common/handlebars-extensions'
], (
  Backbone, Marionette, Handlebars,
  QualityGates,
  QualityGateSidebarListItemView,
  QualityGateActionsView,
  QualityGateEditView,
  QualityGateRouter,
  QualityGateLayout
) ->

  # 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 quality-gates-page');


  # Create a Quality Gate Application
  App = new Marionette.Application


  App.qualityGates = new QualityGates


  App.openFirstQualityGate = ->
    if @qualityGates.length > 0
      @router.navigate "show/#{@qualityGates.models[0].get('id')}", trigger: true
    else
      App.layout.detailsRegion.reset()


  App.deleteQualityGate = (id) ->
    App.qualityGates.remove id
    App.openFirstQualityGate()


  App.unsetDefaults = (id) ->
    App.qualityGates.each (gate) ->
      gate.set('default', false) unless gate.id == id


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


  # Construct actions bar
  App.addInitializer ->
    @codingRulesHeaderView = new QualityGateActionsView
      app: @
    @layout.actionsRegion.show @codingRulesHeaderView


  # Construct sidebar
  App.addInitializer ->
    @qualityGateSidebarListView = new QualityGateSidebarListItemView
      collection: @qualityGates
      app: @
    @layout.resultsRegion.show @qualityGateSidebarListView


  # Construct edit view
  App.addInitializer ->
    @qualityGateEditView = new QualityGateEditView app: @
    @qualityGateEditView.render()


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


  # Open first quality gate when come to the page
  App.addInitializer ->
    initial = Backbone.history.fragment == ''
    App.openFirstQualityGate() if initial


  # Call app, Load metrics and the list of quality gates before start the application
  appXHR = jQuery.ajax
    url: "#{baseUrl}/api/qualitygates/app"
  .done (r) =>
      App.canEdit = r.edit
      App.periods = r.periods
      App.metrics = r.metrics

  qualityGatesXHR = App.qualityGates.fetch()

  # Message bundles
  l10nXHR = window.requestMessages()

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

      # Start the application
      App.start()