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 91
/
app.js
157 lines (138 loc) · 4.89 KB
/
app.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
/* eslint no-console: 0 */
/**
* This file is just meant to facilitate enketo-core development as a standalone library.
*
* When using enketo-core as a library inside your app, it is recommended to just **ignore** this file.
* Place a replacement for this controller elsewhere in your app.
*/
import support from './src/js/support';
import { Form } from './src/js/form';
import fileManager from './src/js/file-manager';
import events from './src/js/event';
import { fixGrid, styleToAll, styleReset } from './src/js/print';
let form;
let formStr;
let modelStr;
const xform = getURLParameter('xform');
// if querystring touch=true is added, override detected touchscreen presence
if (getURLParameter('touch') === 'true') {
support.touch = true;
document.querySelector('html').classList.add('touch');
}
// Check if HTML form is hardcoded or needs to be retrieved
// note: when running this file in enketo-core-performance-monitor xform = 'null'
if (xform && xform !== 'null') {
(async () => {
const isRemote = /^https?:\/\//.test(xform);
const xformURL = isRemote ? xform : `${location.origin}/${xform}`;
const transformerUrl = `http://${location.hostname}:8085/transform?xform=${xformURL}`;
try {
document.querySelector('.guidance').remove();
/** @type {import('enketo-transformer').TransformedSurvey & { modifiedTime?: number } | null} */
let survey = null;
if (!isRemote) {
// This must be dynamically imported or it'll be included in the build
const localForms = (await import(`./${'forms'}.mjs`)).default;
const localForm = localForms[xform];
if (localForm != null) {
survey = {
form: localForm.html_form,
model: localForm.xml_model,
};
}
}
if (survey == null) {
const response = await fetch(transformerUrl);
survey = await response.json();
}
formStr = survey.form;
modelStr = survey.model;
const range = document.createRange();
const formEl = range
.createContextualFragment(formStr)
.querySelector('form');
document.querySelector('.form-header').after(formEl);
initializeForm();
} catch (error) {
// eslint-disable-next-line no-alert
window.alert(
`Error fetching form from enketo-transformer at:
${transformerUrl}.\n\nPlease check that enketo-transformer has been started.
${error}`
);
throw error;
}
})();
} else if (document.querySelector('form.or')) {
document.querySelector('.guidance').remove();
modelStr = window.globalModelStr;
initializeForm();
}
// validate handler for validate button
document.querySelector('#validate-form').addEventListener('click', () => {
// validate form
form.validate().then((valid) => {
if (!valid) {
window.alert(
'Form contains errors. Please see fields marked in red.'
);
} else {
window.alert(
'Form is valid! (see XML record and media files in the console)'
);
form.view.html.dispatchEvent(events.BeforeSave());
console.log('record:', form.getDataStr());
console.log('media files:', fileManager.getCurrentFiles());
}
});
});
// initialize the form
function initializeForm() {
const formEl = document.querySelector('form.or');
form = new Form(
formEl,
{
modelStr,
},
{
printRelevantOnly: false,
}
);
// for debugging
window.form = form;
// initialize form and check for load errors
const loadErrors = form
.init()
.filter((error) => error !== "Can't find last-saved.");
if (loadErrors.length > 0) {
window.alert(`loadErrors: ${loadErrors.join(', ')}`);
}
}
// get query string parameter
function getURLParameter(name) {
return decodeURI(
(new RegExp(`${name}=` + `(.+?)(&|$)`).exec(location.search) || [
null,
null,
])[1]
);
}
// to facilitate developing print-specific issues
function printView(on = true, grid = false) {
if (on) {
document
.querySelectorAll('.question')
.forEach((el) => el.dispatchEvent(events.Printify()));
styleToAll();
if (grid) {
fixGrid({ format: 'letter' }).then(() => console.log('done'));
}
} else {
document
.querySelectorAll('.question')
.forEach((el) => el.dispatchEvent(events.DePrintify()));
styleReset();
}
}
window.printGridView = (on = true) => printView(on, true);
window.printView = printView;