@vonage/vivid 4.14.1 → 4.14.2

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