forked from episphere/quest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localforageDAO.js
173 lines (161 loc) · 6.23 KB
/
localforageDAO.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
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
import { textboxinput, radioAndCheckboxUpdate } from "./questionnaire.js";
export async function restoreResults(results) {
// get the results from localforage...
// retrieved the results... now lets fill the form..
Object.keys(results).forEach((qid) => {
function handleString(questionElement, id, value) {
// check if we have a radiobutton/checkbox...
let element = questionElement.querySelector(`[name='${id}'][value='${value.replaceAll("'", "\\'")}']`)
if (element) {
element.checked = true
radioAndCheckboxUpdate(element)
return;
}
// check for some kind of input element...
element = questionElement.querySelector(`[id='${id}']`);
if (element) {
element.value = value
textboxinput(element, false);
return;
}
console.log("========== dont know how to handle this ==========", questionElement, id, value)
}
console.log('-------checking qids------------')
console.log(qid)
let formElement = document.querySelector("#" + qid);
// not sure have a non-question would be here
// but ignore it...
if (!formElement) {
return;
}
// each question has an object of results...
if (!results[qid]) return;
// CASE 1: it is just a simple value...
if (typeof results[qid] == "string") {
// in this case get the first input/textarea in the form and fill it in.
let element = formElement.querySelector("input,textarea,select");
// If the element is not on the page
// it could have been dynamically create (think SOCcer...) just return..
if (!element) return
// null handle element, skip if null (load failing when participant is in the middle of unanswered SOCcer questions)
if (element?.type == "radio") {
let selector = `input[value='${results[qid]}']`;
let selectedRadioElement = formElement.querySelector(selector);
if (selectedRadioElement) {
selectedRadioElement.checked = true;
} else {
console.log("... problem with ", element);
}
radioAndCheckboxUpdate(selectedRadioElement);
} else {
if (element?.type == "submit") {
console.log(
`local forage is trying to change the value of a submit button. Question ${qid} response value: ${results[qid]}; skipping this 1 value..`
);
return;
}
element.value = results[qid];
textboxinput(element, false);
}
// we should return from here...
// then we should handle the ARRAY case.
// which is likely a combobox...
// create a handleArray function...
// then we should handle the Object case
// again create a handleObject function
// that can be called recursively to handle
// any potential depth of the results JSON.
// also, handleObject should call handleString,
// handleArray and handleObject.
// Unfortunately, we need handleArray/handleString to
// handle a potential null id for the case where
// the results is simply a string or an array.
// CASE 2: we have an object...
} else {
function getFromRbCb(rbCbName, result) {
let checkboxElements = Array.from(
formElement.querySelectorAll(`input[name=${rbCbName}]`)
);
checkboxElements.forEach((checkbox) => {
if (result.includes(checkbox.value)) {
checkbox.checked = true;
radioAndCheckboxUpdate(checkbox);
}
});
}
if (Array.isArray(results[qid])) {
getFromRbCb(qid, results[qid]);
} else {
console.log(
"... for KEY ",
qid,
" ...1 WE HAVE AN OBJECT!!! ... ",
Object.keys(results[qid]),
Object.values(results[qid])
);
Object.keys(results[qid]).forEach((resKey) => {
if (!resKey) {
// added because dynamic questions sometimes muck up the previous button.
console.log(
`empty key in local forage Question ${qid} response value: ${results[qid]}; skipping this 1 value..`
);
return;
}
let resObject = results[qid][resKey];
let handled = false;
if (typeof resObject == 'string') {
handleString(formElement, resKey, resObject)
handled = true;
}
if (Array.isArray(resObject)) {
getFromRbCb(resKey, resObject);
handled = true;
}
if (!handled && typeof resObject == "object") {
// ok wasn't an array .. i.e. it wasnt a radiobutton...
// how about an XOR object...
let element = Array.from(
formElement.querySelectorAll(`[xor="${resKey}"]`)
);
element.forEach((xorElement) => {
if (resObject[xorElement.id])
xorElement.value = resObject[xorElement.id];
});
handled = true;
}
// check for mulitple radio buttons on 1 page...
let multiq = formElement.querySelector(`input[name='${resKey}'][value='${CSS.escape(resObject)}']`)
if (multiq) {
multiq.checked = true
handled = true;
}
if (!handled && typeof resObject == "string") {
let element = document.getElementById(resKey);
if (element.tagName == "DIV" || element.tagName == "FORM") {
// radio in grid???
let selector = `input[value='${results[qid][resKey]}']`;
let selectedRadioElement = element.querySelector(selector);
if (selectedRadioElement) {
selectedRadioElement.checked = true;
} else {
console.log("... problem with ", element);
}
radioAndCheckboxUpdate(selectedRadioElement);
} else {
element.value = resObject;
textboxinput(element, false);
}
}
});
}
}
});
}
export async function removeQuestion(questName, qid) {
//check here for going back issue?
let results = await localforage.getItem(questName);
if (results && results[qid]) {
delete results[qid];
}
localforage.setItem(questName, results);
}