forked from holubv/s-forms-smart-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmartComponents.js
90 lines (85 loc) · 2.56 KB
/
SmartComponents.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
import CompositeQuestion from "./components/CompositeQuestion";
import Constants from "./Constants";
import { Constants as SConstants } from "@kbss-cvut/s-forms";
import WizardStepComponent from "./components/WizardStepComponent";
import QuestionWithUnit from "./components/QuestionWithUnit";
import NullQuestion from "./components/NullQuestion";
import Utils from "./Utils";
import SectionComponent from "./components/SectionComponent";
import "./styles/components.css";
export default class SmartComponents {
static componentCache = {};
static _cached(q, form, key, mapRule) {
let cachedQuestion = SmartComponents.componentCache[q["@id"]];
if (cachedQuestion === undefined) {
cachedQuestion = {};
SmartComponents.componentCache[q["@id"]] = {};
}
let cachedValue = cachedQuestion[key];
if (cachedValue === undefined) {
cachedValue = mapRule(q, form);
SmartComponents.componentCache[q["@id"]][key] = cachedValue;
}
return cachedValue;
}
static getComponentMapping() {
return [
{
component: WizardStepComponent,
mapRule: WizardStepComponent.mappingRule,
},
{
component: SectionComponent,
mapRule: SectionComponent.mappingRule,
},
{
component: CompositeQuestion,
mapRule: CompositeQuestion.mappingRule,
},
{
component: NullQuestion,
mapRule: (q, form) =>
SmartComponents._cached(
q,
form,
"NullQuestion-unit-of-measure",
() => {
const parent = Utils.findParent(form?.root, q["@id"]);
return !!(
parent &&
Utils.isReferencedByProperty(
parent[SConstants.HAS_SUBQUESTION],
q["@id"],
Constants.HAS_UNIT_OF_MEASURE
)
);
}
),
},
{
component: NullQuestion,
mapRule: (q, form) =>
SmartComponents._cached(q, form, "NullQuestion-type-question", () => {
const parent = Utils.findParent(form?.root, q["@id"]);
return Utils.hasPropertyWithValue(
parent,
Constants.HAS_TYPE_QUESTION,
q["@id"]
);
}),
},
{
component: NullQuestion,
mapRule: (q) => {
return !!q[Constants.SHOW_ADVANCED_QUESTION];
},
},
{
component: QuestionWithUnit,
mapRule: (q) => {
return !!q[Constants.HAS_UNIT_OF_MEASURE];
},
},
];
}
}