@telia-ace/widget-core 1.0.2 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{lib-esm → dist}/action-resolver.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/actions-controller.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/changeset-provider.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-configuration-api.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-controller.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-node-collection.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-node.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-platform.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/component-query.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/create-component-model.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/custom-component-handler.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/descriptor.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/extend-component.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/index.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/queries.d.ts +0 -0
- package/{lib-esm → dist}/component-platform/state-processor.d.ts +0 -0
- package/{lib-esm → dist}/component-resolver.d.ts +0 -0
- package/{lib-esm → dist}/event-subscriber.d.ts +0 -0
- package/{lib-esm → dist}/index.d.ts +0 -0
- package/dist/index.js +1047 -0
- package/dist/index.js.map +1 -0
- package/{lib-esm → dist}/types/configuration.d.ts +0 -0
- package/{lib-esm → dist}/types/index.d.ts +0 -0
- package/{lib-esm → dist}/types/ui.d.ts +0 -0
- package/package.json +10 -10
- package/lib/action-resolver.d.ts +0 -8
- package/lib/action-resolver.js +0 -19
- package/lib/component-platform/actions-controller.d.ts +0 -40
- package/lib/component-platform/actions-controller.js +0 -119
- package/lib/component-platform/changeset-provider.d.ts +0 -8
- package/lib/component-platform/changeset-provider.js +0 -2
- package/lib/component-platform/component-configuration-api.d.ts +0 -2
- package/lib/component-platform/component-configuration-api.js +0 -32
- package/lib/component-platform/component-controller.d.ts +0 -38
- package/lib/component-platform/component-controller.js +0 -119
- package/lib/component-platform/component-node-collection.d.ts +0 -18
- package/lib/component-platform/component-node-collection.js +0 -51
- package/lib/component-platform/component-node.d.ts +0 -63
- package/lib/component-platform/component-node.js +0 -190
- package/lib/component-platform/component-platform.d.ts +0 -54
- package/lib/component-platform/component-platform.js +0 -220
- package/lib/component-platform/component-query.d.ts +0 -16
- package/lib/component-platform/component-query.js +0 -35
- package/lib/component-platform/create-component-model.d.ts +0 -36
- package/lib/component-platform/create-component-model.js +0 -141
- package/lib/component-platform/custom-component-handler.d.ts +0 -21
- package/lib/component-platform/custom-component-handler.js +0 -16
- package/lib/component-platform/descriptor.d.ts +0 -8
- package/lib/component-platform/descriptor.js +0 -12
- package/lib/component-platform/extend-component.d.ts +0 -8
- package/lib/component-platform/extend-component.js +0 -57
- package/lib/component-platform/index.d.ts +0 -18
- package/lib/component-platform/index.js +0 -30
- package/lib/component-platform/queries.d.ts +0 -6
- package/lib/component-platform/queries.js +0 -53
- package/lib/component-platform/state-processor.d.ts +0 -12
- package/lib/component-platform/state-processor.js +0 -209
- package/lib/component-resolver.d.ts +0 -14
- package/lib/component-resolver.js +0 -49
- package/lib/event-subscriber.d.ts +0 -7
- package/lib/event-subscriber.js +0 -22
- package/lib/index.d.ts +0 -8
- package/lib/index.js +0 -41
- package/lib/types/configuration.d.ts +0 -86
- package/lib/types/configuration.js +0 -16
- package/lib/types/index.d.ts +0 -2
- package/lib/types/index.js +0 -18
- package/lib/types/ui.d.ts +0 -54
- package/lib/types/ui.js +0 -2
- package/lib-esm/action-resolver.js +0 -17
- package/lib-esm/component-platform/actions-controller.js +0 -116
- package/lib-esm/component-platform/changeset-provider.js +0 -1
- package/lib-esm/component-platform/component-configuration-api.js +0 -30
- package/lib-esm/component-platform/component-controller.js +0 -116
- package/lib-esm/component-platform/component-node-collection.js +0 -48
- package/lib-esm/component-platform/component-node.js +0 -187
- package/lib-esm/component-platform/component-platform.js +0 -217
- package/lib-esm/component-platform/component-query.js +0 -32
- package/lib-esm/component-platform/create-component-model.js +0 -135
- package/lib-esm/component-platform/custom-component-handler.js +0 -13
- package/lib-esm/component-platform/descriptor.js +0 -8
- package/lib-esm/component-platform/extend-component.js +0 -53
- package/lib-esm/component-platform/index.js +0 -10
- package/lib-esm/component-platform/queries.js +0 -46
- package/lib-esm/component-platform/state-processor.js +0 -205
- package/lib-esm/component-resolver.js +0 -47
- package/lib-esm/event-subscriber.js +0 -18
- package/lib-esm/index.js +0 -5
- package/lib-esm/types/configuration.js +0 -13
- package/lib-esm/types/index.js +0 -2
- package/lib-esm/types/ui.js +0 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1047 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { createConfigurationApi } from "@webprovisions/platform";
|
|
21
|
+
import { uuid, Logger } from "@telia-ace/widget-utilities";
|
|
22
|
+
class ActionResolver {
|
|
23
|
+
constructor(container) {
|
|
24
|
+
const { events } = container.get("$widget");
|
|
25
|
+
this.events = events.createChild(this);
|
|
26
|
+
}
|
|
27
|
+
action(target, action, value) {
|
|
28
|
+
this.events.dispatch("data:action", { action, value, target }, { bubbles: true });
|
|
29
|
+
}
|
|
30
|
+
subscribe(origin, fn) {
|
|
31
|
+
return this.events.subscribe("data:action", (e, { action, value, target }) => origin === target && fn(origin, action, value));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const createEventSubscriber = (events) => {
|
|
35
|
+
const subs = [];
|
|
36
|
+
const subscribe = (type, listener) => {
|
|
37
|
+
subs.push(events.subscribe(type, listener));
|
|
38
|
+
};
|
|
39
|
+
return [
|
|
40
|
+
subscribe,
|
|
41
|
+
() => {
|
|
42
|
+
subs.forEach((sub) => {
|
|
43
|
+
sub();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
];
|
|
47
|
+
};
|
|
48
|
+
class ComponentQuery {
|
|
49
|
+
constructor(platform, rules = []) {
|
|
50
|
+
this.platform = platform;
|
|
51
|
+
this.rules = rules;
|
|
52
|
+
}
|
|
53
|
+
match(node) {
|
|
54
|
+
return this.rules.every((rule) => {
|
|
55
|
+
if (rule.id && rule.id !== node.id) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (rule.type && rule.type !== node.type) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
ofType(type) {
|
|
65
|
+
this.rules.push({ type });
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
withId(id) {
|
|
69
|
+
this.rules.push({ id });
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
select() {
|
|
73
|
+
return new AbstractComponentController(this.platform, this);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class ComponentNodeController {
|
|
77
|
+
constructor(platform, node, ns) {
|
|
78
|
+
this.platform = platform;
|
|
79
|
+
this.node = node;
|
|
80
|
+
this.actions = new ActionsController(platform, new ComponentQuery(platform).withId(node.id), ns);
|
|
81
|
+
const [subscribe, unsubscribe] = createEventSubscriber(this.platform.events);
|
|
82
|
+
this.subscribe = subscribe;
|
|
83
|
+
this.unsubscribe = unsubscribe;
|
|
84
|
+
}
|
|
85
|
+
properties(updated) {
|
|
86
|
+
if (updated) {
|
|
87
|
+
this.node.touchProperties((properties) => updated(properties));
|
|
88
|
+
}
|
|
89
|
+
return this.node.attributes.properties;
|
|
90
|
+
}
|
|
91
|
+
context(updated) {
|
|
92
|
+
if (updated) {
|
|
93
|
+
this.subscribe("components:context-changed", (event, { node }) => {
|
|
94
|
+
if (node === this.node) {
|
|
95
|
+
updated(this.node.attributes.context);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return this.node.attributes.context;
|
|
100
|
+
}
|
|
101
|
+
layout(updated) {
|
|
102
|
+
if (updated) {
|
|
103
|
+
this.subscribe("components:layout-changed", (event, { node }) => {
|
|
104
|
+
if (node === this.node) {
|
|
105
|
+
updated(this.node.attributes.layout);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return this.node.attributes.layout;
|
|
110
|
+
}
|
|
111
|
+
writeProperties(properties, options) {
|
|
112
|
+
this.node.writeProperties(properties, options);
|
|
113
|
+
}
|
|
114
|
+
writeContext(context, options) {
|
|
115
|
+
this.node.writeProperties(context, options);
|
|
116
|
+
}
|
|
117
|
+
writeLayout(layout, options) {
|
|
118
|
+
this.node.writeLayout(layout, options);
|
|
119
|
+
}
|
|
120
|
+
dispose() {
|
|
121
|
+
this.unsubscribe();
|
|
122
|
+
this.actions.dispose();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
class AbstractComponentController {
|
|
126
|
+
constructor(platform, target, clearOnDispose = true) {
|
|
127
|
+
this.platform = platform;
|
|
128
|
+
this.target = target;
|
|
129
|
+
this.clearOnDispose = clearOnDispose;
|
|
130
|
+
this.removes = [];
|
|
131
|
+
this.actions = new ActionsController(this.platform, this.target);
|
|
132
|
+
}
|
|
133
|
+
writeProperties(properties, options) {
|
|
134
|
+
this.removes.push(this.platform.write({
|
|
135
|
+
options,
|
|
136
|
+
attributes: { properties },
|
|
137
|
+
target: this.target,
|
|
138
|
+
removable: this.clearOnDispose
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
writeContext(context, options) {
|
|
142
|
+
this.removes.push(this.platform.write({
|
|
143
|
+
options,
|
|
144
|
+
attributes: { context },
|
|
145
|
+
target: this.target,
|
|
146
|
+
removable: this.clearOnDispose
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
writeLayout(layout, options) {
|
|
150
|
+
this.removes.push(this.platform.write({
|
|
151
|
+
options,
|
|
152
|
+
attributes: { layout },
|
|
153
|
+
target: this.target,
|
|
154
|
+
removable: this.clearOnDispose
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
dispose() {
|
|
158
|
+
this.removes.forEach((remove) => {
|
|
159
|
+
remove();
|
|
160
|
+
});
|
|
161
|
+
this.actions.dispose();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const executeActionListeners = (listeners, data, controller) => {
|
|
165
|
+
const stack = listeners.concat();
|
|
166
|
+
const dispatch = (data2, globalOptions, controller2) => {
|
|
167
|
+
let cancelled = true;
|
|
168
|
+
if (stack.length) {
|
|
169
|
+
const handler = stack.shift();
|
|
170
|
+
const next = (data3, options) => {
|
|
171
|
+
cancelled = false;
|
|
172
|
+
return dispatch(data3, Object.assign({}, globalOptions, options), controller2);
|
|
173
|
+
};
|
|
174
|
+
return Promise.resolve(handler(data2, next, controller2)).then((result) => __spreadProps(__spreadValues({}, result), {
|
|
175
|
+
cancelled
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
return Promise.resolve({ data: data2, cancelled: false, options: globalOptions });
|
|
179
|
+
};
|
|
180
|
+
return dispatch(data, {}, controller);
|
|
181
|
+
};
|
|
182
|
+
class ActionsController {
|
|
183
|
+
constructor(platform, target, ns) {
|
|
184
|
+
this.platform = platform;
|
|
185
|
+
this.target = target;
|
|
186
|
+
this.ns = ns;
|
|
187
|
+
this.subscriptions = [];
|
|
188
|
+
this.create = this.create.bind(this);
|
|
189
|
+
}
|
|
190
|
+
create(key, fn = () => {
|
|
191
|
+
}) {
|
|
192
|
+
const handler = {
|
|
193
|
+
key,
|
|
194
|
+
fn,
|
|
195
|
+
target: this.target,
|
|
196
|
+
ns: this.ns
|
|
197
|
+
};
|
|
198
|
+
this.platform.actionsState.handlers.push(handler);
|
|
199
|
+
const subscription = () => {
|
|
200
|
+
const index = this.platform.actionsState.handlers.indexOf(handler);
|
|
201
|
+
if (index > -1) {
|
|
202
|
+
this.platform.actionsState.handlers.splice(index, 1);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
this.subscriptions.push(subscription);
|
|
206
|
+
return subscription;
|
|
207
|
+
}
|
|
208
|
+
dispatch(key, data) {
|
|
209
|
+
const instances = this.platform.nodes.all.concat(this.platform.nodes.detached);
|
|
210
|
+
const output = {};
|
|
211
|
+
const handlers = this.platform.actionsState.handlers.filter((handler) => handler.key === key && (!handler.ns || !this.ns || handler.ns === this.ns));
|
|
212
|
+
return Promise.all(handlers.map((handler) => {
|
|
213
|
+
const matchedInstances = instances.filter((instance) => handler.target.match(instance) && this.target.match(instance));
|
|
214
|
+
const watchers = this.platform.actionsState.watchers.filter((watcher) => {
|
|
215
|
+
return watcher.key === key && (!watcher.ns || !this.ns || watcher.ns === this.ns) && matchedInstances.some((instance) => watcher.target.match(instance));
|
|
216
|
+
}).map((watcher) => watcher.fn);
|
|
217
|
+
return Promise.all(matchedInstances.map((node) => {
|
|
218
|
+
const controller = new ComponentNodeController(this.platform, node, this.ns);
|
|
219
|
+
return executeActionListeners(watchers, data, controller).then((input) => {
|
|
220
|
+
const { data: data2 = {}, options, cancelled } = input;
|
|
221
|
+
return Promise.resolve(cancelled ? {} : handler.fn(data2, options, controller)).then((handlerResponse) => {
|
|
222
|
+
output[node.id] = handlerResponse;
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
}));
|
|
226
|
+
})).then(() => output);
|
|
227
|
+
}
|
|
228
|
+
watch(key, fn) {
|
|
229
|
+
let parsedKey = key;
|
|
230
|
+
let parsedNs = this.ns;
|
|
231
|
+
const sections = key.split(".");
|
|
232
|
+
if (sections.length > 1) {
|
|
233
|
+
parsedNs = sections[0];
|
|
234
|
+
parsedKey = sections.splice(1).join(".");
|
|
235
|
+
}
|
|
236
|
+
const watcher = {
|
|
237
|
+
fn,
|
|
238
|
+
target: this.target,
|
|
239
|
+
key: parsedKey,
|
|
240
|
+
ns: parsedNs
|
|
241
|
+
};
|
|
242
|
+
this.platform.actionsState.watchers.push(watcher);
|
|
243
|
+
const subscription = () => {
|
|
244
|
+
const index = this.platform.actionsState.watchers.indexOf(watcher);
|
|
245
|
+
if (index > -1) {
|
|
246
|
+
this.platform.actionsState.watchers.splice(index, 1);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
this.subscriptions.push(subscription);
|
|
250
|
+
return subscription;
|
|
251
|
+
}
|
|
252
|
+
dispose() {
|
|
253
|
+
this.subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
var componentConfigurationApi = createConfigurationApi((builder) => {
|
|
257
|
+
builder.createCommand("components", (context) => (config) => {
|
|
258
|
+
context.events.subscribeOnce("components:platform-created", (event, { platform }) => {
|
|
259
|
+
if (config.handlers) {
|
|
260
|
+
config.handlers.forEach(([key, handler]) => {
|
|
261
|
+
platform.addComponentHandler(key, handler);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
class ComponentNode {
|
|
268
|
+
constructor(events, type) {
|
|
269
|
+
this.events = events;
|
|
270
|
+
this.tags = [];
|
|
271
|
+
this.children = {};
|
|
272
|
+
this.changesets = [];
|
|
273
|
+
this.mountCount = 0;
|
|
274
|
+
this.id = uuid();
|
|
275
|
+
this.type = type;
|
|
276
|
+
this.attributes = {
|
|
277
|
+
context: {},
|
|
278
|
+
properties: {},
|
|
279
|
+
layout: {},
|
|
280
|
+
tags: []
|
|
281
|
+
};
|
|
282
|
+
this.writeLayout({ size: "full", indent: 0 });
|
|
283
|
+
}
|
|
284
|
+
mount(viewOutletContainer, element) {
|
|
285
|
+
let mountedNodes = viewOutletContainer.get("mounted-nodes");
|
|
286
|
+
if (!mountedNodes) {
|
|
287
|
+
mountedNodes = /* @__PURE__ */ new Map();
|
|
288
|
+
viewOutletContainer.register("mounted-nodes", mountedNodes);
|
|
289
|
+
}
|
|
290
|
+
if (mountedNodes.has(this)) {
|
|
291
|
+
return () => {
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
this.events.dispatch("components:node-mounted", { node: this });
|
|
295
|
+
if (this.mountCount === 0) {
|
|
296
|
+
this.mountCount = this.mountCount + 1;
|
|
297
|
+
this.events.dispatch("components:node-enter", { node: this });
|
|
298
|
+
}
|
|
299
|
+
return () => {
|
|
300
|
+
mountedNodes.delete(this);
|
|
301
|
+
this.events.dispatch("components:node-unmounted", { node: this });
|
|
302
|
+
this.mountCount = this.mountCount - 1;
|
|
303
|
+
if (this.mountCount === 0) {
|
|
304
|
+
this.events.dispatch("components:node-exit", { node: this });
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
appendChild(node, branch = "default") {
|
|
309
|
+
if (node.parent) {
|
|
310
|
+
if (node.parent === this) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
node.parent.removeChild(node);
|
|
314
|
+
}
|
|
315
|
+
node.parent = this;
|
|
316
|
+
if (!this.children[branch]) {
|
|
317
|
+
this.children[branch] = [];
|
|
318
|
+
}
|
|
319
|
+
this.children[branch].push(node);
|
|
320
|
+
this.events.dispatch("components:node-appended", { node });
|
|
321
|
+
}
|
|
322
|
+
prependChild(node, branch = "default") {
|
|
323
|
+
if (node.parent) {
|
|
324
|
+
if (node.parent === this) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
node.parent.removeChild(node);
|
|
328
|
+
}
|
|
329
|
+
node.parent = this;
|
|
330
|
+
if (!this.children[branch]) {
|
|
331
|
+
this.children[branch] = [];
|
|
332
|
+
}
|
|
333
|
+
this.children[branch].unshift(node);
|
|
334
|
+
this.events.dispatch("components:node-appended", { node });
|
|
335
|
+
}
|
|
336
|
+
touchProperties(properties, options = {}, removable) {
|
|
337
|
+
const attributes = { properties };
|
|
338
|
+
const changeset = [attributes, options];
|
|
339
|
+
this.changesets.push(changeset);
|
|
340
|
+
this.events.dispatch("components:set-properties", { node: this });
|
|
341
|
+
return () => {
|
|
342
|
+
if (removable) {
|
|
343
|
+
const indexToRemove = this.changesets.indexOf(changeset);
|
|
344
|
+
if (indexToRemove > -1) {
|
|
345
|
+
this.changesets.splice(indexToRemove, 1);
|
|
346
|
+
this.events.dispatch("components:set-properties", { node: this });
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
writeProperties(properties, options = {}, removable) {
|
|
352
|
+
const attributes = { properties };
|
|
353
|
+
const changeset = [attributes, options];
|
|
354
|
+
this.changesets.push(changeset);
|
|
355
|
+
this.events.dispatch("components:set-properties", { node: this });
|
|
356
|
+
return () => {
|
|
357
|
+
if (removable) {
|
|
358
|
+
const indexToRemove = this.changesets.indexOf(changeset);
|
|
359
|
+
if (indexToRemove > -1) {
|
|
360
|
+
this.changesets.splice(indexToRemove, 1);
|
|
361
|
+
this.events.dispatch("components:set-properties", { node: this });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
writeLayout(layout, options = {}, removable) {
|
|
367
|
+
const attributes = { layout };
|
|
368
|
+
const changeset = [attributes, options];
|
|
369
|
+
this.changesets.push(changeset);
|
|
370
|
+
this.events.dispatch("components:set-layout", { node: this });
|
|
371
|
+
return () => {
|
|
372
|
+
if (!removable) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const indexToRemove = this.changesets.indexOf(changeset);
|
|
376
|
+
if (indexToRemove > -1) {
|
|
377
|
+
this.changesets.splice(indexToRemove, 1);
|
|
378
|
+
this.events.dispatch("components:set-layout", { node: this });
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
writeContext(context, options = {}, removable) {
|
|
383
|
+
const attributes = { context };
|
|
384
|
+
const changeset = [attributes, options];
|
|
385
|
+
this.changesets.push(changeset);
|
|
386
|
+
this.events.dispatch("components:set-context", { node: this });
|
|
387
|
+
return () => {
|
|
388
|
+
if (!removable) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const indexToRemove = this.changesets.indexOf(changeset);
|
|
392
|
+
if (indexToRemove > -1) {
|
|
393
|
+
this.changesets.splice(indexToRemove, 1);
|
|
394
|
+
this.events.dispatch("components:set-context", { node: this });
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
getChildren(branch = "default") {
|
|
399
|
+
const children = this.children[branch] || [];
|
|
400
|
+
return children.reduce((acc, node, i) => {
|
|
401
|
+
const { order: index = i } = node.attributes.layout;
|
|
402
|
+
if (!acc[index]) {
|
|
403
|
+
acc[index] = node;
|
|
404
|
+
} else {
|
|
405
|
+
acc.splice(index, 0, node);
|
|
406
|
+
}
|
|
407
|
+
return acc;
|
|
408
|
+
}, Array(children.length));
|
|
409
|
+
}
|
|
410
|
+
remove() {
|
|
411
|
+
if (this.parent) {
|
|
412
|
+
this.parent.removeChild(this);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
removeChild(node) {
|
|
416
|
+
Object.keys(this.children).forEach((branch) => {
|
|
417
|
+
const children = this.children[branch];
|
|
418
|
+
const indexToRemove = children.indexOf(node);
|
|
419
|
+
if (indexToRemove > -1) {
|
|
420
|
+
children.splice(indexToRemove, 1);
|
|
421
|
+
this.events.dispatch("components:node-removed", { node, parentNode: this });
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
delete node.parent;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
const traverseComponentsRecursive = (nodes, switches, fn) => {
|
|
428
|
+
for (let i = 0, y = nodes.length; i < y; i++) {
|
|
429
|
+
if (fn(nodes[i]) === true) {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
const branch = switches[nodes[i].id] || "default";
|
|
433
|
+
const children = nodes[i].getChildren(branch);
|
|
434
|
+
traverseComponentsRecursive(children, switches, fn);
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
const traverseComponents = (root, switches, fn) => {
|
|
438
|
+
if (fn(root) !== true) {
|
|
439
|
+
const branch = switches[root.id] || "default";
|
|
440
|
+
const children = root.children[branch];
|
|
441
|
+
traverseComponentsRecursive(children, switches, fn);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
const findVisibleComponents = (root, switches) => {
|
|
445
|
+
const nodes = [];
|
|
446
|
+
traverseComponents(root, switches, (node) => {
|
|
447
|
+
nodes.push(node);
|
|
448
|
+
});
|
|
449
|
+
return nodes;
|
|
450
|
+
};
|
|
451
|
+
const findComponent = (root, switches, fn) => {
|
|
452
|
+
let matched;
|
|
453
|
+
traverseComponents(root, switches, (node) => {
|
|
454
|
+
if (fn(node)) {
|
|
455
|
+
matched = node;
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
return matched;
|
|
460
|
+
};
|
|
461
|
+
class ComponentNodeCollection {
|
|
462
|
+
constructor(root, switches, events) {
|
|
463
|
+
this.root = root;
|
|
464
|
+
this.switches = switches;
|
|
465
|
+
this.all = [];
|
|
466
|
+
this.visible = [];
|
|
467
|
+
this.detached = [];
|
|
468
|
+
events.subscribe("components:node-appended", (event, data) => {
|
|
469
|
+
this.all.push(data.node);
|
|
470
|
+
this.visible = findVisibleComponents(root, switches);
|
|
471
|
+
});
|
|
472
|
+
events.subscribe("components:node-removed", (event, data) => {
|
|
473
|
+
let indexToRemove = this.all.indexOf(data.node);
|
|
474
|
+
if (indexToRemove > -1) {
|
|
475
|
+
this.all.splice(indexToRemove, 1);
|
|
476
|
+
}
|
|
477
|
+
indexToRemove = this.visible.indexOf(data.node);
|
|
478
|
+
if (indexToRemove > -1) {
|
|
479
|
+
this.visible.splice(indexToRemove, 1);
|
|
480
|
+
}
|
|
481
|
+
this.visible = findVisibleComponents(root, switches);
|
|
482
|
+
});
|
|
483
|
+
events.subscribe("components:node-switched", (event, data) => {
|
|
484
|
+
this.visible = findVisibleComponents(root, switches);
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
query(query = {}) {
|
|
488
|
+
const nodes = [];
|
|
489
|
+
traverseComponents(this.root, this.switches, (node) => {
|
|
490
|
+
const match = Object.keys(query).every((member) => {
|
|
491
|
+
return query[member] === node[member];
|
|
492
|
+
});
|
|
493
|
+
if (match) {
|
|
494
|
+
nodes.push(node);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
return nodes;
|
|
498
|
+
}
|
|
499
|
+
get(id) {
|
|
500
|
+
return findComponent(this.root, this.switches, (node) => node.id === id) || this.detached.find((node) => node.id === id);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
class CustomComponentHandler {
|
|
504
|
+
constructor() {
|
|
505
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
506
|
+
}
|
|
507
|
+
add(name, fn) {
|
|
508
|
+
this.handlers.set(name, fn);
|
|
509
|
+
}
|
|
510
|
+
get(name) {
|
|
511
|
+
return this.handlers.get(name);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
const calculateNodeIndex = (node) => {
|
|
515
|
+
let currentNode = node;
|
|
516
|
+
let index = 0;
|
|
517
|
+
while (currentNode.parent) {
|
|
518
|
+
index = index + 1;
|
|
519
|
+
currentNode = currentNode.parent;
|
|
520
|
+
}
|
|
521
|
+
return index;
|
|
522
|
+
};
|
|
523
|
+
const sortQueue = (queue) => {
|
|
524
|
+
const mapped = queue.map(([node, options]) => {
|
|
525
|
+
return [calculateNodeIndex(node), node, options];
|
|
526
|
+
});
|
|
527
|
+
return mapped.sort(([a], [b]) => a - b).map(([, node, options]) => {
|
|
528
|
+
return [node, options];
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
const isObject = (item) => {
|
|
532
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
533
|
+
};
|
|
534
|
+
const mergeDeep = (target, ...sources) => {
|
|
535
|
+
if (!sources.length)
|
|
536
|
+
return target;
|
|
537
|
+
const source = sources.shift();
|
|
538
|
+
if (isObject(target) && isObject(source)) {
|
|
539
|
+
for (const key in source) {
|
|
540
|
+
if (isObject(source[key])) {
|
|
541
|
+
if (!target[key])
|
|
542
|
+
Object.assign(target, { [key]: {} });
|
|
543
|
+
mergeDeep(target[key], source[key]);
|
|
544
|
+
} else {
|
|
545
|
+
Object.assign(target, { [key]: source[key] });
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return mergeDeep(target, ...sources);
|
|
550
|
+
};
|
|
551
|
+
const matchesBreakpoint = (b1, b2) => {
|
|
552
|
+
if (!b1 || b1 === "all") {
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
return b1.some((b12) => b2.indexOf(b12) > -1);
|
|
556
|
+
};
|
|
557
|
+
const createStateProcessor = (platform, callback) => {
|
|
558
|
+
let nodeQueue = [];
|
|
559
|
+
let optionsQueue = [];
|
|
560
|
+
let timeout;
|
|
561
|
+
const flush = () => {
|
|
562
|
+
const processHistory = /* @__PURE__ */ new Map();
|
|
563
|
+
const sortedQueue = sortQueue(nodeQueue.map((node, i) => [node, optionsQueue[i]]));
|
|
564
|
+
const processNode = (node, options) => {
|
|
565
|
+
var _a;
|
|
566
|
+
const history = processHistory.get(node) || {};
|
|
567
|
+
const processContext = options.context && !history.context;
|
|
568
|
+
const processProperties = options.properties && !history.properties;
|
|
569
|
+
const processLayout = options.layout && !history.layout;
|
|
570
|
+
processHistory.set(node, {
|
|
571
|
+
context: history.context || processContext,
|
|
572
|
+
properties: history.properties || processProperties,
|
|
573
|
+
layout: history.layout || processLayout
|
|
574
|
+
});
|
|
575
|
+
if (processProperties) {
|
|
576
|
+
node.attributes.properties = {};
|
|
577
|
+
}
|
|
578
|
+
if (processLayout) {
|
|
579
|
+
node.attributes.layout = {};
|
|
580
|
+
}
|
|
581
|
+
if (processContext) {
|
|
582
|
+
node.attributes.context = mergeDeep({}, (_a = node.parent) == null ? void 0 : _a.attributes.context);
|
|
583
|
+
}
|
|
584
|
+
const propertyResolvers = [];
|
|
585
|
+
const layoutResolvers = [];
|
|
586
|
+
const contextResolvers = [];
|
|
587
|
+
node.changesets.forEach((changeset) => {
|
|
588
|
+
const [change, options2] = changeset;
|
|
589
|
+
if (matchesBreakpoint(options2.breakpoints, platform.breakpoints)) {
|
|
590
|
+
if (processProperties) {
|
|
591
|
+
if (typeof change.properties === "function") {
|
|
592
|
+
propertyResolvers.push(change.properties);
|
|
593
|
+
} else {
|
|
594
|
+
mergeDeep(node.attributes.properties, change.properties);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (processLayout) {
|
|
598
|
+
if (typeof change.layout === "function") {
|
|
599
|
+
layoutResolvers.push(change.layout);
|
|
600
|
+
} else {
|
|
601
|
+
mergeDeep(node.attributes.layout, change.layout);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
if (processContext) {
|
|
605
|
+
if (typeof change.context === "function") {
|
|
606
|
+
contextResolvers.push(change.context);
|
|
607
|
+
} else {
|
|
608
|
+
mergeDeep(node.attributes.context, change.context);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
propertyResolvers.forEach((resolver) => {
|
|
614
|
+
mergeDeep(node.attributes.properties, resolver(node.attributes.properties));
|
|
615
|
+
});
|
|
616
|
+
layoutResolvers.forEach((resolver) => {
|
|
617
|
+
mergeDeep(node.attributes.layout, resolver(node.attributes.layout));
|
|
618
|
+
});
|
|
619
|
+
contextResolvers.forEach((resolver) => {
|
|
620
|
+
mergeDeep(node.attributes.context, resolver(node.attributes.context));
|
|
621
|
+
});
|
|
622
|
+
if (processContext || options.recursive) {
|
|
623
|
+
const branch = platform.getBranch(node);
|
|
624
|
+
const childOptions = options.recursive ? options : {
|
|
625
|
+
context: !!processContext
|
|
626
|
+
};
|
|
627
|
+
node.getChildren(branch).forEach((child) => {
|
|
628
|
+
processNode(child, childOptions);
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
sortedQueue.forEach(([node, options]) => {
|
|
633
|
+
processNode(node, options);
|
|
634
|
+
});
|
|
635
|
+
nodeQueue = [];
|
|
636
|
+
optionsQueue = [];
|
|
637
|
+
callback(processHistory);
|
|
638
|
+
};
|
|
639
|
+
const schedule = () => {
|
|
640
|
+
clearTimeout(timeout);
|
|
641
|
+
timeout = setTimeout(flush, 0);
|
|
642
|
+
};
|
|
643
|
+
return (node, options) => {
|
|
644
|
+
const index = nodeQueue.indexOf(node);
|
|
645
|
+
if (index > -1) {
|
|
646
|
+
const summary = optionsQueue[index];
|
|
647
|
+
if (options.context) {
|
|
648
|
+
summary.context = true;
|
|
649
|
+
}
|
|
650
|
+
if (options.properties) {
|
|
651
|
+
summary.properties = true;
|
|
652
|
+
}
|
|
653
|
+
if (options.layout) {
|
|
654
|
+
summary.layout = true;
|
|
655
|
+
}
|
|
656
|
+
if (options.recursive) {
|
|
657
|
+
summary.recursive = true;
|
|
658
|
+
}
|
|
659
|
+
return schedule();
|
|
660
|
+
}
|
|
661
|
+
nodeQueue.push(node);
|
|
662
|
+
optionsQueue.push(options);
|
|
663
|
+
return schedule();
|
|
664
|
+
};
|
|
665
|
+
};
|
|
666
|
+
class ComponentPlatform {
|
|
667
|
+
constructor(widget, providers = {}) {
|
|
668
|
+
this.providers = providers;
|
|
669
|
+
this.breakpoints = [];
|
|
670
|
+
this.actionsState = { handlers: [], watchers: [] };
|
|
671
|
+
this.switches = {};
|
|
672
|
+
this.changesets = [];
|
|
673
|
+
this.subscriptions = [];
|
|
674
|
+
this.customComponentHandler = new CustomComponentHandler();
|
|
675
|
+
this.events = widget.events.createChild(this);
|
|
676
|
+
const root = new ComponentNode(this.events, "root");
|
|
677
|
+
root.name = `root-${widget.name}`;
|
|
678
|
+
this.nodes = new ComponentNodeCollection(root, this.switches, this.events);
|
|
679
|
+
const processNode = this.processNode = createStateProcessor(this, (report) => {
|
|
680
|
+
const nodes = Array.from(report.keys());
|
|
681
|
+
nodes.forEach((node) => {
|
|
682
|
+
const options = report.get(node);
|
|
683
|
+
if (node.parent && node.parent.type === "root") {
|
|
684
|
+
this.events.dispatch("components:root-children-changed", { node });
|
|
685
|
+
}
|
|
686
|
+
if (options == null ? void 0 : options.context) {
|
|
687
|
+
this.events.dispatch("components:context-changed", { node });
|
|
688
|
+
}
|
|
689
|
+
if (options == null ? void 0 : options.properties) {
|
|
690
|
+
this.events.dispatch("components:properties-changed", { node });
|
|
691
|
+
}
|
|
692
|
+
if (options == null ? void 0 : options.layout) {
|
|
693
|
+
this.events.dispatch("components:layout-changed", { node });
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
this.events.dispatch("components:component-model-initialized", {}, { bubbles: true });
|
|
697
|
+
});
|
|
698
|
+
Promise.all(Object.keys(providers).map((key) => {
|
|
699
|
+
const provider = providers[key];
|
|
700
|
+
return provider.load(this);
|
|
701
|
+
}));
|
|
702
|
+
this.subscriptions.push(this.events.subscribe("components:node-appended", (event, data) => {
|
|
703
|
+
const { node } = data;
|
|
704
|
+
this.changesets.forEach((record) => {
|
|
705
|
+
const { changeset, target, removes } = record;
|
|
706
|
+
const [change, options] = changeset;
|
|
707
|
+
if (target.match(data.node)) {
|
|
708
|
+
if (change.properties) {
|
|
709
|
+
removes.push(node.writeProperties(change.properties, options));
|
|
710
|
+
}
|
|
711
|
+
if (change.context) {
|
|
712
|
+
removes.push(node.writeContext(change.context, options));
|
|
713
|
+
}
|
|
714
|
+
if (change.layout) {
|
|
715
|
+
removes.push(node.writeLayout(change.layout, options));
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
}));
|
|
720
|
+
this.subscriptions.push(this.events.subscribe("components:node-appended", (event, data) => {
|
|
721
|
+
processNode(data.node, { properties: true, context: true });
|
|
722
|
+
}));
|
|
723
|
+
this.subscriptions.push(this.events.subscribe("components:set-properties", (event, data) => {
|
|
724
|
+
processNode(data.node, { properties: true });
|
|
725
|
+
}));
|
|
726
|
+
this.subscriptions.push(this.events.subscribe("components:set-context", (event, data) => {
|
|
727
|
+
processNode(data.node, { properties: true, context: true });
|
|
728
|
+
}));
|
|
729
|
+
this.subscriptions.push(this.events.subscribe("components:set-indent", (event, data) => {
|
|
730
|
+
processNode(data.node, { layout: true });
|
|
731
|
+
}));
|
|
732
|
+
this.subscriptions.push(this.events.subscribe("components:set-layout", (event, data) => {
|
|
733
|
+
processNode(data.node, { layout: true });
|
|
734
|
+
}));
|
|
735
|
+
this.subscriptions.push(this.events.subscribe("components:node-switched", (event, { node, branch }) => {
|
|
736
|
+
processNode(node, {
|
|
737
|
+
layout: true,
|
|
738
|
+
recursive: true,
|
|
739
|
+
properties: true,
|
|
740
|
+
context: true
|
|
741
|
+
});
|
|
742
|
+
}));
|
|
743
|
+
this.subscriptions.push(this.events.subscribe("components:node-removed", (event, { parentNode }) => {
|
|
744
|
+
processNode(parentNode, { properties: true });
|
|
745
|
+
}));
|
|
746
|
+
if (this.events.parent) {
|
|
747
|
+
this.events.parent.dispatch("components:platform-created", { platform: this });
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
static getInstance(container) {
|
|
751
|
+
return container.getAsync("components");
|
|
752
|
+
}
|
|
753
|
+
addBreakpoint(name) {
|
|
754
|
+
if (this.breakpoints.indexOf(name) === -1) {
|
|
755
|
+
this.breakpoints.push(name);
|
|
756
|
+
this.processNode(this.nodes.root, {
|
|
757
|
+
context: true,
|
|
758
|
+
properties: true,
|
|
759
|
+
layout: true,
|
|
760
|
+
recursive: true
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
removeBreakpoint(name) {
|
|
765
|
+
const indexToRemove = this.breakpoints.indexOf(name);
|
|
766
|
+
if (indexToRemove > -1) {
|
|
767
|
+
this.breakpoints.splice(indexToRemove, 1);
|
|
768
|
+
this.processNode(this.nodes.root, {
|
|
769
|
+
context: true,
|
|
770
|
+
properties: true,
|
|
771
|
+
layout: true,
|
|
772
|
+
recursive: true
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
setSwitch(node, branch) {
|
|
777
|
+
this.switches[node.id] = branch;
|
|
778
|
+
this.events.dispatch("components:node-switched", { node, branch });
|
|
779
|
+
}
|
|
780
|
+
getBranch(node) {
|
|
781
|
+
return this.switches[node.id];
|
|
782
|
+
}
|
|
783
|
+
createNode(type, options = {}) {
|
|
784
|
+
const node = new ComponentNode(this.events, type);
|
|
785
|
+
if (options.detached) {
|
|
786
|
+
this.nodes.detached.push(node);
|
|
787
|
+
}
|
|
788
|
+
return node;
|
|
789
|
+
}
|
|
790
|
+
components() {
|
|
791
|
+
return new ComponentQuery(this);
|
|
792
|
+
}
|
|
793
|
+
actions(target, ns) {
|
|
794
|
+
return new ActionsController(this, target, ns);
|
|
795
|
+
}
|
|
796
|
+
write(data) {
|
|
797
|
+
const { provider: providerName, target, attributes, options = {}, removable } = data;
|
|
798
|
+
const key = data.key || uuid();
|
|
799
|
+
const changeset = [attributes, options];
|
|
800
|
+
const record = { changeset, target, removes: [] };
|
|
801
|
+
if (providerName) {
|
|
802
|
+
const provider = this.providers[providerName];
|
|
803
|
+
provider.write(key, target, attributes, options);
|
|
804
|
+
record.removes.push(() => {
|
|
805
|
+
provider.remove(key);
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
this.changesets.push(record);
|
|
809
|
+
this.nodes.all.forEach((node) => {
|
|
810
|
+
if (target.match(node)) {
|
|
811
|
+
let remove = () => {
|
|
812
|
+
};
|
|
813
|
+
if (attributes.properties) {
|
|
814
|
+
remove = node.writeProperties(attributes.properties, options, removable);
|
|
815
|
+
}
|
|
816
|
+
if (attributes.context) {
|
|
817
|
+
remove = node.writeContext(attributes.context, options, removable);
|
|
818
|
+
}
|
|
819
|
+
if (attributes.layout) {
|
|
820
|
+
remove = node.writeContext(attributes.layout, options, removable);
|
|
821
|
+
}
|
|
822
|
+
record.removes.push(remove);
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
return () => {
|
|
826
|
+
if (removable) {
|
|
827
|
+
record.removes.forEach((remove) => remove());
|
|
828
|
+
const indexToRemove = this.changesets.indexOf(record);
|
|
829
|
+
if (indexToRemove > -1) {
|
|
830
|
+
this.changesets.splice(indexToRemove, 1);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
addComponentHandler(definitionType, fn) {
|
|
836
|
+
this.customComponentHandler.add(definitionType, fn);
|
|
837
|
+
}
|
|
838
|
+
getComponentHandler(definitionType) {
|
|
839
|
+
return this.customComponentHandler.get(definitionType);
|
|
840
|
+
}
|
|
841
|
+
dispose() {
|
|
842
|
+
this.subscriptions.forEach((sub) => sub());
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
const normalizeReference = (reference) => {
|
|
846
|
+
if (Array.isArray(reference)) {
|
|
847
|
+
const [definitionId, ...overrides] = reference;
|
|
848
|
+
return {
|
|
849
|
+
definitionId,
|
|
850
|
+
overrides
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
return {
|
|
854
|
+
definitionId: reference,
|
|
855
|
+
overrides: []
|
|
856
|
+
};
|
|
857
|
+
};
|
|
858
|
+
const createChildren = (platform, node, references, definitions, views, branch) => {
|
|
859
|
+
references.forEach((ref) => {
|
|
860
|
+
const childRef = normalizeReference(ref);
|
|
861
|
+
const childDef = definitions[childRef.definitionId];
|
|
862
|
+
if (childRef.definitionId === "view") {
|
|
863
|
+
const viewNode = platform.createNode("view");
|
|
864
|
+
viewNode.name = "view";
|
|
865
|
+
Object.keys(views).forEach((key) => {
|
|
866
|
+
const viewDef = views[key];
|
|
867
|
+
createChildren(platform, viewNode, [viewDef.entry], definitions, views, key);
|
|
868
|
+
});
|
|
869
|
+
node.appendChild(viewNode);
|
|
870
|
+
childRef.overrides.forEach((override) => {
|
|
871
|
+
if (override.properties) {
|
|
872
|
+
viewNode.writeProperties(override.properties, {
|
|
873
|
+
breakpoints: override.breakpoints
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
if (override.context) {
|
|
877
|
+
viewNode.writeContext(override.context, { breakpoints: override.breakpoints });
|
|
878
|
+
}
|
|
879
|
+
if (override.layout) {
|
|
880
|
+
viewNode.writeLayout(override.layout, { breakpoints: override.breakpoints });
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
} else if (platform.getComponentHandler(childDef.type)) {
|
|
884
|
+
const handle = platform.getComponentHandler(childDef.type);
|
|
885
|
+
if (handle) {
|
|
886
|
+
handle(childRef, {
|
|
887
|
+
platform,
|
|
888
|
+
node,
|
|
889
|
+
references,
|
|
890
|
+
definitions,
|
|
891
|
+
views,
|
|
892
|
+
branch
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
} else {
|
|
896
|
+
const childDef2 = definitions[childRef.definitionId];
|
|
897
|
+
const childNode = platform.createNode(childDef2.type);
|
|
898
|
+
childNode.name = childRef.definitionId;
|
|
899
|
+
childNode.tags = childDef2.tags || [];
|
|
900
|
+
if (childDef2.properties) {
|
|
901
|
+
childNode.writeProperties(childDef2.properties);
|
|
902
|
+
}
|
|
903
|
+
if (childDef2.context) {
|
|
904
|
+
childNode.writeContext(childDef2.context);
|
|
905
|
+
}
|
|
906
|
+
if (childDef2.layout) {
|
|
907
|
+
childNode.writeLayout(childDef2.layout);
|
|
908
|
+
}
|
|
909
|
+
node.appendChild(childNode, branch);
|
|
910
|
+
childRef.overrides.forEach((override) => {
|
|
911
|
+
const options = { breakpoints: override.breakpoints };
|
|
912
|
+
if (override.properties) {
|
|
913
|
+
childNode.writeProperties(override.properties, options);
|
|
914
|
+
}
|
|
915
|
+
if (override.context) {
|
|
916
|
+
childNode.writeContext(override.context, options);
|
|
917
|
+
}
|
|
918
|
+
if (override.layout) {
|
|
919
|
+
childNode.writeLayout(override.layout, options);
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
if (childDef2.children) {
|
|
923
|
+
createChildren(platform, childNode, childDef2.children, definitions, views);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
};
|
|
928
|
+
const createComponentModel = (platform, settings) => {
|
|
929
|
+
const { entry, components, breakpoints, views } = settings;
|
|
930
|
+
const subscriptions = [];
|
|
931
|
+
Object.keys(breakpoints).forEach((name) => {
|
|
932
|
+
const query = breakpoints[name];
|
|
933
|
+
const list = window.matchMedia(query);
|
|
934
|
+
if (list.matches) {
|
|
935
|
+
platform.breakpoints.push(name);
|
|
936
|
+
}
|
|
937
|
+
const listener = (event) => {
|
|
938
|
+
if (event.matches) {
|
|
939
|
+
platform.addBreakpoint(name);
|
|
940
|
+
} else {
|
|
941
|
+
platform.removeBreakpoint(name);
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
list.addListener(listener);
|
|
945
|
+
subscriptions.push(() => list.removeListener(listener));
|
|
946
|
+
});
|
|
947
|
+
createChildren(platform, platform.nodes.root, [entry], components, views);
|
|
948
|
+
return () => subscriptions.forEach((sub) => {
|
|
949
|
+
sub();
|
|
950
|
+
});
|
|
951
|
+
};
|
|
952
|
+
const asDescriptor = (node, branch) => {
|
|
953
|
+
return {
|
|
954
|
+
id: node.id,
|
|
955
|
+
type: node.type,
|
|
956
|
+
name: node.name,
|
|
957
|
+
branch: branch || "default"
|
|
958
|
+
};
|
|
959
|
+
};
|
|
960
|
+
const extendComponent = (container, typeOrQuery, handler, ns = uuid(), options = {}) => {
|
|
961
|
+
return container.getAsync("components").then((components) => {
|
|
962
|
+
let target;
|
|
963
|
+
if (typeof typeOrQuery === "string") {
|
|
964
|
+
const collection = new ComponentQuery(components);
|
|
965
|
+
collection.ofType(typeOrQuery);
|
|
966
|
+
target = collection;
|
|
967
|
+
} else {
|
|
968
|
+
target = typeOrQuery;
|
|
969
|
+
}
|
|
970
|
+
const managedInstances = {};
|
|
971
|
+
const getOrCreateController = (node) => {
|
|
972
|
+
const existing = managedInstances[node.id];
|
|
973
|
+
if (existing) {
|
|
974
|
+
return Promise.resolve(existing.controller);
|
|
975
|
+
}
|
|
976
|
+
return ComponentPlatform.getInstance(container).then((platform) => {
|
|
977
|
+
return new ComponentNodeController(platform, node, ns);
|
|
978
|
+
});
|
|
979
|
+
};
|
|
980
|
+
components.events.subscribe("components:node-enter", (event, { node }) => {
|
|
981
|
+
if (target.match(node)) {
|
|
982
|
+
getOrCreateController(node).then((controller) => {
|
|
983
|
+
Promise.resolve(handler(controller)).then((dispose) => {
|
|
984
|
+
managedInstances[node.id] = {
|
|
985
|
+
dispose,
|
|
986
|
+
controller
|
|
987
|
+
};
|
|
988
|
+
});
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
components.events.subscribe("components:node-exit", (event, { node }) => {
|
|
993
|
+
const instance = managedInstances[node.id];
|
|
994
|
+
if (instance) {
|
|
995
|
+
instance.dispose && instance.dispose();
|
|
996
|
+
if (!options.reuseController) {
|
|
997
|
+
instance.controller.dispose();
|
|
998
|
+
delete managedInstances[node.id];
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
});
|
|
1003
|
+
};
|
|
1004
|
+
class ComponentResolver {
|
|
1005
|
+
constructor(container) {
|
|
1006
|
+
this.aliases = /* @__PURE__ */ new Map();
|
|
1007
|
+
this.components = /* @__PURE__ */ new Map();
|
|
1008
|
+
this.logger = new Logger();
|
|
1009
|
+
const { events } = container.get("$widget");
|
|
1010
|
+
this.events = events;
|
|
1011
|
+
}
|
|
1012
|
+
getComponent(key) {
|
|
1013
|
+
const alias = this.aliases.get(key);
|
|
1014
|
+
const component = this.components.get(alias || key) || null;
|
|
1015
|
+
return component;
|
|
1016
|
+
}
|
|
1017
|
+
registerComponent(key, component) {
|
|
1018
|
+
this.components.set(key, component);
|
|
1019
|
+
this.events.dispatch("component-resolver:resolve", key);
|
|
1020
|
+
return () => {
|
|
1021
|
+
this.components.delete(key);
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
registerResolver(key, fn) {
|
|
1025
|
+
return this.events.subscribe("component-resolver:resolve", (e, data) => {
|
|
1026
|
+
if (data === key) {
|
|
1027
|
+
const component = this.getComponent(key);
|
|
1028
|
+
fn(component);
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
registerAlias(alias, key) {
|
|
1033
|
+
this.aliases.set(alias, key);
|
|
1034
|
+
this.events.dispatch("component-resolver:resolve", alias);
|
|
1035
|
+
return () => {
|
|
1036
|
+
this.aliases.delete(alias);
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
var WidgetRenderState = /* @__PURE__ */ ((WidgetRenderState2) => {
|
|
1041
|
+
WidgetRenderState2["open"] = "open";
|
|
1042
|
+
WidgetRenderState2["closed"] = "closed";
|
|
1043
|
+
WidgetRenderState2["hidden"] = "hidden";
|
|
1044
|
+
return WidgetRenderState2;
|
|
1045
|
+
})(WidgetRenderState || {});
|
|
1046
|
+
export { ActionResolver, ActionsController, ComponentNode, ComponentNodeController, ComponentPlatform, ComponentQuery, ComponentResolver, WidgetRenderState, asDescriptor, componentConfigurationApi, createChildren, createComponentModel, createEventSubscriber, extendComponent, findComponent, findVisibleComponents, traverseComponents };
|
|
1047
|
+
//# sourceMappingURL=index.js.map
|