fg-dynamic-component 3.0.0 → 4.0.0
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/README.md +77 -12
- package/fesm2022/fg-dynamic-component.mjs +1109 -285
- package/fesm2022/fg-dynamic-component.mjs.map +1 -1
- package/package.json +5 -3
- package/types/fg-dynamic-component.d.ts +204 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fg-dynamic-component",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"author": "Francesco Guagnano <guagnanofrancesco11@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -18,8 +18,10 @@
|
|
|
18
18
|
"life-cycle"
|
|
19
19
|
],
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@angular/common": "22.
|
|
22
|
-
"@angular/core": "22.
|
|
21
|
+
"@angular/common": "^22.2.3",
|
|
22
|
+
"@angular/core": "^22.2.3",
|
|
23
|
+
"@angular/forms": "^22.2.3",
|
|
24
|
+
"rxjs": "^7.8.0"
|
|
23
25
|
},
|
|
24
26
|
"sideEffects": false,
|
|
25
27
|
"module": "fesm2022/fg-dynamic-component.mjs",
|
|
@@ -4,11 +4,52 @@ import { FormGroup } from "@angular/forms";
|
|
|
4
4
|
export type FGDynamicType = Type<unknown> | Promise<Type<unknown>> | (() => Promise<Type<unknown>>);
|
|
5
5
|
export interface IFGDynamicItem {
|
|
6
6
|
type?: string | FGDynamicType;
|
|
7
|
-
inputs?: Record<string,
|
|
7
|
+
inputs?: Record<string, unknown>;
|
|
8
8
|
outputs?: Record<string, string | Function>;
|
|
9
|
-
attributes?: Record<string,
|
|
9
|
+
attributes?: Record<string, unknown>;
|
|
10
10
|
items?: IFGDynamicItem[];
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Controls what expressions may do. By default expressions are read-only and
|
|
14
|
+
* can only call native built-ins and `$vm()`; custom view-model methods and
|
|
15
|
+
* view-model mutation require an explicit opt-in.
|
|
16
|
+
*/
|
|
17
|
+
export interface IFGDynamicExpressionPolicy {
|
|
18
|
+
/** Allow calling any method reachable from the view model. Defaults to `false`. */
|
|
19
|
+
allowCustomMethods?: boolean;
|
|
20
|
+
/** Method names that may be called when `allowCustomMethods` is `false`. */
|
|
21
|
+
allowedMethods?: readonly string[];
|
|
22
|
+
/** Expose `$vm.set` and `$vm.update`. Defaults to `false`. */
|
|
23
|
+
allowViewModelMutation?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Controls how the dynamic component interacts with the host `FormGroup`.
|
|
27
|
+
* By default pre-existing controls and nested groups are not reused.
|
|
28
|
+
*/
|
|
29
|
+
export interface IFGDynamicFormPolicy {
|
|
30
|
+
/** Reuse controls/groups that already exist in the supplied `FormGroup`. */
|
|
31
|
+
allowExistingControls?: boolean;
|
|
32
|
+
/** Optional allowlist of form control names (the evaluated `attributes.name`). */
|
|
33
|
+
allowedControlNames?: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Controls which component `type` values are accepted.
|
|
37
|
+
* Promise/loader values are rejected unless explicitly enabled.
|
|
38
|
+
*/
|
|
39
|
+
export interface IFGDynamicTypePolicy {
|
|
40
|
+
/** Allow promise-returning and function loader `type` values. Defaults to `false`. */
|
|
41
|
+
allowFunctionTypes?: boolean;
|
|
42
|
+
/** Require an explicit scoped registry instead of the global default. */
|
|
43
|
+
requireScopedRegistry?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Controls which DOM attributes may be applied. `strict` is intended for
|
|
47
|
+
* untrusted configurations and disables UI-redress/DOM-clobbering attributes.
|
|
48
|
+
*/
|
|
49
|
+
export interface IFGDynamicAttributePolicy {
|
|
50
|
+
/** Disallow `id`, `class`, `for`, `href` and `src`. Defaults to `false`. */
|
|
51
|
+
strict?: boolean;
|
|
52
|
+
}
|
|
12
53
|
/**
|
|
13
54
|
* A scoped component registry. Backed by a `Map`, so lookups never walk the
|
|
14
55
|
* prototype chain and names such as `__proto__` or `constructor` are ordinary keys.
|
|
@@ -52,6 +93,12 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
52
93
|
viewModel: ModelSignal<unknown>;
|
|
53
94
|
formGroup: InputSignal<FormGroup>;
|
|
54
95
|
registry: InputSignal<FGDynamicRegistry>;
|
|
96
|
+
expressionPolicy: InputSignal<IFGDynamicExpressionPolicy>;
|
|
97
|
+
formControlPolicy: InputSignal<IFGDynamicFormPolicy>;
|
|
98
|
+
typePolicy: InputSignal<IFGDynamicTypePolicy>;
|
|
99
|
+
attributePolicy: InputSignal<IFGDynamicAttributePolicy>;
|
|
100
|
+
/** Current nesting depth; used to bound recursive configurations. */
|
|
101
|
+
depth: InputSignal<number>;
|
|
55
102
|
/**
|
|
56
103
|
* Validated child items. Invalid entries are ignored and the list is capped
|
|
57
104
|
* to avoid pathological configurations blocking the renderer.
|
|
@@ -59,31 +106,69 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
59
106
|
protected readonly items: Signal<readonly IFGDynamicItem[]>;
|
|
60
107
|
private _viewContainerRef;
|
|
61
108
|
private _dynamicItems;
|
|
62
|
-
private _componentRef
|
|
109
|
+
private _componentRef?;
|
|
63
110
|
private _renderer;
|
|
64
111
|
private _outputsSubscriptions;
|
|
65
112
|
private _currentOutputsConfig?;
|
|
66
|
-
private _formControl
|
|
67
|
-
private _formControlDirective
|
|
68
|
-
private _formControlSubscription
|
|
113
|
+
private _formControl?;
|
|
114
|
+
private _formControlDirective?;
|
|
115
|
+
private _formControlSubscription?;
|
|
69
116
|
private _managedValidatorFns;
|
|
70
|
-
private _component
|
|
117
|
+
private _component?;
|
|
71
118
|
private _injector;
|
|
72
119
|
private _ngOnChangesCounter;
|
|
73
|
-
private _lastProjectedItemsSignature
|
|
120
|
+
private _lastProjectedItemsSignature?;
|
|
121
|
+
/** Group that literally contains the current control (may be a nested group). */
|
|
122
|
+
private _formControlContainer?;
|
|
123
|
+
/** Literal key of the control inside `_formControlContainer`. */
|
|
124
|
+
private _formControlKey?;
|
|
125
|
+
/** True only when this component created (and may therefore remove) the control. */
|
|
126
|
+
private _ownsFormControl;
|
|
127
|
+
/** Intermediate groups created while wiring a dotted control name. */
|
|
128
|
+
private _createdFormGroups;
|
|
129
|
+
/** Form wiring currently attached to the rendered component, used to detect rebinds. */
|
|
130
|
+
private _wiredFormGroup?;
|
|
131
|
+
private _wiredControlName?;
|
|
132
|
+
/** Disabled state managed by this component, so it can be restored when `disabled` disappears. */
|
|
133
|
+
private _disabledManaged;
|
|
134
|
+
private _disabledBeforeManagement?;
|
|
135
|
+
/** Attribute names currently applied to the dynamic element. */
|
|
136
|
+
private _appliedAttributes;
|
|
137
|
+
/** Input names currently set on the dynamic component. */
|
|
138
|
+
private _appliedInputs;
|
|
74
139
|
/**
|
|
75
140
|
* A callback method that is invoked immediately after the default change
|
|
76
141
|
* detector has checked data-bound properties if at least one has changed,
|
|
77
142
|
* and before the view and content children are checked.
|
|
78
143
|
*/
|
|
79
144
|
ngOnChanges(changes?: SimpleChanges): Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Destroys the rendered component when it no longer has a resolvable type.
|
|
147
|
+
* Ignored when a newer `ngOnChanges` generation has started.
|
|
148
|
+
*/
|
|
149
|
+
private clearResolvedComponent;
|
|
150
|
+
/**
|
|
151
|
+
* Validates the whole configuration subtree before rendering. Rejects
|
|
152
|
+
* excessive depth, node count, key counts, oversized keys and cycles.
|
|
153
|
+
* The check is iterative so a hostile configuration cannot overflow the
|
|
154
|
+
* JavaScript stack during validation itself.
|
|
155
|
+
*/
|
|
156
|
+
private validateConfiguration;
|
|
157
|
+
private collectConfigurationStats;
|
|
80
158
|
/**
|
|
81
159
|
* A method to lazy-load a component.
|
|
82
160
|
*/
|
|
83
161
|
private loadComponentAsync;
|
|
162
|
+
private isPromiseLike;
|
|
84
163
|
private resolveCandidate;
|
|
85
164
|
private toComponentType;
|
|
86
165
|
private isAngularComponentType;
|
|
166
|
+
/**
|
|
167
|
+
* Function loaders and thenables execute host code, so they are only
|
|
168
|
+
* accepted when the host opts in. Angular component classes stay allowed.
|
|
169
|
+
*/
|
|
170
|
+
private isCandidateAllowed;
|
|
171
|
+
private isUsingGlobalRegistry;
|
|
87
172
|
private getRegistry;
|
|
88
173
|
/**
|
|
89
174
|
* Instantiates a single component and inserts its host view into this container.
|
|
@@ -91,15 +176,52 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
91
176
|
private createComponent;
|
|
92
177
|
/**
|
|
93
178
|
* Register handlers for those events by subscribing to an instance.
|
|
179
|
+
* Supports both `EventEmitter` (`@Output`) and signal outputs created with `output()`.
|
|
94
180
|
*/
|
|
95
181
|
private handleOutputs;
|
|
182
|
+
private isOutputEmitter;
|
|
96
183
|
private handleOutputsIfChanged;
|
|
97
184
|
/**
|
|
98
|
-
*
|
|
185
|
+
* Evaluates the configured `attributes.name` into a safe control path.
|
|
186
|
+
* Returns `undefined` when the view model or name is not available yet, so
|
|
187
|
+
* the wiring is retried on the next change.
|
|
188
|
+
*/
|
|
189
|
+
private resolveFormControlName;
|
|
190
|
+
/**
|
|
191
|
+
* Recreates the dynamic component when the `formGroup`, the evaluated
|
|
192
|
+
* control name or the availability of the view model changed.
|
|
193
|
+
*/
|
|
194
|
+
private rewireFormIfNeeded;
|
|
195
|
+
/**
|
|
196
|
+
* Constructs (or reuses) the form control and attaches it to the rendered
|
|
197
|
+
* component. Dotted names are represented as nested `FormGroup`s so that
|
|
198
|
+
* `formGroup.get('data.firstName')` works and no key contains a dot.
|
|
99
199
|
*/
|
|
100
|
-
private
|
|
200
|
+
private wireFormControl;
|
|
101
201
|
/**
|
|
102
|
-
*
|
|
202
|
+
* Walks/creates the nested `FormGroup`s required by a dotted control path.
|
|
203
|
+
* Created groups are tracked so they can be removed again on cleanup.
|
|
204
|
+
*/
|
|
205
|
+
private ensureFormGroupPath;
|
|
206
|
+
/**
|
|
207
|
+
* Keeps the view model in sync with the control value. Used for both newly
|
|
208
|
+
* created and reused controls.
|
|
209
|
+
*/
|
|
210
|
+
private bindFormControlValueChanges;
|
|
211
|
+
/**
|
|
212
|
+
* Deep-clones arrays and plain objects, preserving functions and non-plain
|
|
213
|
+
* objects by reference. Handles cycles through a `WeakMap`.
|
|
214
|
+
*/
|
|
215
|
+
private cloneForMutation;
|
|
216
|
+
private setControlDisabled;
|
|
217
|
+
/**
|
|
218
|
+
* Restores the disabled state a control had before this component managed
|
|
219
|
+
* it (important for pre-existing/reused controls and removed `disabled` keys).
|
|
220
|
+
*/
|
|
221
|
+
private restoreManagedDisabledState;
|
|
222
|
+
/**
|
|
223
|
+
* Disables/enables the control, normalizing boolean-ish values, and restores
|
|
224
|
+
* the previous state when the `disabled` key is removed.
|
|
103
225
|
*/
|
|
104
226
|
private setFormControlStatus;
|
|
105
227
|
/**
|
|
@@ -112,8 +234,15 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
112
234
|
*/
|
|
113
235
|
private createPatternValidator;
|
|
114
236
|
/**
|
|
115
|
-
*
|
|
116
|
-
* (e.g. `(a+)+`, `(a*)*`, `(\d+){2,}`)
|
|
237
|
+
* Conservative scanner for regular expressions with nested/ambiguous
|
|
238
|
+
* quantifiers (e.g. `(a+)+`, `(\d+(?:\d+)?)+`, `(a*)*`, `(\d+){2,}`) and
|
|
239
|
+
* quantified alternations (e.g. `(a|aa)+`, `(\w|\d)+`) that can cause
|
|
240
|
+
* exponential backtracking.
|
|
241
|
+
*
|
|
242
|
+
* Groups are tracked with an explicit stack, so nested groups and optional
|
|
243
|
+
* inner quantifiers are detected instead of relying on a single regex.
|
|
244
|
+
* This is still a heuristic: it errs on the side of rejecting ambiguous
|
|
245
|
+
* constructs rather than allowing a catastrophic pattern through.
|
|
117
246
|
*/
|
|
118
247
|
private isUnsafeRegex;
|
|
119
248
|
/**
|
|
@@ -126,8 +255,20 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
126
255
|
* `constructor`), which protects both the view model and `FormGroup`.
|
|
127
256
|
*/
|
|
128
257
|
private parsePath;
|
|
258
|
+
/**
|
|
259
|
+
* Strictly parses `a.b`, `a[0]`, `a['b']` and `a["b"]`. Unbalanced brackets,
|
|
260
|
+
* empty segments and stray characters are rejected instead of being silently
|
|
261
|
+
* ignored.
|
|
262
|
+
*/
|
|
263
|
+
private parseStringPath;
|
|
129
264
|
private isSafePath;
|
|
130
265
|
private defineOwn;
|
|
266
|
+
/**
|
|
267
|
+
* Only plain objects and arrays may be traversed/written by form
|
|
268
|
+
* synchronization. This prevents writes into class instances, services and
|
|
269
|
+
* DOM objects referenced by the view model.
|
|
270
|
+
*/
|
|
271
|
+
private isMutableContainer;
|
|
131
272
|
/**
|
|
132
273
|
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
|
|
133
274
|
* Only own properties are read; prototype-chain reads are blocked.
|
|
@@ -147,7 +288,8 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
147
288
|
*/
|
|
148
289
|
private setModelPropertyValue;
|
|
149
290
|
/**
|
|
150
|
-
* Updates specified input names to new values
|
|
291
|
+
* Updates specified input names to new values and resets inputs that were
|
|
292
|
+
* removed from the configuration.
|
|
151
293
|
*/
|
|
152
294
|
private setInputs;
|
|
153
295
|
/**
|
|
@@ -157,6 +299,9 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
157
299
|
* namespaced attributes, `style`/`srcdoc`/`formaction` and unsafe URL
|
|
158
300
|
* schemes are never applied. Non-primitive values are ignored so that
|
|
159
301
|
* configuration data cannot invoke `toString()`/`toJSON()` hooks.
|
|
302
|
+
*
|
|
303
|
+
* Attributes removed from the configuration (or set to a false boolean /
|
|
304
|
+
* `null`) are removed from the element.
|
|
160
305
|
*/
|
|
161
306
|
private setAttributes;
|
|
162
307
|
private isAllowedAttribute;
|
|
@@ -166,14 +311,32 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
166
311
|
*/
|
|
167
312
|
private attributeValueToString;
|
|
168
313
|
private isSafeUrl;
|
|
314
|
+
/**
|
|
315
|
+
* Evaluates a configured attribute value and reports whether it is an
|
|
316
|
+
* expression that could not be evaluated because its context was missing.
|
|
317
|
+
*/
|
|
318
|
+
private evaluateAttribute;
|
|
319
|
+
/**
|
|
320
|
+
* True when a string expression was returned verbatim because its `$vm` or
|
|
321
|
+
* `$event` context was not available. Such values must not be treated as
|
|
322
|
+
* literal booleans/attributes/control names.
|
|
323
|
+
*/
|
|
324
|
+
private isUnresolvedExpression;
|
|
325
|
+
/**
|
|
326
|
+
* Normalizes boolean-ish configuration values. `false`, `'false'`, `0`,
|
|
327
|
+
* `'0'`, `''`, `null` and `undefined` are false; everything else is true.
|
|
328
|
+
*/
|
|
329
|
+
private toBoolean;
|
|
169
330
|
/**
|
|
170
331
|
* Evaluates an expression with a small, allowlisted interpreter. No `eval`,
|
|
171
332
|
* `Function` or other string-to-code primitive is used.
|
|
172
333
|
* @param value The expression or literal value.
|
|
173
|
-
* @param
|
|
334
|
+
* @param viewModelValue The resolved view model value.
|
|
174
335
|
* @param eventParam The event payload.
|
|
336
|
+
* @param hasEventContext Whether an event is currently being handled. This
|
|
337
|
+
* distinguishes "no event" from an output that emitted `null`/`undefined`.
|
|
175
338
|
* @returns Evaluated expression, `undefined` when the expression is rejected,
|
|
176
|
-
* or the original value when it is a literal.
|
|
339
|
+
* or the original value when it is a literal/unresolved expression.
|
|
177
340
|
*/
|
|
178
341
|
private evaluateExpression;
|
|
179
342
|
private getViewModelValue;
|
|
@@ -210,6 +373,31 @@ export declare class FGDynamicComponent implements OnChanges, OnDestroy {
|
|
|
210
373
|
"required": false;
|
|
211
374
|
"isSignal": true;
|
|
212
375
|
};
|
|
376
|
+
"expressionPolicy": {
|
|
377
|
+
"alias": "expressionPolicy";
|
|
378
|
+
"required": false;
|
|
379
|
+
"isSignal": true;
|
|
380
|
+
};
|
|
381
|
+
"formControlPolicy": {
|
|
382
|
+
"alias": "formControlPolicy";
|
|
383
|
+
"required": false;
|
|
384
|
+
"isSignal": true;
|
|
385
|
+
};
|
|
386
|
+
"typePolicy": {
|
|
387
|
+
"alias": "typePolicy";
|
|
388
|
+
"required": false;
|
|
389
|
+
"isSignal": true;
|
|
390
|
+
};
|
|
391
|
+
"attributePolicy": {
|
|
392
|
+
"alias": "attributePolicy";
|
|
393
|
+
"required": false;
|
|
394
|
+
"isSignal": true;
|
|
395
|
+
};
|
|
396
|
+
"depth": {
|
|
397
|
+
"alias": "depth";
|
|
398
|
+
"required": false;
|
|
399
|
+
"isSignal": true;
|
|
400
|
+
};
|
|
213
401
|
}, {
|
|
214
402
|
"viewModel": "viewModelChange";
|
|
215
403
|
}, never, never, true, never>;
|