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

  $ = jQuery


  class extends Marionette.ItemView
    template: Templates['widget']
    className: 'block'


    events:
      'click .js-edit-widget': 'editWidget'
      'click .js-cancel-edit-widget': 'cancelEditWidget'
      'submit .js-edit-widget-form': 'saveWidget'


    initialize: (options) ->
#      @listenTo options.app.state, 'change', @render
      @requestContent()


    onRender: ->
      @$el.data 'id', @model.id
      if @options.app.state.get 'configure'
        @$el.prop 'draggable', true


    requestContent: ->
      payload = id: @model.get 'key'
      if @options.app.resource
        payload.resource = @options.app.resource
      _.extend payload, @getWidgetProps()
      $.get "#{baseUrl}/widget/show", payload, (html) =>
        @model.set 'html', html
        @render()


    getWidgetProps: ->
      properties = @model.get 'properties'
      r = {}
      properties.forEach (prop) ->
        r[prop.key] = prop.value if prop.value?
      r


    editWidget: ->
      $.get "#{baseUrl}/api/dashboards/configure_widget", id: @model.get('key'), (data) =>
        @model.mergeProperties data.widget.properties
        @showEditForm()


    showEditForm: ->
      @render()
      @$('.widget_props').removeClass 'hidden'
      @$('.configure_widget').addClass 'hidden'


    cancelEditWidget: ->
      @$('.widget_props').addClass 'hidden'
      @$('.configure_widget').removeClass 'hidden'


    saveWidget: (e) ->
      e.preventDefault()
      data = id: @model.id
      @$('.js-edit-widget-form').serializeArray().forEach (p) ->
        data[p.name] = p.value
      $.post "#{baseUrl}/api/dashboards/save_widget", data, (data) =>
        @model.set data.widget
        @requestContent()


    serializeData: ->
      _.extend super,
        baseUrl: baseUrl
        state: @options.app.state.toJSON()