forked from formio/formio.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformio.form.js
101 lines (97 loc) · 2.99 KB
/
formio.form.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
import AllComponents from './components';
import Builders from './builders/Builders';
import Components from './components/Components';
import Displays from './displays/Displays';
import Templates from './templates/Templates';
import Providers from './providers';
import Rules from './validator/Rules';
import Widgets from './widgets';
import Formio from './Formio';
import Form from './Form';
import Utils from './utils';
Components.setComponents(AllComponents);
const registerPlugin = (plugin) => {
// Sanity check.
if (typeof plugin !== 'object') {
return;
}
for (const key of Object.keys(plugin)) {
const current = plugin.framework || Templates.framework || 'bootstrap';
switch (key) {
case 'options':
Formio.options = plugin.options;
break;
case 'templates':
for (const framework of Object.keys(plugin.templates)) {
Templates.extendTemplate(framework, plugin.templates[framework]);
}
if (plugin.templates[current]) {
Templates.current = plugin.templates[current];
}
break;
case 'components':
Components.setComponents(plugin.components);
break;
case 'framework':
Templates.framework = plugin.framework;
break;
case 'fetch':
for (const name of Object.keys(plugin.fetch)) {
Formio.registerPlugin(plugin.fetch[name], name);
}
break;
case 'providers':
for (const type of Object.keys(plugin.providers)) {
Providers.addProviders(type, plugin.providers[type]);
}
break;
case 'displays':
Displays.addDisplays(plugin.displays);
break;
case 'builders':
Builders.addBuilders(plugin.builders);
break;
case 'rules':
Rules.addRules(plugin.rules);
break;
default:
console.log('Unknown plugin option', key);
}
}
};
/**
* Allows passing in plugins as multiple arguments or an array of plugins.
*
* Formio.plugins(plugin1, plugin2, etc);
* Formio.plugins([plugin1, plugin2, etc]);
*/
Formio.use = (...plugins) => {
plugins.forEach((plugin) => {
if (Array.isArray(plugin)) {
plugin.forEach(p => registerPlugin(p));
}
else {
registerPlugin(plugin);
}
});
};
Formio.loadModules = (path = `${Formio.getApiUrl() }/externalModules.js`, name = 'externalModules') => {
Formio.requireLibrary(name, name, path, true)
.then((modules) => {
Formio.use(modules);
});
};
// This is needed to maintain correct imports using the "dist" file.
Formio.Components = Components;
Formio.Templates = Templates;
Formio.Builders = Builders;
Formio.Utils = Utils;
Formio.Form = Form;
Formio.Displays = Displays;
Formio.Providers = Providers;
Formio.Rules = Rules;
Formio.Widgets = Widgets;
// This is strange, but is needed for "premium" components to import correctly.
Formio.Formio = Formio;
// Export the components.
export { Builders, Components, Displays, Providers, Rules, Widgets, Templates, Utils, Form, Formio };