@microsoft/fast-element 1.10.5 → 2.0.0-beta.10

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 (122) hide show
  1. package/.eslintrc.json +1 -12
  2. package/CHANGELOG.json +629 -6
  3. package/CHANGELOG.md +152 -5
  4. package/dist/dts/components/attributes.d.ts +14 -1
  5. package/dist/dts/components/{controller.d.ts → element-controller.d.ts} +32 -32
  6. package/dist/dts/components/fast-definitions.d.ts +51 -11
  7. package/dist/dts/components/fast-element.d.ts +18 -23
  8. package/dist/dts/context.d.ts +157 -0
  9. package/dist/{esm/observation/behavior.js → dts/debug.d.ts} +0 -0
  10. package/dist/dts/di/di.d.ts +899 -0
  11. package/dist/dts/index.d.ts +17 -16
  12. package/dist/dts/index.debug.d.ts +2 -0
  13. package/dist/dts/index.rollup.d.ts +2 -0
  14. package/dist/dts/index.rollup.debug.d.ts +3 -0
  15. package/dist/dts/interfaces.d.ts +176 -0
  16. package/dist/dts/metadata.d.ts +25 -0
  17. package/dist/dts/observation/arrays.d.ts +207 -0
  18. package/dist/dts/observation/notifier.d.ts +18 -18
  19. package/dist/dts/observation/observable.d.ts +117 -34
  20. package/dist/dts/observation/update-queue.d.ts +40 -0
  21. package/dist/dts/pending-task.d.ts +20 -0
  22. package/dist/dts/platform.d.ts +23 -66
  23. package/dist/dts/polyfills.d.ts +8 -0
  24. package/dist/dts/state/exports.d.ts +3 -0
  25. package/dist/dts/state/reactive.d.ts +8 -0
  26. package/dist/dts/state/state.d.ts +141 -0
  27. package/dist/dts/state/visitor.d.ts +6 -0
  28. package/dist/dts/state/watch.d.ts +10 -0
  29. package/dist/dts/styles/css-directive.d.ts +44 -6
  30. package/dist/dts/styles/css.d.ts +19 -3
  31. package/dist/dts/styles/element-styles.d.ts +49 -63
  32. package/dist/dts/styles/host.d.ts +68 -0
  33. package/dist/dts/templating/binding-signal.d.ts +21 -0
  34. package/dist/dts/templating/binding-two-way.d.ts +39 -0
  35. package/dist/dts/templating/binding.d.ts +101 -70
  36. package/dist/dts/templating/children.d.ts +18 -15
  37. package/dist/dts/templating/compiler.d.ts +46 -28
  38. package/dist/dts/templating/dom.d.ts +41 -0
  39. package/dist/dts/templating/html-directive.d.ts +239 -45
  40. package/dist/dts/templating/markup.d.ts +48 -0
  41. package/dist/dts/templating/node-observation.d.ts +45 -30
  42. package/dist/dts/templating/ref.d.ts +6 -20
  43. package/dist/dts/templating/render.d.ts +272 -0
  44. package/dist/dts/templating/repeat.d.ts +36 -33
  45. package/dist/dts/templating/slotted.d.ts +13 -14
  46. package/dist/dts/templating/template.d.ts +28 -22
  47. package/dist/dts/templating/view.d.ts +82 -24
  48. package/dist/dts/templating/when.d.ts +3 -3
  49. package/dist/dts/testing/exports.d.ts +3 -0
  50. package/dist/dts/testing/fakes.d.ts +4 -0
  51. package/dist/dts/testing/fixture.d.ts +84 -0
  52. package/dist/dts/testing/timeout.d.ts +7 -0
  53. package/dist/{tsdoc-metadata.json → dts/tsdoc-metadata.json} +1 -1
  54. package/dist/dts/utilities.d.ts +22 -0
  55. package/dist/esm/components/attributes.js +38 -28
  56. package/dist/esm/components/{controller.js → element-controller.js} +150 -140
  57. package/dist/esm/components/fast-definitions.js +48 -46
  58. package/dist/esm/components/fast-element.js +31 -12
  59. package/dist/esm/context.js +163 -0
  60. package/dist/esm/debug.js +61 -0
  61. package/dist/esm/di/di.js +1435 -0
  62. package/dist/esm/index.debug.js +2 -0
  63. package/dist/esm/index.js +20 -14
  64. package/dist/esm/index.rollup.debug.js +3 -0
  65. package/dist/esm/index.rollup.js +2 -0
  66. package/dist/esm/interfaces.js +12 -1
  67. package/dist/esm/metadata.js +60 -0
  68. package/dist/esm/observation/arrays.js +570 -0
  69. package/dist/esm/observation/notifier.js +27 -35
  70. package/dist/esm/observation/observable.js +116 -149
  71. package/dist/esm/observation/update-queue.js +67 -0
  72. package/dist/esm/pending-task.js +16 -0
  73. package/dist/esm/platform.js +60 -42
  74. package/dist/esm/polyfills.js +85 -0
  75. package/dist/esm/state/exports.js +3 -0
  76. package/dist/esm/state/reactive.js +34 -0
  77. package/dist/esm/state/state.js +148 -0
  78. package/dist/esm/state/visitor.js +28 -0
  79. package/dist/esm/state/watch.js +36 -0
  80. package/dist/esm/styles/css-directive.js +29 -13
  81. package/dist/esm/styles/css.js +29 -42
  82. package/dist/esm/styles/element-styles.js +79 -104
  83. package/dist/esm/styles/host.js +1 -0
  84. package/dist/esm/templating/binding-signal.js +83 -0
  85. package/dist/esm/templating/binding-two-way.js +103 -0
  86. package/dist/esm/templating/binding.js +189 -159
  87. package/dist/esm/templating/children.js +33 -23
  88. package/dist/esm/templating/compiler.js +258 -152
  89. package/dist/esm/templating/dom.js +49 -0
  90. package/dist/esm/templating/html-directive.js +193 -36
  91. package/dist/esm/templating/markup.js +75 -0
  92. package/dist/esm/templating/node-observation.js +51 -45
  93. package/dist/esm/templating/ref.js +8 -25
  94. package/dist/esm/templating/render.js +391 -0
  95. package/dist/esm/templating/repeat.js +83 -79
  96. package/dist/esm/templating/slotted.js +23 -20
  97. package/dist/esm/templating/template.js +51 -93
  98. package/dist/esm/templating/view.js +125 -46
  99. package/dist/esm/templating/when.js +6 -4
  100. package/dist/esm/testing/exports.js +3 -0
  101. package/dist/esm/testing/fakes.js +76 -0
  102. package/dist/esm/testing/fixture.js +86 -0
  103. package/dist/esm/testing/timeout.js +24 -0
  104. package/dist/esm/utilities.js +44 -0
  105. package/dist/fast-element.api.json +12153 -5373
  106. package/dist/fast-element.d.ts +1448 -696
  107. package/dist/fast-element.debug.js +4107 -0
  108. package/dist/fast-element.debug.min.js +1 -0
  109. package/dist/fast-element.js +3817 -4029
  110. package/dist/fast-element.min.js +1 -1
  111. package/dist/fast-element.untrimmed.d.ts +2814 -0
  112. package/docs/api-report.md +567 -254
  113. package/docs/fast-element-2-changes.md +15 -0
  114. package/karma.conf.cjs +6 -17
  115. package/package.json +76 -15
  116. package/dist/dts/dom.d.ts +0 -112
  117. package/dist/dts/observation/array-change-records.d.ts +0 -48
  118. package/dist/dts/observation/array-observer.d.ts +0 -9
  119. package/dist/dts/observation/behavior.d.ts +0 -19
  120. package/dist/esm/dom.js +0 -207
  121. package/dist/esm/observation/array-change-records.js +0 -326
  122. package/dist/esm/observation/array-observer.js +0 -177
@@ -1,26 +1,44 @@
1
- import { Controller } from "./controller.js";
1
+ import { isFunction } from "../interfaces.js";
2
+ import { ElementController } from "./element-controller.js";
2
3
  import { FASTElementDefinition, } from "./fast-definitions.js";
3
4
  /* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */
4
5
  function createFASTElement(BaseType) {
5
- return class extends BaseType {
6
+ const type = class extends BaseType {
6
7
  constructor() {
7
8
  /* eslint-disable-next-line */
8
9
  super();
9
- Controller.forCustomElement(this);
10
+ ElementController.forCustomElement(this);
10
11
  }
11
12
  $emit(type, detail, options) {
12
13
  return this.$fastController.emit(type, detail, options);
13
14
  }
14
15
  connectedCallback() {
15
- this.$fastController.onConnectedCallback();
16
+ this.$fastController.connect();
16
17
  }
17
18
  disconnectedCallback() {
18
- this.$fastController.onDisconnectedCallback();
19
+ this.$fastController.disconnect();
19
20
  }
20
21
  attributeChangedCallback(name, oldValue, newValue) {
21
22
  this.$fastController.onAttributeChangedCallback(name, oldValue, newValue);
22
23
  }
23
24
  };
25
+ FASTElementDefinition.registerBaseType(type);
26
+ return type;
27
+ }
28
+ function compose(type, nameOrDef) {
29
+ if (isFunction(type)) {
30
+ return FASTElementDefinition.compose(type, nameOrDef);
31
+ }
32
+ return FASTElementDefinition.compose(this, type);
33
+ }
34
+ function define(type, nameOrDef) {
35
+ if (isFunction(type)) {
36
+ return FASTElementDefinition.compose(type, nameOrDef).define().type;
37
+ }
38
+ return FASTElementDefinition.compose(this, type).define().type;
39
+ }
40
+ function from(BaseType) {
41
+ return createFASTElement(BaseType);
24
42
  }
25
43
  /**
26
44
  * A minimal base class for FASTElements that also provides
@@ -33,18 +51,19 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
33
51
  * provided base type.
34
52
  * @param BaseType - The base element type to inherit from.
35
53
  */
36
- from(BaseType) {
37
- return createFASTElement(BaseType);
38
- },
54
+ from,
39
55
  /**
40
56
  * Defines a platform custom element based on the provided type and definition.
41
57
  * @param type - The custom element type to define.
42
58
  * @param nameOrDef - The name of the element to define or a definition object
43
59
  * that describes the element to define.
44
60
  */
45
- define(type, nameOrDef) {
46
- return new FASTElementDefinition(type, nameOrDef).define().type;
47
- },
61
+ define,
62
+ /**
63
+ * Defines metadata for a FASTElement which can be used to later define the element.
64
+ * @public
65
+ */
66
+ compose,
48
67
  });
49
68
  /**
50
69
  * Decorator: Defines a platform custom element based on `FASTElement`.
@@ -55,6 +74,6 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
55
74
  export function customElement(nameOrDef) {
56
75
  /* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */
57
76
  return function (type) {
58
- new FASTElementDefinition(type, nameOrDef).define();
77
+ define(type, nameOrDef);
59
78
  };
60
79
  }
@@ -0,0 +1,163 @@
1
+ import "./interfaces.js";
2
+ import { Metadata } from "./metadata.js";
3
+ import { FAST } from "./platform.js";
4
+ const contextEventType = "context-request";
5
+ let requestStrategy;
6
+ /**
7
+ * Enables using the {@link https://github.com/webcomponents-cg/community-protocols/blob/main/proposals/context.md | W3C Community Context protocol.}
8
+ * @beta
9
+ */
10
+ export const Context = Object.freeze({
11
+ /**
12
+ * The event type used for W3C Context Protocol requests.
13
+ */
14
+ eventType: contextEventType,
15
+ /**
16
+ * Creates a W3C Community Protocol-based Context object to use in requesting/providing
17
+ * context through the DOM.
18
+ * @param name - The name to use for the connext. Useful in debugging.
19
+ * @param initialValue - An optional initial value to use if a context handler isn't found.
20
+ */
21
+ create(name, initialValue) {
22
+ const Interface = function (target, property, index) {
23
+ if (target == null || new.target !== undefined) {
24
+ throw FAST.error(1501 /* Message.noRegistrationForContext */, {
25
+ name: Interface.name,
26
+ });
27
+ }
28
+ if (property) {
29
+ Context.defineProperty(target, property, Interface);
30
+ }
31
+ else {
32
+ const types = Metadata.getOrCreateAnnotationParamTypes(target);
33
+ types[index] = Interface;
34
+ }
35
+ };
36
+ Interface.$isInterface = true;
37
+ Interface.initialValue = initialValue;
38
+ Reflect.defineProperty(Interface, "name", { value: name });
39
+ Interface.handle = (target, callback) => Context.handle(target, callback, Interface);
40
+ Interface.provide = (target, value) => Context.provide(target, Interface, value);
41
+ Interface.get = (target) => Context.get(target, Interface);
42
+ Interface.request = (target, callback, multiple) => Context.request(target, Interface, callback, multiple);
43
+ Interface.toString = () => `Context<${Interface.name}>`;
44
+ return Interface;
45
+ },
46
+ /**
47
+ * Sets the strategy used by all FAST-specific context requests made through the
48
+ * Context.request, Context.get, Context.defineProperty, and ContextDecorator APIs.
49
+ * @param strategy - The strategy to use. By default, the strategy is Context.dispatch.
50
+ */
51
+ setDefaultRequestStrategy(strategy) {
52
+ requestStrategy = strategy;
53
+ },
54
+ /**
55
+ * Gets the context value for the target node or returns the initial value if
56
+ * a context handler is not found.
57
+ * @param target - The target to get the context for.
58
+ * @param context - The context to locate.
59
+ * @returns The context value.
60
+ * @remarks
61
+ * Uses the default request strategy to locate the context. If no context is found
62
+ * then the initial value of the context is returned.
63
+ */
64
+ get(target, context) {
65
+ var _a;
66
+ let value;
67
+ requestStrategy(target, context, found => (value = found), false);
68
+ return (_a = value) !== null && _a !== void 0 ? _a : context.initialValue;
69
+ },
70
+ /**
71
+ * Requests the context value for the target node.
72
+ * @param target - The target to request the context for.
73
+ * @param context - The context to locate.
74
+ * @param callback - A callback to invoke with the context value.
75
+ * @param multiple - Whether the context requestor expects to handle updates
76
+ * to the context value after the initial request.
77
+ * @remarks
78
+ * Uses the default request strategy to locate the context.
79
+ */
80
+ request(target, context, callback, multiple = false) {
81
+ requestStrategy(target, context, callback, multiple);
82
+ },
83
+ /**
84
+ *
85
+ * @param target - The target to dispatch the context event on.
86
+ * @param context - The context to locate.
87
+ * @param callback - The callback to invoke with the context value.
88
+ * @param multiple - Whether the context requestor expects to handle updates
89
+ * to the context value after the initial request.
90
+ * @remarks
91
+ * This API does NOT use the default request strategy. It always dispatches
92
+ * an event through the DOM.
93
+ */
94
+ dispatch(target, context, callback, multiple = false) {
95
+ target.dispatchEvent(new ContextEvent(context, callback, multiple));
96
+ },
97
+ provide(target, context, value) {
98
+ this.handle(target, (event) => {
99
+ event.stopImmediatePropagation();
100
+ event.callback(value);
101
+ }, context);
102
+ },
103
+ /**
104
+ *
105
+ * @param target - The target on which to handle context requests.
106
+ * @param callback - The callback to invoke when a context request is received.
107
+ * @param context - The context to handle requests for.
108
+ * @remarks
109
+ * If a context is not provided then the callback will be invoked for all context
110
+ * requests that are received on the target.
111
+ */
112
+ handle(target, callback, context) {
113
+ if (context) {
114
+ target.addEventListener(contextEventType, (event) => {
115
+ if (event.context === context) {
116
+ callback(event);
117
+ }
118
+ });
119
+ }
120
+ else {
121
+ target.addEventListener(contextEventType, callback);
122
+ }
123
+ },
124
+ /**
125
+ * Defines a getter-only property on the target that will return the context
126
+ * value for the target.
127
+ * @param target The target to define the property on.
128
+ * @param propertyName The name of the property to define.
129
+ * @param context The context that will be used to retrieve the property value.
130
+ * @remarks
131
+ * Uses the default request strategy to locate the context and will return the
132
+ * initialValue if the context isn't handled.
133
+ */
134
+ defineProperty(target, propertyName, context) {
135
+ const field = `$di_${propertyName}`;
136
+ Reflect.defineProperty(target, propertyName, {
137
+ get: function () {
138
+ var _a;
139
+ return (_a = this[field]) !== null && _a !== void 0 ? _a : (this[field] = Context.get(this, context));
140
+ },
141
+ });
142
+ },
143
+ });
144
+ Context.setDefaultRequestStrategy(Context.dispatch);
145
+ /**
146
+ * An event fired by a context requester to signal it desires a named context.
147
+ *
148
+ * A provider should inspect the `context` property of the event to determine if it has a value that can
149
+ * satisfy the request, calling the `callback` with the requested value if so.
150
+ *
151
+ * If the requested context event contains a truthy `multiple` value, then a provider can call the callback
152
+ * multiple times if the value is changed, if this is the case the provider should pass a `dispose`
153
+ * method to the callback which requesters can invoke to indicate they no longer wish to receive these updates.
154
+ * @public
155
+ */
156
+ export class ContextEvent extends Event {
157
+ constructor(context, callback, multiple) {
158
+ super(contextEventType, { bubbles: true, composed: true });
159
+ this.context = context;
160
+ this.callback = callback;
161
+ this.multiple = multiple;
162
+ }
163
+ }
@@ -0,0 +1,61 @@
1
+ if (globalThis.FAST === void 0) {
2
+ Reflect.defineProperty(globalThis, "FAST", {
3
+ value: Object.create(null),
4
+ configurable: false,
5
+ enumerable: false,
6
+ writable: false,
7
+ });
8
+ }
9
+ const FAST = globalThis.FAST;
10
+ const debugMessages = {
11
+ [1101 /* needsArrayObservation */]: "Must call enableArrayObservation before observing arrays.",
12
+ [1201 /* onlySetHTMLPolicyOnce */]: "The HTML policy can only be set once.",
13
+ [1202 /* bindingInnerHTMLRequiresTrustedTypes */]: "To bind innerHTML, you must use a TrustedTypesPolicy.",
14
+ [1203 /* twoWayBindingRequiresObservables */]: "View=>Model update skipped. To use twoWay binding, the target property must be observable.",
15
+ [1204 /* hostBindingWithoutHost */]: "No host element is present. Cannot bind host with ${name}.",
16
+ [1205 /* unsupportedBindingBehavior */]: "The requested binding behavior is not supported by the binding engine.",
17
+ [1401 /* missingElementDefinition */]: "Missing FASTElement definition.",
18
+ [1501 /* noRegistrationForContext */]: "No registration for Context/Interface '${name}'.",
19
+ [1502 /* noFactoryForResolver */]: "Dependency injection resolver for '${key}' returned a null factory.",
20
+ [1503 /* invalidResolverStrategy */]: "Invalid dependency injection resolver strategy specified '${strategy}'.",
21
+ [1504 /* cannotAutoregisterDependency */]: "Unable to autoregister dependency.",
22
+ [1505 /* cannotResolveKey */]: "Unable to resolve dependency injection key '${key}'.",
23
+ [1506 /* cannotConstructNativeFunction */]: "'${name}' is a native function and therefore cannot be safely constructed by DI. If this is intentional, please use a callback or cachedCallback resolver.",
24
+ [1507 /* cannotJITRegisterNonConstructor */]: "Attempted to jitRegister something that is not a constructor '${value}'. Did you forget to register this dependency?",
25
+ [1508 /* cannotJITRegisterIntrinsic */]: "Attempted to jitRegister an intrinsic type '${value}'. Did you forget to add @inject(Key)?",
26
+ [1509 /* cannotJITRegisterInterface */]: "Attempted to jitRegister an interface '${value}'.",
27
+ [1510 /* invalidResolver */]: "A valid resolver was not returned from the register method.",
28
+ [1511 /* invalidKey */]: "Key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?",
29
+ [1512 /* noDefaultResolver */]: "'${key}' not registered. Did you forget to add @singleton()?",
30
+ [1513 /* cyclicDependency */]: "Cyclic dependency found '${name}'.",
31
+ [1514 /* connectUpdateRequiresController */]: "Injected properties that are updated on changes to DOM connectivity require the target object to be an instance of FASTElement.",
32
+ };
33
+ const allPlaceholders = /(\$\{\w+?})/g;
34
+ const placeholder = /\$\{(\w+?)}/g;
35
+ const noValues = Object.freeze({});
36
+ function formatMessage(message, values) {
37
+ return message
38
+ .split(allPlaceholders)
39
+ .map(v => {
40
+ var _a;
41
+ const replaced = v.replace(placeholder, "$1");
42
+ return String((_a = values[replaced]) !== null && _a !== void 0 ? _a : v);
43
+ })
44
+ .join("");
45
+ }
46
+ Object.assign(FAST, {
47
+ addMessages(messages) {
48
+ Object.assign(debugMessages, messages);
49
+ },
50
+ warn(code, values = noValues) {
51
+ var _a;
52
+ const message = (_a = debugMessages[code]) !== null && _a !== void 0 ? _a : "Unknown Warning";
53
+ console.warn(formatMessage(message, values));
54
+ },
55
+ error(code, values = noValues) {
56
+ var _a;
57
+ const message = (_a = debugMessages[code]) !== null && _a !== void 0 ? _a : "Unknown Error";
58
+ return new Error(formatMessage(message, values));
59
+ },
60
+ });
61
+ export {};