|
1 | | -import getNodeScopedState from "#ehtml/getNodeScopedState.js?v=41ab2bfa"; |
2 | | -import setNodeScopedState from "#ehtml/setNodeScopedState.js?v=7806d68f"; |
3 | | -import evaluatedValueWithParamsFromState from "#ehtml/evaluatedValueWithParamsFromState.js?v=a8e84941"; |
| 1 | +import getNodeScopedState from '#ehtml/getNodeScopedState.js?v=41ab2bfa' |
| 2 | +import setNodeScopedState from '#ehtml/setNodeScopedState.js?v=7806d68f' |
| 3 | +import evaluatedValueWithParamsFromState from '#ehtml/evaluatedValueWithParamsFromState.js?v=a8e84941' |
4 | 4 |
|
5 | 5 | export default class EForEachTemplate extends HTMLTemplateElement { |
6 | 6 | constructor () { |
7 | | - super(); |
8 | | - this.ehtmlActivated = false; |
| 7 | + super() |
| 8 | + this.ehtmlActivated = false |
9 | 9 | } |
10 | 10 |
|
11 | 11 | connectedCallback () { |
12 | | - this.addEventListener("ehtml:activated", this.onEHTMLActivated, { once: true }); |
| 12 | + this.addEventListener('ehtml:activated', this.onEHTMLActivated, { once: true }) |
13 | 13 | } |
14 | 14 |
|
15 | 15 | onEHTMLActivated () { |
16 | 16 | if (this.ehtmlActivated) { |
17 | | - return; |
| 17 | + return |
18 | 18 | } |
19 | | - this.ehtmlActivated = true; |
20 | | - this.run(); |
| 19 | + this.ehtmlActivated = true |
| 20 | + this.run() |
21 | 21 | } |
22 | 22 |
|
23 | 23 | run () { |
24 | | - const listExpr = this.getAttribute("data-list-to-iterate"); |
25 | | - const itemName = this.getAttribute("data-item-name"); |
| 24 | + const listExpr = this.getAttribute('data-list-to-iterate') |
| 25 | + const itemName = this.getAttribute('data-item-name') |
26 | 26 |
|
27 | 27 | if (!listExpr) { |
28 | | - throw new Error("<template is=\"e-for-each\"> must have data-list-to-iterate"); |
| 28 | + throw new Error('<template is="e-for-each"> must have data-list-to-iterate') |
29 | 29 | } |
30 | 30 |
|
31 | 31 | if (!itemName) { |
32 | | - throw new Error("<template is=\"e-for-each\"> must have data-item-name"); |
| 32 | + throw new Error('<template is="e-for-each"> must have data-item-name') |
33 | 33 | } |
34 | 34 |
|
35 | 35 | // 1. Get inherited lexical state at this <template> |
36 | | - const state = getNodeScopedState(this); |
| 36 | + const state = getNodeScopedState(this) |
37 | 37 |
|
38 | 38 | // 2. Evaluate the list expression |
39 | 39 | const list = evaluatedValueWithParamsFromState( |
40 | | - listExpr.replace(/\n/g, " "), |
| 40 | + listExpr.replace(/\n/g, ' '), |
41 | 41 | state, |
42 | 42 | this |
43 | | - ); |
| 43 | + ) |
44 | 44 |
|
45 | 45 | if (!Array.isArray(list)) { |
46 | | - throw new Error("data-list-to-iterate must evaluate to an array"); |
| 46 | + throw new Error('data-list-to-iterate must evaluate to an array') |
47 | 47 | } |
48 | 48 |
|
49 | 49 | // 3. Prepare fragment to insert |
50 | | - const fragment = document.createDocumentFragment(); |
| 50 | + const fragment = document.createDocumentFragment() |
51 | 51 |
|
52 | 52 | list.forEach((item, index) => { |
53 | 53 | // Add index if not set |
54 | | - if (typeof item === "object" && item.index === undefined) { |
55 | | - item.index = index + 1; |
| 54 | + if (typeof item === 'object' && item.index === undefined) { |
| 55 | + item.index = index + 1 |
56 | 56 | } |
57 | 57 |
|
58 | 58 | // Clone inert DOM from template |
59 | | - const cloned = this.content.cloneNode(true); |
| 59 | + const cloned = this.content.cloneNode(true) |
60 | 60 |
|
61 | 61 | // 4. Build item-level state (lexical extension) |
62 | 62 | const itemState = { |
63 | 63 | ...state, |
64 | 64 | [itemName]: item |
65 | | - }; |
| 65 | + } |
66 | 66 |
|
67 | 67 | // 5. Assign the new lexical state to this *iteration wrapper* |
68 | 68 | // |
69 | 69 | // Since `cloned` is a DocumentFragment of the template content, |
70 | 70 | // we assign state to each root element in it. |
71 | 71 | for (const child of cloned.childNodes) { |
72 | 72 | if (child.nodeType === 1) { |
73 | | - setNodeScopedState(child, itemState); |
| 73 | + setNodeScopedState(child, itemState) |
74 | 74 | } |
75 | 75 | } |
76 | 76 |
|
77 | | - fragment.appendChild(cloned); |
78 | | - }); |
| 77 | + fragment.appendChild(cloned) |
| 78 | + }) |
79 | 79 |
|
80 | 80 | // 6. Replace <template> with generated DOM |
81 | 81 | // MutationObserver will activate and process new nodes. |
82 | | - this.replaceWith(fragment); |
| 82 | + this.replaceWith(fragment) |
83 | 83 | } |
84 | 84 | } |
85 | 85 |
|
86 | | -customElements.define("e-for-each", EForEachTemplate, { extends: "template" }); |
| 86 | +customElements.define('e-for-each', EForEachTemplate, { extends: 'template' }) |
0 commit comments