-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.coffee
238 lines (167 loc) · 6.73 KB
/
utils.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
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
_ = require('underscore')
repeatableValueElementsCache = {}
repeatableValueElementsByKeyCache = {}
repeatableValueElementsByDataNameCache = {}
class Utils
@DATE_REGEX: /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
@UUID_REGEX: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i
@toArray = (arrayLike) ->
Array::slice.call arrayLike, 0
@flattenElements = (elements, recurseRepeatables=true, assignParent=false, parent=undefined, recurseSections=true) ->
_.tap [], (flat) ->
_.each elements, (element) ->
element.parent = parent if assignParent
flat.push(element)
recurse = true
recurse = false if not recurseRepeatables and element.type is 'Repeatable'
recurse = false if not recurseSections and element.type is 'Section'
if recurse and element.elements
children = Utils.flattenElements(element.elements, recurseRepeatables, assignParent, element, recurseSections)
Array.prototype.push.apply(flat, children)
@nearestRepeatable: (element) ->
iterator = element?.parent
while iterator
return iterator if iterator.type is 'Repeatable'
iterator = iterator.parent
null
@valueForElement: (element, value) ->
if Utils.isNumericElement(element)
Utils.numberValue(value)
else if Utils.isDateElement(element)
Utils.dateValue(value)
else
value
@isNumericElement: (element) ->
element.numeric or element.display?.style is 'number' or element.display?.style is 'currency'
@isDateElement: (element) ->
element.type is 'DateTimeField' or element.type is 'DateField'
@dateValue: (value) ->
if value and value.length <= 10
new Date("#{value.replace(/-/g, '/')} 00:00:00")
else
value
@numberValue: (value) ->
value ||= undefined
if value? then Number(value) else undefined
@repeatableValueElements: (repeatable) ->
key = repeatable.key
if repeatableValueElementsCache[key]
cached =
all: repeatableValueElementsCache[key]
byKey: repeatableValueElementsByKeyCache[key]
byDataName: repeatableValueElementsByDataNameCache[key]
return cached
elements = Utils.flattenElements(repeatable.elements, false)
repeatableValueElementsCache[key] = all = elements
repeatableValueElementsByKeyCache[key] = byKey = {}
repeatableValueElementsByDataNameCache[key] = byDataName = {}
for element in elements
byKey[element.key] = element
byDataName[element.data_name] = element
{ all: all, byKey: byKey, byDataName: byDataName }
@repeatableValues: (repeatable, items, dataName) ->
return null unless _.isArray(items)
{byDataName} = Utils.repeatableValueElements(repeatable)
element = byDataName[dataName]
return null unless element
values = _.map items, (item) ->
Utils.valueForElement(element, item.form_values[element.key])
values
@formatMachineDate: (date) ->
return null unless date? and not isNaN(date.getTime())
"#{RIGHT('000' + date.getFullYear(), 4)}-#{RIGHT('0' + (date.getMonth() + 1), 2)}-#{RIGHT('0' + date.getDate(), 2)}"
@makeValue: (element, value) ->
return null unless value?
converter = Utils.converters[element.type]
if converter?
converter(value)
else
null
@makeChoiceValue: (choices, others) ->
{ choice_values: choices ? [], other_values: others ? [] }
@isSetValueSupported: (containerElements, dataElement, type) ->
return false unless Utils.converters[type]?
# Make sure the element is contained within the current editing scope. We need to make sure
# that the element is within the repeatable being edited and not further nested as a child or
# part of an ancestor.
validElements = Utils.flattenElements(containerElements, false)
foundElement = _.find validElements, (e) -> e.key is dataElement.key
!!foundElement
@converters:
TextField: (value) ->
return null unless value?
value.toString()
CalculatedField: (value) ->
Utils.converters.TextField(value)
HyperlinkField: (value) ->
Utils.converters.TextField(value)
YesNoField: (value) ->
Utils.converters.TextField(value)
BarcodeField: (value) ->
Utils.converters.TextField(value)
DynamicField: (value) ->
vals = ARRAY()
if _.isArray(value)
_.each value, (val) ->
if(val && val.metadata && val.elements && val.values &&
_.isObject(val.metadata) && _.isString(val.metadata.id) &&
_.isArray(val.elements) &&
_.isObject(val.values) && !_.isArray(val.values)
)
vals.push(val)
vals
DateTimeField: (value) ->
return null unless value?
date = DATEVALUE(value)
return null unless date?
FORMAT('%s-%s-%s',
date.getFullYear(),
LPAD(date.getMonth() + 1, 2, '0'),
LPAD(date.getDate(), 2, '0'))
DateField: (value) ->
Utils.converters.DateTimeField(value)
TimeField: (value) ->
return null unless _.isString(value)
return null if value.length isnt 5
return null if value[2] isnt ':'
parts = value.split(':')
hour = NUM(parts[0])
minute = NUM(parts[1])
return null if hour > 23 or hour < 0
return null if minute > 59 or minute < 0
FORMAT('%s:%s',
LPAD(hour, 2, '0'),
LPAD(minute, 2, '0'))
ChoiceField: (value) ->
choices = null
switch true
when _.isArray(value)
choices = _.map(_.compact(value), (v) -> v.toString())
when _.isString(value) and value.length isnt 0
choices = [ value ]
when _.isNumber(value)
choices = [ value.toString() ]
when _.isObject(value) and _.isArray(value.choice_values)
choices = _.map(_.compact(value.choice_values), (v) -> v.toString())
return null unless _.isArray(choices)
Utils.makeChoiceValue(choices, [])
ClassificationField: (value) ->
Utils.converters.ChoiceField(value)
AddressField: (value) ->
return null unless _.isObject(value)
address =
sub_thoroughfare: value.sub_thoroughfare?.toString() ? null
thoroughfare: value.thoroughfare?.toString() ? null
suite: value.suite?.toString() ? null
locality: value.locality?.toString() ? null
sub_admin_area: value.sub_admin_area?.toString() ? null
admin_area: value.admin_area?.toString() ? null
postal_code: value.postal_code?.toString() ? null
country: value.country?.toString() ? null
address
RecordLinkField: (value) ->
return null unless _.isArray(value)
ids = value.map((id) => '' + id)
ids = _.select ids, (id) => Utils.UUID_REGEX.test(id)
ids.map((id) => { record_id: id })
module.exports = Utils