@react-awesome-query-builder/core
Version:
User-friendly query builder for React. Core
843 lines (835 loc) • 28.1 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "applyJsonLogic", {
enumerable: true,
get: function get() {
return _jsonLogicUtils.applyJsonLogic;
}
});
Object.defineProperty(exports, "cleanJSX", {
enumerable: true,
get: function get() {
return _stuff.cleanJSX;
}
});
exports.decompressConfig = exports.compressConfig = exports.compileConfig = void 0;
Object.defineProperty(exports, "isDirtyJSX", {
enumerable: true,
get: function get() {
return _stuff.isDirtyJSX;
}
});
Object.defineProperty(exports, "isJSX", {
enumerable: true,
get: function get() {
return _stuff.isJSX;
}
});
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _merge = _interopRequireDefault(require("lodash/merge"));
var _pick = _interopRequireDefault(require("lodash/pick"));
var _stuff = require("./stuff");
var _clone = _interopRequireDefault(require("clone"));
var _jsonLogicJs = _interopRequireDefault(require("json-logic-js"));
var _jsonLogicUtils = require("./jsonLogicUtils");
var BasicFuncs = _interopRequireWildcard(require("../config/funcs"));
var _configUtils = require("./configUtils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// Add new operations for JsonLogic
(0, _jsonLogicUtils.addRequiredJsonLogicOperations)();
function applyJsonLogicWithPath(logic, data, path) {
var ret;
try {
ret = _jsonLogicJs["default"].apply(logic, data);
} catch (e) {
e.message = "".concat(path.join("."), " :: ").concat(e.message);
throw e;
}
return ret;
}
function callContextFn(_this, fn, args, path) {
var ret;
try {
ret = fn.call.apply(fn, [_this].concat((0, _toConsumableArray2["default"])(args)));
} catch (e) {
e.message = "".concat(path.join("."), " :: ").concat(e.message);
throw e;
}
return ret;
}
// type:
// x - iterate (with nesting `subfields`)
// "r" - RenderedReactElement
// Will be compiled with renderReactElement() into React element rendered with `React.createElement` (`ctx.RCE`)
// "rf" - JsonLogicFunction/string to render React
// JL data is { props, ctx }
// Should return {type, props} or string, where type or string - React component
// Can use { JSX: ["SomeComponent", {var: "props"}] } or just return "SomeComponent"
// Returned component will be searched in ctx.components/ctx.W/ctx.O, see getReactComponentFromCtx()
// Will be compiled with compileJsonLogicReact() into function with args (props, ctx) that will return renderReactElement()
// "f" - JsonLogicFunction/string
// JL data is { args, ctx } plus named args defined in `args` inside `compileMeta`
// Can use { CALL: [ {var: "ctx.someFunc"}, null, {var: "args[0]" }] }
// If string is passed, it's a path to function in ctx (with dot notation)
// Will be compiled with compileJsonLogic() into function with any args and `this` should be `ctx`
var compileMetaFieldSettings = {
asyncFetch: {
type: "f",
args: ["searchOrValues", "offset"]
},
labelYes: {
type: "r"
},
labelNo: {
type: "r"
},
marks: {
type: "r",
isArr: true
},
validateValue: {
type: "f",
args: ["val", "fieldSettings", "op", "opDef", "rightFieldDef"]
}
};
var compileMetaWidget = _objectSpread(_objectSpread({}, compileMetaFieldSettings), {}, {
factory: {
type: "rf"
},
formatValue: {
type: "f",
args: ["val", "fieldDef", "wgtDef", "isForDisplay", "op", "opDef", "rightFieldDef"]
},
sqlFormatValue: {
type: "f",
args: ["val", "fieldDef", "wgtDef", "op", "opDef", "rightFieldDef", "sqlDialect"]
},
spelFormatValue: {
type: "f",
args: ["val", "fieldDef", "wgtDef", "op", "opDef", "rightFieldDef"]
},
spelImportValue: {
type: "f",
args: ["val", "wgtDef", "args"]
},
sqlImport: {
type: "f",
args: ["sqlObj", "wgtDef", "sqlDialect"]
},
mongoFormatValue: {
type: "f",
args: ["val", "fieldDef", "wgtDef", "op", "opDef"]
},
elasticSearchFormatValue: {
type: "f",
args: ["queryType", "val", "op", "field", "config"]
},
jsonLogic: {
type: "f",
args: ["val", "fieldDef", "wgtDef", "op", "opDef"]
},
jsonLogicImport: {
type: "f",
args: ["val", "wgtDef"]
},
validateValue: {
type: "f",
args: ["val", "fieldSettings", "op", "opDef", "rightFieldDef"]
},
// obsolete
toJS: {
type: "f",
args: ["val"]
}
});
var compileMetaOperator = {
options: {
// proximity
factory: {
type: "rf"
}
},
formatOp: {
type: "f",
args: ["field", "op", "vals", "valueSrc", "valueType", "opDef", "operatorOptions", "isForDisplay", "fieldDef"]
},
mongoFormatOp: {
type: "f",
args: ["field", "op", "vals", "useExpr", "valueSrc", "valueType", "opDef", "operatorOptions", "fieldDef"]
},
sqlFormatOp: {
type: "f",
args: ["field", "op", "vals", "valueSrc", "valueType", "opDef", "operatorOptions", "fieldDef"]
},
spelFormatOp: {
type: "f",
args: ["field", "op", "vals", "valueSrc", "valueType", "opDef", "operatorOptions", "fieldDef"]
},
jsonLogic: {
type: "f",
ignore: "string",
args: ["field", "op", "vals", "opDef", "operatorOptions", "fieldDef", "expectedType", "settings"]
},
sqlImport: {
type: "f",
args: ["sqlObj", "_", "sqlDialect"]
},
elasticSearchQueryType: {
type: "f",
ignore: "string",
args: ["valueType"]
},
textSeparators: {
type: "r",
isArr: true
}
};
var compileMetaConjunction = {
formatConj: {
type: "f",
args: ["children", "conj", "not", "isForDisplay"]
},
sqlFormatConj: {
type: "f",
args: ["children", "conj", "not"]
},
spelFormatConj: {
type: "f",
args: ["children", "conj", "not", "omitBrackets"]
}
};
var compileMetaWidgetForType = {
widgetProps: compileMetaWidget,
opProps: compileMetaOperator
};
var compileMetaFunc = {
renderBrackets: {
type: "r",
isArr: true
},
renderSeps: {
type: "r",
isArr: true
},
jsonLogic: {
type: "f",
ignore: "string",
args: ["formattedArgs"]
},
jsonLogicImport: {
type: "f",
args: ["val"]
},
spelImport: {
type: "f",
args: ["spel"]
},
sqlImport: {
type: "f",
args: ["sqlObj", "_", "sqlDialect"]
},
formatFunc: {
type: "f",
args: ["formattedArgs", "isForDisplay"]
},
sqlFormatFunc: {
type: "f",
args: ["formattedArgs", "sqlDialect"]
},
mongoFormatFunc: {
type: "f",
args: ["formattedArgs"]
},
spelFormatFunc: {
type: "f",
args: ["formattedArgs"]
}
};
var compileMetaFieldLike = _objectSpread(_objectSpread({}, compileMetaFieldSettings), {}, {
fieldSettings: compileMetaFieldSettings
});
var compileMetaField = _objectSpread(_objectSpread({}, compileMetaFieldSettings), {}, {
fieldSettings: compileMetaFieldSettings,
widgets: {
x: compileMetaWidgetForType
},
mainWidgetProps: compileMetaWidget
});
var compileMetaSettings = {
locale: {
mui: {
type: "f",
args: [],
invokeWith: [],
ignore: "jl"
},
material: {
type: "f",
args: [],
invokeWith: [],
ignore: "jl"
},
antd: {
type: "f",
args: [],
invokeWith: [],
ignore: "jl"
}
},
designSettings: {
generateCssVars: {
antd: {
type: "f",
args: ["theme", "config"]
},
mui: {
type: "f",
args: ["theme", "config"]
},
material: {
type: "f",
args: ["theme", "config"]
},
fluent: {
type: "f",
args: ["theme", "config"]
},
bootstrap: {
type: "f",
args: ["theme", "config"]
}
}
},
caseValueField: compileMetaField,
canCompareFieldWithField: {
type: "f",
args: ["leftField", "leftFieldConfig", "rightField", "rightFieldConfig", "op"]
},
formatReverse: {
type: "f",
args: ["q", "op", "reversedOp", "operatorDefinition", "revOperatorDefinition", "isForDisplay"]
},
sqlFormatReverse: {
type: "f",
args: ["q"]
},
spelFormatReverse: {
type: "f",
args: ["q"]
},
formatField: {
type: "f",
args: ["field", "parts", "label2", "fieldDefinition", "config", "isForDisplay"]
},
formatSpelField: {
type: "f",
args: ["field", "parentField", "parts", "partsExt", "fieldDefinition", "config"]
},
formatAggr: {
type: "f",
args: ["whereStr", "aggrField", "operator", "value", "valueSrc", "valueType", "opDef", "operatorOptions", "isForDisplay", "aggrFieldDef"]
},
normalizeListValues: {
type: "f",
args: ["listValues", "type", "fieldSettings"]
},
renderConfirm: {
type: "f",
args: ["props"]
},
useConfirm: {
type: "f",
args: []
},
renderField: {
type: "rf"
},
renderOperator: {
type: "rf"
},
renderFunc: {
type: "rf"
},
renderConjs: {
type: "rf"
},
renderButton: {
type: "rf"
},
renderIcon: {
type: "rf"
},
renderButtonGroup: {
type: "rf"
},
renderValueSources: {
type: "rf"
},
renderProvider: {
type: "rf"
},
renderSwitch: {
type: "rf"
},
renderSwitchPrefix: {
type: "r"
},
renderItem: {
type: "rf"
},
renderBeforeWidget: {
type: "rf"
},
renderAfterWidget: {
type: "rf"
},
renderBeforeActions: {
type: "rf"
},
renderAfterActions: {
type: "rf"
},
renderBeforeCaseValue: {
type: "rf"
},
renderAfterCaseValue: {
type: "rf"
},
renderRuleError: {
type: "rf"
}
};
var compileMeta = {
fields: {
x: compileMetaField
},
widgets: {
x: compileMetaWidget
},
conjunctions: {
x: compileMetaConjunction
},
operators: {
x: compileMetaOperator
},
types: {
x: {
widgets: {
x: compileMetaWidgetForType
}
}
},
funcs: {
x: _objectSpread(_objectSpread(_objectSpread({}, compileMetaFieldLike), compileMetaFunc), {}, {
args: {
x: _objectSpread({}, compileMetaFieldLike)
}
})
},
settings: compileMetaSettings
};
/////////////
var compressConfig = exports.compressConfig = function compressConfig(config, baseConfig) {
if (config.__fieldNames) {
throw new Error("Don't apply `compressConfig()` to extended config");
}
var zipConfig = (0, _pick["default"])(config, _configUtils.configKeys);
delete zipConfig.ctx;
var _clean2 = function _clean(target, base, path, meta) {
if ((0, _stuff.isObject)(target)) {
if ((0, _stuff.isDirtyJSX)(target)) {
target = (0, _stuff.cleanJSX)(target);
}
if (path[0] === "funcs" && !base) {
var funcKey = path[path.length - 1];
// todo: if there will be change in `BasicFuncs` when funcs can be nested, need to chnage code to find `base`
base = (0, _configUtils.getFieldRawConfig)({
funcs: meta.BasicFuncs
}, funcKey, "funcs", "subfields") || undefined;
if (base) {
target["$$key"] = funcKey;
}
}
if (base !== undefined && (0, _stuff.isObject)(base)) {
for (var k in base) {
if (Object.prototype.hasOwnProperty.call(base, k)) {
if (!Object.keys(target).includes(k) || target[k] === undefined && base[k] !== undefined) {
// deleted in target
target[k] = "$$deleted";
} else {
target[k] = _clean2(target[k], base[k], [].concat((0, _toConsumableArray2["default"])(path), [k]), meta);
if (target[k] === undefined) {
delete target[k];
}
}
}
}
}
for (var _k in target) {
if (Object.prototype.hasOwnProperty.call(target, _k)) {
if (!base || !Object.keys(base).includes(_k)) {
var _base;
// new in target
target[_k] = _clean2(target[_k], (_base = base) === null || _base === void 0 ? void 0 : _base[_k], [].concat((0, _toConsumableArray2["default"])(path), [_k]), meta);
}
if (target[_k] === undefined) {
delete target[_k];
}
}
}
if (Object.keys(target).length === 0) {
target = undefined;
}
} else if (Array.isArray(target)) {
// don't deep compare arrays, but allow clean JSX inside array
target.forEach(function (val, ind) {
target[ind] = _clean2(target[ind], undefined, [].concat((0, _toConsumableArray2["default"])(path), [ind]), meta);
});
}
if (base !== undefined && (0, _stuff.shallowEqual)(target, base, true)) {
return undefined;
}
if (typeof target === "function") {
throw new Error("compressConfig: function at ".concat(path.join("."), " should be converted to JsonLogic"));
}
return target;
};
var _iterator = _createForOfIteratorHelper(_configUtils.configKeys),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var rootKey = _step.value;
if (rootKey === "ctx") {
// ignore
} else if (rootKey === "fields") {
// just copy
zipConfig[rootKey] = (0, _clone["default"])(zipConfig[rootKey]);
_clean2(zipConfig[rootKey], {}, [rootKey]);
} else if (rootKey === "funcs") {
// leave only diff for every used func
zipConfig[rootKey] = (0, _clone["default"])(zipConfig[rootKey] || {});
for (var k in zipConfig[rootKey]) {
_clean2(zipConfig[rootKey][k], null, [rootKey, k], {
BasicFuncs: BasicFuncs
});
}
} else {
// leave only diff
zipConfig[rootKey] = (0, _clone["default"])(zipConfig[rootKey]);
_clean2(zipConfig[rootKey], baseConfig[rootKey], [rootKey]);
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return zipConfig;
};
var decompressConfig = exports.decompressConfig = function decompressConfig(zipConfig, baseConfig, ctx) {
if (!zipConfig.settings.useConfigCompress) {
throw new Error("Please enable `useConfigCompress` in config settings to use decompressConfig()");
}
var unzipConfig = {};
var _mergeDeep2 = function _mergeDeep(target, mixin, path) {
if ((0, _stuff.isObject)(mixin)) {
if (!(0, _stuff.isObject)(target)) {
target = {};
}
for (var k in mixin) {
if ((0, _stuff.hasSafeOwnProperty)(mixin, k)) {
if (mixin[k] === "$$deleted") {
delete target[k];
} else {
target[k] = _mergeDeep2(target[k], mixin[k], [].concat((0, _toConsumableArray2["default"])(path), [k]));
}
}
}
} else if (Array.isArray(mixin)) {
// don't merge arrays, just replace
target = (0, _clone["default"])(mixin);
} else {
target = mixin;
}
return target;
};
var _resolveAndMergeDeep2 = function _resolveAndMergeDeep(target, path, meta) {
// try to resolve by $$key and merge
var resolved = false;
if ((0, _stuff.isObject)(target) && Object.prototype.hasOwnProperty.call(target, "$$key") && target["$$key"]) {
var func = (0, _configUtils.getFieldRawConfig)({
funcs: meta.BasicFuncs
}, target["$$key"], "funcs", "subfields");
if (func) {
// deep merge func <- zip
delete target["$$key"];
target = _mergeDeep2((0, _clone["default"])(func), target, path);
resolved = true;
} else {
throw new Error("decompressConfig: basic function not found by key ".concat(target["$$key"], " at ").concat(path.join(".")));
}
}
if (!resolved) {
if ((0, _stuff.isObject)(target)) {
// loop through object to find refs ($$key)
for (var k in target) {
if (Object.prototype.hasOwnProperty.call(target, k)) {
target[k] = _resolveAndMergeDeep2(target[k], [].concat((0, _toConsumableArray2["default"])(path), [k]), meta);
}
}
} else if (Array.isArray(target)) {
// also loop through array to find refs ($$key)
var _iterator2 = _createForOfIteratorHelper(target),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _k2 = _step2.value;
target[_k2] = _resolveAndMergeDeep2(target[_k2], [].concat((0, _toConsumableArray2["default"])(path), [_k2]), meta);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
}
return target;
};
var _iterator3 = _createForOfIteratorHelper(_configUtils.configKeys),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var rootKey = _step3.value;
if (rootKey === "ctx") {
// simple deep merge
unzipConfig[rootKey] = (0, _merge["default"])({}, baseConfig.ctx || {}, ctx || {});
} else if (rootKey === "funcs") {
// use $$key to pick funcs from BasicFuncs
unzipConfig[rootKey] = (0, _clone["default"])(zipConfig[rootKey] || {});
_resolveAndMergeDeep2(unzipConfig[rootKey], [rootKey], {
BasicFuncs: BasicFuncs
});
} else if (rootKey === "fields") {
// just copy
unzipConfig[rootKey] = (0, _clone["default"])(zipConfig[rootKey] || {});
} else {
// deep merge base <- zip
unzipConfig[rootKey] = (0, _clone["default"])(baseConfig[rootKey] || {});
_mergeDeep2(unzipConfig[rootKey], zipConfig[rootKey] || {}, [rootKey]);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return unzipConfig;
};
/////////////
var compileConfig = exports.compileConfig = function compileConfig(config) {
if (config.__compliled) {
return config;
}
config = (0, _clone["default"])(config);
var opts = {
ctx: config.ctx
};
var logs = [];
_compileConfigParts(config, config, opts, compileMeta, logs);
//console.log(logs.join("\n"));
Object.defineProperty(config, "__compliled", {
enumerable: false,
writable: false,
value: true
});
return config;
};
function _compileConfigParts(config, subconfig, opts, meta, logs) {
var path = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
if (!subconfig) return;
var isRoot = !path.length;
for (var k in meta) {
var submeta = meta[k];
var newPath = k === "x" ? path : [].concat((0, _toConsumableArray2["default"])(path), [k]);
// if (isRoot) {
// //logs.push(`Cloned ${newPath.join(".")}`);
// config[k] = clone(config[k]);
// }
if (submeta.type === "r") {
var targetObj = subconfig;
var val = targetObj[k];
if (submeta.isArr) {
for (var ind in val) {
var newVal = renderReactElement(val[ind], opts, [].concat((0, _toConsumableArray2["default"])(newPath), [ind]));
if (newVal !== val[ind]) {
logs.push("Compiled ".concat(newPath.join("."), "[").concat(ind, "]"));
val[ind] = newVal;
}
}
} else {
var _newVal = renderReactElement(val, opts, newPath, undefined);
if (_newVal !== val) {
logs.push("Compiled R ".concat(newPath.join(".")));
targetObj[k] = _newVal;
}
}
} else if (submeta.type === "rf") {
var _targetObj = subconfig;
var _val = _targetObj[k];
var _newVal2 = compileJsonLogicReact(_val, opts, newPath, submeta.ignore);
if (_newVal2 !== _val) {
logs.push("Compiled JL-RF ".concat(newPath.join(".")));
_targetObj[k] = _newVal2;
}
} else if (submeta.type === "f") {
var _targetObj2 = subconfig;
var _val2 = _targetObj2[k];
var _newVal3 = compileJsonLogic(_val2, opts, newPath, submeta.args, submeta.ignore);
if (submeta.invokeWith && _newVal3 && typeof _newVal3 === "function") {
var _newVal4;
_newVal3 = (_newVal4 = _newVal3).call.apply(_newVal4, [null].concat((0, _toConsumableArray2["default"])(submeta.invokeWith)));
}
if (_newVal3 !== _val2) {
logs.push("Compiled JL-F ".concat(newPath.join(".")));
_targetObj2[k] = _newVal3;
}
} else if (k === "x") {
for (var field in subconfig) {
newPath = [].concat((0, _toConsumableArray2["default"])(path), [field]);
var def = subconfig[field];
_compileConfigParts(config, def, opts, submeta, logs, newPath);
if (def.subfields) {
// tip: need to pass `meta`, not `submeta`
_compileConfigParts(config, def.subfields, opts, meta, logs, newPath);
}
}
} else {
var _def = subconfig[k];
_compileConfigParts(config, _def, opts, submeta, logs, newPath);
}
}
}
function compileJsonLogicReact(jl, opts, path) {
var ignore = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
if ((0, _stuff.isJsonLogic)(jl)) {
return function (props, ctx) {
ctx = ctx || (opts === null || opts === void 0 ? void 0 : opts.ctx); // can use context compile-time if not passed at runtime
var data = {
props: props,
ctx: ctx
};
var re = applyJsonLogicWithPath(jl, data, path);
if (typeof re === "string") {
re = {
type: re,
props: props
};
}
var ret = renderReactElement(re, {
ctx: ctx
}, path);
return ret;
};
} else if (typeof jl === "string") {
return function (props, ctx) {
ctx = ctx || (opts === null || opts === void 0 ? void 0 : opts.ctx); // can use context compile-time if not passed at runtime
var fn = jl.split(".").reduce(function (o, k) {
return o === null || o === void 0 ? void 0 : o[k];
}, ctx);
if (fn) {
return callContextFn(this, fn, [props, ctx], path);
} else {
var re = {
type: jl,
props: props
};
var ret = renderReactElement(re, {
ctx: ctx
}, path);
return ret;
}
};
}
return jl;
}
function compileJsonLogic(jl, opts, path, argNames) {
var ignore = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined;
if ((0, _stuff.isJsonLogic)(jl) && ignore !== "jl") {
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var ctx = this || (opts === null || opts === void 0 ? void 0 : opts.ctx); // can use context compile-time if not passed at runtime
var data = (argNames || []).reduce(function (acc, k, i) {
return _objectSpread(_objectSpread({}, acc), {}, (0, _defineProperty2["default"])({}, k, args[i]));
}, {
args: args,
ctx: ctx
});
var ret = applyJsonLogicWithPath(jl, data, path);
return ret;
}.bind(opts === null || opts === void 0 ? void 0 : opts.ctx);
} else if (typeof jl === "string" && ignore !== "string") {
return function () {
var ctx = this || (opts === null || opts === void 0 ? void 0 : opts.ctx); // can use context compile-time if not passed at runtime
var fn = jl.split(".").reduce(function (o, k) {
return o === null || o === void 0 ? void 0 : o[k];
}, ctx);
if (fn) {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return callContextFn(this, fn, args, path);
} else {
throw new Error("".concat(path.join("."), " :: Function ").concat(jl, " is not found in ctx"));
}
}.bind(opts === null || opts === void 0 ? void 0 : opts.ctx);
}
return jl;
}
function getReactComponentFromCtx(name, ctx) {
var _ctx$components;
return (ctx === null || ctx === void 0 || (_ctx$components = ctx.components) === null || _ctx$components === void 0 ? void 0 : _ctx$components[name]) || ctx.W[name] || ctx.O[name];
}
function renderReactElement(jsx, opts, path) {
var key = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
if ((0, _stuff.isJSX)(jsx)) {
var _props;
var type = jsx.type,
props = jsx.props;
if (typeof type !== "string") {
throw new Error("renderReactElement for ".concat(path.join("."), ": type should be string"));
}
var Cmp = getReactComponentFromCtx(type, opts.ctx) || type.toLowerCase();
var children;
if (key !== undefined) {
props = _objectSpread(_objectSpread({}, props), {}, {
key: key
});
}
if ((_props = props) !== null && _props !== void 0 && _props.children) {
children = renderReactElement(props.children, opts, path);
props = _objectSpread(_objectSpread({}, props), {}, {
children: children
});
}
var res = opts.ctx.RCE(Cmp, props);
return res;
} else if (jsx instanceof Array) {
return jsx.map(function (el, i) {
return renderReactElement(el, opts, path, "" + i);
});
}
return jsx;
}