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

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 (68) hide show
  1. package/CHANGELOG.json +147 -0
  2. package/CHANGELOG.md +42 -1
  3. package/dist/dts/components/fast-definitions.d.ts +9 -8
  4. package/dist/dts/components/fast-element.d.ts +12 -24
  5. package/dist/dts/context.d.ts +1 -1
  6. package/dist/dts/di/di.d.ts +858 -0
  7. package/dist/dts/interfaces.d.ts +43 -7
  8. package/dist/dts/observation/observable.d.ts +19 -13
  9. package/dist/dts/state/exports.d.ts +3 -0
  10. package/dist/dts/state/reactive.d.ts +8 -0
  11. package/dist/dts/state/state.d.ts +141 -0
  12. package/dist/dts/state/visitor.d.ts +6 -0
  13. package/dist/dts/state/watch.d.ts +10 -0
  14. package/dist/dts/styles/element-styles.d.ts +6 -0
  15. package/dist/dts/templating/binding-signal.d.ts +10 -27
  16. package/dist/dts/templating/binding-two-way.d.ts +16 -41
  17. package/dist/dts/templating/binding.d.ts +79 -118
  18. package/dist/dts/templating/html-directive.d.ts +28 -2
  19. package/dist/dts/templating/render.d.ts +277 -0
  20. package/dist/dts/templating/repeat.d.ts +12 -16
  21. package/dist/dts/templating/template.d.ts +3 -3
  22. package/dist/dts/templating/when.d.ts +3 -3
  23. package/dist/dts/testing/exports.d.ts +2 -0
  24. package/dist/dts/testing/fixture.d.ts +90 -0
  25. package/dist/dts/testing/timeout.d.ts +7 -0
  26. package/dist/dts/utilities.d.ts +0 -18
  27. package/dist/esm/components/fast-definitions.js +25 -27
  28. package/dist/esm/components/fast-element.js +20 -11
  29. package/dist/esm/context.js +5 -1
  30. package/dist/esm/debug.js +35 -4
  31. package/dist/esm/di/di.js +1351 -0
  32. package/dist/esm/interfaces.js +4 -0
  33. package/dist/esm/observation/arrays.js +303 -2
  34. package/dist/esm/observation/observable.js +11 -6
  35. package/dist/esm/platform.js +1 -1
  36. package/dist/esm/state/exports.js +3 -0
  37. package/dist/esm/state/reactive.js +34 -0
  38. package/dist/esm/state/state.js +148 -0
  39. package/dist/esm/state/visitor.js +28 -0
  40. package/dist/esm/state/watch.js +36 -0
  41. package/dist/esm/styles/element-styles.js +14 -0
  42. package/dist/esm/templating/binding-signal.js +56 -61
  43. package/dist/esm/templating/binding-two-way.js +51 -35
  44. package/dist/esm/templating/binding.js +137 -156
  45. package/dist/esm/templating/compiler.js +29 -7
  46. package/dist/esm/templating/html-directive.js +12 -1
  47. package/dist/esm/templating/render.js +392 -0
  48. package/dist/esm/templating/repeat.js +57 -40
  49. package/dist/esm/templating/template.js +8 -5
  50. package/dist/esm/templating/view.js +3 -1
  51. package/dist/esm/templating/when.js +5 -4
  52. package/dist/esm/testing/exports.js +2 -0
  53. package/dist/esm/testing/fixture.js +88 -0
  54. package/dist/esm/testing/timeout.js +24 -0
  55. package/dist/esm/utilities.js +0 -95
  56. package/dist/fast-element.api.json +2827 -2757
  57. package/dist/fast-element.d.ts +215 -229
  58. package/dist/fast-element.debug.js +650 -256
  59. package/dist/fast-element.debug.min.js +1 -1
  60. package/dist/fast-element.js +615 -252
  61. package/dist/fast-element.min.js +1 -1
  62. package/dist/fast-element.untrimmed.d.ts +223 -234
  63. package/docs/api-report.md +87 -90
  64. package/package.json +18 -9
  65. package/dist/dts/hooks.d.ts +0 -20
  66. package/dist/dts/observation/splice-strategies.d.ts +0 -13
  67. package/dist/esm/hooks.js +0 -32
  68. package/dist/esm/observation/splice-strategies.js +0 -400
@@ -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.
@@ -0,0 +1,392 @@
1
+ import { FASTElementDefinition } from "../components/fast-definitions.js";
2
+ import { isFunction, isString } from "../interfaces.js";
3
+ import { bind, normalizeBinding, oneTime, } from "./binding.js";
4
+ import { Binding, HTMLDirective, } from "./html-directive.js";
5
+ import { Markup } from "./markup.js";
6
+ import { html, } from "./template.js";
7
+ /**
8
+ * A Behavior that enables advanced rendering.
9
+ * @public
10
+ */
11
+ export class RenderBehavior {
12
+ /**
13
+ * Creates an instance of RenderBehavior.
14
+ * @param location - A Node representing the location where this behavior will render.
15
+ * @param dataBinding - A binding expression that returns the data to render.
16
+ * @param templateBinding - A binding expression that returns the template to use with the data.
17
+ */
18
+ constructor(directive, location) {
19
+ this.directive = directive;
20
+ this.location = location;
21
+ this.source = null;
22
+ this.view = null;
23
+ this.data = null;
24
+ this.originalContext = void 0;
25
+ this.childContext = void 0;
26
+ this.dataBindingObserver = directive.dataBinding.createObserver(directive, this);
27
+ this.templateBindingObserver = directive.templateBinding.createObserver(directive, this);
28
+ }
29
+ /**
30
+ * Bind this behavior to the source.
31
+ * @param source - The source to bind to.
32
+ * @param context - The execution context that the binding is operating within.
33
+ */
34
+ bind(source, context) {
35
+ this.source = source;
36
+ this.originalContext = context;
37
+ this.childContext = context.createChildContext(source);
38
+ this.data = this.dataBindingObserver.observe(source, this.originalContext);
39
+ this.template = this.templateBindingObserver.observe(source, this.originalContext);
40
+ this.refreshView();
41
+ }
42
+ /**
43
+ * Unbinds this behavior from the source.
44
+ * @param source - The source to unbind from.
45
+ */
46
+ unbind(source, context) {
47
+ this.source = null;
48
+ this.data = null;
49
+ const view = this.view;
50
+ if (view !== null && view.isComposed) {
51
+ view.unbind();
52
+ view.needsBindOnly = true;
53
+ }
54
+ this.dataBindingObserver.dispose();
55
+ this.templateBindingObserver.dispose();
56
+ }
57
+ /** @internal */
58
+ handleChange(source, observer) {
59
+ if (observer === this.dataBindingObserver) {
60
+ this.data = this.dataBindingObserver.observe(this.source, this.originalContext);
61
+ }
62
+ if (this.directive.templateBindingDependsOnData ||
63
+ observer === this.templateBindingObserver) {
64
+ this.template = this.templateBindingObserver.observe(this.source, this.originalContext);
65
+ }
66
+ this.refreshView();
67
+ }
68
+ refreshView() {
69
+ let view = this.view;
70
+ const template = this.template;
71
+ if (view === null) {
72
+ this.view = view = template.create();
73
+ }
74
+ else {
75
+ // If there is a previous view, but it wasn't created
76
+ // from the same template as the new value, then we
77
+ // need to remove the old view if it's still in the DOM
78
+ // and create a new view from the template.
79
+ if (view.$fastTemplate !== template) {
80
+ if (view.isComposed) {
81
+ view.remove();
82
+ view.unbind();
83
+ }
84
+ this.view = view = template.create();
85
+ }
86
+ }
87
+ // It's possible that the value is the same as the previous template
88
+ // and that there's actually no need to compose it.
89
+ if (!view.isComposed) {
90
+ view.isComposed = true;
91
+ view.bind(this.data, this.childContext);
92
+ view.insertBefore(this.location);
93
+ view.$fastTemplate = template;
94
+ }
95
+ else if (view.needsBindOnly) {
96
+ view.needsBindOnly = false;
97
+ view.bind(this.data, this.childContext);
98
+ }
99
+ }
100
+ }
101
+ /**
102
+ * A Directive that enables use of the RenderBehavior.
103
+ * @public
104
+ */
105
+ export class RenderDirective {
106
+ /**
107
+ * Creates an instance of RenderDirective.
108
+ * @param dataBinding - A binding expression that returns the data to render.
109
+ * @param templateBinding - A binding expression that returns the template to use to render the data.
110
+ */
111
+ constructor(dataBinding, templateBinding, templateBindingDependsOnData) {
112
+ this.dataBinding = dataBinding;
113
+ this.templateBinding = templateBinding;
114
+ this.templateBindingDependsOnData = templateBindingDependsOnData;
115
+ }
116
+ /**
117
+ * Creates HTML to be used within a template.
118
+ * @param add - Can be used to add behavior factories to a template.
119
+ */
120
+ createHTML(add) {
121
+ return Markup.comment(add(this));
122
+ }
123
+ /**
124
+ * Creates a behavior.
125
+ * @param targets - The targets available for behaviors to be attached to.
126
+ */
127
+ createBehavior(targets) {
128
+ return new RenderBehavior(this, targets[this.nodeId]);
129
+ }
130
+ }
131
+ HTMLDirective.define(RenderDirective);
132
+ function isElementRenderOptions(object) {
133
+ return !!object.element || !!object.tagName;
134
+ }
135
+ const typeToInstructionLookup = new Map();
136
+ /* eslint @typescript-eslint/naming-convention: "off"*/
137
+ const defaultAttributes = { ":model": x => x };
138
+ const brand = Symbol("RenderInstruction");
139
+ const defaultViewName = "default-view";
140
+ const nullTemplate = html `
141
+ &nbsp;
142
+ `;
143
+ function instructionToTemplate(def) {
144
+ if (def === void 0) {
145
+ return nullTemplate;
146
+ }
147
+ return def.template;
148
+ }
149
+ function createElementTemplate(tagName, attributes, content) {
150
+ const markup = [];
151
+ const values = [];
152
+ if (attributes) {
153
+ const attrNames = Object.getOwnPropertyNames(attributes);
154
+ markup.push(`<${tagName}`);
155
+ for (let i = 0, ii = attrNames.length; i < ii; ++i) {
156
+ const name = attrNames[i];
157
+ if (i === 0) {
158
+ markup[0] = `${markup[0]} ${name}="`;
159
+ }
160
+ else {
161
+ markup.push(`" ${name}="`);
162
+ }
163
+ values.push(attributes[name]);
164
+ }
165
+ markup.push(`">`);
166
+ }
167
+ else {
168
+ markup.push(`<${tagName}>`);
169
+ }
170
+ if (content && isFunction(content.create)) {
171
+ values.push(content);
172
+ markup.push(`</${tagName}>`);
173
+ }
174
+ else {
175
+ const lastIndex = markup.length - 1;
176
+ markup[lastIndex] = `${markup[lastIndex]}${content !== null && content !== void 0 ? content : ""}</${tagName}>`;
177
+ }
178
+ return html(markup, ...values);
179
+ }
180
+ function create(options) {
181
+ var _a, _b;
182
+ const name = (_a = options.name) !== null && _a !== void 0 ? _a : defaultViewName;
183
+ let template;
184
+ if (isElementRenderOptions(options)) {
185
+ let tagName = options.tagName;
186
+ if (!tagName) {
187
+ const def = FASTElementDefinition.getByType(options.element);
188
+ if (def) {
189
+ tagName = def.name;
190
+ }
191
+ else {
192
+ throw new Error("Invalid element for model rendering.");
193
+ }
194
+ }
195
+ template = createElementTemplate(tagName, (_b = options.attributes) !== null && _b !== void 0 ? _b : defaultAttributes, options.content);
196
+ }
197
+ else {
198
+ template = options.template;
199
+ }
200
+ return {
201
+ brand,
202
+ type: options.type,
203
+ name,
204
+ template,
205
+ };
206
+ }
207
+ function instanceOf(object) {
208
+ return object && object.brand === brand;
209
+ }
210
+ function register(optionsOrInstruction) {
211
+ let lookup = typeToInstructionLookup.get(optionsOrInstruction.type);
212
+ if (lookup === void 0) {
213
+ typeToInstructionLookup.set(optionsOrInstruction.type, (lookup = Object.create(null)));
214
+ }
215
+ const instruction = instanceOf(optionsOrInstruction)
216
+ ? optionsOrInstruction
217
+ : create(optionsOrInstruction);
218
+ return (lookup[instruction.name] = instruction);
219
+ }
220
+ function getByType(type, name) {
221
+ const entries = typeToInstructionLookup.get(type);
222
+ if (entries === void 0) {
223
+ return void 0;
224
+ }
225
+ return entries[name !== null && name !== void 0 ? name : defaultViewName];
226
+ }
227
+ function getForInstance(object, name) {
228
+ if (object) {
229
+ return getByType(object.constructor, name);
230
+ }
231
+ return void 0;
232
+ }
233
+ /**
234
+ * Provides APIs for creating and interacting with render instructions.
235
+ * @public
236
+ */
237
+ export const RenderInstruction = Object.freeze({
238
+ /**
239
+ * Checks whether the provided object is a RenderInstruction.
240
+ * @param object - The object to check.
241
+ * @returns true if the object is a RenderInstruction; false otherwise
242
+ */
243
+ instanceOf,
244
+ /**
245
+ * Creates a RenderInstruction for a set of options.
246
+ * @param options - The options to use when creating the RenderInstruction.
247
+ */
248
+ create,
249
+ /**
250
+ * Creates a template based on a tag name.
251
+ * @param tagName - The tag name to use when creating the template.
252
+ * @param attributes - The attributes to apply to the element.
253
+ * @param content - The content to insert into the element.
254
+ * @returns A template based on the provided specifications.
255
+ */
256
+ createElementTemplate,
257
+ /**
258
+ * Creates and registers an instruction.
259
+ * @param options The options to use when creating the RenderInstruction.
260
+ * @remarks
261
+ * A previously created RenderInstruction can also be registered.
262
+ */
263
+ register,
264
+ /**
265
+ * Finds a previously registered RenderInstruction by type and optionally by name.
266
+ * @param type - The type to retrieve the RenderInstruction for.
267
+ * @param name - An optional name used in differentiating between multiple registered instructions.
268
+ * @returns The located RenderInstruction that matches the criteria or undefined if none is found.
269
+ */
270
+ getByType,
271
+ /**
272
+ * Finds a previously registered RenderInstruction for the instance's type and optionally by name.
273
+ * @param object - The instance to retrieve the RenderInstruction for.
274
+ * @param name - An optional name used in differentiating between multiple registered instructions.
275
+ * @returns The located RenderInstruction that matches the criteria or undefined if none is found.
276
+ */
277
+ getForInstance,
278
+ });
279
+ export function renderWith(value, name) {
280
+ return function (type) {
281
+ if (isFunction(value)) {
282
+ register({ type, element: value, name });
283
+ }
284
+ else if (isFunction(value.create)) {
285
+ register({ type, template: value, name });
286
+ }
287
+ else {
288
+ register(Object.assign({ type }, value));
289
+ }
290
+ };
291
+ }
292
+ /**
293
+ * @internal
294
+ */
295
+ export class NodeTemplate {
296
+ constructor(node) {
297
+ this.node = node;
298
+ node.$fastTemplate = this;
299
+ }
300
+ bind(source, context) { }
301
+ unbind() { }
302
+ insertBefore(refNode) {
303
+ refNode.parentNode.insertBefore(this.node, refNode);
304
+ }
305
+ remove() {
306
+ this.node.parentNode.removeChild(this.node);
307
+ }
308
+ create() {
309
+ return this;
310
+ }
311
+ }
312
+ /**
313
+ * Creates a RenderDirective for use in advanced rendering scenarios.
314
+ * @param value - The binding expression that returns the data to be rendered. The expression
315
+ * can also return a Node to render directly.
316
+ * @param template - A template to render the data with
317
+ * or a string to indicate which RenderInstruction to use when looking up a RenderInstruction.
318
+ * Expressions can also be provided to dynamically determine either the template or the name.
319
+ * @returns A RenderDirective suitable for use in a template.
320
+ * @remarks
321
+ * If no binding is provided, then a default binding that returns the source is created.
322
+ * If no template is provided, then a binding is created that will use registered
323
+ * RenderInstructions to determine the view.
324
+ * If the template binding returns a string, then it will be used to look up a
325
+ * RenderInstruction to determine the view.
326
+ * @public
327
+ */
328
+ export function render(value, template) {
329
+ let dataBinding;
330
+ if (value === void 0) {
331
+ dataBinding = oneTime((source) => source);
332
+ }
333
+ else {
334
+ dataBinding = normalizeBinding(value);
335
+ }
336
+ let templateBinding;
337
+ let templateBindingDependsOnData = false;
338
+ if (template === void 0) {
339
+ templateBindingDependsOnData = true;
340
+ templateBinding = oneTime((s, c) => {
341
+ var _a;
342
+ const data = dataBinding.evaluate(s, c);
343
+ if (data instanceof Node) {
344
+ return (_a = data.$fastTemplate) !== null && _a !== void 0 ? _a : new NodeTemplate(data);
345
+ }
346
+ return instructionToTemplate(getForInstance(data));
347
+ });
348
+ }
349
+ else if (isFunction(template)) {
350
+ templateBinding = bind((s, c) => {
351
+ var _a;
352
+ let result = template(s, c);
353
+ if (isString(result)) {
354
+ result = instructionToTemplate(getForInstance(dataBinding.evaluate(s, c), result));
355
+ }
356
+ else if (result instanceof Node) {
357
+ result = (_a = result.$fastTemplate) !== null && _a !== void 0 ? _a : new NodeTemplate(result);
358
+ }
359
+ return result;
360
+ }, true);
361
+ }
362
+ else if (isString(template)) {
363
+ templateBindingDependsOnData = true;
364
+ templateBinding = oneTime((s, c) => {
365
+ var _a;
366
+ const data = dataBinding.evaluate(s, c);
367
+ if (data instanceof Node) {
368
+ return (_a = data.$fastTemplate) !== null && _a !== void 0 ? _a : new NodeTemplate(data);
369
+ }
370
+ return instructionToTemplate(getForInstance(data, template));
371
+ });
372
+ }
373
+ else if (template instanceof Binding) {
374
+ const evaluateTemplate = template.evaluate;
375
+ template.evaluate = (s, c) => {
376
+ var _a;
377
+ let result = evaluateTemplate(s, c);
378
+ if (isString(result)) {
379
+ result = instructionToTemplate(getForInstance(dataBinding.evaluate(s, c), result));
380
+ }
381
+ else if (result instanceof Node) {
382
+ result = (_a = result.$fastTemplate) !== null && _a !== void 0 ? _a : new NodeTemplate(result);
383
+ }
384
+ return result;
385
+ };
386
+ templateBinding = template;
387
+ }
388
+ else {
389
+ templateBinding = oneTime((s, c) => template);
390
+ }
391
+ return new RenderDirective(dataBinding, templateBinding, templateBindingDependsOnData);
392
+ }