This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
/
enketo-webform.js
264 lines (234 loc) · 7.62 KB
/
enketo-webform.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { FormModel } from 'enketo-core/src/js/form-model';
import gui from './module/gui';
import controller from './module/controller-webform';
import settings from './module/settings';
import connection from './module/connection';
import {
init as initTranslator,
t,
localize,
loadTranslation,
} from './module/translator';
import store from './module/store';
import utils from './module/utils';
import events from './module/event';
import formCache from './module/form-cache';
import applicationCache from './module/application-cache';
const loader = document.querySelector('.main-loader');
const formheader = document.querySelector('.main > .paper > .form-header');
const survey = {
enketoId: settings.enketoId,
xformUrl: settings.xformUrl,
defaults: settings.defaults,
};
const range = document.createRange();
_setEmergencyHandlers();
if (settings.offline) {
console.log('App in offline-capable mode.');
delete survey.xformUrl;
_setAppCacheEventHandlers();
applicationCache
.init(survey)
.then(initTranslator)
.then(formCache.init)
.then(_addBranding)
.then(_swapTheme)
.then(formCache.updateMaxSubmissionSize)
.then(_updateMaxSizeSetting)
.then(_init)
.then((formParts) => {
formParts.languages.forEach(loadTranslation);
return formParts;
})
.then(formCache.updateMedia)
.then(_setFormCacheEventHandlers)
.catch(_showErrorOrAuthenticate);
} else {
console.log('App in online-only mode.');
const isPreview = settings.type === 'preview';
store
.init({ failSilently: true })
.then(() => initTranslator(survey))
.then((props) =>
connection.getFormParts({
...props,
isPreview,
})
)
.then(_swapTheme)
.then(_addBranding)
.then((survey) => {
if (isPreview && settings.xformUrl) {
return survey;
}
return connection.getMaximumSubmissionSize(survey);
})
.then(_updateMaxSizeSetting)
.then(_init)
.catch(_showErrorOrAuthenticate);
}
function _updateMaxSizeSetting(survey) {
if (survey.maxSize) {
// overwrite default max size
settings.maxSize = survey.maxSize;
}
return survey;
}
function _showErrorOrAuthenticate(error) {
error = typeof error === 'string' ? new Error(error) : error;
loader.classList.add('fail');
if (error.status === 401) {
window.location.href = `${
settings.loginUrl
}?return_url=${encodeURIComponent(window.location.href)}`;
} else if (error.status === 404) {
gui.alertLoadErrors([error.message], null, {
omitIntro: true,
omitSupportContact: true,
});
} else {
if (!Array.isArray(error)) {
error = [error.message || t('error.unknown')];
}
gui.alertLoadErrors(error, t('alert.loaderror.entryadvice'));
}
}
function _setAppCacheEventHandlers() {
document.addEventListener(events.OfflineLaunchCapable().type, (event) => {
const { capable } = event.detail;
gui.updateStatus.offlineCapable(capable);
const scriptUrl = applicationCache.serviceWorkerScriptUrl;
if (scriptUrl) {
connection
.getServiceWorkerVersion(scriptUrl)
.then(gui.updateStatus.applicationVersion);
}
});
document.addEventListener(events.ApplicationUpdated().type, () => {
gui.feedback(
t('alert.appupdated.msg'),
20,
t('alert.appupdated.heading')
);
});
}
function _setFormCacheEventHandlers(survey) {
document.addEventListener(events.FormUpdated().type, () => {
gui.feedback(
t('alert.formupdated.msg'),
20,
t('alert.formupdated.heading')
);
});
return survey;
}
/**
* Advanced/emergency handlers that should always be activated even if form loading fails.
*/
function _setEmergencyHandlers() {
const flushBtn = document.querySelector(
'.side-slider__advanced__button.flush-db'
);
if (flushBtn) {
flushBtn.addEventListener('click', () => {
gui.confirm(
{
msg: t('confirm.deleteall.msg'),
heading: t('confirm.deleteall.heading'),
},
{
posButton: t('confirm.deleteall.posButton'),
}
)
.then((confirmed) => {
if (!confirmed) {
throw new Error('Cancelled by user');
}
return store.flush();
})
.then(() => {
location.reload();
})
.catch(() => {});
});
}
}
/**
* Adds/replaces branding if necessary, and unhides branding.
*
* @param { object } survey - [description]
*/
function _addBranding(survey) {
const brandImg = document.querySelector('.form-header__branding img');
const attribute = settings.offline ? 'data-offline-src' : 'src';
if (
brandImg &&
survey.branding &&
survey.branding.source &&
brandImg.src !== survey.branding.source
) {
brandImg.src = '';
brandImg.setAttribute(attribute, survey.branding.source);
}
brandImg.classList.remove('hide');
return survey;
}
/**
* Swaps the theme if necessary.
*
* @param { object } survey - [description]
* @return { object } [description]
*/
function _swapTheme(survey) {
if (survey.form && survey.model) {
return gui.swapTheme(survey);
}
return Promise.reject(new Error('Received form incomplete'));
}
function _prepareInstance(modelStr, defaults) {
let model;
let init;
let existingInstance = null;
for (const path in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, path)) {
model =
model ||
new FormModel(modelStr, {
full: false,
});
init = init || model.init();
if (Object.prototype.hasOwnProperty.call(defaults, path)) {
// if this fails, the FormModel will output a console error and ignore the instruction
model.node(path).setVal(defaults[path]);
}
// TODO: would be good to not include nodes that weren't in the defaults parameter
// HOWEVER, that would also set number of repeats to 0, which may be undesired
// TODO: would be good to just pass model along instead of converting to string first
existingInstance = model.getStr();
}
}
return existingInstance;
}
function _init(formParts) {
const formFragment = range.createContextualFragment(formParts.form);
formheader.after(formFragment);
const formEl = document.querySelector('form.or');
return controller
.init(formEl, {
modelStr: formParts.model,
instanceStr: _prepareInstance(formParts.model, settings.defaults),
external: formParts.externalData,
survey: formParts,
})
.then((form) => {
formParts.languages = form.languages;
document.querySelector('head>title').textContent =
utils.getTitleFromFormStr(formParts.form);
if (settings.print) {
gui.applyPrintStyle();
}
// after widgets have been initialized, localize all data-i18n elements
localize(formEl);
return formParts;
});
}