Skip to content

Commit

Permalink
Changes to experimental exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
travist committed Mar 23, 2024
1 parent 882e7ea commit 20307d4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 105 deletions.
189 changes: 87 additions & 102 deletions src/experimental/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,97 @@ import { Formio } from '../sdk';
import { Evaluator, Utils } from '../utils';
import { Components, render } from './base';
import { Template } from './template';
(Formio as any).render = render;
(Formio as any).Components = Components;
(Formio as any).Evaluator = Evaluator;
(Formio as any).Utils = Utils;
(Formio as any).Templates = Template;
import { merge } from 'lodash';
import components from './components';
import modules from '../modules';

/**
* Register a specific plugin.
*
* @param key
* @param plugin
* @returns
*/
export function usePlugin(key: string, plugin: any) {
switch (key) {
case 'options':
if (!(Formio as any).options) {
return;
}
(Formio as any).options = merge((Formio as any).options, plugin);
break;
case 'templates':
if (!(Formio as any).Templates) {
return;
}
const current = (Formio as any).Templates.framework || 'bootstrap';
for (const framework of Object.keys(plugin)) {
(Formio as any).Templates.extendTemplate(framework, plugin[framework]);
}
if (plugin[current]) {
(Formio as any).Templates.current = plugin[current];
}
break;
case 'components':
if (!(Formio as any).Components) {
return;
}
(Formio as any).Components.setComponents(plugin);
break;
case 'framework':
if (!(Formio as any).Templates) {
return;
}
(Formio as any).Templates.framework = plugin;
break;
case 'fetch':
for (const name of Object.keys(plugin)) {
Formio.registerPlugin(plugin[name], name);
}
break;
case 'rules':
if (!(Formio as any).Rules) {
return;
}
(Formio as any).Rules.addRules(plugin);
break;
case 'evaluator':
if (!(Formio as any).Evaluator) {
return;
}
(Formio as any).Evaluator.registerEvaluator(plugin);
break;
default:
console.log('Unknown plugin option', key);
}
};

/**
* Register a new module.
*
* @param module
* @returns
*/
export function useModule(module: any) {
// Sanity check.
if (typeof module !== 'object') {
return;
}
for (const key of Object.keys(module)) {
usePlugin(key, module[key]);
export default class FormioCore extends Formio {
static Components = Components;
static render = render;
static Evaluator = Evaluator;
static Utils = Utils;
static Templates = Template;
static usePlugin(key: string, plugin: any) {
switch (key) {
case 'options':
if (!(Formio as any).options) {
return;
}
(Formio as any).options = merge((Formio as any).options, plugin);
break;
case 'templates':
if (!(Formio as any).Templates) {
return;
}
const current = (Formio as any).Templates.framework || 'bootstrap';
for (const framework of Object.keys(plugin)) {
(Formio as any).Templates.extendTemplate(framework, plugin[framework]);
}
if (plugin[current]) {
(Formio as any).Templates.current = plugin[current];
}
break;
case 'components':
if (!(Formio as any).Components) {
return;
}
(Formio as any).Components.setComponents(plugin);
break;
case 'framework':
if (!(Formio as any).Templates) {
return;
}
(Formio as any).Templates.framework = plugin;
break;
case 'fetch':
for (const name of Object.keys(plugin)) {
Formio.registerPlugin(plugin[name], name);
}
break;
case 'rules':
if (!(Formio as any).Rules) {
return;
}
(Formio as any).Rules.addRules(plugin);
break;
case 'evaluator':
if (!(Formio as any).Evaluator) {
return;
}
(Formio as any).Evaluator.registerEvaluator(plugin);
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]);
*/
export function use(...mods: any) {
mods.forEach((mod: any) => {
if (Array.isArray(mod)) {
mod.forEach(p => useModule(p));
static useModule(module: any) {
// Sanity check.
if (typeof module !== 'object') {
return;
}
else {
useModule(mod);
for (const key of Object.keys(module)) {
FormioCore.usePlugin(key, module[key]);
}
});
};
}

(Formio as any).useModule = useModule;
(Formio as any).usePlugin = usePlugin;
(Formio as any).use = use;
import components from './components';
(Formio as any).use(components);
import modules from '../modules';
(Formio as any).use(modules);
export { Formio };
/**
* Allows passing in plugins as multiple arguments or an array of plugins.
*
* Formio.plugins(plugin1, plugin2, etc);
* Formio.plugins([plugin1, plugin2, etc]);
*/
static use(...mods: any) {
mods.forEach((mod: any) => {
if (Array.isArray(mod)) {
mod.forEach(p => FormioCore.useModule(p));
}
else {
FormioCore.useModule(mod);
}
});
}
}

FormioCore.use(components);
FormioCore.use(modules);
9 changes: 6 additions & 3 deletions src/experimental/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from './template';
export * from './core';
import FormioCore from './core';
export * from './base';
export * from './model';
export * from './model';
export * from './components';
export * from './template';
export { FormioCore as Formio };
export default FormioCore;

0 comments on commit 20307d4

Please sign in to comment.