-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathview.component.js
More file actions
173 lines (147 loc) · 6.62 KB
/
view.component.js
File metadata and controls
173 lines (147 loc) · 6.62 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { ReferenceComponent } from "./reference.component.js";
import { ContainerBaseComponent } from "./container-base.component.js";
const TAG = "[ViewComponent]";
export class ViewComponent extends ContainerBaseComponent {
READ_ONLY_DETAILS_TEMPLATES = [
"Details",
"DetailsOneColumn",
"DetailsTwoColumn",
"DetailsThreeColumn",
"NarrowWideDetails",
"WideNarrowDetails",
];
DETAILS_TEMPLATES = [...this.READ_ONLY_DETAILS_TEMPLATES, "DetailsFields", "DetailsSubTabs"];
SUPPORTED_FORM_TEMPLATES = ["DefaultForm", "OneColumn"];
UNSUPPORTED_FORM_TEMPLATES = ["TwoColumn", "ThreeColumn", "WideNarrow"];
SUPPORTED_TEMPLATES = [...this.SUPPORTED_FORM_TEMPLATES, "SimpleTable", "DataReference"];
NO_HEADER_TEMPLATES = ['SimpleTable'];
jsComponentPConnectData = {};
props = {
children: [],
visible: true,
label: "",
showLabel: false,
};
init() {
this.jsComponentPConnectData =
this.jsComponentPConnect.registerAndSubscribeComponent(this, this.#checkAndUpdate);
this.componentsManager.onComponentAdded(this);
this.#checkAndUpdate();
}
destroy() {
super.destroy();
this.jsComponentPConnectData.unsubscribeFn?.();
this.destroyChildren();
this.props.children = [];
this.componentsManager.onComponentPropsUpdate(this);
this.componentsManager.onComponentRemoved(this);
}
update(pConn) {
if (this.pConn !== pConn) {
this.pConn = pConn;
this.#checkAndUpdate();
}
}
onEvent(event) {
this.childrenComponents.forEach((component) => {
component.onEvent(event);
});
}
#checkAndUpdate() {
if (this.jsComponentPConnect.shouldComponentUpdate(this)) {
this.#updateSelf();
}
}
#updateSelf() {
if (this.jsComponentPConnect.getComponentID(this) === undefined) {
return;
}
// normalize this.pConn in case it contains a 'reference'
this.pConn = ReferenceComponent.normalizePConn(this.pConn);
const configProps = this.pConn.resolveConfigProps(this.pConn.getConfigProps());
const inheritedProps = this.pConn.getInheritedProps();
const template = this.#resolveTemplateType(configProps);
const label = configProps.label ?? "";
const showLabel = configProps.showLabel || this.DETAILS_TEMPLATES.includes(template);
const isTemplateWithHeader = !this.NO_HEADER_TEMPLATES.includes(template);
this.props.label = inheritedProps.label ?? label;
this.props.showLabel = (inheritedProps.showLabel || showLabel) && isTemplateWithHeader;
this.props.visible = configProps.visibility ?? this.props.visible;
if (this.READ_ONLY_DETAILS_TEMPLATES.includes(template)) {
this.pConn.setInheritedProp("displayMode", "DISPLAY_ONLY");
this.pConn.setInheritedProp("readOnly", true);
}
/**
* In instances where there is context, like with "shippingAddress," the pageReference becomes "caseInfo.content.shippingAddress."
* This leads to problems in the getProperty API, as it incorrectly assesses the visibility condition by looking in the wrong location
* in the Store for the property values. Reference component should be able to handle such scenarios(as done in SDK-R) since it has the
* expected pageReference values, the View component currently cannot handle this.
* The resolution lies in transferring this responsibility to the Reference component, eliminating the need for this code when Reference
* component is able to handle it.
*/
if (!configProps.visibility && this.pConn.getPageReference().length > "caseInfo.content".length) {
this.props.visible = this.#evaluateVisibility(this.pConn, configProps.referenceContext);
}
if (this.SUPPORTED_TEMPLATES.includes(template)) {
this.#includeTemplate(template);
} else {
if (template) console.warn(TAG, `${template} not supported. Rendering children components directly.`);
this.reconcileChildren();
}
this.props.children = this.getChildrenComponentsIds();
this.componentsManager.onComponentPropsUpdate(this);
}
#includeTemplate(template) {
if (this.childrenComponents[0] && this.childrenComponents[0].type !== template) {
this.destroyChildren();
}
this.childrenComponents[0] = this.componentsManager.upsert(this.childrenComponents[0], template, [this.pConn]);
}
#evaluateVisibility(pConn, referenceContext) {
const visibilityExpression = pConn.meta.config.visibility;
if (!visibilityExpression || visibilityExpression.length === 0) return true;
let dataPage = this.#getDataPage(pConn.getContextName(), referenceContext);
if (!dataPage) return false;
const visibilityConditions = visibilityExpression.replace("@E ", "");
return PCore.getExpressionEngine().evaluate(visibilityConditions, dataPage, {
pConnect: {
getPConnect: () => {
return this.pConn;
},
},
});
}
#getDataPage(context, referenceContext) {
let pageReferenceKeys = referenceContext.replace("caseInfo.content.", "").split(".");
let page = PCore.getStore().getState()?.data[context].caseInfo.content;
for (const key of pageReferenceKeys) {
const arrayStartingBracketIndex = key.indexOf("[");
const arrayEndingBracketIndex = key.indexOf("]");
if (arrayStartingBracketIndex !== -1 && arrayEndingBracketIndex !== -1) {
const keyName = key.substring(0, arrayStartingBracketIndex);
const keyIndex = parseInt(key.substring(arrayStartingBracketIndex + 1, arrayEndingBracketIndex));
if (page[keyName][keyIndex] !== undefined) {
page = page[keyName][keyIndex];
} else {
return null;
}
} else {
if (key in page) {
page = page[key];
} else {
return null;
}
}
}
return page;
}
#resolveTemplateType(configProps) {
const template = configProps.template ?? "";
if (this.UNSUPPORTED_FORM_TEMPLATES.includes(template)) {
console.warn(TAG, `${template} not supported. Falling back to DefaultForm.`);
return "DefaultForm";
} else {
return template;
}
}
}