@ea-lab/reactive-json
Version:
A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.
183 lines (182 loc) • 5.67 kB
JavaScript
import { jsx as v } from "react/jsx-runtime";
import { J as A } from "../jsonpath-B9kE9k9e.js";
import { l as C } from "../lodash-CYNxjS-I.js";
import { useContext as O } from "react";
import { GlobalDataContext as g } from "./GlobalDataContext.js";
import { TemplateContext as x } from "./TemplateContext.js";
import { isTemplateValue as E, evaluateTemplateValue as b } from "./TemplateSystem.js";
import { reactEventProps as D } from "./utility/reactEventProps.js";
const P = (e) => e && e.charAt(0).toUpperCase() + e.slice(1), p = (e, c, s) => {
const { globalDataContext: h, templateContext: l } = c;
if (Array.isArray(e.andConditions))
return e.andConditions.reduce((t, o) => t && p(o, c, s), !0);
if (Array.isArray(e.orConditions)) {
const t = e.orConditions;
for (const o of t)
if (p(o, c, s))
return !0;
return !1;
}
if (e.when !== void 0 && !E(e.when))
return !1;
const i = (t) => b({
globalDataContext: h,
templateContext: l,
valueToEvaluate: t
});
let n;
if (e.when === void 0 && e.hasOwnProperty("whenDataCountOf")) {
let t;
switch (e.inContext) {
case "root":
t = h.getRootContext().templateData;
break;
case "template":
t = l.templateData;
break;
default:
t = h.templateData;
break;
}
n = A.query(t, e.whenDataCountOf).length;
} else
n = (() => {
if (s) {
for (const [t, o] of s)
if (e.hasOwnProperty(t))
return o({
condition: e,
templateContexts: c,
evaluateAgainstTemplates: (a) => i(a)
});
}
return i(e.when);
})();
if (e.hasOwnProperty("isEmpty") || e.hasOwnProperty("isNotEmpty")) {
const t = (o) => e.hasOwnProperty("isEmpty") ? e.isEmpty === "not" ? !o : o : !o;
if (n === null)
return t(!0);
switch (typeof n) {
case "undefined":
return t(!0);
case "string":
return t(n.length === 0);
case "object":
return t(Object.keys(n).length === 0);
default:
return t(!n);
}
}
const r = e.compareAsDates;
if (e.hasOwnProperty("isNot"))
return u(r, n) !== u(r, i(e.isNot));
if (e.hasOwnProperty("is"))
return u(r, n) === u(r, i(e.is));
if (e.hasOwnProperty("containsNot") || e.hasOwnProperty("contains")) {
const t = e.hasOwnProperty("containsNot"), o = t ? e.containsNot : e.contains, a = u(r, i(o));
if (typeof n == "string") {
if (typeof a != "string")
return t;
const f = n.toLowerCase().indexOf(a.toLowerCase()) !== -1;
return t ? !f : f;
} else
for (const f of Object.values(n))
if (C.isEqual(u(r, f), a))
return !t;
return t;
}
if (e.hasOwnProperty("containedByNot") || e.hasOwnProperty("containedBy")) {
const t = e.hasOwnProperty("containedByNot"), o = t ? e.containedByNot : e.containedBy, a = u(r, i(o));
if (typeof n == "string") {
if (typeof a != "string")
return t;
const f = a.toLowerCase().indexOf(n.toLowerCase()) !== -1;
return t ? !f : f;
} else
for (const f of Object.values(a))
if (C.isEqual(u(r, f), n))
return !t;
return t;
}
return e.hasOwnProperty(">") ? u(r, n) > u(r, i(e[">"])) : e.hasOwnProperty(">=") ? u(r, n) >= u(r, i(e[">="])) : e.hasOwnProperty("<") ? u(r, n) < u(r, i(e["<"])) : e.hasOwnProperty("<=") ? u(r, n) <= u(r, i(e["<="])) : !0;
}, L = (e, c) => {
const s = [];
let h = !1;
const l = {};
if (!Array.isArray(e))
return s;
const { globalDataContext: i } = c, n = i.plugins ?? {}, r = (n == null ? void 0 : n.action) ?? {}, t = (n == null ? void 0 : n.reaction) ?? {};
if (!r)
return s;
for (const [o, a] of e.entries()) {
const f = (a == null ? void 0 : a.what) ?? void 0;
if (!f)
continue;
let m = r[f] ?? void 0, w = t[f] ?? void 0;
if (!m && !w && (m = r[P(f)]), !m) {
if (!w || a.on === void 0 || !p(a, c))
continue;
if (a.on === "message") {
if (!r.MessageListener)
continue;
s.push({
ActionComponent: r.MessageListener,
actionProps: a,
actionIndex: o
});
continue;
}
if (a.on === "hashchange") {
if (!r.HashChangeListener)
continue;
s.push({
ActionComponent: r.HashChangeListener,
actionProps: a,
actionIndex: o
});
continue;
}
if (D.has(a.on)) {
h = !0;
const y = "on" + P(a.on);
Array.isArray(l[y]) || (l[y] = []), l[y].push(a);
continue;
}
if (!r.CustomEventListener)
continue;
s.push({
ActionComponent: r.CustomEventListener,
actionProps: a,
actionIndex: o
});
continue;
}
p(a, c) && s.push({ ActionComponent: m, actionProps: a, actionIndex: o });
}
if (h) {
if (!r.ReactOnEvent)
return s;
s.push({
ActionComponent: r.ReactOnEvent,
actionProps: l,
actionIndex: "_reactOnEvent"
});
}
return s;
}, u = (e, c) => e ? new Date(c) : c, F = (e) => {
const c = O(g), s = O(x);
return e ? L((e == null ? void 0 : e.actions) ?? [], { globalDataContext: c, templateContext: s }).reverse().reduce((l, { ActionComponent: i, actionProps: n, actionIndex: r }) => /* @__PURE__ */ v(
i,
{
componentProps: e,
actionProps: n,
actionIndex: r,
attributesHolderRef: e.attributesHolderRef,
children: l
}
), (e == null ? void 0 : e.children) ?? null) : null;
};
export {
F as ActionDependant,
p as isValid
};