-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathInlineEmbed.js
119 lines (114 loc) · 4.82 KB
/
InlineEmbed.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
import { Formio } from './Embed';
/**
* Inline embed a form within a webpage.
* @param {*} config - Configuration to configure how the inline embed is rendered.
*/
export function embed(config = {}) {
const scripts = document.getElementsByTagName('script');
config = Object.assign({}, window.FormioConfig || {}, config);
let thisScript = null;
let i = scripts.length;
const scriptName = config.scriptName || 'formio.embed.';
while (i--) {
if (
scripts[i].src && (scripts[i].src.indexOf(scriptName) !== -1)
) {
thisScript = scripts[i];
break;
}
}
if (thisScript) {
const query = {};
const queryString = thisScript.src.replace(/^[^?]+\??/, '');
queryString.replace(/\?/g, '&').split('&').forEach((item) => {
query[item.split('=')[0]] = item.split('=')[1] && decodeURIComponent(item.split('=')[1]);
});
let scriptSrc = thisScript.src.replace(/^([^?]+).*/, '$1').split('/');
scriptSrc.pop();
let cdnSrc = '';
if (['js', 'offline'].includes(scriptSrc[scriptSrc.length - 1])) {
scriptSrc.pop();
scriptSrc = cdnSrc = scriptSrc.join('/');
scriptSrc += '/js';
}
else {
scriptSrc = scriptSrc.join('/');
}
const debug = (query.debug === 'true' || query.debug === '1');
const renderer = debug ? 'formio.form' : 'formio.form.min';
Formio.config = Object.assign({
script: query.script || (`${scriptSrc}/${renderer}.js`),
style: query.styles || (`${scriptSrc}/${renderer}.css`),
cdn: query.cdn || cdnSrc,
class: (query.class || 'formio-form-wrapper'),
src: query.src,
form: null,
submission: null,
project: query.project,
base: query.base || 'https://api.form.io',
submit: query.submit,
includeLibs: (query.libs === 'true' || query.libs === '1'),
noshadow: (query.shadow === 'false' || query.shadow === '0'),
template: query.template || 'bootstrap',
debug: debug,
config: {},
redirect: (query.return || query.redirect),
embedCSS: (`${scriptSrc}/formio.embed.css`),
success: query.success || 'Thank you for your submission!',
before: null,
after: null
}, config);
if (Formio.config.alter) {
Formio.config.alter(Formio.config);
}
const form = (Formio.config.form || Formio.config.src);
if (form) {
Formio.debug('Embedding Configuration', config);
// The id for this embedded form.
Formio.config.id = `formio-${Math.random().toString(36).substring(7)}`;
Formio.debug('Creating form element');
const element = Formio.createElement('div', {
'id': Formio.config.id,
class: Formio.config.class
});
// insertAfter doesn't exist, but effect is identical.
thisScript.parentNode.insertBefore(element, thisScript.parentNode.firstElementChild.nextSibling);
Formio.createForm(element, form, Formio.config.config).then((instance) => {
if (Formio.config.submit) {
instance.nosubmit = true;
}
// Trigger the submit done event.
instance.on('submitDone', (submission) => Formio.submitDone(instance, submission));
// Configure a redirect.
instance.on('submit', (submission) => {
Formio.debug("on('submit')", submission);
if (Formio.config.submit) {
Formio.debug(`Sending submission to ${Formio.config.submit}`);
const headers = {
'content-type': 'application/json'
};
const token = Formio.FormioClass.getToken();
if (token) {
headers['x-jwt-token'] = token;
}
Formio.FormioClass.fetch(Formio.config.submit, {
body: JSON.stringify(submission),
headers: headers,
method: 'POST',
mode: 'cors',
})
.then(resp => resp.json())
.then((submission) => {
Formio.submitDone(instance, submission);
});
}
});
});
}
}
else {
// Show an error if the script cannot be found.
document.write('<span>Could not locate the Embedded form.</span>');
}
}
export { Formio };