@zcomponent/core 0.0.12 → 0.0.14

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/zcomponent.js CHANGED
@@ -4,6 +4,7 @@ import { Context } from './context';
4
4
  import { isDesignTime } from './contexts/environmentcontext';
5
5
  import { TagContext } from './contexts/tagcontext';
6
6
  import { computeBehaviorHierarchy, computeNodeHierarchy } from './data';
7
+ import { EntityPropOverrideType } from './interfaces';
7
8
  import { Observable } from './observable';
8
9
  import { setCurrentZComponentConstruction } from './zcomponentconstruction';
9
10
  export class ZComponentContext extends Context {
@@ -26,8 +27,10 @@ export class ZComponent extends Component {
26
27
  this.isConstructed = false;
27
28
  this._nodesById = new Map();
28
29
  this._behaviorsToInitialize = [];
30
+ this._constructorPropOverridesByEntityID = new Map();
29
31
  const contextManagerForChildren = contextManager.forkMultiple([ZComponentContext, { zcomponent: this }], [TagContext, {}])[0];
30
32
  this.id = this._opts.data.id;
33
+ this._initializeConstructorPropOverrides();
31
34
  const rootConstructor = this._constructorForNode(this._opts.data.root);
32
35
  if (rootConstructor) {
33
36
  this.constructChildren([
@@ -36,6 +39,7 @@ export class ZComponent extends Component {
36
39
  }
37
40
  this._inflateBehaviors();
38
41
  this._initializeComponentProps();
42
+ this._initializeOverrides();
39
43
  this.isConstructed = true;
40
44
  this._constructedResolve();
41
45
  }
@@ -65,6 +69,7 @@ export class ZComponent extends Component {
65
69
  ...constructorProps,
66
70
  ...(that._opts.data.entityConstructorProps?.[nodeId] ?? {}),
67
71
  ...(that._opts.constructorPropReplacement?.[nodeId] ?? {}),
72
+ ...(that._constructorPropOverridesByEntityID.get(nodeId) ?? {}),
68
73
  children
69
74
  };
70
75
  setCurrentZComponentConstruction(that);
@@ -145,6 +150,7 @@ export class ZComponent extends Component {
145
150
  ...constructorProps,
146
151
  ...(that._opts.data.entityConstructorProps?.[behaviorId] ?? {}),
147
152
  ...(that._opts.constructorPropReplacement?.[behaviorId] ?? {}),
153
+ ...(that._constructorPropOverridesByEntityID.get(behaviorId) ?? {}),
148
154
  };
149
155
  setCurrentZComponentConstruction(that);
150
156
  super(contextManager, instance, constructorProps);
@@ -205,26 +211,84 @@ export class ZComponent extends Component {
205
211
  }
206
212
  }
207
213
  }
208
- _initializeComponentProps() {
209
- const componentProps = this._opts.data.propEntityOverrides ?? {};
210
- for (const [prop, entry] of Object.entries(componentProps)) {
211
- const propInfo = this._opts.data.props[prop];
212
- const entries = Object.entries(entry).map(e => [e[0], Object.keys(e[1])]);
213
- this[prop] = new Observable(propInfo?.default, v => {
214
- for (const e of entries) {
215
- const entity = this.entityByID.get(e[0]);
216
- if (!entity)
217
- continue;
218
- for (const overriddenProp of e[1]) {
219
- try {
220
- this._setEntityProp(entity, overriddenProp, v);
221
- }
222
- catch (err) {
223
- console.log('Warning - unable to set prop', overriddenProp, v);
214
+ _initializeOverrides() {
215
+ const overrides = this._opts.data.entityPropOverrides ?? {};
216
+ for (const override of Object.values(overrides)) {
217
+ const entity = this.entityByID.get(override.entityID);
218
+ if (!entity)
219
+ continue;
220
+ try {
221
+ switch (override.type) {
222
+ case EntityPropOverrideType.Import: {
223
+ const v = this._opts.importMapping[override.imp]?.();
224
+ if (!v)
225
+ continue;
226
+ if (v instanceof Observable)
227
+ v.withValue(val => this._setEntityPropPath(entity, override.entityPropPath, val));
228
+ else
229
+ this._setEntityPropPath(entity, override.entityPropPath, v);
230
+ break;
231
+ }
232
+ case EntityPropOverrideType.ContextValue: {
233
+ const ctx = this._opts.importMapping[override.imp]?.();
234
+ const ctxInstance = entity.contextManager.getOrThrow(ctx);
235
+ const v = ctxInstance[override.val];
236
+ if (!v)
237
+ continue;
238
+ if (v instanceof Observable)
239
+ v.withValue(val => this._setEntityPropPath(entity, override.entityPropPath, val));
240
+ else
241
+ this._setEntityPropPath(entity, override.entityPropPath, v);
242
+ break;
243
+ }
244
+ case EntityPropOverrideType.ComponentProp: {
245
+ if (override.isConstructorProp) {
246
+ const prop = this._opts.data.constructorProps?.[override.propName];
247
+ if (!prop)
248
+ break;
249
+ const val = this.constructorProps[override.propName] ?? prop.default;
250
+ if (val !== undefined)
251
+ this._setEntityPropPath(entity, override.entityPropPath, val);
252
+ break;
224
253
  }
254
+ const v = this[override.propName];
255
+ if (!v)
256
+ continue;
257
+ if (v instanceof Observable)
258
+ v.withValue(val => this._setEntityPropPath(entity, override.entityPropPath, val));
259
+ else
260
+ this._setEntityPropPath(entity, override.entityPropPath, v);
261
+ break;
225
262
  }
226
263
  }
227
- });
264
+ }
265
+ catch (err) {
266
+ console.log('Unable to set entity prop override', err);
267
+ }
268
+ }
269
+ }
270
+ _initializeConstructorPropOverrides() {
271
+ for (const entry of Object.values(this._opts.data.entityPropOverrides ?? {})) {
272
+ if (entry.type !== EntityPropOverrideType.ComponentProp)
273
+ continue;
274
+ if (!entry.isConstructorProp)
275
+ continue;
276
+ if (entry.entityPropPath.length !== 1)
277
+ continue;
278
+ const obj = this._constructorPropOverridesByEntityID.get(entry.entityID) ?? Object.create(null);
279
+ this._constructorPropOverridesByEntityID.set(entry.entityID, obj);
280
+ const prop = this._opts.data.constructorProps?.[entry.propName];
281
+ if (!prop)
282
+ continue;
283
+ const val = this.constructorProps[entry.propName] ?? prop.default;
284
+ if (val !== undefined)
285
+ obj[entry.entityPropPath[0]] = val;
286
+ }
287
+ }
288
+ _initializeComponentProps() {
289
+ const componentProps = this._opts.data.props ?? {};
290
+ for (const prop of Object.values(componentProps)) {
291
+ this[prop.name] = new Observable(prop.default);
228
292
  }
229
293
  }
230
294
  _setEntityProp(entity, prop, v) {
@@ -233,6 +297,22 @@ export class ZComponent extends Component {
233
297
  else
234
298
  entity[prop] = v;
235
299
  }
300
+ _setEntityPropPath(entity, propPath, v) {
301
+ if (propPath.length < 1)
302
+ return;
303
+ const prop = propPath[0];
304
+ let parent = entity;
305
+ let currentKey = prop;
306
+ if (entity[prop] instanceof Observable) {
307
+ parent = entity[prop];
308
+ currentKey = 'value';
309
+ }
310
+ for (let i = 1; i < propPath.length; i++) {
311
+ parent = parent[currentKey];
312
+ currentKey = propPath[i];
313
+ }
314
+ parent[currentKey] = v;
315
+ }
236
316
  _getNodeById(id) {
237
317
  return this._nodesById.get(id);
238
318
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -30,10 +30,11 @@
30
30
  "@types/ua-parser-js": "^0.7.36",
31
31
  "parcel": "^2.8.2",
32
32
  "typescript": "^4.9.4",
33
- "ua-parser-js": "^1.0.2"
33
+ "ua-parser-js": "^1.0.33"
34
34
  },
35
35
  "zexports": [
36
36
  "./lib/components/**/*",
37
- "./lib/behaviors/**/*"
37
+ "./lib/behaviors/**/*",
38
+ "./lib/contexts/**/*"
38
39
  ]
39
40
  }