-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtangy-form-response-model.js
109 lines (102 loc) · 3.01 KB
/
tangy-form-response-model.js
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
export class TangyFormResponseModel {
constructor(props) {
this._id = uuid()
this.collection = 'TangyFormResponse'
// Placeholders for where element.getProps() info will go.
this.form = {}
this.items = []
// States.
this.complete = false
// Focus indexes.
// @TODO: We can probably get rid of these indexes.
this.focusIndex = 0
this.nextFocusIndex = 1
this.previousFocusIndex = -1
// Info.
this.startDatetime = (new Date()).toLocaleString(),
this.startUnixtime = Date.now(),
this.uploadDatetime = ''
this.location = {}
this.type = 'response'
if (props && props.hasOwnProperty('inputs')) delete props.inputs
Object.assign(this, props)
}
get inputs() {
// Reduce to an array.
return this.items.reduce((inputsArray, item) => {
item.inputs.forEach(input => {
if (input.tagName === 'TANGY-CARDS') {
input.value.forEach(card => card.value.forEach(input => inputsArray.push(input)))
} else {
inputsArray.push(input)
}
})
return inputsArray
}, [])
}
get inputsByName() {
// Reduce to an object keyed on input.name. If multiple inputs with the same name, put them in an array.
return this.inputs.reduce((inputsObject, input) => {
if (inputsObject.hasOwnProperty(input.name)) {
if (Array.isArray(inputsObject[input.name])) {
inputsObject[input.name] = inputsObject[input.name].push(input)
} else {
inputsObject[input.name] = [input, inputsObject[input.name]]
}
} else {
inputsObject[input.name] = input
}
return inputsObject
}, {})
}
get(name) {
let value = ''
let foundInput = this.inputsByName[name]
if (foundInput && typeof foundInput.value === 'object') {
let values = []
foundInput.value.forEach(subInput => {
if (subInput.value) {
values.push(subInput.name)
}
})
value = values
} else if (foundInput && foundInput.value !== undefined) {
value = foundInput.value
}
// Return radio buttons as a single value chosen, not a single entry array.
if (foundInput && foundInput.tagName === 'TANGY-RADIO-BUTTONS' && Array.isArray(value)) {
value = (value.length > 0) ? value[0] : ''
}
if (!value) {
value = ''
}
return value
}
set(name, value) {
if (this.inputsByName[name]) {
this.inputsByName[name].value = this.inputsByName[name].tagName === 'TANGY-RADIO-BUTTONS'
? this.inputsByName[name].value.map(option => {
option.name === value
? 'on'
: ''
})
: value
} else {
this.items[0].inputs.push({
name,
value
})
}
}
}
function uuid() {
var uuid = "", i, random;
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i == 8 || i == 12 || i == 16 || i == 20) {
uuid += "-"
}
uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
}
return uuid;
}