forked from episphere/quest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restoreResponses.js
150 lines (127 loc) · 4.92 KB
/
restoreResponses.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
import { moduleParams, textboxinput, radioAndCheckboxUpdate } from "./questionnaire.js";
import { getStateManager } from "./stateManager.js";
function getFromRbCb(formElement, rbCbName, result) {
const checkboxElements = Array.from(formElement.querySelectorAll(`input[name=${rbCbName}]`));
checkboxElements.forEach((checkbox) => {
if (result.includes(checkbox.value)) {
checkbox.checked = true;
radioAndCheckboxUpdate(checkbox);
}
});
}
// Get the first input / textarea in the form and fill it in.
// If the element is not on the page, it could have been dynamically create (think SOCcer) just return.
function handleSimpleStringResponse(formElement, response) {
const element = formElement.querySelector("input,textarea,select");
if (!element) return;
if (element?.type === "radio") {
const selector = `input[value='${response}']`;
const selectedRadioElement = formElement.querySelector(selector);
if (selectedRadioElement) {
selectedRadioElement.checked = true;
} else {
moduleParams.errorLogger("RESTORE RESPONSE: Problem with radio:", element);
}
radioAndCheckboxUpdate(selectedRadioElement);
} else if (element?.type === "submit") {
moduleParams.errorLogger(`RESTORE RESPONSE: Response value: ${response}. Submit button: skipping update.`);
return;
} else {
element.value = response;
textboxinput(element, false);
}
}
function handleObjectResponse(formElement, response) {
Object.keys(response).forEach((resKey) => {
if (!resKey) {
moduleParams.errorLogger(`RESTORE RESPONSE: Response value: ${response}; skipping.`);
return;
}
const resObject = response[resKey];
const multiq = formElement.querySelector(`input[name='${resKey}'][value='${CSS.escape(resObject)}']`);
let handled = false;
if (typeof resObject === 'string') {
handleStringInObjectResponse(formElement, resKey, resObject);
handled = true;
} else if (typeof resObject === 'object') {
// Handle the array case
if (Array.isArray(resObject)) {
getFromRbCb(formElement, resKey, resObject);
handled = true;
// Handle XOR objects
} else {
const 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.
if (multiq) {
multiq.checked = true
handled = true;
}
if (handled) return;
if (typeof resObject === "string") {
const element = document.getElementById(resKey);
if (element.tagName == "DIV" || element.tagName == "FORM") {
const selector = `input[value='${response[resKey]}']`;
const selectedRadioElement = element.querySelector(selector);
if (selectedRadioElement) {
selectedRadioElement.checked = true;
} else {
moduleParams.errorLogger("RESTORE RESPONSE: Problem with DIV/FORM:", element);
}
radioAndCheckboxUpdate(selectedRadioElement);
} else {
element.value = resObject;
textboxinput(element, false);
}
}
});
}
// Handle radio/checkbox and input elements
function handleStringInObjectResponse(questionElement, id, value) {
const radioOrCheckboxElement = questionElement.querySelector(`[name='${id}'][value='${value.replaceAll("'", "\\'")}']`)
if (radioOrCheckboxElement) {
radioOrCheckboxElement.checked = true
radioAndCheckboxUpdate(radioOrCheckboxElement)
return;
}
const inputElement = questionElement.querySelector(`[id='${id}']`);
if (inputElement) {
inputElement.value = value
textboxinput(inputElement, false);
return;
}
console.warn('RESTORE RESPONSES (unhandled response)', questionElement, id, value)
}
/**
* Restore Responses to already answered questions.
* This activates on survey load (for unfinished surveys) and when the 'Back' button is clicked.
* @param {Object} results - The surveyState object.
* @param {string} questionID - The question ID.
* @returns {void}
*/
export function restoreResponses(results, questionID) {
const appState = getStateManager();
appState.clearActiveQuestionState();
const formElement = document.querySelector("#" + CSS.escape(questionID));
if (!formElement || !results[questionID]) return;
const response = results[questionID];
// CASE 1: The response is a simple string value.
if (typeof response === "string") {
handleSimpleStringResponse(formElement, response);
// CASE 2: Array
} else if (Array.isArray(results[questionID])) {
getFromRbCb(formElement, questionID, results[questionID]);
// CASE 3: Object
} else if (typeof response === "object") {
handleObjectResponse(formElement, response);
} else {
moduleParams.errorLogger('RESTORE RESPONSES: (unhandled response type):', response);
}
}