@formio/js 5.0.0-rc.20 → 5.0.0-rc.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. package/dist/formio.embed.css +1 -39
  2. package/dist/formio.embed.js +2 -90
  3. package/dist/formio.embed.min.js +1 -1
  4. package/dist/formio.embed.min.js.LICENSE.txt +1 -1
  5. package/dist/formio.form.js +57 -35
  6. package/dist/formio.form.min.js +1 -1
  7. package/dist/formio.form.min.js.LICENSE.txt +1 -1
  8. package/dist/formio.full.js +58 -36
  9. package/dist/formio.full.min.js +1 -1
  10. package/dist/formio.full.min.js.LICENSE.txt +1 -1
  11. package/dist/formio.js +2 -2
  12. package/dist/formio.min.js +1 -1
  13. package/dist/formio.min.js.LICENSE.txt +1 -1
  14. package/dist/formio.utils.js +1 -1
  15. package/dist/formio.utils.min.js +1 -1
  16. package/dist/formio.utils.min.js.LICENSE.txt +1 -1
  17. package/lib/cjs/CDN.d.ts +1 -0
  18. package/lib/cjs/CDN.js +1 -0
  19. package/lib/cjs/Embed.d.ts +21 -1
  20. package/lib/cjs/Embed.js +211 -308
  21. package/lib/cjs/Form.d.ts +1 -1
  22. package/lib/cjs/Form.js +1 -1
  23. package/lib/cjs/Webform.d.ts +6 -6
  24. package/lib/cjs/Webform.js +6 -6
  25. package/lib/cjs/WebformBuilder.js +1 -1
  26. package/lib/cjs/components/_classes/component/Component.d.ts +16 -6
  27. package/lib/cjs/components/_classes/component/Component.js +26 -6
  28. package/lib/cjs/components/_classes/nestedarray/NestedArrayComponent.d.ts +1 -0
  29. package/lib/cjs/components/_classes/nestedarray/NestedArrayComponent.js +4 -0
  30. package/lib/cjs/components/_classes/nesteddata/NestedDataComponent.d.ts +1 -0
  31. package/lib/cjs/components/_classes/nesteddata/NestedDataComponent.js +4 -0
  32. package/lib/cjs/components/address/Address.js +5 -0
  33. package/lib/cjs/components/button/Button.d.ts +1 -0
  34. package/lib/cjs/components/button/Button.js +3 -0
  35. package/lib/cjs/components/checkbox/Checkbox.d.ts +28 -0
  36. package/lib/cjs/components/checkbox/Checkbox.js +17 -2
  37. package/lib/cjs/components/columns/Columns.d.ts +1 -0
  38. package/lib/cjs/components/columns/Columns.js +3 -0
  39. package/lib/cjs/components/container/Container.js +3 -0
  40. package/lib/cjs/components/content/Content.d.ts +1 -0
  41. package/lib/cjs/components/content/Content.js +3 -0
  42. package/lib/cjs/components/datamap/DataMap.d.ts +1 -0
  43. package/lib/cjs/components/datamap/DataMap.js +3 -0
  44. package/lib/cjs/components/datetime/DateTime.d.ts +19 -0
  45. package/lib/cjs/components/datetime/DateTime.js +9 -10
  46. package/lib/cjs/components/day/Day.d.ts +21 -0
  47. package/lib/cjs/components/day/Day.js +8 -10
  48. package/lib/cjs/components/fieldset/Fieldset.d.ts +1 -0
  49. package/lib/cjs/components/fieldset/Fieldset.js +3 -0
  50. package/lib/cjs/components/file/File.d.ts +21 -1
  51. package/lib/cjs/components/file/File.js +15 -7
  52. package/lib/cjs/components/form/Form.d.ts +1 -0
  53. package/lib/cjs/components/form/Form.js +3 -0
  54. package/lib/cjs/components/html/HTML.d.ts +1 -0
  55. package/lib/cjs/components/html/HTML.js +3 -0
  56. package/lib/cjs/components/number/Number.d.ts +20 -1
  57. package/lib/cjs/components/number/Number.js +10 -15
  58. package/lib/cjs/components/panel/Panel.d.ts +1 -0
  59. package/lib/cjs/components/panel/Panel.js +3 -0
  60. package/lib/cjs/components/radio/Radio.d.ts +19 -0
  61. package/lib/cjs/components/radio/Radio.js +30 -0
  62. package/lib/cjs/components/recaptcha/ReCaptcha.d.ts +1 -0
  63. package/lib/cjs/components/recaptcha/ReCaptcha.js +3 -0
  64. package/lib/cjs/components/select/Select.d.ts +19 -1
  65. package/lib/cjs/components/select/Select.js +37 -3
  66. package/lib/cjs/components/selectboxes/SelectBoxes.d.ts +26 -1
  67. package/lib/cjs/components/selectboxes/SelectBoxes.js +8 -1
  68. package/lib/cjs/components/signature/Signature.d.ts +21 -0
  69. package/lib/cjs/components/signature/Signature.js +9 -1
  70. package/lib/cjs/components/survey/Survey.d.ts +21 -0
  71. package/lib/cjs/components/survey/Survey.js +7 -1
  72. package/lib/cjs/components/table/Table.d.ts +1 -0
  73. package/lib/cjs/components/table/Table.js +3 -0
  74. package/lib/cjs/components/tabs/Tabs.d.ts +1 -0
  75. package/lib/cjs/components/tabs/Tabs.js +3 -0
  76. package/lib/cjs/components/tags/Tags.d.ts +21 -0
  77. package/lib/cjs/components/tags/Tags.js +9 -1
  78. package/lib/cjs/components/textfield/TextField.d.ts +21 -0
  79. package/lib/cjs/components/textfield/TextField.js +7 -1
  80. package/lib/cjs/components/time/Time.d.ts +8 -1
  81. package/lib/cjs/components/well/Well.d.ts +1 -0
  82. package/lib/cjs/components/well/Well.js +3 -0
  83. package/lib/cjs/formio.embed.d.ts +2 -1
  84. package/lib/cjs/formio.embed.js +96 -1
  85. package/lib/cjs/formio.form.d.ts +4 -3
  86. package/lib/cjs/formio.form.js +17 -8
  87. package/lib/cjs/licenses/Licenses.d.ts +7 -0
  88. package/lib/cjs/licenses/Licenses.js +22 -0
  89. package/lib/cjs/licenses/index.d.ts +2 -0
  90. package/lib/cjs/licenses/index.js +7 -0
  91. package/lib/cjs/utils/utils.d.ts +10 -0
  92. package/lib/cjs/utils/utils.js +21 -1
  93. package/lib/mjs/CDN.d.ts +1 -0
  94. package/lib/mjs/CDN.js +1 -0
  95. package/lib/mjs/Embed.d.ts +21 -1
  96. package/lib/mjs/Embed.js +208 -322
  97. package/lib/mjs/Form.d.ts +1 -1
  98. package/lib/mjs/Form.js +1 -1
  99. package/lib/mjs/Webform.d.ts +6 -6
  100. package/lib/mjs/Webform.js +6 -6
  101. package/lib/mjs/WebformBuilder.js +1 -1
  102. package/lib/mjs/components/_classes/component/Component.d.ts +16 -6
  103. package/lib/mjs/components/_classes/component/Component.js +26 -6
  104. package/lib/mjs/components/_classes/nestedarray/NestedArrayComponent.d.ts +1 -0
  105. package/lib/mjs/components/_classes/nestedarray/NestedArrayComponent.js +4 -0
  106. package/lib/mjs/components/_classes/nesteddata/NestedDataComponent.d.ts +1 -0
  107. package/lib/mjs/components/_classes/nesteddata/NestedDataComponent.js +4 -0
  108. package/lib/mjs/components/address/Address.js +5 -0
  109. package/lib/mjs/components/button/Button.d.ts +1 -0
  110. package/lib/mjs/components/button/Button.js +4 -1
  111. package/lib/mjs/components/checkbox/Checkbox.d.ts +28 -0
  112. package/lib/mjs/components/checkbox/Checkbox.js +18 -3
  113. package/lib/mjs/components/columns/Columns.d.ts +1 -0
  114. package/lib/mjs/components/columns/Columns.js +3 -0
  115. package/lib/mjs/components/container/Container.js +4 -1
  116. package/lib/mjs/components/content/Content.d.ts +1 -0
  117. package/lib/mjs/components/content/Content.js +3 -0
  118. package/lib/mjs/components/datamap/DataMap.d.ts +1 -0
  119. package/lib/mjs/components/datamap/DataMap.js +4 -1
  120. package/lib/mjs/components/datetime/DateTime.d.ts +19 -0
  121. package/lib/mjs/components/datetime/DateTime.js +11 -12
  122. package/lib/mjs/components/day/Day.d.ts +21 -0
  123. package/lib/mjs/components/day/Day.js +12 -14
  124. package/lib/mjs/components/fieldset/Fieldset.d.ts +1 -0
  125. package/lib/mjs/components/fieldset/Fieldset.js +3 -0
  126. package/lib/mjs/components/file/File.d.ts +21 -1
  127. package/lib/mjs/components/file/File.js +16 -8
  128. package/lib/mjs/components/form/Form.d.ts +1 -0
  129. package/lib/mjs/components/form/Form.js +4 -1
  130. package/lib/mjs/components/html/HTML.d.ts +1 -0
  131. package/lib/mjs/components/html/HTML.js +3 -0
  132. package/lib/mjs/components/number/Number.d.ts +20 -1
  133. package/lib/mjs/components/number/Number.js +11 -12
  134. package/lib/mjs/components/panel/Panel.d.ts +1 -0
  135. package/lib/mjs/components/panel/Panel.js +3 -0
  136. package/lib/mjs/components/radio/Radio.d.ts +19 -0
  137. package/lib/mjs/components/radio/Radio.js +34 -1
  138. package/lib/mjs/components/recaptcha/ReCaptcha.d.ts +1 -0
  139. package/lib/mjs/components/recaptcha/ReCaptcha.js +3 -0
  140. package/lib/mjs/components/select/Select.d.ts +19 -1
  141. package/lib/mjs/components/select/Select.js +39 -5
  142. package/lib/mjs/components/selectboxes/SelectBoxes.d.ts +26 -1
  143. package/lib/mjs/components/selectboxes/SelectBoxes.js +8 -1
  144. package/lib/mjs/components/signature/Signature.d.ts +21 -0
  145. package/lib/mjs/components/signature/Signature.js +9 -1
  146. package/lib/mjs/components/survey/Survey.d.ts +21 -0
  147. package/lib/mjs/components/survey/Survey.js +8 -2
  148. package/lib/mjs/components/table/Table.d.ts +1 -0
  149. package/lib/mjs/components/table/Table.js +3 -0
  150. package/lib/mjs/components/tabs/Tabs.d.ts +1 -0
  151. package/lib/mjs/components/tabs/Tabs.js +3 -0
  152. package/lib/mjs/components/tags/Tags.d.ts +21 -0
  153. package/lib/mjs/components/tags/Tags.js +10 -2
  154. package/lib/mjs/components/textfield/TextField.d.ts +21 -0
  155. package/lib/mjs/components/textfield/TextField.js +8 -2
  156. package/lib/mjs/components/time/Time.d.ts +8 -1
  157. package/lib/mjs/components/well/Well.d.ts +1 -0
  158. package/lib/mjs/components/well/Well.js +3 -0
  159. package/lib/mjs/formio.embed.d.ts +2 -1
  160. package/lib/mjs/formio.embed.js +96 -2
  161. package/lib/mjs/formio.form.d.ts +4 -3
  162. package/lib/mjs/formio.form.js +16 -8
  163. package/lib/mjs/licenses/Licenses.d.ts +7 -0
  164. package/lib/mjs/licenses/Licenses.js +17 -0
  165. package/lib/mjs/licenses/index.d.ts +2 -0
  166. package/lib/mjs/licenses/index.js +2 -0
  167. package/lib/mjs/utils/utils.d.ts +10 -0
  168. package/lib/mjs/utils/utils.js +19 -0
  169. package/package.json +5 -1
  170. package/types/index.d.ts +1 -0
  171. package/types/licenses.d.ts +7 -0
  172. package/types/utils.d.ts +2 -0
package/lib/mjs/Embed.js CHANGED
@@ -1,181 +1,179 @@
1
- import CDN from './CDN';
2
- // eslint-disable-next-line max-statements
3
- export function embed(config = {}) {
4
- const scripts = document.getElementsByTagName('script');
5
- config = Object.assign(config, window.FormioConfig);
6
- let thisScript = null;
7
- let i = scripts.length;
8
- const scriptName = config.scriptName || 'formio.embed.';
9
- while (i--) {
10
- if (scripts[i].src && (scripts[i].src.indexOf(scriptName) !== -1)) {
11
- thisScript = scripts[i];
12
- break;
1
+ import CDN from './CDN.js';
2
+ class Formio {
3
+ static baseUrl;
4
+ static projectUrl;
5
+ static language;
6
+ static wrapper;
7
+ static element;
8
+ static config = {};
9
+ static loader = null;
10
+ static cdn = null;
11
+ static proxy = true;
12
+ static async setBaseUrl(url) {
13
+ Formio.baseUrl = url;
14
+ }
15
+ static async setProjectUrl(url) {
16
+ Formio.projectUrl = url;
17
+ }
18
+ static debug(...args) {
19
+ if (Formio.config.debug) {
20
+ console.log(...args);
21
+ }
22
+ }
23
+ static global(prop) {
24
+ const globalValue = window[prop];
25
+ if (globalValue && globalValue.proxy) {
26
+ return null;
13
27
  }
28
+ Formio.debug(`Getting global ${prop}`, globalValue);
29
+ return globalValue;
14
30
  }
15
- if (thisScript) {
16
- const query = {};
17
- const queryString = thisScript.src.replace(/^[^?]+\??/, '');
18
- queryString.replace(/\?/g, '&').split('&').forEach((item) => {
19
- query[item.split('=')[0]] = item.split('=')[1] && decodeURIComponent(item.split('=')[1]);
31
+ static createElement(type, attrs, children) {
32
+ const element = document.createElement(type);
33
+ Object.keys(attrs).forEach(key => {
34
+ element.setAttribute(key, attrs[key]);
20
35
  });
21
- let scriptSrc = thisScript.src.replace(/^([^?]+).*/, '$1').split('/');
22
- scriptSrc.pop();
23
- if (config.formioPath) {
24
- config.formioPath(scriptSrc);
36
+ (children || []).forEach(child => {
37
+ element.appendChild(Formio.createElement(child.tag, child.attrs, child.children));
38
+ });
39
+ return element;
40
+ }
41
+ static async addScript(src, name) {
42
+ if (!src) {
43
+ return Promise.resolve();
25
44
  }
26
- scriptSrc = scriptSrc.join('/');
27
- query.script = query.script || (`${config.updatePath ? config.updatePath() : scriptSrc}/formio.form.min.js`);
28
- query.styles = query.styles || (`${config.updatePath ? config.updatePath() : scriptSrc}/formio.form.min.css`);
29
- const cdn = query.cdn || 'https://cdn.jsdelivr.net/npm';
30
- const resolveLibs = (cdn) => {
31
- const libs = {
32
- uswds: {
33
- fa: true,
34
- js: `${cdn}/uswds@2.10.0/dist/js/uswds.min.js`,
35
- css: `${cdn}/uswds@2.10.0/dist/css/uswds.min.css`
36
- },
37
- fontawesome: {
38
- css: `${cdn}/font-awesome@4.7.0/css/font-awesome.min.css`
39
- },
40
- bootstrap: {
41
- css: `${cdn}/bootstrap@4.6.0/dist/css/bootstrap.min.css`
42
- }
43
- };
44
- // Check if using cdn.form.io standart folders format
45
- if (cdn instanceof CDN) {
46
- const url = cdn.baseUrl;
47
- libs.uswds.js = `${url}/uswds/${cdn.getVersion('uswds')}/uswds.min.js`;
48
- libs.uswds.css = `${url}/uswds/${cdn.getVersion('uswds')}/uswds.min.css`;
49
- libs.fontawesome.css = `${url}/font-awesome/${cdn.getVersion('font-awesome')}/css/font-awesome.min.css`;
50
- libs.bootstrap.css = `${url}/bootstrap/${cdn.getVersion('bootstrap')}/css/bootstrap.min.css`;
51
- }
52
- return libs;
53
- };
54
- config = Object.assign({
55
- script: query.script,
56
- style: query.styles,
57
- class: (query.class || 'formio-form-wrapper'),
58
- src: query.src,
59
- form: null,
60
- submission: null,
61
- project: query.project,
62
- base: query.base,
63
- submit: query.submit,
64
- includeLibs: (query.libs === 'true' || query.libs === '1'),
65
- template: query.template,
66
- debug: (query.debug === 'true' || query.debug === '1'),
67
- config: {},
68
- redirect: (query.return || query.redirect),
69
- before: () => { },
70
- after: () => { }
71
- }, config);
72
- /**
73
- * Print debug statements.
74
- *
75
- * @param {...any} args Arguments to pass to the console.log method.
76
- */
77
- const debug = (...args) => {
78
- if (config.debug) {
79
- console.log(...args);
80
- }
81
- };
82
- /**
83
- * Creates a new DOM element.
84
- *
85
- * @param {string} tag The HTMLElement to add to the wrapper or shadow dom.
86
- * @param {Object} attrs The attributes to add to this element.
87
- * @param {Array<Object>} children The children attached to this element.
88
- */
89
- const createElement = (tag, attrs, children) => {
90
- const element = document.createElement(tag);
91
- for (const attr in attrs) {
92
- if (attrs.hasOwnProperty(attr)) {
93
- element.setAttribute(attr, attrs[attr]);
45
+ if (typeof src !== 'string' && src.length) {
46
+ return Promise.all(src.map(ref => Formio.addScript(ref)));
47
+ }
48
+ if (name && Formio.global(name)) {
49
+ Formio.debug(`${name} already loaded.`);
50
+ return Promise.resolve(Formio.global(name));
51
+ }
52
+ Formio.debug('Adding Script', src);
53
+ Formio.wrapper.appendChild(Formio.createElement('script', {
54
+ src
55
+ }));
56
+ if (!name) {
57
+ return Promise.resolve();
58
+ }
59
+ return new Promise((resolve) => {
60
+ Formio.debug(`Waiting to load ${name}`);
61
+ const wait = setInterval(() => {
62
+ if (Formio.global(name)) {
63
+ clearInterval(wait);
64
+ Formio.debug(`${name} loaded.`);
65
+ resolve(Formio.global(name));
94
66
  }
95
- }
96
- (children || []).forEach(child => {
97
- element.appendChild(createElement(child.tag, child.attrs, child.children));
98
- });
99
- return element;
100
- };
101
- debug('Embedding Configuration', config);
102
- if (config.addPremiumLib) {
103
- config.addPremiumLib(config, scriptSrc);
67
+ }, 100);
68
+ });
69
+ }
70
+ static async addStyles(href, addGlobally = false) {
71
+ if (!href) {
72
+ return;
73
+ }
74
+ if (typeof href !== 'string' && href.length) {
75
+ href.forEach(ref => Formio.addStyles(ref));
76
+ return;
104
77
  }
105
- // The id for this embedded form.
106
- const id = `formio-${Math.random().toString(36).substring(7)}`;
107
- config.id = id;
108
- debug('Creating form wrapper');
109
- let wrapper = createElement('div', {
110
- 'id': `"${id}-wrapper"`
78
+ Formio.debug('Adding Styles', href);
79
+ const link = Formio.createElement('link', {
80
+ rel: 'stylesheet',
81
+ href
111
82
  });
112
- // insertAfter doesn't exist, but effect is identical.
113
- thisScript.parentNode.insertBefore(wrapper, thisScript.parentNode.firstElementChild.nextSibling);
114
- // If we include the libraries, then we will attempt to run this in shadow dom.
115
- if (config.includeLibs && (typeof wrapper.attachShadow === 'function') && !config.premium) {
116
- wrapper = wrapper.attachShadow({
117
- mode: 'open'
118
- });
119
- config.config.shadowRoot = wrapper;
83
+ if (addGlobally) {
84
+ // Add globally as well.
85
+ document.head.appendChild(link);
120
86
  }
121
- const global = (name) => {
122
- const globalValue = window[name];
123
- debug(`Getting global ${name}`, globalValue);
124
- return globalValue;
125
- };
126
- const addStyles = (href, global) => {
127
- if (!href) {
128
- return;
129
- }
130
- if (typeof href !== 'string' && href.length) {
131
- href.forEach(ref => addStyles(ref));
132
- return;
133
- }
134
- debug('Adding Styles', href);
135
- const link = createElement('link', {
136
- rel: 'stylesheet',
137
- href
138
- });
139
- if (global) {
140
- // Add globally as well.
141
- document.head.appendChild(link);
142
- }
143
- wrapper.appendChild(createElement('link', {
144
- rel: 'stylesheet',
145
- href
146
- }));
147
- };
148
- const addScript = (src, globalProp, onReady) => {
149
- if (!src) {
150
- return;
151
- }
152
- if (typeof src !== 'string' && src.length) {
153
- src.forEach(ref => addScript(ref));
154
- return;
155
- }
156
- if (globalProp && global(globalProp)) {
157
- debug(`${globalProp} already loaded.`);
158
- return global(globalProp);
87
+ Formio.wrapper.appendChild(Formio.createElement('link', {
88
+ rel: 'stylesheet',
89
+ href
90
+ }));
91
+ }
92
+ static async submitDone(instance, submission) {
93
+ Formio.debug('Submision Complete', submission);
94
+ let returnUrl = Formio.config.redirect;
95
+ // Allow form based configuration for return url.
96
+ if (!returnUrl &&
97
+ (instance._form &&
98
+ instance._form.settings &&
99
+ (instance._form.settings.returnUrl ||
100
+ instance._form.settings.redirect))) {
101
+ Formio.debug('Return url found in form configuration');
102
+ returnUrl = instance._form.settings.returnUrl || instance._form.settings.redirect;
103
+ }
104
+ if (returnUrl) {
105
+ const formSrc = instance.formio ? instance.formio.formUrl : '';
106
+ const hasQuery = !!returnUrl.match(/\?/);
107
+ const isOrigin = returnUrl.indexOf(location.origin) === 0;
108
+ returnUrl += hasQuery ? '&' : '?';
109
+ returnUrl += `sub=${submission._id}`;
110
+ if (!isOrigin && formSrc) {
111
+ returnUrl += `&form=${encodeURIComponent(formSrc)}`;
112
+ }
113
+ Formio.debug('Return URL', returnUrl);
114
+ window.location.href = returnUrl;
115
+ if (isOrigin) {
116
+ window.location.reload();
159
117
  }
160
- debug('Adding Script', src);
161
- wrapper.appendChild(createElement('script', {
162
- src
163
- }));
164
- if (globalProp && onReady) {
165
- debug(`Waiting to load ${globalProp}`);
166
- const wait = setInterval(() => {
167
- if (global(globalProp)) {
168
- clearInterval(wait);
169
- debug(`${globalProp} loaded.`);
170
- onReady(global(globalProp));
171
- }
172
- }, 100);
118
+ }
119
+ }
120
+ static async renderForm(form, options) {
121
+ if (Formio.config.before) {
122
+ await Formio.config.before(Formio.FormioClass, Formio.element, Formio.config);
123
+ }
124
+ Formio.FormioClass.license = true;
125
+ return Formio.FormioClass.createForm(Formio.element, form, {
126
+ ...options,
127
+ ...{ noLoader: true }
128
+ }).then((instance) => {
129
+ Formio.debug('Form created', instance);
130
+ // Remove the loader.
131
+ Formio.debug('Removing loader');
132
+ Formio.wrapper.removeChild(Formio.loader);
133
+ // Set the default submission data.
134
+ if (Formio.config.submission) {
135
+ Formio.debug('Setting submission', Formio.config.submission);
136
+ instance.submission = Formio.config.submission;
137
+ }
138
+ // Allow them to provide additional configs.
139
+ Formio.debug('Triggering embed event');
140
+ Formio.FormioClass.events.emit('formEmbedded', instance);
141
+ // Trigger the after handler.
142
+ if (Formio.config.after) {
143
+ Formio.debug('Calling ready callback');
144
+ Formio.config.after(instance, Formio.config);
145
+ }
146
+ return instance;
147
+ });
148
+ }
149
+ // eslint-disable-next-line max-statements
150
+ static async createForm(element, form, options) {
151
+ Formio.element = element;
152
+ Formio.cdn = new CDN(Formio.config.cdn);
153
+ Formio.config.libs = Formio.config.libs || {
154
+ uswds: {
155
+ fa: true,
156
+ js: `${Formio.cdn.uswds}/uswds.min.js`,
157
+ css: `${Formio.cdn.uswds}/uswds.min.css`,
158
+ },
159
+ fontawesome: {
160
+ css: `${Formio.cdn['font-awesome']}/css/font-awesome.min.css`
161
+ },
162
+ bootstrap: {
163
+ css: `${Formio.cdn.bootstrap}/css/bootstrap.min.css`
173
164
  }
174
165
  };
175
- // Create a loader
176
- addStyles(`${config.updatePath ? config.updatePath() : scriptSrc}/formio.embed.min.css`);
177
- debug('Creating loader');
178
- const loader = createElement('div', {
166
+ Formio.config.id = Formio.config.id || `formio-${Math.random().toString(36).substring(7)}`;
167
+ // Create a new wrapper and add the element inside of a new wrapper.
168
+ Formio.wrapper = Formio.createElement('div', {
169
+ 'id': `"${Formio.config.id}-wrapper"`
170
+ });
171
+ element.parentNode.insertBefore(Formio.wrapper, element);
172
+ element.parentNode.removeChild(element);
173
+ Formio.wrapper.appendChild(element);
174
+ // Load the renderer styles.
175
+ await Formio.addStyles(Formio.config.embedCSS || `${Formio.cdn.js}/formio.embed.css`);
176
+ Formio.loader = Formio.createElement('div', {
179
177
  'class': 'formio-loader'
180
178
  }, [{
181
179
  tag: 'div',
@@ -189,163 +187,51 @@ export function embed(config = {}) {
189
187
  }
190
188
  }]
191
189
  }]);
192
- wrapper.appendChild(loader);
193
- // Add the wrapper for the rendered form.
194
- debug('Creating form element');
195
- const formElement = createElement('div', {
196
- class: config.class
197
- });
198
- wrapper.appendChild(formElement);
199
- // Add the main formio script.
200
- addScript(config.script, 'Formio', (Formio) => {
201
- const renderForm = () => {
202
- addStyles(config.style);
203
- const isReady = config.before(Formio, formElement, config) || Formio.Promise.resolve();
204
- const form = (config.form || config.src);
205
- debug('Creating form', form, config.config);
206
- isReady.then(() => {
207
- Formio.license = true;
208
- Formio.createForm(formElement, form, {
209
- ...config.config,
210
- ...{ noLoader: true }
211
- }).then((instance) => {
212
- const submitDone = (submission) => {
213
- debug('Submision Complete', submission);
214
- let returnUrl = config.redirect;
215
- // Allow form based configuration for return url.
216
- if (!returnUrl &&
217
- (instance._form &&
218
- instance._form.settings &&
219
- (instance._form.settings.returnUrl ||
220
- instance._form.settings.redirect))) {
221
- debug('Return url found in form configuration');
222
- returnUrl = instance._form.settings.returnUrl || instance._form.settings.redirect;
223
- }
224
- if (returnUrl) {
225
- const formSrc = instance.formio ? instance.formio.formUrl : '';
226
- const hasQuery = !!returnUrl.match(/\?/);
227
- const isOrigin = returnUrl.indexOf(location.origin) === 0;
228
- returnUrl += hasQuery ? '&' : '?';
229
- returnUrl += `sub=${submission._id}`;
230
- if (!isOrigin && formSrc) {
231
- returnUrl += `&form=${encodeURIComponent(formSrc)}`;
232
- }
233
- debug('Return URL', returnUrl);
234
- window.location.href = returnUrl;
235
- if (isOrigin) {
236
- window.location.reload();
237
- }
238
- }
239
- };
240
- if (config.submit) {
241
- instance.nosubmit = true;
242
- }
243
- debug('Form created', instance);
244
- // Remove the loader.
245
- debug('Removing loader');
246
- wrapper.removeChild(loader);
247
- // Set the default submission data.
248
- if (config.submission) {
249
- debug('Setting submission', config.submission);
250
- instance.submission = config.submission;
251
- }
252
- // Allow them to provide additional configs.
253
- debug('Triggering embed event');
254
- Formio.events.emit('formEmbedded', instance);
255
- debug('Calling ready callback');
256
- config.after(instance, config);
257
- // Configure a redirect.
258
- instance.on('submit', (submission) => {
259
- debug("on('submit')", submission);
260
- if (config.submit) {
261
- debug(`Sending submission to ${config.submit}`);
262
- const headers = {
263
- 'content-type': 'application/json'
264
- };
265
- const token = Formio.getToken();
266
- if (token) {
267
- headers['x-jwt-token'] = token;
268
- }
269
- Formio.fetch(config.submit, {
270
- body: JSON.stringify(submission),
271
- headers: headers,
272
- method: 'POST',
273
- mode: 'cors',
274
- })
275
- .then(resp => resp.json())
276
- .then(submission => submitDone(submission));
277
- }
278
- else {
279
- submitDone(submission);
280
- }
281
- });
282
- });
283
- });
284
- };
285
- if (config.base) {
286
- Formio.setBaseUrl(config.base);
287
- }
288
- if (config.project) {
289
- Formio.setProjectUrl(config.project);
290
- }
291
- // Add premium modules
292
- if (global('premium')) {
293
- debug('Using premium module.');
294
- Formio.use(global('premium'));
295
- }
296
- if (global('vpat')) {
297
- debug('Using vpat module.');
298
- Formio.use(global('vpat'));
299
- }
300
- if (config.template) {
301
- if (config.includeLibs) {
302
- addStyles(config.libs[config.template].css);
303
- addScript(config.libs[config.template].js);
304
- if (config.libs[config.template].fa) {
305
- addStyles(config.libs.fontawesome.css, true);
306
- }
307
- }
308
- let templateSrc;
309
- if (cdn instanceof CDN) {
310
- templateSrc = `${cdn[config.template]}/${config.template}.min`;
311
- }
312
- else {
313
- templateSrc = `${cdn}/@formio/${config.template}@latest/dist/${config.template}.min`;
190
+ Formio.wrapper.appendChild(Formio.loader);
191
+ Formio.FormioClass = await Formio.addScript(Formio.config.script || `${Formio.cdn.js}/formio.form.min.js`, 'Formio');
192
+ Formio.FormioClass.setBaseUrl(Formio.baseUrl || Formio.config.base);
193
+ Formio.FormioClass.setProjectUrl(Formio.projectUrl || Formio.config.project);
194
+ Formio.FormioClass.language = Formio.language;
195
+ // Add premium modules
196
+ if (Formio.global('premium')) {
197
+ Formio.debug('Using premium module.');
198
+ Formio.FormioClass.use(Formio.global('premium'));
199
+ }
200
+ if (Formio.global('vpat')) {
201
+ Formio.debug('Using vpat module.');
202
+ Formio.FormioClass.use(Formio.global('vpat'));
203
+ }
204
+ if (Formio.config.template) {
205
+ if (Formio.config.includeLibs) {
206
+ await Formio.addStyles(Formio.config.libs[Formio.config.template].css);
207
+ await Formio.addScript(Formio.config.libs[Formio.config.template].js);
208
+ if (Formio.config.libs[Formio.config.template].fa) {
209
+ await Formio.addStyles(Formio.config.libs.fontawesome.css, true);
314
210
  }
315
- addStyles(`${templateSrc}.css`);
316
- addScript(`${templateSrc}.js`, config.template, (template) => {
317
- debug(`Using ${config.template}`);
318
- Formio.use(template);
319
- renderForm();
320
- });
321
- }
322
- else if (global('uswds')) {
323
- debug('Using uswds module.');
324
- Formio.use(global('uswds'));
325
- }
326
- // Default bootstrap + fontawesome.
327
- else if (config.includeLibs) {
328
- Formio.cdn = new CDN();
329
- config.libs = resolveLibs(query.cdn || Formio.cdn);
330
- addStyles(config.libs.fontawesome.css, true);
331
- addStyles(config.libs.bootstrap.css);
332
211
  }
333
- if (config.premium) {
334
- addStyles(config.premium.css);
335
- addScript(config.premium.js, 'premium', (premium) => {
336
- debug('Using premium');
337
- Formio.use(premium);
338
- renderForm();
339
- });
212
+ if (Formio.cdn[Formio.config.template]) {
213
+ const templateSrc = `${Formio.cdn[Formio.config.template]}/${Formio.config.template}.min`;
214
+ await Formio.addStyles(`${templateSrc}.css`);
215
+ Formio.debug(`Using ${Formio.config.template}`);
216
+ Formio.FormioClass.use(await Formio.addScript(`${templateSrc}.js`, Formio.config.template));
340
217
  }
341
- // Render the form if no template is provided.
342
- if (!config.template && !config.premium) {
343
- renderForm();
344
- }
345
- });
346
- }
347
- else {
348
- // Show an error if the script cannot be found.
349
- document.write('<span>Could not locate the Embedded form.</span>');
218
+ }
219
+ else if (Formio.global('uswds')) {
220
+ Formio.debug('Using uswds module.');
221
+ Formio.FormioClass.use(Formio.global('uswds'));
222
+ }
223
+ // Default bootstrap + fontawesome.
224
+ else if (Formio.config.includeLibs) {
225
+ await Formio.addStyles(Formio.config.libs.fontawesome.css, true);
226
+ await Formio.addStyles(Formio.config.libs.bootstrap.css);
227
+ }
228
+ if (Formio.config.premium) {
229
+ await Formio.addStyles(Formio.config.premium.css);
230
+ Formio.debug('Using premium');
231
+ Formio.FormioClass.use(await Formio.addScript(Formio.config.premium.js, 'premium'));
232
+ }
233
+ await Formio.addStyles(Formio.config.style || `${Formio.cdn.js}/formio.form.min.css`);
234
+ return await Formio.renderForm(form, options);
350
235
  }
351
236
  }
237
+ export { Formio };
package/lib/mjs/Form.d.ts CHANGED
@@ -12,7 +12,7 @@ export default class Form extends Element {
12
12
  * @param {boolean} options.template - Provides a way to inject custom logic into the creation of every element rendered within the form.
13
13
  *
14
14
  * @example
15
- * import Form from 'formiojs/Form';
15
+ * import Form from '@formio/js/Form';
16
16
  * const form = new Form(document.getElementById('formio'), 'https://examples.form.io/example');
17
17
  * form.build();
18
18
  */
package/lib/mjs/Form.js CHANGED
@@ -16,7 +16,7 @@ export default class Form extends Element {
16
16
  * @param {boolean} options.template - Provides a way to inject custom logic into the creation of every element rendered within the form.
17
17
  *
18
18
  * @example
19
- * import Form from 'formiojs/Form';
19
+ * import Form from '@formio/js/Form';
20
20
  * const form = new Form(document.getElementById('formio'), 'https://examples.form.io/example');
21
21
  * form.build();
22
22
  */
@@ -65,7 +65,7 @@ declare class Webform extends NestedDataComponent {
65
65
  * @type {Promise}
66
66
  *
67
67
  * @example
68
- * import Webform from 'formiojs/Webform';
68
+ * import Webform from '@formio/js/Webform';
69
69
  * let form = new Webform(document.getElementById('formio'));
70
70
  * form.formReady.then(() => {
71
71
  * console.log('The form is ready!');
@@ -90,7 +90,7 @@ declare class Webform extends NestedDataComponent {
90
90
  * @type {Promise}
91
91
  *
92
92
  * @example
93
- * import Webform from 'formiojs/Webform';
93
+ * import Webform from '@formio/js/Webform';
94
94
  * let form = new Webform(document.getElementById('formio'));
95
95
  * form.submissionReady.then(() => {
96
96
  * console.log('The submission is ready!');
@@ -140,7 +140,7 @@ declare class Webform extends NestedDataComponent {
140
140
  * @param {string} value - The value of the form embed url.
141
141
  *
142
142
  * @example
143
- * import Webform from 'formiojs/Webform';
143
+ * import Webform from '@formio/js/Webform';
144
144
  * let form = new Webform(document.getElementById('formio'));
145
145
  * form.formReady.then(() => {
146
146
  * console.log('The form is formReady!');
@@ -207,7 +207,7 @@ declare class Webform extends NestedDataComponent {
207
207
  * Sets the JSON schema for the form to be rendered.
208
208
  *
209
209
  * @example
210
- * import Webform from 'formiojs/Webform';
210
+ * import Webform from '@formio/js/Webform';
211
211
  * let form = new Webform(document.getElementById('formio'));
212
212
  * form.setForm({
213
213
  * components: [
@@ -257,7 +257,7 @@ declare class Webform extends NestedDataComponent {
257
257
  * Sets the submission of a form.
258
258
  *
259
259
  * @example
260
- * import Webform from 'formiojs/Webform';
260
+ * import Webform from '@formio/js/Webform';
261
261
  * let form = new Webform(document.getElementById('formio'));
262
262
  * form.src = 'https://examples.form.io/example';
263
263
  * form.submission = {data: {
@@ -372,7 +372,7 @@ declare class Webform extends NestedDataComponent {
372
372
  * Submits the form.
373
373
  *
374
374
  * @example
375
- * import Webform from 'formiojs/Webform';
375
+ * import Webform from '@formio/js/Webform';
376
376
  * let form = new Webform(document.getElementById('formio'));
377
377
  * form.src = 'https://examples.form.io/example';
378
378
  * form.submission = {data: {