@vonage/vivid 4.14.2 → 4.14.3

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.
Files changed (78) hide show
  1. package/custom-elements.json +371 -27
  2. package/index.cjs +1 -0
  3. package/index.js +1 -1
  4. package/lib/accordion/definition.d.ts +5 -1
  5. package/lib/combobox/combobox.d.ts +5 -1
  6. package/lib/file-picker/file-picker.d.ts +1 -0
  7. package/lib/menu-item/definition.d.ts +1 -1
  8. package/lib/menu-item/menu-item-role.d.ts +7 -0
  9. package/lib/menu-item/menu-item.d.ts +1 -7
  10. package/lib/number-field/number-field.d.ts +1 -1
  11. package/lib/radio/radio.d.ts +2 -2
  12. package/lib/searchable-select/option-tag.d.ts +2 -2
  13. package/lib/select/select.form-associated.d.ts +1 -1
  14. package/lib/switch/switch.d.ts +1 -0
  15. package/lib/tabs/tabs.d.ts +5 -1
  16. package/lib/text-anchor/text-anchor.d.ts +1 -1
  17. package/lib/tree-item/tree-item.d.ts +1 -1
  18. package/lib/tree-view/tree-view.d.ts +1 -1
  19. package/package.json +1 -1
  20. package/shared/applyMixinsWithObservables.cjs +1 -1
  21. package/shared/applyMixinsWithObservables.js +1 -1
  22. package/shared/definition11.cjs +1 -1
  23. package/shared/definition11.js +1 -1
  24. package/shared/definition16.cjs +63 -30
  25. package/shared/definition16.js +61 -28
  26. package/shared/definition2.cjs +9 -4
  27. package/shared/definition2.js +9 -5
  28. package/shared/definition25.cjs +25 -18
  29. package/shared/definition25.js +26 -19
  30. package/shared/definition29.cjs +12 -30
  31. package/shared/definition29.js +12 -30
  32. package/shared/definition33.cjs +1 -1
  33. package/shared/definition33.js +1 -1
  34. package/shared/definition34.cjs +4 -294
  35. package/shared/definition34.js +4 -294
  36. package/shared/definition41.cjs +1 -1
  37. package/shared/definition41.js +1 -1
  38. package/shared/definition42.cjs +1 -2
  39. package/shared/definition42.js +1 -2
  40. package/shared/definition43.cjs +1 -2
  41. package/shared/definition43.js +1 -2
  42. package/shared/definition46.cjs +2 -2
  43. package/shared/definition46.js +3 -3
  44. package/shared/definition48.cjs +22 -15
  45. package/shared/definition48.js +22 -15
  46. package/shared/definition5.cjs +3 -2
  47. package/shared/definition5.js +3 -2
  48. package/shared/definition51.cjs +11 -402
  49. package/shared/definition51.js +17 -408
  50. package/shared/definition54.js +1 -1
  51. package/shared/definition56.js +1 -1
  52. package/shared/direction.cjs +2 -16
  53. package/shared/direction.js +3 -17
  54. package/shared/foundation/utilities/direction.d.ts +2 -0
  55. package/shared/listbox.cjs +22 -3
  56. package/shared/listbox.js +21 -2
  57. package/shared/text-anchor.cjs +1 -1
  58. package/shared/text-anchor.js +1 -1
  59. package/shared/text-field2.js +1 -1
  60. package/shared/vivid-element.cjs +0 -2
  61. package/shared/vivid-element.js +1 -1
  62. package/styles/core/all.css +1 -1
  63. package/styles/core/theme.css +1 -1
  64. package/styles/core/typography.css +1 -1
  65. package/styles/tokens/theme-dark.css +4 -4
  66. package/styles/tokens/theme-light.css +4 -4
  67. package/styles/tokens/vivid-2-compat.css +1 -1
  68. package/vivid.api.json +70 -3
  69. package/shared/apply-mixins2.cjs +0 -25
  70. package/shared/apply-mixins2.js +0 -23
  71. package/shared/form-associated2.cjs +0 -383
  72. package/shared/form-associated2.js +0 -381
  73. package/shared/foundation-element.cjs +0 -1417
  74. package/shared/foundation-element.js +0 -1414
  75. package/shared/key-codes2.cjs +0 -96
  76. package/shared/key-codes2.js +0 -88
  77. package/shared/start-end.cjs +0 -52
  78. package/shared/start-end.js +0 -50
@@ -1,1414 +0,0 @@
1
- import { F as FASTElement, e as emptyArray, E as ElementStyles, o as observable } from './vivid-element.js';
2
-
3
- /*! *****************************************************************************
4
- Copyright (c) Microsoft Corporation.
5
-
6
- Permission to use, copy, modify, and/or distribute this software for any
7
- purpose with or without fee is hereby granted.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- PERFORMANCE OF THIS SOFTWARE.
16
- ***************************************************************************** */
17
- /* global Reflect, Promise */
18
-
19
-
20
- function __decorate(decorators, target, key, desc) {
21
- var c = arguments.length, r = c < 3 ? target : desc, d;
22
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
23
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
24
- return c > 3 && r && Object.defineProperty(target, key, r), r;
25
- }
26
-
27
- /**
28
- * Big thanks to https://github.com/fkleuver and the https://github.com/aurelia/aurelia project
29
- * for the bulk of this code and many of the associated tests.
30
- */
31
- // Tiny polyfill for TypeScript's Reflect metadata API.
32
- const metadataByTarget = new Map();
33
- if (!("metadata" in Reflect)) {
34
- Reflect.metadata = function (key, value) {
35
- return function (target) {
36
- Reflect.defineMetadata(key, value, target);
37
- };
38
- };
39
- Reflect.defineMetadata = function (key, value, target) {
40
- let metadata = metadataByTarget.get(target);
41
- if (metadata === void 0) {
42
- metadataByTarget.set(target, (metadata = new Map()));
43
- }
44
- metadata.set(key, value);
45
- };
46
- Reflect.getOwnMetadata = function (key, target) {
47
- const metadata = metadataByTarget.get(target);
48
- if (metadata !== void 0) {
49
- return metadata.get(key);
50
- }
51
- return void 0;
52
- };
53
- }
54
- /**
55
- * A utility class used that constructs and registers resolvers for a dependency
56
- * injection container. Supports a standard set of object lifetimes.
57
- * @public
58
- */
59
- class ResolverBuilder {
60
- /**
61
- *
62
- * @param container - The container to create resolvers for.
63
- * @param key - The key to register resolvers under.
64
- */
65
- constructor(container, key) {
66
- this.container = container;
67
- this.key = key;
68
- }
69
- /**
70
- * Creates a resolver for an existing object instance.
71
- * @param value - The instance to resolve.
72
- * @returns The resolver.
73
- */
74
- instance(value) {
75
- return this.registerResolver(0 /* instance */, value);
76
- }
77
- /**
78
- * Creates a resolver that enforces a singleton lifetime.
79
- * @param value - The type to create and cache the singleton for.
80
- * @returns The resolver.
81
- */
82
- singleton(value) {
83
- return this.registerResolver(1 /* singleton */, value);
84
- }
85
- /**
86
- * Creates a resolver that creates a new instance for every dependency request.
87
- * @param value - The type to create instances of.
88
- * @returns - The resolver.
89
- */
90
- transient(value) {
91
- return this.registerResolver(2 /* transient */, value);
92
- }
93
- /**
94
- * Creates a resolver that invokes a callback function for every dependency resolution
95
- * request, allowing custom logic to return the dependency.
96
- * @param value - The callback to call during resolution.
97
- * @returns The resolver.
98
- */
99
- callback(value) {
100
- return this.registerResolver(3 /* callback */, value);
101
- }
102
- /**
103
- * Creates a resolver that invokes a callback function the first time that a dependency
104
- * resolution is requested. The returned value is then cached and provided for all
105
- * subsequent requests.
106
- * @param value - The callback to call during the first resolution.
107
- * @returns The resolver.
108
- */
109
- cachedCallback(value) {
110
- return this.registerResolver(3 /* callback */, cacheCallbackResult(value));
111
- }
112
- /**
113
- * Aliases the current key to a different key.
114
- * @param destinationKey - The key to point the alias to.
115
- * @returns The resolver.
116
- */
117
- aliasTo(destinationKey) {
118
- return this.registerResolver(5 /* alias */, destinationKey);
119
- }
120
- registerResolver(strategy, state) {
121
- const { container, key } = this;
122
- /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
123
- this.container = this.key = (void 0);
124
- return container.registerResolver(key, new ResolverImpl(key, strategy, state));
125
- }
126
- }
127
- function cloneArrayWithPossibleProps(source) {
128
- const clone = source.slice();
129
- const keys = Object.keys(source);
130
- const len = keys.length;
131
- let key;
132
- for (let i = 0; i < len; ++i) {
133
- key = keys[i];
134
- if (!isArrayIndex(key)) {
135
- clone[key] = source[key];
136
- }
137
- }
138
- return clone;
139
- }
140
- /**
141
- * A set of default resolvers useful in configuring a container.
142
- * @public
143
- */
144
- const DefaultResolver = Object.freeze({
145
- /**
146
- * Disables auto-registration and throws for all un-registered dependencies.
147
- * @param key - The key to create the resolver for.
148
- */
149
- none(key) {
150
- throw Error(`${key.toString()} not registered, did you forget to add @singleton()?`);
151
- },
152
- /**
153
- * Provides default singleton resolution behavior during auto-registration.
154
- * @param key - The key to create the resolver for.
155
- * @returns The resolver.
156
- */
157
- singleton(key) {
158
- return new ResolverImpl(key, 1 /* singleton */, key);
159
- },
160
- /**
161
- * Provides default transient resolution behavior during auto-registration.
162
- * @param key - The key to create the resolver for.
163
- * @returns The resolver.
164
- */
165
- transient(key) {
166
- return new ResolverImpl(key, 2 /* transient */, key);
167
- },
168
- });
169
- /**
170
- * Configuration for a dependency injection container.
171
- * @public
172
- */
173
- const ContainerConfiguration = Object.freeze({
174
- /**
175
- * The default configuration used when creating a DOM-disconnected container.
176
- * @remarks
177
- * The default creates a root container, with no parent container. It does not handle
178
- * owner requests and it uses singleton resolution behavior for auto-registration.
179
- */
180
- default: Object.freeze({
181
- parentLocator: () => null,
182
- responsibleForOwnerRequests: false,
183
- defaultResolver: DefaultResolver.singleton,
184
- }),
185
- });
186
- const dependencyLookup = new Map();
187
- function getParamTypes(key) {
188
- return (Type) => {
189
- return Reflect.getOwnMetadata(key, Type);
190
- };
191
- }
192
- let rootDOMContainer = null;
193
- /**
194
- * The gateway to dependency injection APIs.
195
- * @public
196
- */
197
- const DI = Object.freeze({
198
- /**
199
- * Creates a new dependency injection container.
200
- * @param config - The configuration for the container.
201
- * @returns A newly created dependency injection container.
202
- */
203
- createContainer(config) {
204
- return new ContainerImpl(null, Object.assign({}, ContainerConfiguration.default, config));
205
- },
206
- /**
207
- * Finds the dependency injection container responsible for providing dependencies
208
- * to the specified node.
209
- * @param node - The node to find the responsible container for.
210
- * @returns The container responsible for providing dependencies to the node.
211
- * @remarks
212
- * This will be the same as the parent container if the specified node
213
- * does not itself host a container configured with responsibleForOwnerRequests.
214
- */
215
- findResponsibleContainer(node) {
216
- const owned = node.$$container$$;
217
- if (owned && owned.responsibleForOwnerRequests) {
218
- return owned;
219
- }
220
- return DI.findParentContainer(node);
221
- },
222
- /**
223
- * Find the dependency injection container up the DOM tree from this node.
224
- * @param node - The node to find the parent container for.
225
- * @returns The parent container of this node.
226
- * @remarks
227
- * This will be the same as the responsible container if the specified node
228
- * does not itself host a container configured with responsibleForOwnerRequests.
229
- */
230
- findParentContainer(node) {
231
- const event = new CustomEvent(DILocateParentEventType, {
232
- bubbles: true,
233
- composed: true,
234
- cancelable: true,
235
- detail: { container: void 0 },
236
- });
237
- node.dispatchEvent(event);
238
- return event.detail.container || DI.getOrCreateDOMContainer();
239
- },
240
- /**
241
- * Returns a dependency injection container if one is explicitly owned by the specified
242
- * node. If one is not owned, then a new container is created and assigned to the node.
243
- * @param node - The node to find or create the container for.
244
- * @param config - The configuration for the container if one needs to be created.
245
- * @returns The located or created container.
246
- * @remarks
247
- * This API does not search for a responsible or parent container. It looks only for a container
248
- * directly defined on the specified node and creates one at that location if one does not
249
- * already exist.
250
- */
251
- getOrCreateDOMContainer(node, config) {
252
- if (!node) {
253
- return (rootDOMContainer ||
254
- (rootDOMContainer = new ContainerImpl(null, Object.assign({}, ContainerConfiguration.default, config, {
255
- parentLocator: () => null,
256
- }))));
257
- }
258
- return (node.$$container$$ ||
259
- new ContainerImpl(node, Object.assign({}, ContainerConfiguration.default, config, {
260
- parentLocator: DI.findParentContainer,
261
- })));
262
- },
263
- /**
264
- * Gets the "design:paramtypes" metadata for the specified type.
265
- * @param Type - The type to get the metadata for.
266
- * @returns The metadata array or undefined if no metadata is found.
267
- */
268
- getDesignParamtypes: getParamTypes("design:paramtypes"),
269
- /**
270
- * Gets the "di:paramtypes" metadata for the specified type.
271
- * @param Type - The type to get the metadata for.
272
- * @returns The metadata array or undefined if no metadata is found.
273
- */
274
- getAnnotationParamtypes: getParamTypes("di:paramtypes"),
275
- /**
276
- *
277
- * @param Type - Gets the "di:paramtypes" metadata for the specified type. If none is found,
278
- * an empty metadata array is created and added.
279
- * @returns The metadata array.
280
- */
281
- getOrCreateAnnotationParamTypes(Type) {
282
- let annotationParamtypes = this.getAnnotationParamtypes(Type);
283
- if (annotationParamtypes === void 0) {
284
- Reflect.defineMetadata("di:paramtypes", (annotationParamtypes = []), Type);
285
- }
286
- return annotationParamtypes;
287
- },
288
- /**
289
- * Gets the dependency keys representing what is needed to instantiate the specified type.
290
- * @param Type - The type to get the dependencies for.
291
- * @returns An array of dependency keys.
292
- */
293
- getDependencies(Type) {
294
- // Note: Every detail of this getDependencies method is pretty deliberate at the moment, and probably not yet 100% tested from every possible angle,
295
- // so be careful with making changes here as it can have a huge impact on complex end user apps.
296
- // Preferably, only make changes to the dependency resolution process via a RFC.
297
- let dependencies = dependencyLookup.get(Type);
298
- if (dependencies === void 0) {
299
- // Type.length is the number of constructor parameters. If this is 0, it could mean the class has an empty constructor
300
- // but it could also mean the class has no constructor at all (in which case it inherits the constructor from the prototype).
301
- // Non-zero constructor length + no paramtypes means emitDecoratorMetadata is off, or the class has no decorator.
302
- // We're not doing anything with the above right now, but it's good to keep in mind for any future issues.
303
- const inject = Type.inject;
304
- if (inject === void 0) {
305
- // design:paramtypes is set by tsc when emitDecoratorMetadata is enabled.
306
- const designParamtypes = DI.getDesignParamtypes(Type);
307
- // di:paramtypes is set by the parameter decorator from DI.createInterface or by @inject
308
- const annotationParamtypes = DI.getAnnotationParamtypes(Type);
309
- if (designParamtypes === void 0) {
310
- if (annotationParamtypes === void 0) {
311
- // Only go up the prototype if neither static inject nor any of the paramtypes is defined, as
312
- // there is no sound way to merge a type's deps with its prototype's deps
313
- const Proto = Object.getPrototypeOf(Type);
314
- if (typeof Proto === "function" && Proto !== Function.prototype) {
315
- dependencies = cloneArrayWithPossibleProps(DI.getDependencies(Proto));
316
- }
317
- else {
318
- dependencies = [];
319
- }
320
- }
321
- else {
322
- // No design:paramtypes so just use the di:paramtypes
323
- dependencies = cloneArrayWithPossibleProps(annotationParamtypes);
324
- }
325
- }
326
- else if (annotationParamtypes === void 0) {
327
- // No di:paramtypes so just use the design:paramtypes
328
- dependencies = cloneArrayWithPossibleProps(designParamtypes);
329
- }
330
- else {
331
- // We've got both, so merge them (in case of conflict on same index, di:paramtypes take precedence)
332
- dependencies = cloneArrayWithPossibleProps(designParamtypes);
333
- let len = annotationParamtypes.length;
334
- let auAnnotationParamtype;
335
- for (let i = 0; i < len; ++i) {
336
- auAnnotationParamtype = annotationParamtypes[i];
337
- if (auAnnotationParamtype !== void 0) {
338
- dependencies[i] = auAnnotationParamtype;
339
- }
340
- }
341
- const keys = Object.keys(annotationParamtypes);
342
- len = keys.length;
343
- let key;
344
- for (let i = 0; i < len; ++i) {
345
- key = keys[i];
346
- if (!isArrayIndex(key)) {
347
- dependencies[key] = annotationParamtypes[key];
348
- }
349
- }
350
- }
351
- }
352
- else {
353
- // Ignore paramtypes if we have static inject
354
- dependencies = cloneArrayWithPossibleProps(inject);
355
- }
356
- dependencyLookup.set(Type, dependencies);
357
- }
358
- return dependencies;
359
- },
360
- /**
361
- * Defines a property on a web component class. The value of this property will
362
- * be resolved from the dependency injection container responsible for the element
363
- * instance, based on where it is connected in the DOM.
364
- * @param target - The target to define the property on.
365
- * @param propertyName - The name of the property to define.
366
- * @param key - The dependency injection key.
367
- * @param respectConnection - Indicates whether or not to update the property value if the
368
- * hosting component is disconnected and then re-connected at a different location in the DOM.
369
- * @remarks
370
- * The respectConnection option is only applicable to elements that descend from FASTElement.
371
- */
372
- defineProperty(target, propertyName, key, respectConnection = false) {
373
- const diPropertyKey = `$di_${propertyName}`;
374
- Reflect.defineProperty(target, propertyName, {
375
- get: function () {
376
- let value = this[diPropertyKey];
377
- if (value === void 0) {
378
- const container = this instanceof HTMLElement
379
- ? DI.findResponsibleContainer(this)
380
- : DI.getOrCreateDOMContainer();
381
- value = container.get(key);
382
- this[diPropertyKey] = value;
383
- if (respectConnection && this instanceof FASTElement) {
384
- const notifier = this.$fastController;
385
- const handleChange = () => {
386
- const newContainer = DI.findResponsibleContainer(this);
387
- const newValue = newContainer.get(key);
388
- const oldValue = this[diPropertyKey];
389
- if (newValue !== oldValue) {
390
- this[diPropertyKey] = value;
391
- notifier.notify(propertyName);
392
- }
393
- };
394
- notifier.subscribe({ handleChange }, "isConnected");
395
- }
396
- }
397
- return value;
398
- },
399
- });
400
- },
401
- /**
402
- * Creates a dependency injection key.
403
- * @param nameConfigOrCallback - A friendly name for the key or a lambda that configures a
404
- * default resolution for the dependency.
405
- * @param configuror - If a friendly name was provided for the first parameter, then an optional
406
- * lambda that configures a default resolution for the dependency can be provided second.
407
- * @returns The created key.
408
- * @remarks
409
- * The created key can be used as a property decorator or constructor parameter decorator,
410
- * in addition to its standard use in an inject array or through direct container APIs.
411
- */
412
- createInterface(nameConfigOrCallback, configuror) {
413
- const configure = typeof nameConfigOrCallback === "function"
414
- ? nameConfigOrCallback
415
- : configuror;
416
- const friendlyName = typeof nameConfigOrCallback === "string"
417
- ? nameConfigOrCallback
418
- : nameConfigOrCallback && "friendlyName" in nameConfigOrCallback
419
- ? nameConfigOrCallback.friendlyName || defaultFriendlyName
420
- : defaultFriendlyName;
421
- const respectConnection = typeof nameConfigOrCallback === "string"
422
- ? false
423
- : nameConfigOrCallback && "respectConnection" in nameConfigOrCallback
424
- ? nameConfigOrCallback.respectConnection || false
425
- : false;
426
- const Interface = function (target, property, index) {
427
- if (target == null || new.target !== undefined) {
428
- throw new Error(`No registration for interface: '${Interface.friendlyName}'`);
429
- }
430
- if (property) {
431
- DI.defineProperty(target, property, Interface, respectConnection);
432
- }
433
- else {
434
- const annotationParamtypes = DI.getOrCreateAnnotationParamTypes(target);
435
- annotationParamtypes[index] = Interface;
436
- }
437
- };
438
- Interface.$isInterface = true;
439
- Interface.friendlyName = friendlyName == null ? "(anonymous)" : friendlyName;
440
- if (configure != null) {
441
- Interface.register = function (container, key) {
442
- return configure(new ResolverBuilder(container, key !== null && key !== void 0 ? key : Interface));
443
- };
444
- }
445
- Interface.toString = function toString() {
446
- return `InterfaceSymbol<${Interface.friendlyName}>`;
447
- };
448
- return Interface;
449
- },
450
- /**
451
- * A decorator that specifies what to inject into its target.
452
- * @param dependencies - The dependencies to inject.
453
- * @returns The decorator to be applied to the target class.
454
- * @remarks
455
- * The decorator can be used to decorate a class, listing all of the classes dependencies.
456
- * Or it can be used to decorate a constructor paramter, indicating what to inject for that
457
- * parameter.
458
- * Or it can be used for a web component property, indicating what that property should resolve to.
459
- */
460
- inject(...dependencies) {
461
- return function (target, key, descriptor) {
462
- if (typeof descriptor === "number") {
463
- // It's a parameter decorator.
464
- const annotationParamtypes = DI.getOrCreateAnnotationParamTypes(target);
465
- const dep = dependencies[0];
466
- if (dep !== void 0) {
467
- annotationParamtypes[descriptor] = dep;
468
- }
469
- }
470
- else if (key) {
471
- DI.defineProperty(target, key, dependencies[0]);
472
- }
473
- else {
474
- const annotationParamtypes = descriptor
475
- ? DI.getOrCreateAnnotationParamTypes(descriptor.value)
476
- : DI.getOrCreateAnnotationParamTypes(target);
477
- let dep;
478
- for (let i = 0; i < dependencies.length; ++i) {
479
- dep = dependencies[i];
480
- if (dep !== void 0) {
481
- annotationParamtypes[i] = dep;
482
- }
483
- }
484
- }
485
- };
486
- },
487
- /**
488
- * Registers the `target` class as a transient dependency; each time the dependency is resolved
489
- * a new instance will be created.
490
- *
491
- * @param target - The class / constructor function to register as transient.
492
- * @returns The same class, with a static `register` method that takes a container and returns the appropriate resolver.
493
- *
494
- * @example
495
- * On an existing class
496
- * ```ts
497
- * class Foo { }
498
- * DI.transient(Foo);
499
- * ```
500
- *
501
- * @example
502
- * Inline declaration
503
- *
504
- * ```ts
505
- * const Foo = DI.transient(class { });
506
- * // Foo is now strongly typed with register
507
- * Foo.register(container);
508
- * ```
509
- *
510
- * @public
511
- */
512
- transient(target) {
513
- target.register = function register(container) {
514
- const registration = Registration.transient(target, target);
515
- return registration.register(container);
516
- };
517
- target.registerInRequestor = false;
518
- return target;
519
- },
520
- /**
521
- * Registers the `target` class as a singleton dependency; the class will only be created once. Each
522
- * consecutive time the dependency is resolved, the same instance will be returned.
523
- *
524
- * @param target - The class / constructor function to register as a singleton.
525
- * @returns The same class, with a static `register` method that takes a container and returns the appropriate resolver.
526
- * @example
527
- * On an existing class
528
- * ```ts
529
- * class Foo { }
530
- * DI.singleton(Foo);
531
- * ```
532
- *
533
- * @example
534
- * Inline declaration
535
- * ```ts
536
- * const Foo = DI.singleton(class { });
537
- * // Foo is now strongly typed with register
538
- * Foo.register(container);
539
- * ```
540
- *
541
- * @public
542
- */
543
- singleton(target, options = defaultSingletonOptions) {
544
- target.register = function register(container) {
545
- const registration = Registration.singleton(target, target);
546
- return registration.register(container);
547
- };
548
- target.registerInRequestor = options.scoped;
549
- return target;
550
- },
551
- });
552
- /**
553
- * The interface key that resolves the dependency injection container itself.
554
- * @public
555
- */
556
- const Container = DI.createInterface("Container");
557
- /**
558
- * A decorator that specifies what to inject into its target.
559
- * @param dependencies - The dependencies to inject.
560
- * @returns The decorator to be applied to the target class.
561
- * @remarks
562
- * The decorator can be used to decorate a class, listing all of the classes dependencies.
563
- * Or it can be used to decorate a constructor paramter, indicating what to inject for that
564
- * parameter.
565
- * Or it can be used for a web component property, indicating what that property should resolve to.
566
- *
567
- * @public
568
- */
569
- DI.inject;
570
- const defaultSingletonOptions = { scoped: false };
571
- /** @internal */
572
- class ResolverImpl {
573
- constructor(key, strategy, state) {
574
- this.key = key;
575
- this.strategy = strategy;
576
- this.state = state;
577
- this.resolving = false;
578
- }
579
- get $isResolver() {
580
- return true;
581
- }
582
- register(container) {
583
- return container.registerResolver(this.key, this);
584
- }
585
- resolve(handler, requestor) {
586
- switch (this.strategy) {
587
- case 0 /* instance */:
588
- return this.state;
589
- case 1 /* singleton */: {
590
- if (this.resolving) {
591
- throw new Error(`Cyclic dependency found: ${this.state.name}`);
592
- }
593
- this.resolving = true;
594
- this.state = handler
595
- .getFactory(this.state)
596
- .construct(requestor);
597
- this.strategy = 0 /* instance */;
598
- this.resolving = false;
599
- return this.state;
600
- }
601
- case 2 /* transient */: {
602
- // Always create transients from the requesting container
603
- const factory = handler.getFactory(this.state);
604
- if (factory === null) {
605
- throw new Error(`Resolver for ${String(this.key)} returned a null factory`);
606
- }
607
- return factory.construct(requestor);
608
- }
609
- case 3 /* callback */:
610
- return this.state(handler, requestor, this);
611
- case 4 /* array */:
612
- return this.state[0].resolve(handler, requestor);
613
- case 5 /* alias */:
614
- return requestor.get(this.state);
615
- default:
616
- throw new Error(`Invalid resolver strategy specified: ${this.strategy}.`);
617
- }
618
- }
619
- getFactory(container) {
620
- var _a, _b, _c;
621
- switch (this.strategy) {
622
- case 1 /* singleton */:
623
- case 2 /* transient */:
624
- return container.getFactory(this.state);
625
- case 5 /* alias */:
626
- return (_c = (_b = (_a = container.getResolver(this.state)) === null || _a === void 0 ? void 0 : _a.getFactory) === null || _b === void 0 ? void 0 : _b.call(_a, container)) !== null && _c !== void 0 ? _c : null;
627
- default:
628
- return null;
629
- }
630
- }
631
- }
632
- function containerGetKey(d) {
633
- return this.get(d);
634
- }
635
- function transformInstance(inst, transform) {
636
- return transform(inst);
637
- }
638
- /** @internal */
639
- class FactoryImpl {
640
- constructor(Type, dependencies) {
641
- this.Type = Type;
642
- this.dependencies = dependencies;
643
- this.transformers = null;
644
- }
645
- construct(container, dynamicDependencies) {
646
- let instance;
647
- if (dynamicDependencies === void 0) {
648
- instance = new this.Type(...this.dependencies.map(containerGetKey, container));
649
- }
650
- else {
651
- instance = new this.Type(...this.dependencies.map(containerGetKey, container), ...dynamicDependencies);
652
- }
653
- if (this.transformers == null) {
654
- return instance;
655
- }
656
- return this.transformers.reduce(transformInstance, instance);
657
- }
658
- registerTransformer(transformer) {
659
- (this.transformers || (this.transformers = [])).push(transformer);
660
- }
661
- }
662
- const containerResolver = {
663
- $isResolver: true,
664
- resolve(handler, requestor) {
665
- return requestor;
666
- },
667
- };
668
- function isRegistry(obj) {
669
- return typeof obj.register === "function";
670
- }
671
- function isSelfRegistry(obj) {
672
- return isRegistry(obj) && typeof obj.registerInRequestor === "boolean";
673
- }
674
- function isRegisterInRequester(obj) {
675
- return isSelfRegistry(obj) && obj.registerInRequestor;
676
- }
677
- function isClass(obj) {
678
- return obj.prototype !== void 0;
679
- }
680
- const InstrinsicTypeNames = new Set([
681
- "Array",
682
- "ArrayBuffer",
683
- "Boolean",
684
- "DataView",
685
- "Date",
686
- "Error",
687
- "EvalError",
688
- "Float32Array",
689
- "Float64Array",
690
- "Function",
691
- "Int8Array",
692
- "Int16Array",
693
- "Int32Array",
694
- "Map",
695
- "Number",
696
- "Object",
697
- "Promise",
698
- "RangeError",
699
- "ReferenceError",
700
- "RegExp",
701
- "Set",
702
- "SharedArrayBuffer",
703
- "String",
704
- "SyntaxError",
705
- "TypeError",
706
- "Uint8Array",
707
- "Uint8ClampedArray",
708
- "Uint16Array",
709
- "Uint32Array",
710
- "URIError",
711
- "WeakMap",
712
- "WeakSet",
713
- ]);
714
- const DILocateParentEventType = "__DI_LOCATE_PARENT__";
715
- const factories = new Map();
716
- /**
717
- * @internal
718
- */
719
- class ContainerImpl {
720
- constructor(owner, config) {
721
- this.owner = owner;
722
- this.config = config;
723
- this._parent = void 0;
724
- this.registerDepth = 0;
725
- this.context = null;
726
- if (owner !== null) {
727
- owner.$$container$$ = this;
728
- }
729
- this.resolvers = new Map();
730
- this.resolvers.set(Container, containerResolver);
731
- if (owner instanceof Node) {
732
- owner.addEventListener(DILocateParentEventType, (e) => {
733
- if (e.composedPath()[0] !== this.owner) {
734
- e.detail.container = this;
735
- e.stopImmediatePropagation();
736
- }
737
- });
738
- }
739
- }
740
- get parent() {
741
- if (this._parent === void 0) {
742
- this._parent = this.config.parentLocator(this.owner);
743
- }
744
- return this._parent;
745
- }
746
- get depth() {
747
- return this.parent === null ? 0 : this.parent.depth + 1;
748
- }
749
- get responsibleForOwnerRequests() {
750
- return this.config.responsibleForOwnerRequests;
751
- }
752
- registerWithContext(context, ...params) {
753
- this.context = context;
754
- this.register(...params);
755
- this.context = null;
756
- return this;
757
- }
758
- register(...params) {
759
- if (++this.registerDepth === 100) {
760
- throw new Error("Unable to autoregister dependency");
761
- // Most likely cause is trying to register a plain object that does not have a
762
- // register method and is not a class constructor
763
- }
764
- let current;
765
- let keys;
766
- let value;
767
- let j;
768
- let jj;
769
- const context = this.context;
770
- for (let i = 0, ii = params.length; i < ii; ++i) {
771
- current = params[i];
772
- if (!isObject(current)) {
773
- continue;
774
- }
775
- if (isRegistry(current)) {
776
- current.register(this, context);
777
- }
778
- else if (isClass(current)) {
779
- Registration.singleton(current, current).register(this);
780
- }
781
- else {
782
- keys = Object.keys(current);
783
- j = 0;
784
- jj = keys.length;
785
- for (; j < jj; ++j) {
786
- value = current[keys[j]];
787
- if (!isObject(value)) {
788
- continue;
789
- }
790
- // note: we could remove this if-branch and call this.register directly
791
- // - the extra check is just a perf tweak to create fewer unnecessary arrays by the spread operator
792
- if (isRegistry(value)) {
793
- value.register(this, context);
794
- }
795
- else {
796
- this.register(value);
797
- }
798
- }
799
- }
800
- }
801
- --this.registerDepth;
802
- return this;
803
- }
804
- registerResolver(key, resolver) {
805
- validateKey(key);
806
- const resolvers = this.resolvers;
807
- const result = resolvers.get(key);
808
- if (result == null) {
809
- resolvers.set(key, resolver);
810
- }
811
- else if (result instanceof ResolverImpl &&
812
- result.strategy === 4 /* array */) {
813
- result.state.push(resolver);
814
- }
815
- else {
816
- resolvers.set(key, new ResolverImpl(key, 4 /* array */, [result, resolver]));
817
- }
818
- return resolver;
819
- }
820
- registerTransformer(key, transformer) {
821
- const resolver = this.getResolver(key);
822
- if (resolver == null) {
823
- return false;
824
- }
825
- if (resolver.getFactory) {
826
- const factory = resolver.getFactory(this);
827
- if (factory == null) {
828
- return false;
829
- }
830
- // This type cast is a bit of a hacky one, necessary due to the duplicity of IResolverLike.
831
- // Problem is that that interface's type arg can be of type Key, but the getFactory method only works on
832
- // type Constructable. So the return type of that optional method has this additional constraint, which
833
- // seems to confuse the type checker.
834
- factory.registerTransformer(transformer);
835
- return true;
836
- }
837
- return false;
838
- }
839
- getResolver(key, autoRegister = true) {
840
- validateKey(key);
841
- if (key.resolve !== void 0) {
842
- return key;
843
- }
844
- /* eslint-disable-next-line @typescript-eslint/no-this-alias */
845
- let current = this;
846
- let resolver;
847
- while (current != null) {
848
- resolver = current.resolvers.get(key);
849
- if (resolver == null) {
850
- if (current.parent == null) {
851
- const handler = isRegisterInRequester(key)
852
- ? this
853
- : current;
854
- return autoRegister ? this.jitRegister(key, handler) : null;
855
- }
856
- current = current.parent;
857
- }
858
- else {
859
- return resolver;
860
- }
861
- }
862
- return null;
863
- }
864
- has(key, searchAncestors = false) {
865
- return this.resolvers.has(key)
866
- ? true
867
- : searchAncestors && this.parent != null
868
- ? this.parent.has(key, true)
869
- : false;
870
- }
871
- get(key) {
872
- validateKey(key);
873
- if (key.$isResolver) {
874
- return key.resolve(this, this);
875
- }
876
- /* eslint-disable-next-line @typescript-eslint/no-this-alias */
877
- let current = this;
878
- let resolver;
879
- while (current != null) {
880
- resolver = current.resolvers.get(key);
881
- if (resolver == null) {
882
- if (current.parent == null) {
883
- const handler = isRegisterInRequester(key)
884
- ? this
885
- : current;
886
- resolver = this.jitRegister(key, handler);
887
- return resolver.resolve(current, this);
888
- }
889
- current = current.parent;
890
- }
891
- else {
892
- return resolver.resolve(current, this);
893
- }
894
- }
895
- throw new Error(`Unable to resolve key: ${String(key)}`);
896
- }
897
- getAll(key, searchAncestors = false) {
898
- validateKey(key);
899
- /* eslint-disable-next-line @typescript-eslint/no-this-alias */
900
- const requestor = this;
901
- let current = requestor;
902
- let resolver;
903
- if (searchAncestors) {
904
- let resolutions = emptyArray;
905
- while (current != null) {
906
- resolver = current.resolvers.get(key);
907
- if (resolver != null) {
908
- resolutions = resolutions.concat(
909
- /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
910
- buildAllResponse(resolver, current, requestor));
911
- }
912
- current = current.parent;
913
- }
914
- return resolutions;
915
- }
916
- else {
917
- while (current != null) {
918
- resolver = current.resolvers.get(key);
919
- if (resolver == null) {
920
- current = current.parent;
921
- if (current == null) {
922
- return emptyArray;
923
- }
924
- }
925
- else {
926
- return buildAllResponse(resolver, current, requestor);
927
- }
928
- }
929
- }
930
- return emptyArray;
931
- }
932
- getFactory(Type) {
933
- let factory = factories.get(Type);
934
- if (factory === void 0) {
935
- if (isNativeFunction(Type)) {
936
- throw new Error(`${Type.name} is a native function and therefore cannot be safely constructed by DI. If this is intentional, please use a callback or cachedCallback resolver.`);
937
- }
938
- factories.set(Type, (factory = new FactoryImpl(Type, DI.getDependencies(Type))));
939
- }
940
- return factory;
941
- }
942
- registerFactory(key, factory) {
943
- factories.set(key, factory);
944
- }
945
- createChild(config) {
946
- return new ContainerImpl(null, Object.assign({}, this.config, config, { parentLocator: () => this }));
947
- }
948
- jitRegister(keyAsValue, handler) {
949
- if (typeof keyAsValue !== "function") {
950
- throw new Error(`Attempted to jitRegister something that is not a constructor: '${keyAsValue}'. Did you forget to register this dependency?`);
951
- }
952
- if (InstrinsicTypeNames.has(keyAsValue.name)) {
953
- throw new Error(`Attempted to jitRegister an intrinsic type: ${keyAsValue.name}. Did you forget to add @inject(Key)`);
954
- }
955
- if (isRegistry(keyAsValue)) {
956
- const registrationResolver = keyAsValue.register(handler);
957
- if (!(registrationResolver instanceof Object) ||
958
- registrationResolver.resolve == null) {
959
- const newResolver = handler.resolvers.get(keyAsValue);
960
- if (newResolver != void 0) {
961
- return newResolver;
962
- }
963
- throw new Error("A valid resolver was not returned from the static register method");
964
- }
965
- return registrationResolver;
966
- }
967
- else if (keyAsValue.$isInterface) {
968
- throw new Error(`Attempted to jitRegister an interface: ${keyAsValue.friendlyName}`);
969
- }
970
- else {
971
- const resolver = this.config.defaultResolver(keyAsValue, handler);
972
- handler.resolvers.set(keyAsValue, resolver);
973
- return resolver;
974
- }
975
- }
976
- }
977
- const cache = new WeakMap();
978
- function cacheCallbackResult(fun) {
979
- return function (handler, requestor, resolver) {
980
- if (cache.has(resolver)) {
981
- return cache.get(resolver);
982
- }
983
- const t = fun(handler, requestor, resolver);
984
- cache.set(resolver, t);
985
- return t;
986
- };
987
- }
988
- /**
989
- * You can use the resulting Registration of any of the factory methods
990
- * to register with the container.
991
- *
992
- * @example
993
- * ```
994
- * class Foo {}
995
- * const container = DI.createContainer();
996
- * container.register(Registration.instance(Foo, new Foo()));
997
- * container.get(Foo);
998
- * ```
999
- *
1000
- * @public
1001
- */
1002
- const Registration = Object.freeze({
1003
- /**
1004
- * Allows you to pass an instance.
1005
- * Every time you request this {@link Key} you will get this instance back.
1006
- *
1007
- * @example
1008
- * ```
1009
- * Registration.instance(Foo, new Foo()));
1010
- * ```
1011
- *
1012
- * @param key - The key to register the instance under.
1013
- * @param value - The instance to return when the key is requested.
1014
- */
1015
- instance(key, value) {
1016
- return new ResolverImpl(key, 0 /* instance */, value);
1017
- },
1018
- /**
1019
- * Creates an instance from the class.
1020
- * Every time you request this {@link Key} you will get the same one back.
1021
- *
1022
- * @example
1023
- * ```
1024
- * Registration.singleton(Foo, Foo);
1025
- * ```
1026
- *
1027
- * @param key - The key to register the singleton under.
1028
- * @param value - The class to instantiate as a singleton when first requested.
1029
- */
1030
- singleton(key, value) {
1031
- return new ResolverImpl(key, 1 /* singleton */, value);
1032
- },
1033
- /**
1034
- * Creates an instance from a class.
1035
- * Every time you request this {@link Key} you will get a new instance.
1036
- *
1037
- * @example
1038
- * ```
1039
- * Registration.instance(Foo, Foo);
1040
- * ```
1041
- *
1042
- * @param key - The key to register the instance type under.
1043
- * @param value - The class to instantiate each time the key is requested.
1044
- */
1045
- transient(key, value) {
1046
- return new ResolverImpl(key, 2 /* transient */, value);
1047
- },
1048
- /**
1049
- * Delegates to a callback function to provide the dependency.
1050
- * Every time you request this {@link Key} the callback will be invoked to provide
1051
- * the dependency.
1052
- *
1053
- * @example
1054
- * ```
1055
- * Registration.callback(Foo, () => new Foo());
1056
- * Registration.callback(Bar, (c: Container) => new Bar(c.get(Foo)));
1057
- * ```
1058
- *
1059
- * @param key - The key to register the callback for.
1060
- * @param callback - The function that is expected to return the dependency.
1061
- */
1062
- callback(key, callback) {
1063
- return new ResolverImpl(key, 3 /* callback */, callback);
1064
- },
1065
- /**
1066
- * Delegates to a callback function to provide the dependency and then caches the
1067
- * dependency for future requests.
1068
- *
1069
- * @example
1070
- * ```
1071
- * Registration.cachedCallback(Foo, () => new Foo());
1072
- * Registration.cachedCallback(Bar, (c: Container) => new Bar(c.get(Foo)));
1073
- * ```
1074
- *
1075
- * @param key - The key to register the callback for.
1076
- * @param callback - The function that is expected to return the dependency.
1077
- * @remarks
1078
- * If you pass the same Registration to another container, the same cached value will be used.
1079
- * Should all references to the resolver returned be removed, the cache will expire.
1080
- */
1081
- cachedCallback(key, callback) {
1082
- return new ResolverImpl(key, 3 /* callback */, cacheCallbackResult(callback));
1083
- },
1084
- /**
1085
- * Creates an alternate {@link Key} to retrieve an instance by.
1086
- *
1087
- * @example
1088
- * ```
1089
- * Register.singleton(Foo, Foo)
1090
- * Register.aliasTo(Foo, MyFoos);
1091
- *
1092
- * container.getAll(MyFoos) // contains an instance of Foo
1093
- * ```
1094
- *
1095
- * @param originalKey - The original key that has been registered.
1096
- * @param aliasKey - The alias to the original key.
1097
- */
1098
- aliasTo(originalKey, aliasKey) {
1099
- return new ResolverImpl(aliasKey, 5 /* alias */, originalKey);
1100
- },
1101
- });
1102
- /** @internal */
1103
- function validateKey(key) {
1104
- if (key === null || key === void 0) {
1105
- throw new Error("key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?");
1106
- }
1107
- }
1108
- function buildAllResponse(resolver, handler, requestor) {
1109
- if (resolver instanceof ResolverImpl &&
1110
- resolver.strategy === 4 /* array */) {
1111
- const state = resolver.state;
1112
- let i = state.length;
1113
- const results = new Array(i);
1114
- while (i--) {
1115
- results[i] = state[i].resolve(handler, requestor);
1116
- }
1117
- return results;
1118
- }
1119
- return [resolver.resolve(handler, requestor)];
1120
- }
1121
- const defaultFriendlyName = "(anonymous)";
1122
- function isObject(value) {
1123
- return (typeof value === "object" && value !== null) || typeof value === "function";
1124
- }
1125
- /**
1126
- * Determine whether the value is a native function.
1127
- *
1128
- * @param fn - The function to check.
1129
- * @returns `true` is the function is a native function, otherwise `false`
1130
- */
1131
- const isNativeFunction = (function () {
1132
- const lookup = new WeakMap();
1133
- let isNative = false;
1134
- let sourceText = "";
1135
- let i = 0;
1136
- return function (fn) {
1137
- isNative = lookup.get(fn);
1138
- if (isNative === void 0) {
1139
- sourceText = fn.toString();
1140
- i = sourceText.length;
1141
- // http://www.ecma-international.org/ecma-262/#prod-NativeFunction
1142
- isNative =
1143
- // 29 is the length of 'function () { [native code] }' which is the smallest length of a native function string
1144
- i >= 29 &&
1145
- // 100 seems to be a safe upper bound of the max length of a native function. In Chrome and FF it's 56, in Edge it's 61.
1146
- i <= 100 &&
1147
- // This whole heuristic *could* be tricked by a comment. Do we need to care about that?
1148
- sourceText.charCodeAt(i - 1) === 0x7d && // }
1149
- // TODO: the spec is a little vague about the precise constraints, so we do need to test this across various browsers to make sure just one whitespace is a safe assumption.
1150
- sourceText.charCodeAt(i - 2) <= 0x20 && // whitespace
1151
- sourceText.charCodeAt(i - 3) === 0x5d && // ]
1152
- sourceText.charCodeAt(i - 4) === 0x65 && // e
1153
- sourceText.charCodeAt(i - 5) === 0x64 && // d
1154
- sourceText.charCodeAt(i - 6) === 0x6f && // o
1155
- sourceText.charCodeAt(i - 7) === 0x63 && // c
1156
- sourceText.charCodeAt(i - 8) === 0x20 && //
1157
- sourceText.charCodeAt(i - 9) === 0x65 && // e
1158
- sourceText.charCodeAt(i - 10) === 0x76 && // v
1159
- sourceText.charCodeAt(i - 11) === 0x69 && // i
1160
- sourceText.charCodeAt(i - 12) === 0x74 && // t
1161
- sourceText.charCodeAt(i - 13) === 0x61 && // a
1162
- sourceText.charCodeAt(i - 14) === 0x6e && // n
1163
- sourceText.charCodeAt(i - 15) === 0x58; // [
1164
- lookup.set(fn, isNative);
1165
- }
1166
- return isNative;
1167
- };
1168
- })();
1169
- const isNumericLookup = {};
1170
- function isArrayIndex(value) {
1171
- switch (typeof value) {
1172
- case "number":
1173
- return value >= 0 && (value | 0) === value;
1174
- case "string": {
1175
- const result = isNumericLookup[value];
1176
- if (result !== void 0) {
1177
- return result;
1178
- }
1179
- const length = value.length;
1180
- if (length === 0) {
1181
- return (isNumericLookup[value] = false);
1182
- }
1183
- let ch = 0;
1184
- for (let i = 0; i < length; ++i) {
1185
- ch = value.charCodeAt(i);
1186
- if ((i === 0 && ch === 0x30 && length > 1) /* must not start with 0 */ ||
1187
- ch < 0x30 /* 0 */ ||
1188
- ch > 0x39 /* 9 */) {
1189
- return (isNumericLookup[value] = false);
1190
- }
1191
- }
1192
- return (isNumericLookup[value] = true);
1193
- }
1194
- default:
1195
- return false;
1196
- }
1197
- }
1198
-
1199
- function presentationKeyFromTag(tagName) {
1200
- return `${tagName.toLowerCase()}:presentation`;
1201
- }
1202
- const presentationRegistry = new Map();
1203
- /**
1204
- * An API gateway to component presentation features.
1205
- * @public
1206
- */
1207
- const ComponentPresentation = Object.freeze({
1208
- /**
1209
- * Defines a component presentation for an element.
1210
- * @param tagName - The element name to define the presentation for.
1211
- * @param presentation - The presentation that will be applied to matching elements.
1212
- * @param container - The dependency injection container to register the configuration in.
1213
- * @public
1214
- */
1215
- define(tagName, presentation, container) {
1216
- const key = presentationKeyFromTag(tagName);
1217
- const existing = presentationRegistry.get(key);
1218
- if (existing === void 0) {
1219
- presentationRegistry.set(key, presentation);
1220
- }
1221
- else {
1222
- // false indicates that we have more than one presentation
1223
- // registered for a tagName and we must resolve through DI
1224
- presentationRegistry.set(key, false);
1225
- }
1226
- container.register(Registration.instance(key, presentation));
1227
- },
1228
- /**
1229
- * Finds a component presentation for the specified element name,
1230
- * searching the DOM hierarchy starting from the provided element.
1231
- * @param tagName - The name of the element to locate the presentation for.
1232
- * @param element - The element to begin the search from.
1233
- * @returns The component presentation or null if none is found.
1234
- * @public
1235
- */
1236
- forTag(tagName, element) {
1237
- const key = presentationKeyFromTag(tagName);
1238
- const existing = presentationRegistry.get(key);
1239
- if (existing === false) {
1240
- const container = DI.findResponsibleContainer(element);
1241
- return container.get(key);
1242
- }
1243
- return existing || null;
1244
- },
1245
- });
1246
- /**
1247
- * The default implementation of ComponentPresentation, used by FoundationElement.
1248
- * @public
1249
- */
1250
- class DefaultComponentPresentation {
1251
- /**
1252
- * Creates an instance of DefaultComponentPresentation.
1253
- * @param template - The template to apply to the element.
1254
- * @param styles - The styles to apply to the element.
1255
- * @public
1256
- */
1257
- constructor(template, styles) {
1258
- this.template = template || null;
1259
- this.styles =
1260
- styles === void 0
1261
- ? null
1262
- : Array.isArray(styles)
1263
- ? ElementStyles.create(styles)
1264
- : styles instanceof ElementStyles
1265
- ? styles
1266
- : ElementStyles.create([styles]);
1267
- }
1268
- /**
1269
- * Applies the presentation details to the specified element.
1270
- * @param element - The element to apply the presentation details to.
1271
- * @public
1272
- */
1273
- applyTo(element) {
1274
- const controller = element.$fastController;
1275
- if (controller.template === null) {
1276
- controller.template = this.template;
1277
- }
1278
- if (controller.styles === null) {
1279
- controller.styles = this.styles;
1280
- }
1281
- }
1282
- }
1283
-
1284
- /**
1285
- * Defines a foundation element class that:
1286
- * 1. Connects the element to its ComponentPresentation
1287
- * 2. Allows resolving the element template from the instance or ComponentPresentation
1288
- * 3. Allows resolving the element styles from the instance or ComponentPresentation
1289
- *
1290
- * @public
1291
- */
1292
- class FoundationElement extends FASTElement {
1293
- constructor() {
1294
- super(...arguments);
1295
- this._presentation = void 0;
1296
- }
1297
- /**
1298
- * A property which resolves the ComponentPresentation instance
1299
- * for the current component.
1300
- * @public
1301
- */
1302
- get $presentation() {
1303
- if (this._presentation === void 0) {
1304
- this._presentation = ComponentPresentation.forTag(this.tagName, this);
1305
- }
1306
- return this._presentation;
1307
- }
1308
- templateChanged() {
1309
- if (this.template !== undefined) {
1310
- this.$fastController.template = this.template;
1311
- }
1312
- }
1313
- stylesChanged() {
1314
- if (this.styles !== undefined) {
1315
- this.$fastController.styles = this.styles;
1316
- }
1317
- }
1318
- /**
1319
- * The connected callback for this FASTElement.
1320
- * @remarks
1321
- * This method is invoked by the platform whenever this FoundationElement
1322
- * becomes connected to the document.
1323
- * @public
1324
- */
1325
- connectedCallback() {
1326
- if (this.$presentation !== null) {
1327
- this.$presentation.applyTo(this);
1328
- }
1329
- super.connectedCallback();
1330
- }
1331
- /**
1332
- * Defines an element registry function with a set of element definition defaults.
1333
- * @param elementDefinition - The definition of the element to create the registry
1334
- * function for.
1335
- * @public
1336
- */
1337
- static compose(elementDefinition) {
1338
- return (overrideDefinition = {}) => new FoundationElementRegistry(this === FoundationElement
1339
- ? class extends FoundationElement {
1340
- }
1341
- : this, elementDefinition, overrideDefinition);
1342
- }
1343
- }
1344
- __decorate([
1345
- observable
1346
- ], FoundationElement.prototype, "template", void 0);
1347
- __decorate([
1348
- observable
1349
- ], FoundationElement.prototype, "styles", void 0);
1350
- function resolveOption(option, context, definition) {
1351
- if (typeof option === "function") {
1352
- return option(context, definition);
1353
- }
1354
- return option;
1355
- }
1356
- /**
1357
- * Registry capable of defining presentation properties for a DOM Container hierarchy.
1358
- *
1359
- * @internal
1360
- */
1361
- /* eslint-disable @typescript-eslint/no-unused-vars */
1362
- class FoundationElementRegistry {
1363
- constructor(type, elementDefinition, overrideDefinition) {
1364
- this.type = type;
1365
- this.elementDefinition = elementDefinition;
1366
- this.overrideDefinition = overrideDefinition;
1367
- this.definition = Object.assign(Object.assign({}, this.elementDefinition), this.overrideDefinition);
1368
- }
1369
- register(container, context) {
1370
- const definition = this.definition;
1371
- const overrideDefinition = this.overrideDefinition;
1372
- const prefix = definition.prefix || context.elementPrefix;
1373
- const name = `${prefix}-${definition.baseName}`;
1374
- context.tryDefineElement({
1375
- name,
1376
- type: this.type,
1377
- baseClass: this.elementDefinition.baseClass,
1378
- callback: x => {
1379
- const presentation = new DefaultComponentPresentation(resolveOption(definition.template, x, definition), resolveOption(definition.styles, x, definition));
1380
- x.definePresentation(presentation);
1381
- let shadowOptions = resolveOption(definition.shadowOptions, x, definition);
1382
- if (x.shadowRootMode) {
1383
- // If the design system has overridden the shadow root mode, we need special handling.
1384
- if (shadowOptions) {
1385
- // If there are shadow options present in the definition, then
1386
- // either the component itself has specified an option or the
1387
- // registry function has overridden it.
1388
- if (!overrideDefinition.shadowOptions) {
1389
- // There were shadow options provided by the component and not overridden by
1390
- // the registry.
1391
- shadowOptions.mode = x.shadowRootMode;
1392
- }
1393
- }
1394
- else if (shadowOptions !== null) {
1395
- // If the component author did not provide shadow options,
1396
- // and did not null them out (light dom opt-in) then they
1397
- // were relying on the FASTElement default. So, if the
1398
- // design system provides a mode, we need to create the options
1399
- // to override the default.
1400
- shadowOptions = { mode: x.shadowRootMode };
1401
- }
1402
- }
1403
- x.defineElement({
1404
- elementOptions: resolveOption(definition.elementOptions, x, definition),
1405
- shadowOptions,
1406
- attributes: resolveOption(definition.attributes, x, definition),
1407
- });
1408
- },
1409
- });
1410
- }
1411
- }
1412
- /* eslint-enable @typescript-eslint/no-unused-vars */
1413
-
1414
- export { FoundationElement as F, __decorate as _ };