@player-ui/player 0.8.0-next.4 → 0.8.0-next.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 (37) hide show
  1. package/dist/Player.native.js +240 -252
  2. package/dist/Player.native.js.map +1 -1
  3. package/dist/cjs/index.cjs +208 -268
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/index.legacy-esm.js +206 -270
  6. package/dist/index.mjs +206 -270
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +4 -4
  9. package/src/plugins/default-view-plugin.ts +4 -0
  10. package/src/view/__tests__/view.immutable.test.ts +8 -1
  11. package/src/view/__tests__/view.test.ts +59 -1
  12. package/src/view/parser/__tests__/parser.test.ts +16 -1
  13. package/src/view/parser/index.ts +46 -262
  14. package/src/view/parser/utils.ts +23 -3
  15. package/src/view/plugins/__tests__/__snapshots__/asset.test.ts.snap +215 -0
  16. package/src/view/plugins/__tests__/__snapshots__/multi-node.test.ts.snap +67 -0
  17. package/src/view/plugins/__tests__/__snapshots__/template.test.ts.snap +56 -54
  18. package/src/view/plugins/__tests__/applicability.test.ts +24 -16
  19. package/src/view/plugins/__tests__/asset.test.ts +140 -0
  20. package/src/view/plugins/__tests__/multi-node.test.ts +38 -0
  21. package/src/view/plugins/__tests__/template.test.ts +48 -388
  22. package/src/view/plugins/applicability.ts +39 -23
  23. package/src/view/plugins/asset.ts +42 -0
  24. package/src/view/plugins/index.ts +3 -1
  25. package/src/view/plugins/multi-node.ts +73 -0
  26. package/src/view/plugins/switch.ts +81 -50
  27. package/src/view/plugins/{template-plugin.ts → template.ts} +38 -24
  28. package/src/view/resolver/__tests__/edgecases.test.ts +14 -1
  29. package/src/view/view.ts +2 -7
  30. package/types/view/parser/index.d.ts +6 -11
  31. package/types/view/parser/utils.d.ts +11 -2
  32. package/types/view/plugins/applicability.d.ts +1 -0
  33. package/types/view/plugins/asset.d.ts +8 -0
  34. package/types/view/plugins/index.d.ts +3 -1
  35. package/types/view/plugins/multi-node.d.ts +8 -0
  36. package/types/view/plugins/switch.d.ts +2 -1
  37. package/types/view/plugins/{template-plugin.d.ts → template.d.ts} +2 -2
@@ -4,135 +4,27 @@ import type { DataModelWithParser } from "../../../data";
4
4
  import { LocalModel, withParser } from "../../../data";
5
5
  import { ExpressionEvaluator } from "../../../expressions";
6
6
  import { SchemaController } from "../../../schema";
7
- import { NodeType } from "../../parser";
8
7
  import { Parser } from "../../parser";
9
8
  import { ViewInstance } from "../../view";
10
9
  import type { Options } from "../options";
11
- import TemplatePlugin from "../template-plugin";
10
+ import { TemplatePlugin, MultiNodePlugin, AssetPlugin } from "../";
12
11
  import { StringResolverPlugin, toNodeResolveOptions } from "../..";
13
12
 
14
13
  const templateJoinValues = {
15
- id: "snippet-of-json",
16
- topic: "Snippet",
17
- schema: {},
18
- data: {
19
- forms: {
20
- "1099-A": [
21
- {
22
- description: "Desciption of concept 1099 1",
23
- amount: "Help",
24
- },
25
- ],
26
- "1099-B": [
27
- {
28
- description: "Desciption of concept 1099 2",
29
- amount: "Help",
30
- },
31
- ],
32
- },
33
- },
14
+ id: "generated-flow",
34
15
  views: [
35
16
  {
36
- id: "overviewGroup",
37
- type: "overviewGroup",
38
- metaData: {
39
- role: "stateful",
40
- },
41
- modifiers: [
42
- {
43
- type: "tag",
44
- value: "fancy-header",
45
- },
46
- ],
47
- headers: {
48
- label: {
49
- asset: {
50
- id: "line-of-work-summary-gh-header-label",
51
- type: "text",
52
- value: "Header",
53
- },
54
- },
55
- values: [
56
- {
57
- asset: {
58
- id: "line-of-work-summary-gh-expenses-simple-header-previous-year",
59
- type: "text",
60
- value: "Type",
61
- },
62
- },
63
- {
64
- asset: {
65
- id: "line-of-work-summary-gh-expenses-simple-header-cy",
66
- type: "text",
67
- value: "2022",
68
- },
69
- },
70
- ],
71
- },
17
+ id: "collection",
18
+ type: "collection",
72
19
  template: [
73
20
  {
74
- data: "forms.1099-A",
21
+ data: "foo",
75
22
  output: "values",
76
23
  value: {
77
24
  asset: {
78
- id: "overviewItem3",
79
- type: "overviewItem",
80
- label: {
81
- asset: {
82
- id: "overviewItem3-label",
83
- type: "text",
84
- value: "1099-A",
85
- },
86
- },
87
- values: [
88
- {
89
- asset: {
90
- id: "overviewItem3-year",
91
- type: "text",
92
- value: "Desciption of concept 1099 1",
93
- },
94
- },
95
- {
96
- asset: {
97
- id: "loverviewItem3-cy",
98
- type: "text",
99
- value: "4000",
100
- },
101
- },
102
- ],
103
- },
104
- },
105
- },
106
- {
107
- data: "forms.1099-B",
108
- output: "values",
109
- value: {
110
- asset: {
111
- id: "overviewItem4",
112
- type: "overviewItem",
113
- label: {
114
- asset: {
115
- id: "overviewItem4-label",
116
- type: "text",
117
- value: "1099-B",
118
- },
119
- },
120
- values: [
121
- {
122
- asset: {
123
- id: "overviewItem4-year",
124
- type: "text",
125
- value: "Desciption of concept 1099 2",
126
- },
127
- },
128
- {
129
- asset: {
130
- id: "loverviewItem3-cy",
131
- type: "text",
132
- value: "6000",
133
- },
134
- },
135
- ],
25
+ id: "value-_index_",
26
+ type: "text",
27
+ value: "item {{foo._index_}}",
136
28
  },
137
29
  },
138
30
  },
@@ -140,238 +32,71 @@ const templateJoinValues = {
140
32
  values: [
141
33
  {
142
34
  asset: {
143
- id: "overviewItem1",
144
- type: "overviewItem",
145
- label: {
146
- asset: {
147
- id: "overviewItem1-label",
148
- type: "text",
149
- value: "First Summary",
150
- },
151
- },
152
- values: [
153
- {
154
- asset: {
155
- id: "overviewItem1-year",
156
- type: "text",
157
- value: "Desciption of year summary 1",
158
- },
159
- },
160
- {
161
- asset: {
162
- id: "loverviewItem1-cy",
163
- type: "text",
164
- value: "14000",
165
- },
166
- },
167
- ],
35
+ id: "value-2",
36
+ type: "text",
37
+ value: "First value in the collection",
168
38
  },
169
39
  },
170
40
  {
171
41
  asset: {
172
- id: "overviewItem2",
173
- type: "overviewItem",
174
- label: {
175
- asset: {
176
- id: "overviewItem2-label",
177
- type: "text",
178
- value: "Second year Summary",
179
- },
180
- },
181
- values: [
182
- {
183
- asset: {
184
- id: "overviewItem2-year",
185
- type: "text",
186
- value: "Desciption of year summary item 2",
187
- },
188
- },
189
- {
190
- asset: {
191
- id: "loverviewItem1-cy",
192
- type: "text",
193
- value: "19000",
194
- },
195
- },
196
- ],
42
+ id: "value-3",
43
+ type: "text",
44
+ value: "Second value in the collection",
197
45
  },
198
46
  },
199
47
  ],
200
48
  },
201
49
  {
202
- id: "overviewGroup",
203
- type: "overviewGroup",
204
- metaData: {
205
- role: "stateful",
206
- },
207
- modifiers: [
208
- {
209
- type: "tag",
210
- value: "fancy-header",
211
- },
212
- ],
213
- headers: {
214
- label: {
215
- asset: {
216
- id: "line-of-work-summary-gh-header-label",
217
- type: "text",
218
- value: "Header",
219
- },
220
- },
221
- values: [
222
- {
223
- asset: {
224
- id: "line-of-work-summary-gh-expenses-simple-header-previous-year",
225
- type: "text",
226
- value: "Type",
227
- },
228
- },
229
- {
230
- asset: {
231
- id: "line-of-work-summary-gh-expenses-simple-header-cy",
232
- type: "text",
233
- value: "2022",
234
- },
235
- },
236
- ],
237
- },
50
+ id: "collection",
51
+ type: "collection",
238
52
  values: [
239
53
  {
240
54
  asset: {
241
- id: "overviewItem1",
242
- type: "overviewItem",
243
- label: {
244
- asset: {
245
- id: "overviewItem1-label",
246
- type: "text",
247
- value: "First Summary",
248
- },
249
- },
250
- values: [
251
- {
252
- asset: {
253
- id: "overviewItem1-year",
254
- type: "text",
255
- value: "Desciption of year summary 1",
256
- },
257
- },
258
- {
259
- asset: {
260
- id: "loverviewItem1-cy",
261
- type: "text",
262
- value: "14000",
263
- },
264
- },
265
- ],
55
+ id: "value-2",
56
+ type: "text",
57
+ value: "First value in the collection",
266
58
  },
267
59
  },
268
60
  {
269
61
  asset: {
270
- id: "overviewItem2",
271
- type: "overviewItem",
272
- label: {
273
- asset: {
274
- id: "overviewItem2-label",
275
- type: "text",
276
- value: "Second year Summary",
277
- },
278
- },
279
- values: [
280
- {
281
- asset: {
282
- id: "overviewItem2-year",
283
- type: "text",
284
- value: "Desciption of year summary item 2",
285
- },
286
- },
287
- {
288
- asset: {
289
- id: "loverviewItem1-cy",
290
- type: "text",
291
- value: "19000",
292
- },
293
- },
294
- ],
62
+ id: "value-3",
63
+ type: "text",
64
+ value: "Second value in the collection",
295
65
  },
296
66
  },
297
67
  ],
298
68
  template: [
299
69
  {
300
- data: "forms.1099-A",
301
- output: "values",
302
- value: {
303
- asset: {
304
- id: "overviewItem3",
305
- type: "overviewItem",
306
- label: {
307
- asset: {
308
- id: "overviewItem3-label",
309
- type: "text",
310
- value: "1099-A",
311
- },
312
- },
313
- values: [
314
- {
315
- asset: {
316
- id: "overviewItem3-year",
317
- type: "text",
318
- value: "Desciption of concept 1099 1",
319
- },
320
- },
321
- {
322
- asset: {
323
- id: "loverviewItem3-cy",
324
- type: "text",
325
- value: "4000",
326
- },
327
- },
328
- ],
329
- },
330
- },
331
- },
332
- {
333
- data: "forms.1099-B",
70
+ data: "foo",
334
71
  output: "values",
335
72
  value: {
336
73
  asset: {
337
- id: "overviewItem4",
338
- type: "overviewItem",
339
- label: {
340
- asset: {
341
- id: "overviewItem4-label",
342
- type: "text",
343
- value: "1099-B",
344
- },
345
- },
346
- values: [
347
- {
348
- asset: {
349
- id: "overviewItem4-year",
350
- type: "text",
351
- value: "Desciption of concept 1099 2",
352
- },
353
- },
354
- {
355
- asset: {
356
- id: "loverviewItem3-cy",
357
- type: "text",
358
- value: "6000",
359
- },
360
- },
361
- ],
74
+ id: "value-_index_",
75
+ type: "text",
76
+ value: "item {{foo._index_}}",
362
77
  },
363
78
  },
364
79
  },
365
80
  ],
366
81
  },
367
82
  ],
83
+ data: {
84
+ foo: [1, 2],
85
+ },
368
86
  navigation: {
369
- BEGIN: "SnippetFlow",
370
- SnippetFlow: {
371
- startState: "VIEW_Snippet-View1",
372
- "VIEW_Snippet-View1": {
373
- ref: "overviewGroup",
87
+ BEGIN: "FLOW_1",
88
+ FLOW_1: {
89
+ startState: "VIEW_1",
90
+ VIEW_1: {
374
91
  state_type: "VIEW",
92
+ ref: "collection",
93
+ transitions: {
94
+ "*": "END_Done",
95
+ },
96
+ },
97
+ END_Done: {
98
+ state_type: "END",
99
+ outcome: "done",
375
100
  },
376
101
  },
377
102
  },
@@ -401,6 +126,7 @@ describe("templates", () => {
401
126
  },
402
127
  };
403
128
  new TemplatePlugin(options).applyParser(parser);
129
+ new AssetPlugin().applyParser(parser);
404
130
  });
405
131
 
406
132
  it("works with simple ones", () => {
@@ -459,76 +185,6 @@ describe("templates", () => {
459
185
  }),
460
186
  ).toMatchSnapshot();
461
187
  });
462
-
463
- it("determines if nodeType is template", () => {
464
- const nodeTest = "template";
465
- const nodeType = parser.hooks.determineNodeType.call(nodeTest);
466
- expect(nodeType).toStrictEqual("template");
467
- });
468
-
469
- it("Does not return a nodeType", () => {
470
- const nodeTest = {
471
- value: "foo",
472
- };
473
- const nodeType = parser.hooks.determineNodeType.call(nodeTest);
474
- expect(nodeType).toBe(undefined);
475
- });
476
-
477
- it("returns templateNode if template exists", () => {
478
- const obj = {
479
- dynamic: true,
480
- data: "foo.bar",
481
- output: "values",
482
- value: {
483
- value: "{{foo.bar._index_}}",
484
- },
485
- };
486
- const nodeOptions = {
487
- templateDepth: 1,
488
- };
489
- const parsedNode = parser.hooks.parseNode.call(
490
- obj,
491
- NodeType.Value,
492
- nodeOptions,
493
- NodeType.Template,
494
- );
495
- expect(parsedNode).toStrictEqual({
496
- data: "foo.bar",
497
- depth: 1,
498
- dynamic: true,
499
- template: {
500
- value: "{{foo.bar._index_}}",
501
- },
502
- type: "template",
503
- });
504
- });
505
-
506
- it("returns templateNode if template exists, and templateDepth is not set", () => {
507
- const obj = {
508
- data: "foo.bar2",
509
- output: "values",
510
- dynamic: true,
511
- value: {
512
- value: "{{foo.bar2._index_}}",
513
- },
514
- };
515
- const nodeOptions = {};
516
- const parsedNode = parser.hooks.parseNode.call(
517
- obj,
518
- NodeType.Value,
519
- nodeOptions,
520
- NodeType.Template,
521
- );
522
- expect(parsedNode).toStrictEqual({
523
- data: "foo.bar2",
524
- depth: 0,
525
- dynamic: true,
526
- template: {
527
- value: "{{foo.bar2._index_}}",
528
- },
529
- type: "template",
530
- });
531
- });
532
188
  });
533
189
 
534
190
  describe("dynamic templates", () => {
@@ -695,13 +351,15 @@ describe("dynamic templates", () => {
695
351
  });
696
352
 
697
353
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
354
+ new AssetPlugin().apply(view);
698
355
  new TemplatePlugin(pluginOptions).apply(view);
699
356
  new StringResolverPlugin().apply(view);
357
+ new MultiNodePlugin().apply(view);
700
358
 
701
359
  const resolved = view.update();
702
360
 
703
361
  expect(resolved.values).toHaveLength(4);
704
- expect(resolved.values[0]).toMatchSnapshot();
362
+ expect(resolved.values).toMatchSnapshot();
705
363
  });
706
364
  it("Should show template item last when coming after values on lexical order", () => {
707
365
  const view = new ViewInstance(templateJoinValues.views[1], {
@@ -712,13 +370,15 @@ describe("dynamic templates", () => {
712
370
  });
713
371
 
714
372
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
373
+ new AssetPlugin().apply(view);
715
374
  new TemplatePlugin(pluginOptions).apply(view);
716
375
  new StringResolverPlugin().apply(view);
376
+ new MultiNodePlugin().apply(view);
717
377
 
718
378
  const resolved = view.update();
719
379
 
720
380
  expect(resolved.values).toHaveLength(4);
721
- expect(resolved.values[0]).toMatchSnapshot();
381
+ expect(resolved.values).toMatchSnapshot();
722
382
  });
723
383
  });
724
384
  });
@@ -1,12 +1,21 @@
1
1
  import { omit } from "timm";
2
2
  import type { Options } from "./options";
3
3
  import type { Resolver } from "../resolver";
4
- import type { Node, ParseObjectOptions, Parser } from "../parser";
4
+ import type {
5
+ Node,
6
+ ParseObjectOptions,
7
+ ParseObjectChildOptions,
8
+ Parser,
9
+ } from "../parser";
5
10
  import { NodeType } from "../parser";
6
11
  import { ViewInstance, ViewPlugin } from "../view";
7
12
 
8
13
  /** A view plugin to remove inapplicable assets from the tree */
9
14
  export default class ApplicabilityPlugin implements ViewPlugin {
15
+ private isApplicability(obj: any) {
16
+ return obj && Object.prototype.hasOwnProperty.call(obj, "applicability");
17
+ }
18
+
10
19
  applyResolver(resolver: Resolver) {
11
20
  resolver.hooks.beforeResolve.tap(
12
21
  "applicability",
@@ -29,43 +38,50 @@ export default class ApplicabilityPlugin implements ViewPlugin {
29
38
  }
30
39
 
31
40
  applyParser(parser: Parser) {
32
- /** Switches resolved during the parsing phase are static */
33
- parser.hooks.determineNodeType.tap("applicability", (obj: any) => {
34
- if (Object.prototype.hasOwnProperty.call(obj, "applicability")) {
35
- return NodeType.Applicability;
36
- }
37
- });
38
-
39
41
  parser.hooks.parseNode.tap(
40
42
  "applicability",
41
43
  (
42
44
  obj: any,
43
45
  nodeType: Node.ChildrenTypes,
44
46
  options: ParseObjectOptions,
45
- determinedNodeType: null | NodeType,
47
+ childOptions?: ParseObjectChildOptions,
46
48
  ) => {
47
- if (determinedNodeType === NodeType.Applicability) {
49
+ if (this.isApplicability(obj)) {
48
50
  const parsedApplicability = parser.parseObject(
49
51
  omit(obj, "applicability"),
50
52
  nodeType,
51
53
  options,
52
54
  );
53
- if (parsedApplicability !== null) {
54
- const applicabilityNode = parser.createASTNode(
55
- {
56
- type: NodeType.Applicability,
57
- expression: (obj as any).applicability,
58
- value: parsedApplicability,
59
- },
60
- obj,
61
- );
62
55
 
63
- if (applicabilityNode?.type === NodeType.Applicability) {
64
- applicabilityNode.value.parent = applicabilityNode;
65
- }
56
+ if (!parsedApplicability) {
57
+ return childOptions ? [] : null;
58
+ }
66
59
 
67
- return applicabilityNode;
60
+ const applicabilityNode = parser.createASTNode(
61
+ {
62
+ type: NodeType.Applicability,
63
+ expression: (obj as any).applicability,
64
+ value: parsedApplicability,
65
+ },
66
+ obj,
67
+ );
68
+
69
+ if (!applicabilityNode) {
70
+ return childOptions ? [] : null;
71
+ }
72
+
73
+ if (applicabilityNode.type === NodeType.Applicability) {
74
+ applicabilityNode.value.parent = applicabilityNode;
68
75
  }
76
+
77
+ return childOptions
78
+ ? [
79
+ {
80
+ path: [...childOptions.path, childOptions.key],
81
+ value: applicabilityNode,
82
+ },
83
+ ]
84
+ : applicabilityNode;
69
85
  }
70
86
  },
71
87
  );
@@ -0,0 +1,42 @@
1
+ import { ViewInstance, ViewPlugin } from "../view";
2
+ import type {
3
+ Parser,
4
+ Node,
5
+ ParseObjectOptions,
6
+ ParseObjectChildOptions,
7
+ } from "../parser";
8
+ import { NodeType } from "../parser";
9
+
10
+ /** A view plugin to resolve assets */
11
+ export default class AssetPlugin implements ViewPlugin {
12
+ applyParser(parser: Parser) {
13
+ parser.hooks.parseNode.tap(
14
+ "asset",
15
+ (
16
+ obj: any,
17
+ nodeType: Node.ChildrenTypes,
18
+ options: ParseObjectOptions,
19
+ childOptions?: ParseObjectChildOptions,
20
+ ) => {
21
+ if (childOptions?.key === "asset" && typeof obj === "object") {
22
+ const assetAST = parser.parseObject(obj, NodeType.Asset, options);
23
+
24
+ if (!assetAST) {
25
+ return [];
26
+ }
27
+
28
+ return [
29
+ {
30
+ path: [...childOptions.path, childOptions.key],
31
+ value: assetAST,
32
+ },
33
+ ];
34
+ }
35
+ },
36
+ );
37
+ }
38
+
39
+ apply(view: ViewInstance) {
40
+ view.hooks.parser.tap("asset", this.applyParser.bind(this));
41
+ }
42
+ }
@@ -1,4 +1,6 @@
1
- export { default as TemplatePlugin } from "./template-plugin";
1
+ export { default as TemplatePlugin } from "./template";
2
2
  export { default as StringResolverPlugin } from "./string-resolver";
3
3
  export { default as ApplicabilityPlugin } from "./applicability";
4
4
  export { default as SwitchPlugin } from "./switch";
5
+ export { default as MultiNodePlugin } from "./multi-node";
6
+ export { default as AssetPlugin } from "./asset";