-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroot-container.component.js
More file actions
137 lines (123 loc) · 4.72 KB
/
root-container.component.js
File metadata and controls
137 lines (123 loc) · 4.72 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
import { Utils } from "../../helpers/utils.js";
import { BaseComponent } from "../base.component.js";
const options = {context: "app"};
const TAG = "[RootContainerComponent]";
export class RootContainerComponent extends BaseComponent {
#viewContainerComponent;
#modalViewContainerComponent;
jsComponentPConnectData = {};
props = {
viewContainer: "",
httpMessages: {},
};
init() {
const {containers} = PCore.getStore().getState();
const items = Object.keys(containers).filter((item) => item.includes("root"));
PCore.getContainerUtils().getContainerAPI().addContainerItems(items);
this.#configureModalContainer();
Utils.setHasViewContainer("false");
this.jsComponentPConnectData = this.jsComponentPConnect.registerAndSubscribeComponent(
this,
this.#checkAndUpdate
);
this.componentsManager.onComponentAdded(this);
this.#checkAndUpdate();
}
destroy() {
super.destroy();
this.jsComponentPConnectData.unsubscribeFn?.();
this.#viewContainerComponent?.destroy?.();
this.#sendPropsUpdate();
this.componentsManager.onComponentRemoved(this);
}
#sendPropsUpdate() {
const httpMessages = this.jsComponentPConnectData.httpMessages || [];
this.props = {
viewContainer: this.#viewContainerComponent.compId,
modalViewContainer: this.#modalViewContainerComponent.compId,
httpMessages: httpMessages,
};
this.componentsManager.onComponentPropsUpdate(this);
// even if the http issue no longer exists the error is persisted in core js so we need to clear it
if (this.jsComponentPConnectData.httpMessages && this.jsComponentPConnectData.httpMessages.length > 0) {
this.#clearHttpMessages();
}
}
#clearHttpMessages() {
const context = PCore.getContainerUtils().getActiveContainerItemName(
`${PCore.getConstants().APP.APP}/${this.pConn.getContainerName()}`
);
PCore.getMessageManager().clearMessages({
category: "HTTP",
type: "error",
context: context,
});
}
#checkAndUpdate() {
if (this.jsComponentPConnect.shouldComponentUpdate(this)) {
this.#updateSelf();
}
}
#updateSelf() {
const myProps = this.jsComponentPConnect.getCurrentCompleteProps(this);
const {renderingMode} = myProps;
if (renderingMode === "noPortal") {
this.#generateViewContainerForNoPortal();
} else {
console.error(TAG, "'noPortal' rendering mode supported only.");
}
if (this.compId !== "1") {
console.error(TAG, "RootComponent id must be '1' to match root container on consumer side");
return;
}
this.#sendPropsUpdate();
}
#generateViewContainerForNoPortal() {
const arChildren = this.pConn.getChildren();
if (
!arChildren ||
arChildren.length !== 1 ||
arChildren[0].getPConnect().getComponentName() !== "ViewContainer"
) {
console.error(TAG, "Only ViewContainer in RootContainer supported for 'noPortal' mode.");
return;
}
const configProps = this.pConn.getConfigProps();
const viewContConfig = {
meta: {
type: "ViewContainer",
config: configProps,
},
options,
};
const viewContainerPConn = PCore.createPConnect(viewContConfig).getPConnect();
if (this.#viewContainerComponent) {
this.#viewContainerComponent.update(viewContainerPConn)
} else {
this.#viewContainerComponent = this.componentsManager.create(viewContainerPConn.meta.type, [viewContainerPConn])
this.#viewContainerComponent.init();
}
}
#configureModalContainer() {
const configObjModal = PCore.createPConnect({
meta: {
type: 'ModalViewContainer',
config: {
name: 'modal'
}
},
options
});
const modalViewContainerPConn = configObjModal.getPConnect();
if (this.#modalViewContainerComponent) {
this.#modalViewContainerComponent.update(modalViewContainerPConn)
} else {
this.#modalViewContainerComponent = this.componentsManager.create(modalViewContainerPConn.meta.type, [modalViewContainerPConn])
this.#modalViewContainerComponent.init();
}
if (this.compId !== "1") {
console.error(TAG, "RootComponent id must be '1' to match root container on consumer side");
return;
}
}
}