@cas-smartdesign/tree 4.0.3
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 +8 -0
- package/dist/docs/3_lazy_loading.js +1 -0
- package/dist/docs/doc.css +1 -0
- package/dist/docs/doc.mjs +755 -0
- package/dist/docs/index.html +25 -0
- package/dist/src/checkbox-node.d.ts +9 -0
- package/dist/src/constants.d.ts +2 -0
- package/dist/src/loading-indicator.d.ts +14 -0
- package/dist/src/node.d.ts +43 -0
- package/dist/src/radio-button-node.d.ts +13 -0
- package/dist/src/tree-node.d.ts +44 -0
- package/dist/src/tree.d.ts +98 -0
- package/dist/src/types.d.ts +21 -0
- package/dist/src/utils.d.ts +9 -0
- package/dist/src/utils.spec.d.ts +1 -0
- package/dist/tree-with-externals.js +203 -0
- package/dist/tree-with-externals.js.map +7 -0
- package/dist/tree.d.ts +1 -0
- package/dist/tree.mjs +737 -0
- package/dist/tree.mjs.map +1 -0
- package/dist/vitest.config.d.mts +2 -0
- package/npm-third-party-licenses.json +192 -0
- package/package.json +44 -0
- package/readme.md +111 -0
package/dist/tree.mjs
ADDED
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
import { LitElement as I, css as C, unsafeCSS as k, html as r, render as R } from "lit";
|
|
2
|
+
import { property as a } from "lit/decorators/property.js";
|
|
3
|
+
import { SDCheckbox as O } from "@cas-smartdesign/checkbox";
|
|
4
|
+
import { RadioButton as B } from "@cas-smartdesign/radio-button-group";
|
|
5
|
+
const $ = 16, S = 35;
|
|
6
|
+
class E {
|
|
7
|
+
constructor(e, s, t, i) {
|
|
8
|
+
this.id = e, this.text = s, this.path = t, this.nodes = i, this.depth = 0, this.selectionState = "unchecked", this.expanded = !1, this.parentSelectionAllowed = !1, this.lastLoadingChild = !1, this.loading = !1, this.disabled = !1, this.parent = [], t ? (this.depth = t.length - 1, this.parent = t.slice(0, -1)) : this.path = [e], this.childCount = i ? i.length : 0;
|
|
9
|
+
}
|
|
10
|
+
updateSelectionStateOfChildren() {
|
|
11
|
+
this.nodes.forEach((e) => {
|
|
12
|
+
if (this.selectionState === "checked") {
|
|
13
|
+
if (e.childrenType === "radio" || A(e))
|
|
14
|
+
return;
|
|
15
|
+
e.selectionState = "checked";
|
|
16
|
+
} else
|
|
17
|
+
this.selectionState === "unchecked" && (e.selectionState = "unchecked");
|
|
18
|
+
e.updateSelectionStateOfChildren();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
updateParent() {
|
|
22
|
+
const e = this.parentNode;
|
|
23
|
+
return e && (!this.parentSelectionAllowed || this.selectionState !== "unchecked" || e.disabled) && (e.updateSelectionStateByChildren(), e.updateParent()), e;
|
|
24
|
+
}
|
|
25
|
+
get parentNode() {
|
|
26
|
+
return this.parent.length ? this.nodeGetter(this.parent) : null;
|
|
27
|
+
}
|
|
28
|
+
check() {
|
|
29
|
+
this.selectionState !== "checked" && (this.selectionState = "checked", this.updateBranch());
|
|
30
|
+
}
|
|
31
|
+
uncheck() {
|
|
32
|
+
this.selectionState !== "unchecked" && (this.selectionState = "unchecked", this.updateBranch());
|
|
33
|
+
}
|
|
34
|
+
hasRadioButtonDescendant() {
|
|
35
|
+
if (this.childrenType === "radio")
|
|
36
|
+
return !0;
|
|
37
|
+
for (const e of this.nodes)
|
|
38
|
+
if (e.hasRadioButtonDescendant())
|
|
39
|
+
return !0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const T = (d) => (d == null ? void 0 : d.type) === "checkbox", A = (d) => (d == null ? void 0 : d.type) === "radio";
|
|
43
|
+
class P extends E {
|
|
44
|
+
constructor(e, s, t, i) {
|
|
45
|
+
super(e, s, t, i), this.type = "checkbox";
|
|
46
|
+
}
|
|
47
|
+
toggle() {
|
|
48
|
+
this.selectionState === "checked" ? this.selectionState = "unchecked" : this.selectionState = "checked", this.updateBranch();
|
|
49
|
+
}
|
|
50
|
+
updateBranch() {
|
|
51
|
+
(!this.parentSelectionAllowed || this.selectionState === "unchecked") && (this.updateSelectionStateOfChildren(), this.updateSelectionStateByChildren()), this.updateParent();
|
|
52
|
+
}
|
|
53
|
+
updateSelectionStateByChildren() {
|
|
54
|
+
if (this.nodes.length === 0)
|
|
55
|
+
return;
|
|
56
|
+
const e = this.nodes.map((t) => t.selectionState === "checked" || t.selectionState === "indeterminate"), s = e.some(Boolean);
|
|
57
|
+
if (this.parentSelectionAllowed)
|
|
58
|
+
this.selectionState = s ? "checked" : "unchecked";
|
|
59
|
+
else {
|
|
60
|
+
const t = e.every(Boolean), i = this.nodes.map((n) => n.selectionState).some((n) => n === "indeterminate");
|
|
61
|
+
this.childrenType === "radio" ? this.selectionState = s ? "checked" : "unchecked" : i ? this.selectionState = "indeterminate" : t ? this.selectionState = "checked" : s ? this.selectionState = "indeterminate" : this.selectionState = "unchecked";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
getFlattenedSubtree(e) {
|
|
65
|
+
return e.reduce((s, t) => s.concat(
|
|
66
|
+
T(t) && t.childCount > 0 && t.expanded ? [t, ...this.getFlattenedSubtree(t.nodes)] : t
|
|
67
|
+
), []);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
class v extends E {
|
|
71
|
+
constructor(e, s, t, i) {
|
|
72
|
+
super(e, s, t, i), this.type = "radio";
|
|
73
|
+
}
|
|
74
|
+
toggle() {
|
|
75
|
+
this.selectionState === "checked" ? this.selectionState = "unchecked" : (this.selectionState = "checked", this.clearSiblings()), this.updateBranch();
|
|
76
|
+
}
|
|
77
|
+
check() {
|
|
78
|
+
this.selectionState !== "checked" && (this.selectionState = "checked", this.clearSiblings(), this.updateBranch());
|
|
79
|
+
}
|
|
80
|
+
updateBranch() {
|
|
81
|
+
this.selectionState === "unchecked" && this.updateSelectionStateOfChildren(), this.updateParent();
|
|
82
|
+
}
|
|
83
|
+
updateParent() {
|
|
84
|
+
const e = super.updateParent();
|
|
85
|
+
return e instanceof v && e.clearSiblings(), e;
|
|
86
|
+
}
|
|
87
|
+
updateSelectionStateByChildren() {
|
|
88
|
+
if (this.nodes.length === 0)
|
|
89
|
+
return;
|
|
90
|
+
this.nodes.map((s) => s.selectionState === "checked").some(Boolean) ? this.selectionState = "checked" : this.selectionState = "unchecked";
|
|
91
|
+
}
|
|
92
|
+
clearSiblings() {
|
|
93
|
+
this.getSiblings().forEach((s) => {
|
|
94
|
+
s.uncheck();
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const q = `:host{width:100%;display:block;contain:layout;position:absolute}:host([readonly][focused]){background-color:#1467ba4d}.node{height:35px;display:flex;align-items:center}.node .expander,.node .placeholder{height:28px;width:30px;flex-shrink:0}.node .expander{cursor:pointer;background-position:center;background-size:24px;flex:0 0 auto}.node .expander.more{background-image:url("data:image/svg+xml,%3csvg%20height='32'%20viewBox='0%200%2032%2032'%20width='32'%20xmlns='http://www.w3.org/2000/svg'%3e%3cpath%20d='m15%2021%209.5-9.5%209.5%209.5'%20fill='none'%20stroke='%23999'%20stroke-linecap='square'%20stroke-width='2'%20transform='matrix(0%201%20-1%200%2032.25%20-8.25)'/%3e%3c/svg%3e")}.node .expander.less{background-image:url("data:image/svg+xml,%3csvg%20height='32'%20viewBox='0%200%2032%2032'%20width='32'%20xmlns='http://www.w3.org/2000/svg'%3e%3cpath%20d='m19.75%206.75%209.5%209.5-9.5%209.5'%20fill='none'%20stroke='%23999'%20stroke-linecap='square'%20stroke-width='2'%20transform='matrix(0%201%20-1%200%2032.25%20-8.25)'/%3e%3c/svg%3e")}.node .label{outline:none}.node .input{overflow:hidden}.node ul{list-style-type:none;margin:0;padding:0}`;
|
|
99
|
+
var z = Object.defineProperty, M = Object.getOwnPropertyDescriptor, u = (d, e, s, t) => {
|
|
100
|
+
for (var i = t > 1 ? void 0 : t ? M(e, s) : e, n = d.length - 1, o; n >= 0; n--)
|
|
101
|
+
(o = d[n]) && (i = (t ? o(e, s, i) : o(i)) || i);
|
|
102
|
+
return t && i && z(e, s, i), i;
|
|
103
|
+
}, f;
|
|
104
|
+
const c = (f = class extends I {
|
|
105
|
+
constructor() {
|
|
106
|
+
super(...arguments), this.text = "", this.expanded = !1, this.childCount = 0, this.focused = !1, this.readonly = !1, this.depth = 0, this.path = [], this.type = "checkbox", this.selectionState = "unchecked", this.disabled = !1, this.loading = !1, this.onSelectionChange = () => {
|
|
107
|
+
if (!this.readonly) {
|
|
108
|
+
if (this.type == "checkbox") {
|
|
109
|
+
const e = this.shadowRoot.querySelector(".input");
|
|
110
|
+
this.selectionState = e.checked ? "checked" : e.indeterminate ? "indeterminate" : "unchecked";
|
|
111
|
+
} else {
|
|
112
|
+
const e = this.shadowRoot.querySelector(".input");
|
|
113
|
+
this.selectionState = e.checked ? "checked" : "unchecked";
|
|
114
|
+
}
|
|
115
|
+
this.updateComplete.then(() => {
|
|
116
|
+
this.dispatchEvent(
|
|
117
|
+
new CustomEvent("selection", {
|
|
118
|
+
detail: {
|
|
119
|
+
path: this.path,
|
|
120
|
+
checked: this.selectionState === "checked",
|
|
121
|
+
indeterminate: this.selectionState === "indeterminate"
|
|
122
|
+
},
|
|
123
|
+
bubbles: !0,
|
|
124
|
+
composed: !0
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
static get styles() {
|
|
132
|
+
return [
|
|
133
|
+
C`
|
|
134
|
+
${k(q)}
|
|
135
|
+
`
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
render() {
|
|
139
|
+
const e = this.childCount > 0 || this.children.length > 0;
|
|
140
|
+
return r`
|
|
141
|
+
<li class="node" style="margin-left: ${this.depth * $}px">
|
|
142
|
+
${e ? r` <div class="expander ${this.expanded ? "less" : "more"}" @click="${this.toggle}"></div> ` : r` <div class="placeholder"></div> `}
|
|
143
|
+
${this.readonly ? r` <div class="label">${this.text}</div> ` : r`
|
|
144
|
+
${this.type === "checkbox" ? r`
|
|
145
|
+
<sd-checkbox
|
|
146
|
+
class="input"
|
|
147
|
+
tabIndex="-1"
|
|
148
|
+
label="${this.text}"
|
|
149
|
+
?checked="${this.selectionState === "checked"}"
|
|
150
|
+
?focused="${this.focused}"
|
|
151
|
+
?disabled="${this.disabled}"
|
|
152
|
+
.indeterminate="${this.selectionState === "indeterminate"}"
|
|
153
|
+
@value-change="${this.onSelectionChange}"
|
|
154
|
+
oneline
|
|
155
|
+
></sd-checkbox>
|
|
156
|
+
` : r`
|
|
157
|
+
<sd-radio-button
|
|
158
|
+
class="input"
|
|
159
|
+
tabIndex="-1"
|
|
160
|
+
label="${this.text}"
|
|
161
|
+
?checked="${this.selectionState === "checked"}"
|
|
162
|
+
?focused="${this.focused}"
|
|
163
|
+
?disabled="${this.disabled}"
|
|
164
|
+
@click="${this.onSelectionChange}"
|
|
165
|
+
></sd-radio-button>
|
|
166
|
+
`}
|
|
167
|
+
`}
|
|
168
|
+
${e && this.expanded ? r`
|
|
169
|
+
<ul>
|
|
170
|
+
<slot></slot>
|
|
171
|
+
</ul>
|
|
172
|
+
` : ""}
|
|
173
|
+
</li>
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
176
|
+
toggle() {
|
|
177
|
+
this.loading || (this.expanded = !this.expanded, this.dispatchExpansionEvent());
|
|
178
|
+
}
|
|
179
|
+
expand() {
|
|
180
|
+
if (!this.loading) {
|
|
181
|
+
const e = !this.expanded;
|
|
182
|
+
this.expanded = !0, e && this.dispatchExpansionEvent();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
collapse() {
|
|
186
|
+
if (!this.loading) {
|
|
187
|
+
const e = this.expanded;
|
|
188
|
+
this.expanded = !1, e && this.dispatchExpansionEvent();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
getState(e) {
|
|
192
|
+
var n;
|
|
193
|
+
const s = Array.isArray(e) ? [...e, this.nodeId] : [this.nodeId], t = [...this.children].map((o) => o.getState(s));
|
|
194
|
+
let i;
|
|
195
|
+
return this.type === "checkbox" ? i = new P(this.nodeId, this.text, s, t) : i = new v(this.nodeId, this.text, s, t), i.selectionState = this.selectionState, i.childrenType = (n = t[0]) == null ? void 0 : n.type, i.expanded = this.expanded, i.disabled = this.disabled, i;
|
|
196
|
+
}
|
|
197
|
+
dispatchExpansionEvent() {
|
|
198
|
+
this.dispatchEvent(
|
|
199
|
+
new CustomEvent("expansion", {
|
|
200
|
+
detail: { path: this.path, expanded: this.expanded },
|
|
201
|
+
bubbles: !0,
|
|
202
|
+
composed: !0
|
|
203
|
+
})
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}, f.ID = "sd-tree-node", f.ensureDefined = () => {
|
|
207
|
+
B.ensureDefined(), O.ensureDefined(), customElements.get(f.ID) || customElements.define(f.ID, f);
|
|
208
|
+
}, f);
|
|
209
|
+
u([
|
|
210
|
+
a({ type: String, reflect: !0, attribute: !0 })
|
|
211
|
+
], c.prototype, "text", 2);
|
|
212
|
+
u([
|
|
213
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
214
|
+
], c.prototype, "expanded", 2);
|
|
215
|
+
u([
|
|
216
|
+
a({ type: String, reflect: !0, attribute: "node-id" })
|
|
217
|
+
], c.prototype, "nodeId", 2);
|
|
218
|
+
u([
|
|
219
|
+
a({ type: Number, reflect: !0, attribute: "child-count" })
|
|
220
|
+
], c.prototype, "childCount", 2);
|
|
221
|
+
u([
|
|
222
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
223
|
+
], c.prototype, "focused", 2);
|
|
224
|
+
u([
|
|
225
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
226
|
+
], c.prototype, "readonly", 2);
|
|
227
|
+
u([
|
|
228
|
+
a({ type: Number })
|
|
229
|
+
], c.prototype, "depth", 2);
|
|
230
|
+
u([
|
|
231
|
+
a({ type: Array, attribute: !1 })
|
|
232
|
+
], c.prototype, "path", 2);
|
|
233
|
+
u([
|
|
234
|
+
a({ type: String, reflect: !0, attribute: !0 })
|
|
235
|
+
], c.prototype, "type", 2);
|
|
236
|
+
u([
|
|
237
|
+
a({ type: String, reflect: !0, attribute: "selection-state" })
|
|
238
|
+
], c.prototype, "selectionState", 2);
|
|
239
|
+
u([
|
|
240
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
241
|
+
], c.prototype, "disabled", 2);
|
|
242
|
+
u([
|
|
243
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
244
|
+
], c.prototype, "loading", 2);
|
|
245
|
+
let F = c;
|
|
246
|
+
const U = ":host{position:absolute;width:100%}:host([readonly]) li{padding-left:30px}li{height:35px;padding-left:34px}.text{vertical-align:middle;line-height:35px;font-family:Segoe UI,Lucida Sans,Arial,sans-serif;color:#111;font-size:16px}";
|
|
247
|
+
var V = Object.defineProperty, J = Object.getOwnPropertyDescriptor, L = (d, e, s, t) => {
|
|
248
|
+
for (var i = t > 1 ? void 0 : t ? J(e, s) : e, n = d.length - 1, o; n >= 0; n--)
|
|
249
|
+
(o = d[n]) && (i = (t ? o(e, s, i) : o(i)) || i);
|
|
250
|
+
return t && i && V(e, s, i), i;
|
|
251
|
+
}, g;
|
|
252
|
+
const w = (g = class extends I {
|
|
253
|
+
constructor() {
|
|
254
|
+
super(...arguments), this.readonly = !1, this.depth = 0;
|
|
255
|
+
}
|
|
256
|
+
static get styles() {
|
|
257
|
+
return [
|
|
258
|
+
C`
|
|
259
|
+
${k(U)}
|
|
260
|
+
`
|
|
261
|
+
];
|
|
262
|
+
}
|
|
263
|
+
render() {
|
|
264
|
+
return r`
|
|
265
|
+
<li class="root" style="margin-left: ${this.depth * $}px;">
|
|
266
|
+
<div class="text">
|
|
267
|
+
<slot>Loading...</slot>
|
|
268
|
+
</div>
|
|
269
|
+
</li>
|
|
270
|
+
`;
|
|
271
|
+
}
|
|
272
|
+
}, g.ID = "sd-tree-loading-indicator", g.ensureDefined = () => {
|
|
273
|
+
customElements.get(g.ID) || customElements.define(g.ID, g);
|
|
274
|
+
}, g);
|
|
275
|
+
L([
|
|
276
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
277
|
+
], w.prototype, "readonly", 2);
|
|
278
|
+
L([
|
|
279
|
+
a({ type: Number })
|
|
280
|
+
], w.prototype, "depth", 2);
|
|
281
|
+
let K = w;
|
|
282
|
+
const y = (d, e = 1, s = 0) => e > 0 ? d.reduce(
|
|
283
|
+
(t, i) => t.concat(
|
|
284
|
+
i.childCount > 0 && i.expanded ? [i, ...y(i.nodes, e - 1, s + 1)] : i
|
|
285
|
+
),
|
|
286
|
+
[]
|
|
287
|
+
) : d.slice(), G = (d, e, s = 0) => {
|
|
288
|
+
if (!e)
|
|
289
|
+
return null;
|
|
290
|
+
const t = e[s], i = d.find((n) => n.id === t);
|
|
291
|
+
return e.length - 1 === s ? i : (i == null ? void 0 : i.nodes.length) > 0 ? G(i.nodes, e, s + 1) : null;
|
|
292
|
+
}, H = (d, e, s, t, i, n) => {
|
|
293
|
+
const o = Math.max(0, n * t), l = Math.min(o, d * t), h = Math.max(0, d * t - i + t);
|
|
294
|
+
switch (e) {
|
|
295
|
+
case "start":
|
|
296
|
+
return l;
|
|
297
|
+
case "end":
|
|
298
|
+
return h;
|
|
299
|
+
case "center": {
|
|
300
|
+
const _ = Math.round(h + (l - h) / 2);
|
|
301
|
+
return _ < Math.ceil(i / 2) ? 0 : _ > o + Math.floor(i / 2) ? o : _;
|
|
302
|
+
}
|
|
303
|
+
case "auto":
|
|
304
|
+
default:
|
|
305
|
+
return s >= h && s <= l ? s : s < h ? h : l;
|
|
306
|
+
}
|
|
307
|
+
}, D = (d) => d && typeof d != "string", se = (d, e) => {
|
|
308
|
+
const s = d.find((t) => D(t) && JSON.stringify(t.path) === JSON.stringify(e));
|
|
309
|
+
return D(s) ? s : null;
|
|
310
|
+
}, b = (d, e) => d.findIndex((s) => typeof s == "string" && s === JSON.stringify(e)), p = (d) => typeof d == "string", j = ":host{display:block;position:relative;contain:layout;overflow:auto}:host(:focus){outline:none}ul{list-style-type:none;margin:0;padding:0}.root{width:100%}";
|
|
311
|
+
var Y = Object.defineProperty, Q = Object.getOwnPropertyDescriptor, m = (d, e, s, t) => {
|
|
312
|
+
for (var i = t > 1 ? void 0 : t ? Q(e, s) : e, n = d.length - 1, o; n >= 0; n--)
|
|
313
|
+
(o = d[n]) && (i = (t ? o(e, s, i) : o(i)) || i);
|
|
314
|
+
return t && i && Y(e, s, i), i;
|
|
315
|
+
}, x;
|
|
316
|
+
const N = (x = class extends I {
|
|
317
|
+
constructor() {
|
|
318
|
+
super(), this.pageSize = 50, this.loadingText = "Loading...", this.parentSelectionAllowed = !1, this.readonly = !1, this._lastKnownHeight = 0, this._lastKnownScrollTop = 0, this._lastRenderedScrollTop = 0, this._visibleItemsNum = 0, this._firstVisibleIndex = 0, this._lastVisibleIndex = 0, this._firstRenderedIndex = 0, this._lastRenderedIndex = 0, this._focusIndex = -1, this._flattenedNodes = [], this._rootItemsLoading = !1, this._rootNodeCount = 0, this._loadedRootNodeCount = 0, this._allNodeCount = 0, this._nodeStates = /* @__PURE__ */ new Map(), this.onScroll = () => {
|
|
319
|
+
this._lastKnownScrollTop = this.scrollTop;
|
|
320
|
+
const e = this._lastRenderedScrollTop - this._lastKnownScrollTop;
|
|
321
|
+
Math.abs(e) >= S && (this._lastRenderedScrollTop = this._lastKnownScrollTop, this.requestUpdate());
|
|
322
|
+
}, this.handleNodeExpansion = (e) => {
|
|
323
|
+
const { path: s, expanded: t } = e.detail;
|
|
324
|
+
this.updateAfterExpansion(s, t);
|
|
325
|
+
}, this.handleKeyDown = (e) => {
|
|
326
|
+
let s = !0;
|
|
327
|
+
switch (e.key) {
|
|
328
|
+
case "ArrowDown": {
|
|
329
|
+
this.focusIndex = this.normalizeIndex(this.focusIndex + 1);
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
case "ArrowUp": {
|
|
333
|
+
this.focusIndex = this.normalizeIndex(this.focusIndex - 1);
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case "ArrowRight": {
|
|
337
|
+
const t = this.focusedNode;
|
|
338
|
+
if (t)
|
|
339
|
+
if (t.childCount > 0 && !t.expanded) {
|
|
340
|
+
const i = this.querySelector(`[node-id='${t.id}']`);
|
|
341
|
+
i && i.expand();
|
|
342
|
+
} else
|
|
343
|
+
this.focusIndex = this.normalizeIndex(this.focusIndex + 1);
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
case "ArrowLeft": {
|
|
347
|
+
const t = this.focusedNode;
|
|
348
|
+
if (t)
|
|
349
|
+
if (t.childCount > 0 && t.expanded) {
|
|
350
|
+
const i = this.querySelector(`[node-id='${t.id}']`);
|
|
351
|
+
i && i.collapse();
|
|
352
|
+
} else
|
|
353
|
+
this.focusIndex = this.normalizeIndex(this.focusIndex - 1);
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
case "Enter": {
|
|
357
|
+
const t = this.focusedNode;
|
|
358
|
+
t && (this.toggleSelection(t.path), this.updateComplete.then(() => {
|
|
359
|
+
this.dispatchEvent(
|
|
360
|
+
new CustomEvent("selection", {
|
|
361
|
+
detail: {
|
|
362
|
+
path: t.path,
|
|
363
|
+
checked: t.selectionState === "checked",
|
|
364
|
+
indeterminate: t.selectionState === "indeterminate"
|
|
365
|
+
},
|
|
366
|
+
bubbles: !0,
|
|
367
|
+
composed: !0
|
|
368
|
+
})
|
|
369
|
+
);
|
|
370
|
+
}));
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
default: {
|
|
374
|
+
s = !1;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
s && (e.preventDefault(), e.stopPropagation());
|
|
379
|
+
}, this.handleNodeSelection = (e) => {
|
|
380
|
+
if (e.target == this)
|
|
381
|
+
return;
|
|
382
|
+
this._reusePreviousRenderData = !0;
|
|
383
|
+
const { path: s } = e.detail;
|
|
384
|
+
this.focusIndex = b(this._flattenedNodes, s), this.focus(), this.toggleSelection(s), this.updateComplete.then(() => {
|
|
385
|
+
this._reusePreviousRenderData = !1;
|
|
386
|
+
});
|
|
387
|
+
}, this.getFlattenedNodeId = (e) => JSON.stringify(e.path), this.nodeGetter = (e) => e ? this._nodeStates.get(JSON.stringify(e)) : null, this._resizeObserver = new ResizeObserver(() => {
|
|
388
|
+
this._lastKnownHeight !== this.offsetHeight && (this._lastKnownHeight = this.offsetHeight, this.requestUpdate());
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
get rootNodeCount() {
|
|
392
|
+
return this._rootNodeCount;
|
|
393
|
+
}
|
|
394
|
+
set rootNodeCount(e) {
|
|
395
|
+
e === 0 ? this._allNodeCount = 0 : e > 0 && (this._allNodeCount += e - this.rootNodeCount), this._rootNodeCount = e, this.requestUpdate();
|
|
396
|
+
}
|
|
397
|
+
get focusIndex() {
|
|
398
|
+
return this._focusIndex;
|
|
399
|
+
}
|
|
400
|
+
set focusIndex(e) {
|
|
401
|
+
if (e >= -1 && e < this._allNodeCount) {
|
|
402
|
+
const s = this._focusIndex;
|
|
403
|
+
this._focusIndex = e, (e <= this._firstVisibleIndex || this._lastVisibleIndex <= e) && this.scrollToNode(e), s != e && this.requestUpdate("focusIndex", s);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
setNodes(e) {
|
|
407
|
+
this.stopLoadingIndicator(), this.setLastLoadingChildren(e, this.rootNodeCount, e.length), e.forEach((s, t) => {
|
|
408
|
+
const i = [...e];
|
|
409
|
+
i.splice(t, 1), this.updateNodeMap(s, i);
|
|
410
|
+
}), this.updateCount(e), this._loadedRootNodeCount = e.length, this._flattenedNodes = y(e, 1 / 0).map(this.getFlattenedNodeId), this.requestUpdate();
|
|
411
|
+
}
|
|
412
|
+
addNodes(e, s) {
|
|
413
|
+
this.stopLoadingIndicator(s), e.forEach((i, n) => {
|
|
414
|
+
const o = [...e];
|
|
415
|
+
o.splice(n, 1), this.updateNodeMap(i, o);
|
|
416
|
+
});
|
|
417
|
+
const t = this.nodeGetter(s);
|
|
418
|
+
t ? t.expanded && (this._allNodeCount += e.length + this.getNodeCount(e)) : this._loadedRootNodeCount += e.length, this.updateFlattenedNodes(e, s), this.requestUpdate();
|
|
419
|
+
}
|
|
420
|
+
connectedCallback() {
|
|
421
|
+
super.connectedCallback(), this._resizeObserver.observe(this);
|
|
422
|
+
}
|
|
423
|
+
disconnectedCallback() {
|
|
424
|
+
super.disconnectedCallback(), this._resizeObserver.disconnect();
|
|
425
|
+
}
|
|
426
|
+
static get styles() {
|
|
427
|
+
return [
|
|
428
|
+
C`
|
|
429
|
+
${k(j)}
|
|
430
|
+
`
|
|
431
|
+
];
|
|
432
|
+
}
|
|
433
|
+
firstUpdated(e) {
|
|
434
|
+
super.firstUpdated(e), this.addEventListener("scroll", this.onScroll), this.addEventListener("keydown", this.handleKeyDown), this.addEventListener("expansion", this.handleNodeExpansion), this.addEventListener("selection", this.handleNodeSelection), this.addEventListener("focus", () => {
|
|
435
|
+
this.focusIndex == -1 && (this.focusIndex = this.normalizeIndex(0));
|
|
436
|
+
}), this._nodeStates.size === 0 && (this.children.length > 0 ? this.syncState() : this.requestData());
|
|
437
|
+
}
|
|
438
|
+
updated(e) {
|
|
439
|
+
super.updated(e), this.updateNodes(), e.has("parentSelectionAllowed") && this.updateParentSelectionAllowed();
|
|
440
|
+
}
|
|
441
|
+
render() {
|
|
442
|
+
return this.updateRenderData(), r`
|
|
443
|
+
<div class="root" style="height: ${this._allNodeCount * S}px">
|
|
444
|
+
<ul>
|
|
445
|
+
<slot></slot>
|
|
446
|
+
</ul>
|
|
447
|
+
</div>
|
|
448
|
+
`;
|
|
449
|
+
}
|
|
450
|
+
scrollToNode(e, s = "auto") {
|
|
451
|
+
this.scrollTop = H(
|
|
452
|
+
this.normalizeIndex(e),
|
|
453
|
+
s,
|
|
454
|
+
this.scrollTop,
|
|
455
|
+
S,
|
|
456
|
+
this.height,
|
|
457
|
+
this._allNodeCount
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
updateNodeMap(e, s) {
|
|
461
|
+
e && (e.nodeGetter = this.nodeGetter, e.getSiblings = () => s, this._nodeStates.set(JSON.stringify(e.path), e), e.nodes.forEach((t, i) => {
|
|
462
|
+
const n = [...e.nodes];
|
|
463
|
+
n.splice(i, 1), this.updateNodeMap(t, n);
|
|
464
|
+
}));
|
|
465
|
+
}
|
|
466
|
+
updateParentSelectionAllowed() {
|
|
467
|
+
this._nodeStates.forEach((e) => {
|
|
468
|
+
e.parentSelectionAllowed = this.parentSelectionAllowed;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
updateNodes() {
|
|
472
|
+
const e = this._flattenedNodes.slice(this._firstRenderedIndex, this._lastRenderedIndex + 1);
|
|
473
|
+
R(
|
|
474
|
+
r`
|
|
475
|
+
${e.map((s, t) => {
|
|
476
|
+
const i = t + this._firstRenderedIndex, n = this.getTopPosition(i);
|
|
477
|
+
if (!p(s))
|
|
478
|
+
return r`
|
|
479
|
+
<sd-tree-loading-indicator .depth="${s.depth}" style="transform: translateY(${n}px)"
|
|
480
|
+
>${this.loadingText}</sd-tree-loading-indicator
|
|
481
|
+
>
|
|
482
|
+
`;
|
|
483
|
+
const o = this._nodeStates.get(s), l = i === this.focusIndex;
|
|
484
|
+
let h = o.disabled;
|
|
485
|
+
return this.parentSelectionAllowed || (h = h || o.hasRadioButtonDescendant()), r`
|
|
486
|
+
<sd-tree-node
|
|
487
|
+
text="${o.text}"
|
|
488
|
+
selection-state="${o.selectionState}"
|
|
489
|
+
.nodeId="${o.id}"
|
|
490
|
+
.depth="${o.depth}"
|
|
491
|
+
style="transform: translateY(${n}px);"
|
|
492
|
+
.path="${o.path}"
|
|
493
|
+
?focused="${l}"
|
|
494
|
+
?readonly="${this.readonly}"
|
|
495
|
+
type="${o.type}"
|
|
496
|
+
?expanded="${o.expanded}"
|
|
497
|
+
child-count="${o.childCount}"
|
|
498
|
+
?disabled="${h}"
|
|
499
|
+
.loading="${o.loading}"
|
|
500
|
+
></sd-tree-node>
|
|
501
|
+
`;
|
|
502
|
+
})}
|
|
503
|
+
`,
|
|
504
|
+
this
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
getTopPosition(e) {
|
|
508
|
+
return e * S;
|
|
509
|
+
}
|
|
510
|
+
updateRenderData() {
|
|
511
|
+
if (!this._reusePreviousRenderData)
|
|
512
|
+
if (this._lastRenderedScrollTop = this.scrollTop, this._visibleItemsNum = Math.min(Math.ceil(this.height / S), this._allNodeCount), this._visibleItemsNum > 0) {
|
|
513
|
+
this._firstVisibleIndex = this.normalizeIndex(Math.floor(this._lastRenderedScrollTop / S)), this._lastVisibleIndex = this.normalizeIndex(this._firstVisibleIndex + this._visibleItemsNum), this._firstRenderedIndex = this.normalizeIndex(this._firstVisibleIndex - 2), this._lastRenderedIndex = this.normalizeIndex(this._lastVisibleIndex + 2);
|
|
514
|
+
for (const e of this._flattenedNodes.slice(this._firstRenderedIndex, this._lastRenderedIndex + 1)) {
|
|
515
|
+
const s = p(e) && this._nodeStates.get(e);
|
|
516
|
+
if (s) {
|
|
517
|
+
if (s.lastLoadingChild) {
|
|
518
|
+
const t = s.parentNode;
|
|
519
|
+
if (t) {
|
|
520
|
+
if (this.needsFurtherData(t)) {
|
|
521
|
+
this.requestData(t);
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
} else if (this._loadedRootNodeCount < this.rootNodeCount && !this._rootItemsLoading) {
|
|
525
|
+
this.requestData();
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
} else if (this.needsFurtherData(s)) {
|
|
529
|
+
this.requestData(s);
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
} else
|
|
535
|
+
this._firstVisibleIndex = 0, this._lastVisibleIndex = 0, this._firstRenderedIndex = 0, this._lastRenderedIndex = 0;
|
|
536
|
+
}
|
|
537
|
+
needsFurtherData(e) {
|
|
538
|
+
return e.expanded && e.childCount > e.nodes.length && this.nodeIndex(e.id) + e.nodes.length < this._lastRenderedIndex;
|
|
539
|
+
}
|
|
540
|
+
nodeIndex(e) {
|
|
541
|
+
return this._flattenedNodes.findIndex((s) => s === e);
|
|
542
|
+
}
|
|
543
|
+
normalizeIndex(e) {
|
|
544
|
+
return Math.max(0, Math.min(e, this._allNodeCount - 1));
|
|
545
|
+
}
|
|
546
|
+
get height() {
|
|
547
|
+
return this.offsetHeight;
|
|
548
|
+
}
|
|
549
|
+
requestData(e) {
|
|
550
|
+
if (!(e != null && e.loading)) {
|
|
551
|
+
const s = e == null ? void 0 : e.path;
|
|
552
|
+
this.startLoadingIndicator(s), this.dispatchEvent(
|
|
553
|
+
new CustomEvent("data-request", {
|
|
554
|
+
detail: { path: s }
|
|
555
|
+
})
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
updateAfterExpansion(e, s) {
|
|
560
|
+
const t = this._nodeStates.get(JSON.stringify(e));
|
|
561
|
+
if (t) {
|
|
562
|
+
t.expanded = s;
|
|
563
|
+
const i = b(this._flattenedNodes, e), n = this._flattenedNodes[this.focusIndex];
|
|
564
|
+
if (s) {
|
|
565
|
+
const o = this.addSubtree(t, i + 1), l = t.childCount > t.nodes.length, h = i + t.nodes.length < this._lastRenderedIndex;
|
|
566
|
+
l && h && this.requestData(t), this._allNodeCount += o;
|
|
567
|
+
} else {
|
|
568
|
+
const o = this.removeSubtree(i);
|
|
569
|
+
this._allNodeCount -= o;
|
|
570
|
+
}
|
|
571
|
+
if (n) {
|
|
572
|
+
const o = this._flattenedNodes.findIndex((l) => l == n);
|
|
573
|
+
o !== this.focusIndex && (this._focusIndex = o);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
this.requestUpdate();
|
|
577
|
+
}
|
|
578
|
+
addSubtree(e, s) {
|
|
579
|
+
const t = this.getFlattenedSubtree(e.nodes);
|
|
580
|
+
return this._flattenedNodes.splice(s, 0, ...t), t.length;
|
|
581
|
+
}
|
|
582
|
+
getFlattenedSubtree(e) {
|
|
583
|
+
return e.reduce((s, t) => s.concat(
|
|
584
|
+
t && t.childCount > 0 && t.expanded ? [JSON.stringify(t.path), ...this.getFlattenedSubtree(t.nodes)] : JSON.stringify(t.path)
|
|
585
|
+
), []);
|
|
586
|
+
}
|
|
587
|
+
removeSubtree(e) {
|
|
588
|
+
const s = this.getNextNonDescendantIndex(e);
|
|
589
|
+
let t = 0;
|
|
590
|
+
return s !== -1 ? (t = s - e - 1, this._flattenedNodes.splice(e + 1, t)) : (t = this._flattenedNodes.length - e - 1, this._flattenedNodes.splice(e + 1)), t;
|
|
591
|
+
}
|
|
592
|
+
// Returns the index of the node which is the next node
|
|
593
|
+
// in this._flattenedNodes after the descendants of the given node.
|
|
594
|
+
// This is used for removing the descendans of a node from this._flattenedNodes
|
|
595
|
+
// after collapsing it.
|
|
596
|
+
getNextNonDescendantIndex(e) {
|
|
597
|
+
const s = this._flattenedNodes[e], t = p(s) && this._nodeStates.get(s);
|
|
598
|
+
if (t)
|
|
599
|
+
for (let i = e + 1; i < this._flattenedNodes.length; i++) {
|
|
600
|
+
const n = this._flattenedNodes[i], o = p(n) && this._nodeStates.get(n);
|
|
601
|
+
if (o && o.path.length <= t.path.length)
|
|
602
|
+
return i;
|
|
603
|
+
}
|
|
604
|
+
return -1;
|
|
605
|
+
}
|
|
606
|
+
get focusedNode() {
|
|
607
|
+
const e = this._flattenedNodes[this.focusIndex];
|
|
608
|
+
return p(e) && this._nodeStates.get(e);
|
|
609
|
+
}
|
|
610
|
+
toggleSelection(e) {
|
|
611
|
+
this.nodeGetter(e).toggle(), this.requestUpdate();
|
|
612
|
+
}
|
|
613
|
+
updateFlattenedNodes(e, s) {
|
|
614
|
+
if (s) {
|
|
615
|
+
const t = b(this._flattenedNodes, s), i = this._flattenedNodes[t];
|
|
616
|
+
if (p(i)) {
|
|
617
|
+
const n = this._nodeStates.get(i);
|
|
618
|
+
if (n) {
|
|
619
|
+
const o = t + n.nodes.length + 1, l = n.depth, h = y(e, 1 / 0, l + 1).map(
|
|
620
|
+
this.getFlattenedNodeId
|
|
621
|
+
);
|
|
622
|
+
n.expanded && this._flattenedNodes.splice(o, 0, ...h), n.nodes = [...n.nodes, ...e], n.loading = !1, this.updateLastLoadingChild(n.nodes, h);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
} else {
|
|
626
|
+
const t = y(e, 1 / 0).map(this.getFlattenedNodeId);
|
|
627
|
+
this.updateLastLoadingChild(this._flattenedNodes, t), this._flattenedNodes = [...this._flattenedNodes, ...t];
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
updateLastLoadingChild(e, s) {
|
|
631
|
+
const t = e[e.length - 1], i = p(t) && this._nodeStates.get(t), n = s[s.length - 1], o = p(n) && this._nodeStates.get(n);
|
|
632
|
+
if (i && (i.lastLoadingChild = !1), o) {
|
|
633
|
+
const l = o.parentNode;
|
|
634
|
+
(l && l.childCount > l.nodes.length || !l && this.rootNodeCount > this._loadedRootNodeCount) && (o.lastLoadingChild = !0);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
setLastLoadingChildren(e, s, t) {
|
|
638
|
+
if (t < s) {
|
|
639
|
+
const i = e[e.length - 1];
|
|
640
|
+
i && (i.lastLoadingChild = !0);
|
|
641
|
+
}
|
|
642
|
+
e.forEach((i) => {
|
|
643
|
+
this.setLastLoadingChildren(i.nodes, i.childCount, i.nodes.length);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
startLoadingIndicator(e) {
|
|
647
|
+
if (e) {
|
|
648
|
+
const s = this._nodeStates.get(JSON.stringify(e));
|
|
649
|
+
if (s) {
|
|
650
|
+
const t = b(this._flattenedNodes, e);
|
|
651
|
+
s.loading = !0, this.addPlaceholders(t, s);
|
|
652
|
+
}
|
|
653
|
+
} else
|
|
654
|
+
this._rootItemsLoading = !0, this._flattenedNodes = [
|
|
655
|
+
...this._flattenedNodes,
|
|
656
|
+
...new Array(this.rootNodeCount - this._loadedRootNodeCount).fill({ depth: 0 })
|
|
657
|
+
];
|
|
658
|
+
this.updateNodes();
|
|
659
|
+
}
|
|
660
|
+
addPlaceholders(e, s) {
|
|
661
|
+
const t = e + s.nodes.length + 1, i = s.childCount - s.nodes.length;
|
|
662
|
+
this._flattenedNodes.splice(
|
|
663
|
+
t,
|
|
664
|
+
0,
|
|
665
|
+
...new Array(i).fill({
|
|
666
|
+
depth: s.depth + 1
|
|
667
|
+
})
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
stopLoadingIndicator(e) {
|
|
671
|
+
if (e) {
|
|
672
|
+
const s = this._nodeStates.get(JSON.stringify(e));
|
|
673
|
+
if (s && (s.loading = !1, s.expanded)) {
|
|
674
|
+
const t = b(this._flattenedNodes, e);
|
|
675
|
+
this.removePlaceHolders(t, s);
|
|
676
|
+
}
|
|
677
|
+
} else {
|
|
678
|
+
this._rootItemsLoading = !1;
|
|
679
|
+
const s = this._flattenedNodes.findIndex((t) => !p(t));
|
|
680
|
+
this._flattenedNodes.splice(s);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
removePlaceHolders(e, s) {
|
|
684
|
+
const t = e + s.nodes.length + 1, i = s.childCount - s.nodes.length;
|
|
685
|
+
this._flattenedNodes.splice(t, i);
|
|
686
|
+
}
|
|
687
|
+
// this method is used to synchronize this.nodes property
|
|
688
|
+
// to the declaratively created nodes
|
|
689
|
+
syncState() {
|
|
690
|
+
const s = [...this.children].map((t) => t.getState());
|
|
691
|
+
this.addNodes(s), this.rootNodeCount = s.length, this.innerHTML = "";
|
|
692
|
+
}
|
|
693
|
+
updateCount(e) {
|
|
694
|
+
this._allNodeCount = e.length + this.getNodeCount(e);
|
|
695
|
+
}
|
|
696
|
+
getNodeCount(e) {
|
|
697
|
+
let s = 0;
|
|
698
|
+
return e.forEach((t) => {
|
|
699
|
+
t.expanded && (s += t.childCount + this.getNodeCount(t.nodes));
|
|
700
|
+
}), s;
|
|
701
|
+
}
|
|
702
|
+
}, x.ID = "sd-tree", x.ensureDefined = () => {
|
|
703
|
+
F.ensureDefined(), K.ensureDefined(), customElements.get(x.ID) || customElements.define(x.ID, x);
|
|
704
|
+
}, x);
|
|
705
|
+
m([
|
|
706
|
+
a({ type: String })
|
|
707
|
+
], N.prototype, "loadingText", 2);
|
|
708
|
+
m([
|
|
709
|
+
a({ type: Boolean, reflect: !0, attribute: "parent-selection-allowed" })
|
|
710
|
+
], N.prototype, "parentSelectionAllowed", 2);
|
|
711
|
+
m([
|
|
712
|
+
a({ type: Boolean, reflect: !0, attribute: !0 })
|
|
713
|
+
], N.prototype, "readonly", 2);
|
|
714
|
+
m([
|
|
715
|
+
a({ type: Number })
|
|
716
|
+
], N.prototype, "rootNodeCount", 1);
|
|
717
|
+
m([
|
|
718
|
+
a({ type: Number, attribute: "focus-index", reflect: !0 })
|
|
719
|
+
], N.prototype, "focusIndex", 1);
|
|
720
|
+
let W = N;
|
|
721
|
+
W.ensureDefined();
|
|
722
|
+
export {
|
|
723
|
+
P as CheckboxNode,
|
|
724
|
+
E as Node,
|
|
725
|
+
v as RadioButtonNode,
|
|
726
|
+
W as Tree,
|
|
727
|
+
se as findNode,
|
|
728
|
+
G as findNodeByPath,
|
|
729
|
+
b as findNodeIndex,
|
|
730
|
+
y as flattenNodes,
|
|
731
|
+
H as getOffsetForIndexAndAlignment,
|
|
732
|
+
T as isCheckboxNode,
|
|
733
|
+
p as isFlattenedNode,
|
|
734
|
+
D as isNode,
|
|
735
|
+
A as isRadioButtonNode
|
|
736
|
+
};
|
|
737
|
+
//# sourceMappingURL=tree.mjs.map
|