@joist/templating 4.2.3-next.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.
- package/LICENSE +21 -0
- package/README.md +370 -0
- package/package.json +69 -0
- package/src/lib/bind.ts +40 -0
- package/src/lib/define.ts +5 -0
- package/src/lib/elements/async.element.test.ts +90 -0
- package/src/lib/elements/async.element.ts +119 -0
- package/src/lib/elements/for.element.test.ts +221 -0
- package/src/lib/elements/for.element.ts +176 -0
- package/src/lib/elements/if.element.test.ts +90 -0
- package/src/lib/elements/if.element.ts +85 -0
- package/src/lib/elements/props.element.test.ts +62 -0
- package/src/lib/elements/props.element.ts +76 -0
- package/src/lib/elements/scope.ts +39 -0
- package/src/lib/elements/value.element.test.ts +20 -0
- package/src/lib/elements/value.element.ts +39 -0
- package/src/lib/events.ts +21 -0
- package/src/lib/token.test.ts +74 -0
- package/src/lib/token.ts +34 -0
- package/src/lib.ts +2 -0
- package/target/lib/bind.d.ts +1 -0
- package/target/lib/bind.js +30 -0
- package/target/lib/bind.js.map +1 -0
- package/target/lib/define.d.ts +5 -0
- package/target/lib/define.js +6 -0
- package/target/lib/define.js.map +1 -0
- package/target/lib/elements/async.element.d.ts +17 -0
- package/target/lib/elements/async.element.js +112 -0
- package/target/lib/elements/async.element.js.map +1 -0
- package/target/lib/elements/async.element.test.d.ts +1 -0
- package/target/lib/elements/async.element.test.js +75 -0
- package/target/lib/elements/async.element.test.js.map +1 -0
- package/target/lib/elements/for.element.d.ts +24 -0
- package/target/lib/elements/for.element.js +182 -0
- package/target/lib/elements/for.element.js.map +1 -0
- package/target/lib/elements/for.element.test.d.ts +2 -0
- package/target/lib/elements/for.element.test.js +153 -0
- package/target/lib/elements/for.element.test.js.map +1 -0
- package/target/lib/elements/if.element.d.ts +12 -0
- package/target/lib/elements/if.element.js +81 -0
- package/target/lib/elements/if.element.js.map +1 -0
- package/target/lib/elements/if.element.test.d.ts +1 -0
- package/target/lib/elements/if.element.test.js +78 -0
- package/target/lib/elements/if.element.test.js.map +1 -0
- package/target/lib/elements/props.element.d.ts +11 -0
- package/target/lib/elements/props.element.js +90 -0
- package/target/lib/elements/props.element.js.map +1 -0
- package/target/lib/elements/props.element.test.d.ts +1 -0
- package/target/lib/elements/props.element.test.js +53 -0
- package/target/lib/elements/props.element.test.js.map +1 -0
- package/target/lib/elements/scope.d.ts +13 -0
- package/target/lib/elements/scope.js +56 -0
- package/target/lib/elements/scope.js.map +1 -0
- package/target/lib/elements/value.element.d.ts +9 -0
- package/target/lib/elements/value.element.js +54 -0
- package/target/lib/elements/value.element.js.map +1 -0
- package/target/lib/elements/value.element.test.d.ts +1 -0
- package/target/lib/elements/value.element.test.js +16 -0
- package/target/lib/elements/value.element.test.js.map +1 -0
- package/target/lib/events.d.ts +12 -0
- package/target/lib/events.js +10 -0
- package/target/lib/events.js.map +1 -0
- package/target/lib/token.d.ts +8 -0
- package/target/lib/token.js +27 -0
- package/target/lib/token.js.map +1 -0
- package/target/lib/token.test.d.ts +1 -0
- package/target/lib/token.test.js +56 -0
- package/target/lib/token.test.js.map +1 -0
- package/target/lib.d.ts +2 -0
- package/target/lib.js +3 -0
- package/target/lib.js.map +1 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { attr, element, queryAll, css, html } from "@joist/element";
|
|
2
|
+
|
|
3
|
+
import { bind } from "../bind.js";
|
|
4
|
+
import { JoistValueEvent } from "../events.js";
|
|
5
|
+
import { JToken } from "../token.js";
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
interface HTMLElementTagNameMap {
|
|
9
|
+
"j-async": JoistAsyncElement;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type AsyncState<T = unknown, E = unknown> = {
|
|
14
|
+
status: "loading" | "error" | "success";
|
|
15
|
+
data?: T;
|
|
16
|
+
error?: E;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
@element({
|
|
20
|
+
tagName: "j-async",
|
|
21
|
+
// prettier-ignore
|
|
22
|
+
shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
|
|
23
|
+
})
|
|
24
|
+
export class JoistAsyncElement extends HTMLElement {
|
|
25
|
+
@attr()
|
|
26
|
+
accessor bind = "";
|
|
27
|
+
|
|
28
|
+
@bind()
|
|
29
|
+
accessor state: AsyncState | null = null;
|
|
30
|
+
|
|
31
|
+
#templates = queryAll<HTMLTemplateElement>("template", this);
|
|
32
|
+
#currentNodes: Node[] = [];
|
|
33
|
+
#cachedTemplates: {
|
|
34
|
+
loading?: HTMLTemplateElement;
|
|
35
|
+
error?: HTMLTemplateElement;
|
|
36
|
+
success?: HTMLTemplateElement;
|
|
37
|
+
} = {
|
|
38
|
+
loading: undefined,
|
|
39
|
+
error: undefined,
|
|
40
|
+
success: undefined,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
connectedCallback(): void {
|
|
44
|
+
this.#clean();
|
|
45
|
+
|
|
46
|
+
// Cache all templates
|
|
47
|
+
const templates = Array.from(this.#templates());
|
|
48
|
+
|
|
49
|
+
this.#cachedTemplates = {
|
|
50
|
+
loading: templates.find((t) => t.hasAttribute("loading")),
|
|
51
|
+
error: templates.find((t) => t.hasAttribute("error")),
|
|
52
|
+
success: templates.find((t) => t.hasAttribute("success")),
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const token = new JToken(this.bind);
|
|
56
|
+
|
|
57
|
+
this.dispatchEvent(
|
|
58
|
+
new JoistValueEvent(token, ({ newValue, oldValue }) => {
|
|
59
|
+
if (newValue !== oldValue) {
|
|
60
|
+
if (newValue instanceof Promise) {
|
|
61
|
+
this.#handlePromise(newValue);
|
|
62
|
+
} else {
|
|
63
|
+
console.warn("j-async bind value must be a Promise or AsyncState");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async #handlePromise(promise: Promise<unknown>): Promise<void> {
|
|
71
|
+
try {
|
|
72
|
+
this.#handleState({ status: "loading" });
|
|
73
|
+
const data = await promise;
|
|
74
|
+
this.#handleState({ status: "success", data });
|
|
75
|
+
} catch (error) {
|
|
76
|
+
this.#handleState({ status: "error", error });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#handleState(state: AsyncState): void {
|
|
81
|
+
this.#clean();
|
|
82
|
+
|
|
83
|
+
let template: HTMLTemplateElement | undefined = undefined;
|
|
84
|
+
|
|
85
|
+
this.state = state;
|
|
86
|
+
|
|
87
|
+
switch (state.status) {
|
|
88
|
+
case "loading":
|
|
89
|
+
template = this.#cachedTemplates.loading;
|
|
90
|
+
break;
|
|
91
|
+
|
|
92
|
+
case "error":
|
|
93
|
+
template = this.#cachedTemplates.error;
|
|
94
|
+
break;
|
|
95
|
+
|
|
96
|
+
case "success":
|
|
97
|
+
template = this.#cachedTemplates.success;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (template) {
|
|
102
|
+
const content = document.importNode(template.content, true);
|
|
103
|
+
const nodes = Array.from(content.childNodes);
|
|
104
|
+
this.appendChild(content);
|
|
105
|
+
this.#currentNodes = nodes;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#clean(): void {
|
|
110
|
+
for (const node of this.#currentNodes) {
|
|
111
|
+
node.parentNode?.removeChild(node);
|
|
112
|
+
}
|
|
113
|
+
this.#currentNodes = [];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
disconnectedCallback(): void {
|
|
117
|
+
this.#clean();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import "./for.element.js";
|
|
2
|
+
import "./value.element.js";
|
|
3
|
+
|
|
4
|
+
import { fixtureSync, html } from "@open-wc/testing";
|
|
5
|
+
import { assert } from "chai";
|
|
6
|
+
|
|
7
|
+
import type { JoistValueEvent } from "../events.js";
|
|
8
|
+
|
|
9
|
+
it("should iterate over an iterable", () => {
|
|
10
|
+
const element = fixtureSync(html`
|
|
11
|
+
<div
|
|
12
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
13
|
+
e.update({
|
|
14
|
+
oldValue: null,
|
|
15
|
+
newValue: new Set([
|
|
16
|
+
{ id: "123", label: "Hello" },
|
|
17
|
+
{ id: "456", label: "World" },
|
|
18
|
+
]),
|
|
19
|
+
});
|
|
20
|
+
}}
|
|
21
|
+
>
|
|
22
|
+
<ul>
|
|
23
|
+
<j-for bind="items" key="id">
|
|
24
|
+
<template>
|
|
25
|
+
<li>
|
|
26
|
+
<j-value bind="each.value.label"></j-value>
|
|
27
|
+
</li>
|
|
28
|
+
</template>
|
|
29
|
+
</j-for>
|
|
30
|
+
</ul>
|
|
31
|
+
</div>
|
|
32
|
+
`);
|
|
33
|
+
|
|
34
|
+
const listItems = element.querySelectorAll("li");
|
|
35
|
+
|
|
36
|
+
assert.equal(listItems.length, 2);
|
|
37
|
+
assert.equal(listItems[0].textContent?.trim(), "Hello");
|
|
38
|
+
assert.equal(listItems[1].textContent?.trim(), "World");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should handle empty arrays", () => {
|
|
42
|
+
const element = fixtureSync(html`
|
|
43
|
+
<div
|
|
44
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
45
|
+
e.update({
|
|
46
|
+
oldValue: null,
|
|
47
|
+
newValue: [],
|
|
48
|
+
});
|
|
49
|
+
}}
|
|
50
|
+
>
|
|
51
|
+
<j-for bind="items">
|
|
52
|
+
<template>
|
|
53
|
+
<div>Item</div>
|
|
54
|
+
</template>
|
|
55
|
+
</j-for>
|
|
56
|
+
</div>
|
|
57
|
+
`);
|
|
58
|
+
|
|
59
|
+
assert.equal(element.querySelectorAll("div").length, 0);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should update when items are added or removed", () => {
|
|
63
|
+
const element = fixtureSync(html`
|
|
64
|
+
<div
|
|
65
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
66
|
+
// Initial items
|
|
67
|
+
e.update({
|
|
68
|
+
oldValue: null,
|
|
69
|
+
newValue: [
|
|
70
|
+
{ id: "1", text: "First" },
|
|
71
|
+
{ id: "2", text: "Second" },
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Add an item
|
|
76
|
+
e.update({
|
|
77
|
+
oldValue: null,
|
|
78
|
+
newValue: [
|
|
79
|
+
{ id: "1", text: "First" },
|
|
80
|
+
{ id: "2", text: "Second" },
|
|
81
|
+
{ id: "3", text: "Third" },
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Remove an item
|
|
86
|
+
e.update({
|
|
87
|
+
oldValue: null,
|
|
88
|
+
newValue: [
|
|
89
|
+
{ id: "1", text: "First" },
|
|
90
|
+
{ id: "3", text: "Third" },
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
<j-for bind="items" key="id">
|
|
96
|
+
<template>
|
|
97
|
+
<j-value bind="each.value.text"></j-value>
|
|
98
|
+
</template>
|
|
99
|
+
</j-for>
|
|
100
|
+
</div>
|
|
101
|
+
`);
|
|
102
|
+
|
|
103
|
+
const items = element.querySelectorAll("j-value");
|
|
104
|
+
assert.equal(items.length, 2);
|
|
105
|
+
assert.equal(items[0].textContent?.trim(), "First");
|
|
106
|
+
assert.equal(items[1].textContent?.trim(), "Third");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should provide index and position information", () => {
|
|
110
|
+
const element = fixtureSync(html`
|
|
111
|
+
<div
|
|
112
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
113
|
+
e.update({
|
|
114
|
+
oldValue: null,
|
|
115
|
+
newValue: ["A", "B", "C"],
|
|
116
|
+
});
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
<j-for bind="items">
|
|
120
|
+
<template>
|
|
121
|
+
<j-value bind="each.value"></j-value>
|
|
122
|
+
(index: <j-value bind="each.index"></j-value>,
|
|
123
|
+
position: <j-value bind="each.position"></j-value>)
|
|
124
|
+
</template>
|
|
125
|
+
</j-for>
|
|
126
|
+
</div>
|
|
127
|
+
`);
|
|
128
|
+
|
|
129
|
+
const items = element.querySelectorAll("j-for-scope");
|
|
130
|
+
assert.equal(items.length, 3);
|
|
131
|
+
assert.equal(
|
|
132
|
+
items[0].textContent?.trim().replaceAll("\n", "").replaceAll(" ", ""),
|
|
133
|
+
"A(index:0,position:1)",
|
|
134
|
+
);
|
|
135
|
+
assert.equal(
|
|
136
|
+
items[1].textContent?.trim().replaceAll("\n", "").replaceAll(" ", ""),
|
|
137
|
+
"B(index:1,position:2)",
|
|
138
|
+
);
|
|
139
|
+
assert.equal(
|
|
140
|
+
items[2].textContent?.trim().replaceAll("\n", "").replaceAll(" ", ""),
|
|
141
|
+
"C(index:2,position:3)",
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// it("should handle nested j-for elements", () => {
|
|
146
|
+
// const element = fixtureSync(html`
|
|
147
|
+
// <div
|
|
148
|
+
// @joist::value=${(e: JoistValueEvent) => {
|
|
149
|
+
// e.update({
|
|
150
|
+
// oldValue: null,
|
|
151
|
+
// newValue: [
|
|
152
|
+
// { id: "1", items: ["A", "B"] },
|
|
153
|
+
// { id: "2", items: ["C", "D"] },
|
|
154
|
+
// ],
|
|
155
|
+
// });
|
|
156
|
+
// }}
|
|
157
|
+
// >
|
|
158
|
+
// <j-for bind="groups" key="id">
|
|
159
|
+
// <template>
|
|
160
|
+
// <div class="group">
|
|
161
|
+
// <j-for bind="each.value.items">
|
|
162
|
+
// <template>
|
|
163
|
+
// <j-value class="child" bind="each.value"></j-value>
|
|
164
|
+
// </template>
|
|
165
|
+
// </j-for>
|
|
166
|
+
// </div>
|
|
167
|
+
// </template>
|
|
168
|
+
// </j-for>
|
|
169
|
+
// </div>
|
|
170
|
+
// `);
|
|
171
|
+
|
|
172
|
+
// const groups = element.querySelectorAll(".group");
|
|
173
|
+
// assert.equal(groups.length, 2);
|
|
174
|
+
|
|
175
|
+
// const items = element.querySelectorAll(".child");
|
|
176
|
+
// assert.equal(items.length, 4);
|
|
177
|
+
// assert.equal(items[0].textContent?.trim(), "A");
|
|
178
|
+
// assert.equal(items[1].textContent?.trim(), "B");
|
|
179
|
+
// assert.equal(items[2].textContent?.trim(), "C");
|
|
180
|
+
// assert.equal(items[3].textContent?.trim(), "D");
|
|
181
|
+
// });
|
|
182
|
+
|
|
183
|
+
it("should maintain DOM order when items are reordered", () => {
|
|
184
|
+
const element = fixtureSync(html`
|
|
185
|
+
<div
|
|
186
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
187
|
+
// Initial order
|
|
188
|
+
e.update({
|
|
189
|
+
oldValue: null,
|
|
190
|
+
newValue: [
|
|
191
|
+
{ id: "1", text: "First" },
|
|
192
|
+
{ id: "2", text: "Second" },
|
|
193
|
+
{ id: "3", text: "Third" },
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Reorder items
|
|
198
|
+
e.update({
|
|
199
|
+
oldValue: null,
|
|
200
|
+
newValue: [
|
|
201
|
+
{ id: "3", text: "Third" },
|
|
202
|
+
{ id: "1", text: "First" },
|
|
203
|
+
{ id: "2", text: "Second" },
|
|
204
|
+
],
|
|
205
|
+
});
|
|
206
|
+
}}
|
|
207
|
+
>
|
|
208
|
+
<j-for bind="items" key="id">
|
|
209
|
+
<template>
|
|
210
|
+
<j-value bind="each.value.text"></j-value>
|
|
211
|
+
</template>
|
|
212
|
+
</j-for>
|
|
213
|
+
</div>
|
|
214
|
+
`);
|
|
215
|
+
|
|
216
|
+
const items = element.querySelectorAll("j-value");
|
|
217
|
+
assert.equal(items.length, 3);
|
|
218
|
+
assert.equal(items[0].textContent?.trim(), "Third");
|
|
219
|
+
assert.equal(items[1].textContent?.trim(), "First");
|
|
220
|
+
assert.equal(items[2].textContent?.trim(), "Second");
|
|
221
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { attr, element, query, css, html } from "@joist/element";
|
|
2
|
+
|
|
3
|
+
import { bind } from "../bind.js";
|
|
4
|
+
import { JoistValueEvent } from "../events.js";
|
|
5
|
+
import { JToken } from "../token.js";
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
interface HTMLElementTagNameMap {
|
|
9
|
+
"j-for": JositForElement;
|
|
10
|
+
"j-for-scope": JForScope;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface EachCtx<T> {
|
|
15
|
+
value: T | null;
|
|
16
|
+
index: number | null;
|
|
17
|
+
position: number | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@element({
|
|
21
|
+
tagName: "j-for-scope",
|
|
22
|
+
// prettier-ignore
|
|
23
|
+
shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
|
|
24
|
+
})
|
|
25
|
+
export class JForScope<T = unknown> extends HTMLElement {
|
|
26
|
+
@bind()
|
|
27
|
+
accessor each: EachCtx<T> = {
|
|
28
|
+
value: null,
|
|
29
|
+
index: null,
|
|
30
|
+
position: null,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
@attr()
|
|
34
|
+
accessor key = "";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@element({
|
|
38
|
+
tagName: "j-for",
|
|
39
|
+
// prettier-ignore
|
|
40
|
+
shadowDom: [css`:host{display:contents;}`, html`<slot></slot>`],
|
|
41
|
+
})
|
|
42
|
+
export class JositForElement extends HTMLElement {
|
|
43
|
+
@attr()
|
|
44
|
+
accessor bind = "";
|
|
45
|
+
|
|
46
|
+
@attr()
|
|
47
|
+
accessor key = "";
|
|
48
|
+
|
|
49
|
+
#template = query("template", this);
|
|
50
|
+
#items: Iterable<unknown> = [];
|
|
51
|
+
#scopes = new Map<unknown, JForScope>();
|
|
52
|
+
|
|
53
|
+
connectedCallback(): void {
|
|
54
|
+
const template = this.#template();
|
|
55
|
+
|
|
56
|
+
if (this.firstElementChild !== template) {
|
|
57
|
+
throw new Error("The first Node in j-for needs to be a template");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// collect all scopes from the template to be matched against later
|
|
61
|
+
let currentScope = template.nextElementSibling;
|
|
62
|
+
while (currentScope instanceof JForScope) {
|
|
63
|
+
this.#scopes.set(currentScope.key, currentScope);
|
|
64
|
+
currentScope = currentScope.nextElementSibling;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const token = new JToken(this.bind);
|
|
68
|
+
|
|
69
|
+
this.dispatchEvent(
|
|
70
|
+
new JoistValueEvent(token, ({ newValue, oldValue }) => {
|
|
71
|
+
if (newValue !== oldValue) {
|
|
72
|
+
if (isIterable(newValue)) {
|
|
73
|
+
this.#items = newValue;
|
|
74
|
+
} else {
|
|
75
|
+
this.#items = [];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// If there are no existing items in the DOM (template is the only child),
|
|
79
|
+
// create all items from scratch
|
|
80
|
+
if (template.nextSibling === null) {
|
|
81
|
+
this.createFromEmpty();
|
|
82
|
+
} else {
|
|
83
|
+
// Otherwise update existing items, reusing DOM nodes where possible
|
|
84
|
+
this.updateItems();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Updates the DOM by either inserting new scopes or moving existing ones
|
|
92
|
+
// to their correct positions based on the current iteration order
|
|
93
|
+
createFromEmpty(): void {
|
|
94
|
+
const template = this.#template();
|
|
95
|
+
const templateContent = template.content;
|
|
96
|
+
const keyProperty = this.key;
|
|
97
|
+
const fragment = document.createDocumentFragment();
|
|
98
|
+
|
|
99
|
+
let index = 0;
|
|
100
|
+
for (const value of this.#items) {
|
|
101
|
+
const key = keyProperty && hasProperty(value, keyProperty) ? value[keyProperty] : index;
|
|
102
|
+
|
|
103
|
+
const scope = new JForScope();
|
|
104
|
+
scope.append(document.importNode(templateContent, true));
|
|
105
|
+
scope.key = String(key);
|
|
106
|
+
scope.each = { position: index + 1, index, value };
|
|
107
|
+
|
|
108
|
+
fragment.appendChild(scope);
|
|
109
|
+
this.#scopes.set(key, scope);
|
|
110
|
+
index++;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.append(fragment);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Updates the DOM by either inserting new scopes or moving existing ones
|
|
117
|
+
// to their correct positions based on the current iteration order
|
|
118
|
+
updateItems(): void {
|
|
119
|
+
const template = this.#template();
|
|
120
|
+
const leftoverScopes = new Map<unknown, JForScope>(this.#scopes);
|
|
121
|
+
const keyProperty = this.key; // Cache the key property
|
|
122
|
+
|
|
123
|
+
let index = 0;
|
|
124
|
+
|
|
125
|
+
for (const value of this.#items) {
|
|
126
|
+
// Optimize key lookup by caching the property check
|
|
127
|
+
const key = keyProperty && hasProperty(value, keyProperty) ? value[keyProperty] : index;
|
|
128
|
+
|
|
129
|
+
let scope = leftoverScopes.get(key);
|
|
130
|
+
|
|
131
|
+
if (!scope) {
|
|
132
|
+
scope = new JForScope();
|
|
133
|
+
scope.append(document.importNode(template.content, true));
|
|
134
|
+
this.#scopes.set(key, scope);
|
|
135
|
+
} else {
|
|
136
|
+
leftoverScopes.delete(key); // Remove from map to track unused scopes
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Only update if values have changed
|
|
140
|
+
if (scope.key !== key || scope.each.value !== value) {
|
|
141
|
+
scope.key = String(key);
|
|
142
|
+
scope.each = { position: index + 1, index, value };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const child = this.children[index + 1];
|
|
146
|
+
|
|
147
|
+
if (child !== scope) {
|
|
148
|
+
this.insertBefore(scope, child);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
index++;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Remove unused scopes
|
|
155
|
+
for (const scope of leftoverScopes.values()) {
|
|
156
|
+
scope.remove();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
disconnectedCallback(): void {
|
|
161
|
+
for (const scope of this.#scopes.values()) {
|
|
162
|
+
scope.remove();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.#scopes.clear();
|
|
166
|
+
this.#items = [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function isIterable<T = unknown>(obj: any): obj is Iterable<T> {
|
|
171
|
+
return obj != null && typeof obj[Symbol.iterator] === "function";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function hasProperty(item: unknown, key: string): item is Record<string, unknown> {
|
|
175
|
+
return Object.prototype.hasOwnProperty.call(item, key);
|
|
176
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import "./if.element.js";
|
|
2
|
+
|
|
3
|
+
import { fixtureSync, html } from "@open-wc/testing";
|
|
4
|
+
import { assert } from "chai";
|
|
5
|
+
|
|
6
|
+
import type { JoistValueEvent } from "../events.js";
|
|
7
|
+
|
|
8
|
+
it("should render content when the bind value is truthy", () => {
|
|
9
|
+
const element = fixtureSync(html`
|
|
10
|
+
<div
|
|
11
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
12
|
+
e.update({ oldValue: null, newValue: true });
|
|
13
|
+
}}
|
|
14
|
+
>
|
|
15
|
+
<j-if bind="test">
|
|
16
|
+
<template>Visible Content</template>
|
|
17
|
+
</j-if>
|
|
18
|
+
</div>
|
|
19
|
+
`);
|
|
20
|
+
|
|
21
|
+
assert.equal(element.textContent?.trim(), "Visible Content");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should not render content when the bind value is falsy", () => {
|
|
25
|
+
const element = fixtureSync(html`
|
|
26
|
+
<div
|
|
27
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
28
|
+
e.update({ oldValue: null, newValue: true });
|
|
29
|
+
e.update({ oldValue: null, newValue: false });
|
|
30
|
+
}}
|
|
31
|
+
>
|
|
32
|
+
<j-if bind="test">
|
|
33
|
+
<template>Visible Content</template>
|
|
34
|
+
</j-if>
|
|
35
|
+
</div>
|
|
36
|
+
`);
|
|
37
|
+
|
|
38
|
+
assert.equal(element.textContent?.trim(), "");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should handle negated tokens correctly", () => {
|
|
42
|
+
const element = fixtureSync(html`
|
|
43
|
+
<div
|
|
44
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
45
|
+
e.update({ oldValue: null, newValue: false });
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
<j-if bind="!test">
|
|
49
|
+
<template>Visible Content</template>
|
|
50
|
+
</j-if>
|
|
51
|
+
</div>
|
|
52
|
+
`);
|
|
53
|
+
|
|
54
|
+
assert.equal(element.textContent?.trim(), "Visible Content");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should render else template when condition is falsy", () => {
|
|
58
|
+
const element = fixtureSync(html`
|
|
59
|
+
<div
|
|
60
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
61
|
+
e.update({ oldValue: null, newValue: false });
|
|
62
|
+
}}
|
|
63
|
+
>
|
|
64
|
+
<j-if bind="test">
|
|
65
|
+
<template>If Content</template>
|
|
66
|
+
<template else>Else Content</template>
|
|
67
|
+
</j-if>
|
|
68
|
+
</div>
|
|
69
|
+
`);
|
|
70
|
+
|
|
71
|
+
assert.equal(element.textContent?.trim(), "Else Content");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should switch between if and else templates", () => {
|
|
75
|
+
const element = fixtureSync(html`
|
|
76
|
+
<div
|
|
77
|
+
@joist::value=${(e: JoistValueEvent) => {
|
|
78
|
+
e.update({ oldValue: null, newValue: false });
|
|
79
|
+
e.update({ oldValue: false, newValue: true });
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
<j-if bind="test">
|
|
83
|
+
<template>If Content</template>
|
|
84
|
+
<template else>Else Content</template>
|
|
85
|
+
</j-if>
|
|
86
|
+
</div>
|
|
87
|
+
`);
|
|
88
|
+
|
|
89
|
+
assert.equal(element.textContent?.trim(), "If Content");
|
|
90
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { attr, element, queryAll, css, html } from "@joist/element";
|
|
2
|
+
|
|
3
|
+
import { JoistValueEvent } from "../events.js";
|
|
4
|
+
import { JToken } from "../token.js";
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
interface HTMLElementTagNameMap {
|
|
8
|
+
"j-if": JoistIfElement;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@element({
|
|
13
|
+
tagName: "j-if",
|
|
14
|
+
// prettier-ignore
|
|
15
|
+
shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
|
|
16
|
+
})
|
|
17
|
+
export class JoistIfElement extends HTMLElement {
|
|
18
|
+
@attr()
|
|
19
|
+
accessor bind = "";
|
|
20
|
+
|
|
21
|
+
#templates = queryAll<HTMLTemplateElement>("template", this);
|
|
22
|
+
|
|
23
|
+
connectedCallback(): void {
|
|
24
|
+
const templates = Array.from(this.#templates());
|
|
25
|
+
|
|
26
|
+
if (templates.length === 0) {
|
|
27
|
+
throw new Error("j-if requires at least one template element");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (templates.length > 2) {
|
|
31
|
+
throw new Error("j-if can only have two template elements (if and else)");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (templates.length === 2 && !templates.some((t) => t.hasAttribute("else"))) {
|
|
35
|
+
throw new Error("When using two templates, one must have the else attribute");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (templates.length === 2 && templates[0].hasAttribute("else")) {
|
|
39
|
+
// Swap templates to ensure if template is first
|
|
40
|
+
[templates[0], templates[1]] = [templates[1], templates[0]];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// make sure there are no other nodes after the template
|
|
44
|
+
this.#clean();
|
|
45
|
+
|
|
46
|
+
const token = new JToken(this.bind);
|
|
47
|
+
|
|
48
|
+
this.dispatchEvent(
|
|
49
|
+
new JoistValueEvent(token, ({ newValue, oldValue }) => {
|
|
50
|
+
if (newValue !== oldValue) {
|
|
51
|
+
if (typeof newValue === "object" && newValue !== null) {
|
|
52
|
+
this.apply(token.readTokenValueFrom(newValue), token.isNegated);
|
|
53
|
+
} else {
|
|
54
|
+
this.apply(newValue, token.isNegated);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
apply(value: unknown, isNegative: boolean): void {
|
|
62
|
+
this.#clean();
|
|
63
|
+
|
|
64
|
+
const templates = this.#templates();
|
|
65
|
+
|
|
66
|
+
const shouldShowIf = isNegative ? !value : value;
|
|
67
|
+
const templateToUse = shouldShowIf ? templates[0] : templates[1];
|
|
68
|
+
|
|
69
|
+
if (templateToUse) {
|
|
70
|
+
const content = document.importNode(templateToUse.content, true);
|
|
71
|
+
|
|
72
|
+
this.appendChild(content);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#clean(): void {
|
|
77
|
+
while (!(this.lastChild instanceof HTMLTemplateElement)) {
|
|
78
|
+
this.lastChild?.remove();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
disconnectedCallback(): void {
|
|
83
|
+
this.#clean();
|
|
84
|
+
}
|
|
85
|
+
}
|