@microsoft/fast-element 2.0.0-beta.3 → 2.0.0-beta.4

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 (50) hide show
  1. package/CHANGELOG.json +81 -0
  2. package/CHANGELOG.md +20 -1
  3. package/dist/dts/components/fast-definitions.d.ts +9 -8
  4. package/dist/dts/components/fast-element.d.ts +8 -4
  5. package/dist/dts/context.d.ts +1 -1
  6. package/dist/dts/di/di.d.ts +854 -0
  7. package/dist/dts/hooks.d.ts +2 -2
  8. package/dist/dts/interfaces.d.ts +38 -7
  9. package/dist/dts/observation/observable.d.ts +19 -13
  10. package/dist/dts/styles/element-styles.d.ts +6 -0
  11. package/dist/dts/templating/binding-signal.d.ts +10 -27
  12. package/dist/dts/templating/binding-two-way.d.ts +16 -41
  13. package/dist/dts/templating/binding.d.ts +79 -118
  14. package/dist/dts/templating/html-directive.d.ts +28 -2
  15. package/dist/dts/templating/render.d.ts +277 -0
  16. package/dist/dts/templating/repeat.d.ts +12 -16
  17. package/dist/dts/templating/template.d.ts +3 -3
  18. package/dist/dts/templating/when.d.ts +3 -3
  19. package/dist/dts/testing/exports.d.ts +2 -0
  20. package/dist/dts/testing/fixture.d.ts +90 -0
  21. package/dist/dts/testing/timeout.d.ts +7 -0
  22. package/dist/esm/components/fast-definitions.js +25 -27
  23. package/dist/esm/components/fast-element.js +16 -8
  24. package/dist/esm/context.js +5 -1
  25. package/dist/esm/debug.js +34 -4
  26. package/dist/esm/di/di.js +1349 -0
  27. package/dist/esm/observation/observable.js +4 -4
  28. package/dist/esm/platform.js +1 -1
  29. package/dist/esm/styles/element-styles.js +14 -0
  30. package/dist/esm/templating/binding-signal.js +56 -61
  31. package/dist/esm/templating/binding-two-way.js +51 -35
  32. package/dist/esm/templating/binding.js +137 -156
  33. package/dist/esm/templating/compiler.js +29 -7
  34. package/dist/esm/templating/html-directive.js +12 -1
  35. package/dist/esm/templating/render.js +392 -0
  36. package/dist/esm/templating/repeat.js +54 -40
  37. package/dist/esm/templating/template.js +8 -5
  38. package/dist/esm/templating/when.js +5 -4
  39. package/dist/esm/testing/exports.js +2 -0
  40. package/dist/esm/testing/fixture.js +88 -0
  41. package/dist/esm/testing/timeout.js +24 -0
  42. package/dist/fast-element.api.json +3257 -3151
  43. package/dist/fast-element.d.ts +210 -209
  44. package/dist/fast-element.debug.js +329 -248
  45. package/dist/fast-element.debug.min.js +1 -1
  46. package/dist/fast-element.js +295 -244
  47. package/dist/fast-element.min.js +1 -1
  48. package/dist/fast-element.untrimmed.d.ts +218 -214
  49. package/docs/api-report.md +83 -85
  50. package/package.json +13 -1
@@ -1,9 +1,9 @@
1
- import "../interfaces.js";
1
+ import { isFunction } from "../interfaces.js";
2
2
  import { ExecutionContext, Observable, } from "../observation/observable.js";
3
3
  import { FAST } from "../platform.js";
4
4
  import { DOM } from "./dom.js";
5
- import { Aspect, HTMLDirective, } from "./html-directive.js";
6
- import { Markup } from "./markup.js";
5
+ import { Aspect, Binding, HTMLDirective, } from "./html-directive.js";
6
+ import { Markup, nextId } from "./markup.js";
7
7
  const createInnerHTMLBinding = globalThis.TrustedHTML
8
8
  ? (binding) => (s, c) => {
9
9
  const value = binding(s, c);
@@ -13,99 +13,28 @@ const createInnerHTMLBinding = globalThis.TrustedHTML
13
13
  throw FAST.error(1202 /* Message.bindingInnerHTMLRequiresTrustedTypes */);
14
14
  }
15
15
  : (binding) => binding;
16
- /**
17
- * Describes how aspects of an HTML element will be affected by bindings.
18
- * @public
19
- */
20
- export const BindingMode = Object.freeze({
21
- /**
22
- * Creates a binding mode based on the supplied behavior types.
23
- * @param UpdateType - The base behavior type used to update aspects.
24
- * @param EventType - The base behavior type used to respond to events.
25
- * @returns A new binding mode.
26
- */
27
- define(UpdateType, EventType = EventBinding) {
28
- return Object.freeze({
29
- [1]: d => new UpdateType(d, DOM.setAttribute),
30
- [2]: d => new UpdateType(d, DOM.setBooleanAttribute),
31
- [3]: d => new UpdateType(d, (t, a, v) => (t[a] = v)),
32
- [4]: d => new (createContentBinding(UpdateType))(d, updateContentTarget),
33
- [5]: d => new UpdateType(d, updateTokenListTarget),
34
- [6]: d => new EventType(d),
35
- });
36
- },
37
- });
38
- /**
39
- * Describes the configuration for a binding expression.
40
- * @public
41
- */
42
- export const BindingConfig = Object.freeze({
43
- /**
44
- * Creates a binding configuration based on the provided mode and options.
45
- * @param mode - The mode to use for the configuration.
46
- * @param defaultOptions - The default options to use for the configuration.
47
- * @returns A new binding configuration.
48
- */
49
- define(mode, defaultOptions) {
50
- const config = (options) => {
51
- return {
52
- mode: config.mode,
53
- options: Object.assign({}, defaultOptions, options),
54
- };
55
- };
56
- config.options = defaultOptions;
57
- config.mode = mode;
58
- return config;
59
- },
60
- });
61
- /**
62
- * A base binding behavior for DOM updates.
63
- * @public
64
- */
65
- export class UpdateBinding {
66
- /**
67
- * Creates an instance of UpdateBinding.
68
- * @param directive - The directive that has the configuration for this behavior.
69
- * @param updateTarget - The function used to update the target with the latest value.
70
- */
71
- constructor(directive, updateTarget) {
72
- this.directive = directive;
73
- this.updateTarget = updateTarget;
16
+ class OnChangeBinding extends Binding {
17
+ constructor(evaluate, isVolatile) {
18
+ super();
19
+ this.evaluate = evaluate;
20
+ this.isVolatile = isVolatile;
74
21
  }
75
- /**
76
- * Bind this behavior to the source.
77
- * @param source - The source to bind to.
78
- * @param context - The execution context that the binding is operating within.
79
- * @param targets - The targets that behaviors in a view can attach to.
80
- */
81
- bind(source, context, targets) { }
82
- /**
83
- * Unbinds this behavior from the source.
84
- * @param source - The source to unbind from.
85
- * @param context - The execution context that the binding is operating within.
86
- * @param targets - The targets that behaviors in a view can attach to.
87
- */
88
- unbind(source, context, targets) { }
89
- /**
90
- * Creates a behavior.
91
- * @param targets - The targets available for behaviors to be attached to.
92
- */
93
- createBehavior(targets) {
94
- return this;
22
+ createObserver(_, subscriber) {
23
+ return Observable.binding(this.evaluate, subscriber, this.isVolatile);
95
24
  }
96
25
  }
97
- function createContentBinding(Type) {
98
- return class extends Type {
99
- unbind(source, context, targets) {
100
- super.unbind(source, context, targets);
101
- const target = targets[this.directive.nodeId];
102
- const view = target.$fastView;
103
- if (view !== void 0 && view.isComposed) {
104
- view.unbind();
105
- view.needsBindOnly = true;
106
- }
107
- }
108
- };
26
+ class OneTimeBinding extends Binding {
27
+ constructor(evaluate) {
28
+ super();
29
+ this.evaluate = evaluate;
30
+ }
31
+ createObserver() {
32
+ return this;
33
+ }
34
+ observe(source, context) {
35
+ return this.evaluate(source, context);
36
+ }
37
+ dispose() { }
109
38
  }
110
39
  function updateContentTarget(target, aspect, value, source, context) {
111
40
  // If there's no actual value, then this equates to the
@@ -113,7 +42,7 @@ function updateContentTarget(target, aspect, value, source, context) {
113
42
  if (value === null || value === undefined) {
114
43
  value = "";
115
44
  }
116
- // If the value has a "create" method, then it's a template-like.
45
+ // If the value has a "create" method, then it's a ContentTemplate.
117
46
  if (value.create) {
118
47
  target.textContent = "";
119
48
  let view = target.$fastView;
@@ -199,46 +128,21 @@ function updateTokenListTarget(target, aspect, value) {
199
128
  }
200
129
  }
201
130
  }
202
- /**
203
- * A binding behavior for one-time bindings.
204
- * @public
205
- */
206
- export class OneTimeBinding extends UpdateBinding {
207
- /**
208
- * Bind this behavior to the source.
209
- * @param source - The source to bind to.
210
- * @param context - The execution context that the binding is operating within.
211
- * @param targets - The targets that behaviors in a view can attach to.
212
- */
213
- bind(source, context, targets) {
214
- const directive = this.directive;
215
- this.updateTarget(targets[directive.nodeId], directive.targetAspect, directive.binding(source, context), source, context);
216
- }
217
- }
218
131
  /**
219
132
  * A binding behavior for bindings that change.
220
133
  * @public
221
134
  */
222
- export class ChangeBinding extends UpdateBinding {
135
+ export class BindingBehavior {
223
136
  /**
224
137
  * Creates an instance of ChangeBinding.
225
138
  * @param directive - The directive that has the configuration for this behavior.
226
139
  * @param updateTarget - The function used to update the target with the latest value.
227
140
  */
228
141
  constructor(directive, updateTarget) {
229
- super(directive, updateTarget);
230
- this.isBindingVolatile = Observable.isVolatileBinding(directive.binding);
142
+ this.directive = directive;
143
+ this.updateTarget = updateTarget;
231
144
  this.observerProperty = `${directive.id}-o`;
232
145
  }
233
- /**
234
- * Returns the binding observer used to update the node.
235
- * @param target - The target node.
236
- * @returns A BindingObserver.
237
- */
238
- getObserver(target) {
239
- var _a;
240
- return ((_a = target[this.observerProperty]) !== null && _a !== void 0 ? _a : (target[this.observerProperty] = Observable.binding(this.directive.binding, this, this.isBindingVolatile)));
241
- }
242
146
  /**
243
147
  * Bind this behavior to the source.
244
148
  * @param source - The source to bind to.
@@ -275,12 +179,49 @@ export class ChangeBinding extends UpdateBinding {
275
179
  const context = observer.context;
276
180
  this.updateTarget(target, this.directive.targetAspect, observer.observe(source, context), source, context);
277
181
  }
182
+ /**
183
+ * Returns the binding observer used to update the node.
184
+ * @param target - The target node.
185
+ * @returns A BindingObserver.
186
+ */
187
+ getObserver(target) {
188
+ var _a;
189
+ return ((_a = target[this.observerProperty]) !== null && _a !== void 0 ? _a : (target[this.observerProperty] = this.directive.dataBinding.createObserver(this.directive, this)));
190
+ }
191
+ /**
192
+ * Creates a behavior.
193
+ * @param targets - The targets available for behaviors to be attached to.
194
+ */
195
+ createBehavior(targets) {
196
+ return this;
197
+ }
198
+ }
199
+ /**
200
+ * A special binding behavior that can bind node content.
201
+ * @public
202
+ */
203
+ export class ContentBehavior extends BindingBehavior {
204
+ /**
205
+ * Unbinds this behavior from the source.
206
+ * @param source - The source to unbind from.
207
+ * @param context - The execution context that the binding is operating within.
208
+ * @param targets - The targets that behaviors in a view can attach to.
209
+ */
210
+ unbind(source, context, targets) {
211
+ super.unbind(source, context, targets);
212
+ const target = targets[this.directive.nodeId];
213
+ const view = target.$fastView;
214
+ if (view !== void 0 && view.isComposed) {
215
+ view.unbind();
216
+ view.needsBindOnly = true;
217
+ }
218
+ }
278
219
  }
279
220
  /**
280
221
  * A binding behavior for handling events.
281
222
  * @public
282
223
  */
283
- export class EventBinding {
224
+ export class EventBehavior {
284
225
  /**
285
226
  * Creates an instance of EventBinding.
286
227
  * @param directive - The directive that has the configuration for this behavior.
@@ -301,7 +242,7 @@ export class EventBinding {
301
242
  const target = targets[directive.nodeId];
302
243
  target[this.sourceProperty] = source;
303
244
  target[this.contextProperty] = context;
304
- target.addEventListener(directive.targetAspect, this, directive.options);
245
+ target.addEventListener(directive.targetAspect, this, directive.dataBinding.options);
305
246
  }
306
247
  /**
307
248
  * Unbinds this behavior from the source.
@@ -313,7 +254,7 @@ export class EventBinding {
313
254
  const directive = this.directive;
314
255
  const target = targets[directive.nodeId];
315
256
  target[this.sourceProperty] = target[this.contextProperty] = null;
316
- target.removeEventListener(directive.targetAspect, this, directive.options);
257
+ target.removeEventListener(directive.targetAspect, this, directive.dataBinding.options);
317
258
  }
318
259
  /**
319
260
  * Creates a behavior.
@@ -328,25 +269,13 @@ export class EventBinding {
328
269
  handleEvent(event) {
329
270
  const target = event.currentTarget;
330
271
  ExecutionContext.setEvent(event);
331
- const result = this.directive.binding(target[this.sourceProperty], target[this.contextProperty]);
272
+ const result = this.directive.dataBinding.evaluate(target[this.sourceProperty], target[this.contextProperty]);
332
273
  ExecutionContext.setEvent(null);
333
274
  if (result !== true) {
334
275
  event.preventDefault();
335
276
  }
336
277
  }
337
278
  }
338
- /**
339
- * The default onChange binding configuration.
340
- * @public
341
- */
342
- export const onChange = BindingConfig.define(BindingMode.define(ChangeBinding), {});
343
- /**
344
- * The default onTime binding configuration.
345
- * @public
346
- */
347
- export const oneTime = BindingConfig.define(BindingMode.define(OneTimeBinding), {
348
- once: true,
349
- });
350
279
  /**
351
280
  * A directive that applies bindings.
352
281
  * @public
@@ -354,15 +283,15 @@ export const oneTime = BindingConfig.define(BindingMode.define(OneTimeBinding),
354
283
  export class HTMLBindingDirective {
355
284
  /**
356
285
  * Creates an instance of HTMLBindingDirective.
357
- * @param binding - The binding to apply.
358
- * @param mode - The binding mode to use when applying the binding.
359
- * @param options - The options to configure the binding with.
286
+ * @param dataBinding - The binding configuration to apply.
360
287
  */
361
- constructor(binding, mode, options) {
362
- this.binding = binding;
363
- this.mode = mode;
364
- this.options = options;
288
+ constructor(dataBinding) {
289
+ this.dataBinding = dataBinding;
365
290
  this.factory = null;
291
+ /**
292
+ * The unique id of the factory.
293
+ */
294
+ this.id = nextId();
366
295
  /**
367
296
  * The type of aspect to target.
368
297
  */
@@ -382,24 +311,76 @@ export class HTMLBindingDirective {
382
311
  createBehavior(targets) {
383
312
  if (this.factory == null) {
384
313
  if (this.targetAspect === "innerHTML") {
385
- this.binding = createInnerHTMLBinding(this.binding);
314
+ this.dataBinding.evaluate = createInnerHTMLBinding(this.dataBinding.evaluate);
315
+ }
316
+ switch (this.aspectType) {
317
+ case 1:
318
+ this.factory = new BindingBehavior(this, DOM.setAttribute);
319
+ break;
320
+ case 2:
321
+ this.factory = new BindingBehavior(this, DOM.setBooleanAttribute);
322
+ break;
323
+ case 3:
324
+ this.factory = new BindingBehavior(this, (t, a, v) => (t[a] = v));
325
+ break;
326
+ case 4:
327
+ this.factory = new ContentBehavior(this, updateContentTarget);
328
+ break;
329
+ case 5:
330
+ this.factory = new BindingBehavior(this, updateTokenListTarget);
331
+ break;
332
+ case 6:
333
+ this.factory = new EventBehavior(this);
334
+ break;
335
+ default:
336
+ throw FAST.error(1205 /* Message.unsupportedBindingBehavior */);
386
337
  }
387
- this.factory = this.mode[this.aspectType](this);
388
338
  }
389
339
  return this.factory.createBehavior(targets);
390
340
  }
391
341
  }
392
342
  HTMLDirective.define(HTMLBindingDirective, { aspected: true });
393
343
  /**
394
- * Creates a binding directive with the specified configuration.
395
- * @param binding - The binding expression.
396
- * @param config - The binding configuration.
397
- * @returns A binding directive.
344
+ * Creates an standard binding.
345
+ * @param binding - The binding to refresh when changed.
346
+ * @param isVolatile - Indicates whether the binding is volatile or not.
347
+ * @returns A binding configuration.
398
348
  * @public
399
349
  */
400
- export function bind(binding, config = onChange) {
401
- if (!("mode" in config)) {
402
- config = onChange(config);
403
- }
404
- return new HTMLBindingDirective(binding, config.mode, config.options);
350
+ export function bind(binding, isVolatile = Observable.isVolatileBinding(binding)) {
351
+ return new OnChangeBinding(binding, isVolatile);
352
+ }
353
+ /**
354
+ * Creates a one time binding
355
+ * @param binding - The binding to refresh when signaled.
356
+ * @returns A binding configuration.
357
+ * @public
358
+ */
359
+ export function oneTime(binding) {
360
+ return new OneTimeBinding(binding);
361
+ }
362
+ /**
363
+ * Creates an event listener binding.
364
+ * @param binding - The binding to invoke when the event is raised.
365
+ * @param options - Event listener options.
366
+ * @returns A binding configuration.
367
+ * @public
368
+ */
369
+ export function listener(binding, options) {
370
+ const config = new OnChangeBinding(binding, false);
371
+ config.options = options;
372
+ return config;
373
+ }
374
+ /**
375
+ * Normalizes the input value into a binding.
376
+ * @param value - The value to create the default binding for.
377
+ * @returns A binding configuration for the provided value.
378
+ * @public
379
+ */
380
+ export function normalizeBinding(value) {
381
+ return isFunction(value)
382
+ ? bind(value)
383
+ : value instanceof Binding
384
+ ? value
385
+ : oneTime(() => value);
405
386
  }
@@ -1,7 +1,7 @@
1
- import { isString } from "../interfaces.js";
1
+ import { isFunction, isString } from "../interfaces.js";
2
2
  import { FAST } from "../platform.js";
3
3
  import { Parser } from "./markup.js";
4
- import { bind, oneTime } from "./binding.js";
4
+ import { HTMLBindingDirective, oneTime } from "./binding.js";
5
5
  import { Aspect } from "./html-directive.js";
6
6
  import { HTMLView } from "./view.js";
7
7
  const targetIdFrom = (parentId, nodeIndex) => `${parentId}.${nodeIndex}`;
@@ -11,6 +11,22 @@ const next = {
11
11
  index: 0,
12
12
  node: null,
13
13
  };
14
+ function tryWarn(name) {
15
+ if (!name.startsWith("fast-")) {
16
+ FAST.warn(1204 /* Message.hostBindingWithoutHost */, { name });
17
+ }
18
+ }
19
+ const warningHost = new Proxy(document.createElement("div"), {
20
+ get(target, property) {
21
+ tryWarn(property);
22
+ const value = Reflect.get(target, property);
23
+ return isFunction(value) ? value.bind(target) : value;
24
+ },
25
+ set(target, property, value) {
26
+ tryWarn(property);
27
+ return Reflect.set(target, property, value);
28
+ },
29
+ });
14
30
  class CompilationContext {
15
31
  constructor(fragment, directives) {
16
32
  this.fragment = fragment;
@@ -61,7 +77,7 @@ class CompilationContext {
61
77
  const fragment = this.fragment.cloneNode(true);
62
78
  const targets = Object.create(this.proto);
63
79
  targets.r = fragment;
64
- targets.h = hostBindingTarget !== null && hostBindingTarget !== void 0 ? hostBindingTarget : fragment;
80
+ targets.h = hostBindingTarget !== null && hostBindingTarget !== void 0 ? hostBindingTarget : warningHost;
65
81
  for (const id of this.nodeIds) {
66
82
  targets[id]; // trigger locator
67
83
  }
@@ -78,7 +94,7 @@ function compileAttributes(context, parentId, node, nodeId, nodeIndex, includeBa
78
94
  let result = null;
79
95
  if (parseResult === null) {
80
96
  if (includeBasicValues) {
81
- result = bind(() => attrValue, oneTime);
97
+ result = new HTMLBindingDirective(oneTime(() => attrValue));
82
98
  Aspect.assign(result, attr.name);
83
99
  }
84
100
  }
@@ -248,22 +264,28 @@ export const Compiler = {
248
264
  return parts[0];
249
265
  }
250
266
  let sourceAspect;
267
+ let binding;
268
+ let isVolatile = false;
251
269
  const partCount = parts.length;
252
270
  const finalParts = parts.map((x) => {
253
271
  if (isString(x)) {
254
272
  return () => x;
255
273
  }
256
274
  sourceAspect = x.sourceAspect || sourceAspect;
257
- return x.binding;
275
+ binding = x.dataBinding || binding;
276
+ isVolatile = isVolatile || x.dataBinding.isVolatile;
277
+ return x.dataBinding.evaluate;
258
278
  });
259
- const binding = (scope, context) => {
279
+ const expression = (scope, context) => {
260
280
  let output = "";
261
281
  for (let i = 0; i < partCount; ++i) {
262
282
  output += finalParts[i](scope, context);
263
283
  }
264
284
  return output;
265
285
  };
266
- const directive = bind(binding);
286
+ binding.evaluate = expression;
287
+ binding.isVolatile = isVolatile;
288
+ const directive = new HTMLBindingDirective(binding);
267
289
  Aspect.assign(directive, sourceAspect);
268
290
  return directive;
269
291
  },
@@ -1,5 +1,5 @@
1
1
  import { createTypeRegistry } from "../platform.js";
2
- import { Markup } from "./markup.js";
2
+ import { Markup, nextId } from "./markup.js";
3
3
  const registry = createTypeRegistry();
4
4
  /**
5
5
  * Instructs the template engine to apply behavior to a node.
@@ -39,6 +39,13 @@ export function htmlDirective(options) {
39
39
  HTMLDirective.define(type, options);
40
40
  };
41
41
  }
42
+ /**
43
+ * Captures a binding expression along with related information and capabilities.
44
+ *
45
+ * @public
46
+ */
47
+ export class Binding {
48
+ }
42
49
  /**
43
50
  * The type of HTML aspect to target.
44
51
  * @public
@@ -132,6 +139,10 @@ export class StatelessAttachedAttributeDirective {
132
139
  */
133
140
  constructor(options) {
134
141
  this.options = options;
142
+ /**
143
+ * The unique id of the factory.
144
+ */
145
+ this.id = nextId();
135
146
  }
136
147
  /**
137
148
  * Creates a behavior.