This repository has been archived by the owner on May 11, 2021. It is now read-only.
/
form.coffee
119 lines (93 loc) · 3.36 KB
/
form.coffee
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
define [
'../core'
'./item'
'./stats'
'./controls'
'../charts'
'tpl!templates/views/field-form.html'
], (c, item, stats, controls, charts, templates...) ->
templates = c._.object ['form'], templates
# Contained within the ConceptForm containing views for a single FieldModel
class FieldForm extends c.Marionette.Layout
className: 'field-form'
template: templates.form
options:
managed: true
showChart: true
constructor: ->
super
@context = @options.context
events:
'click .concept-actions [data-toggle=add]': 'save'
'click .concept-actions [data-toggle=update]': 'save'
'click .concept-actions [data-toggle=remove]': 'clear'
ui:
actions: '.field-actions'
add: '.field-actions [data-toggle=add]'
remove: '.field-actions [data-toggle=remove]'
update: '.field-actions [data-toggle=update]'
regions:
main: '.field-main'
stats: '.field-stats'
control: '.field-control'
chart: '.field-chart'
# Map corresponding view class to region. This makes it
# easier to extend. This can also be a function that returns
# an object.
regionViews:
main: item.Field
stats: stats.FieldStats
control: controls.FieldControl
onRender: ->
for key, klass of c._.result @, 'regionViews'
view = new klass
model: @model
context: @context
@[key].show view
# Only represent for fields that support distributions
if @options.showChart and @model.links.distribution?
chart = new charts.FieldChart
model: @model
context: @context
chart:
height: 200
@chart.show chart
@setDefaultState()
setDefaultState: ->
if @options.managed then @ui.actions.hide()
if @context?.isValid()
@setUpdateState()
else
@setNewState()
setUpdateState: ->
@ui.add.hide()
@ui.update.show()
@ui.remove.show()
setNewState: ->
@ui.add.show()
@ui.update.hide()
@ui.remove.hide()
# Saves the current state of the context which enables it to be
# synced with the server.
save: ->
@context?.save()
if @options.managed then @setUpdateState()
# Clears the local context of conditions
clear: ->
@context?.clear()
@context?.save()
if @options.managed then @setNewState()
class FieldFormCollection extends c.Marionette.CollectionView
itemView: FieldForm
itemViewOptions: (model, index) ->
context = @options.context
options =
model: model
context: context.fetch
field: model.id
concept: context.get 'concept'
if not @fieldChartIndex? and model.links.distribution?
@fieldChartIndex = index
options.showChart = true
return options
{ FieldForm, FieldFormCollection }