forked from M-Labs/web2019
63 lines
2.3 KiB
JavaScript
63 lines
2.3 KiB
JavaScript
import React from "react";
|
|
import {apply as json_logic_apply} from "json-logic-js";
|
|
import {componentsList} from "./components/components";
|
|
import {true_type_of} from "./utils";
|
|
|
|
export function ProcessOptions({options, data, target, id}) {
|
|
let options_t = true_type_of(options);
|
|
|
|
if (options_t === "array") {
|
|
return Array.from(
|
|
options.map((option_item, i) => ProcessOptions({
|
|
options: option_item,
|
|
data: data,
|
|
target: target,
|
|
id: id + i
|
|
}))
|
|
);
|
|
} else if (options_t === "object") {
|
|
if (
|
|
true_type_of(options.type) === "string" &&
|
|
(true_type_of(options.args) === "object" || true_type_of(options.items) === "array")
|
|
) {
|
|
if (options.type in componentsList) {
|
|
return componentsList[options.type](target, id + options.type, data, options.args);
|
|
} else if (options.type === "Group") {
|
|
return (
|
|
<div className="border rounded" key={id + "group"}>
|
|
{ProcessOptions({
|
|
options: json_logic_apply(options.items, data),
|
|
data: data,
|
|
target: target,
|
|
id: id
|
|
})}
|
|
</div>);
|
|
} else {
|
|
return componentsList["Default"](options.type, id + "missing");
|
|
}
|
|
} else {
|
|
return ProcessOptions({options: json_logic_apply(options, data), data: data, target: target, id: id});
|
|
}
|
|
}
|
|
}
|
|
|
|
export function ProcessOptionsToData({options, data}) {
|
|
let options_t = true_type_of(options);
|
|
if (options_t === "array") {
|
|
return Array.from(
|
|
options.map((option_item, _i) => ProcessOptionsToData({
|
|
options: option_item,
|
|
data: data,
|
|
}))
|
|
).filter((item, _i) => !!item).flat();
|
|
} else if (options_t === "object") {
|
|
if (true_type_of(options.title) === "string") {
|
|
return options;
|
|
} else {
|
|
return ProcessOptionsToData({options: json_logic_apply(options, data), data: data});
|
|
}
|
|
} else {
|
|
//throw Error("Incompatible type for the option: " + options_t)
|
|
return null;
|
|
}
|
|
} |