@pure-ds/core 0.6.10 → 0.7.1
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/.github/copilot-instructions.md +6 -1
- package/custom-elements.json +803 -16
- package/dist/types/pds.d.ts +1 -0
- package/dist/types/public/assets/js/pds-ask.d.ts +2 -0
- package/dist/types/public/assets/js/pds-ask.d.ts.map +1 -0
- package/dist/types/public/assets/js/pds-auto-definer.d.ts +14 -0
- package/dist/types/public/assets/js/pds-auto-definer.d.ts.map +1 -0
- package/dist/types/public/assets/js/pds-autocomplete.d.ts +79 -0
- package/dist/types/public/assets/js/pds-autocomplete.d.ts.map +1 -0
- package/dist/types/public/assets/js/pds-enhancers.d.ts +7 -0
- package/dist/types/public/assets/js/pds-enhancers.d.ts.map +1 -0
- package/dist/types/public/assets/js/pds-manager.d.ts +98 -1
- package/dist/types/public/assets/js/pds-manager.d.ts.map +1 -1
- package/dist/types/public/assets/js/pds-toast.d.ts +8 -0
- package/dist/types/public/assets/js/pds-toast.d.ts.map +1 -0
- package/dist/types/public/assets/js/pds.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-drawer.d.ts +1 -143
- package/dist/types/public/assets/pds/components/pds-drawer.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-form.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-icon.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-live-converter.d.ts +8 -0
- package/dist/types/public/assets/pds/components/pds-live-converter.d.ts.map +1 -0
- package/dist/types/public/assets/pds/components/pds-live-importer.d.ts +2 -0
- package/dist/types/public/assets/pds/components/pds-live-importer.d.ts.map +1 -0
- package/dist/types/public/assets/pds/components/pds-live-template-canvas.d.ts +2 -0
- package/dist/types/public/assets/pds/components/pds-live-template-canvas.d.ts.map +1 -0
- package/dist/types/public/assets/pds/components/pds-omnibox.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-richtext.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-scrollrow.d.ts +1 -63
- package/dist/types/public/assets/pds/components/pds-scrollrow.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-splitpanel.d.ts +1 -89
- package/dist/types/public/assets/pds/components/pds-splitpanel.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-theme.d.ts +1 -22
- package/dist/types/public/assets/pds/components/pds-theme.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-toaster.d.ts +1 -1
- package/dist/types/public/assets/pds/components/pds-toaster.d.ts.map +1 -1
- package/dist/types/public/assets/pds/components/pds-treeview.d.ts +37 -0
- package/dist/types/public/assets/pds/components/pds-treeview.d.ts.map +1 -0
- package/dist/types/public/assets/pds/components/pds-upload.d.ts.map +1 -1
- package/dist/types/src/js/common/ask.d.ts.map +1 -1
- package/dist/types/src/js/common/toast.d.ts +8 -0
- package/dist/types/src/js/common/toast.d.ts.map +1 -1
- package/dist/types/src/js/pds-ask.d.ts +2 -0
- package/dist/types/src/js/pds-ask.d.ts.map +1 -0
- package/dist/types/src/js/pds-auto-definer.d.ts +2 -0
- package/dist/types/src/js/pds-auto-definer.d.ts.map +1 -0
- package/dist/types/src/js/pds-autocomplete.d.ts +2 -0
- package/dist/types/src/js/pds-autocomplete.d.ts.map +1 -0
- package/dist/types/src/js/pds-core/pds-enhancers.d.ts.map +1 -1
- package/dist/types/src/js/pds-core/pds-generator.d.ts.map +1 -1
- package/dist/types/src/js/pds-core/pds-live.d.ts +2 -1
- package/dist/types/src/js/pds-core/pds-live.d.ts.map +1 -1
- package/dist/types/src/js/pds-core/pds-start-helpers.d.ts.map +1 -1
- package/dist/types/src/js/pds-enhancers.d.ts +2 -0
- package/dist/types/src/js/pds-enhancers.d.ts.map +1 -0
- package/dist/types/src/js/pds-live-manager/conversion-service.d.ts +66 -0
- package/dist/types/src/js/pds-live-manager/conversion-service.d.ts.map +1 -0
- package/dist/types/src/js/pds-live-manager/import-contract.d.ts +15 -0
- package/dist/types/src/js/pds-live-manager/import-contract.d.ts.map +1 -0
- package/dist/types/src/js/pds-live-manager/import-history-service.d.ts +32 -0
- package/dist/types/src/js/pds-live-manager/import-history-service.d.ts.map +1 -0
- package/dist/types/src/js/pds-live-manager/import-service.d.ts +21 -0
- package/dist/types/src/js/pds-live-manager/import-service.d.ts.map +1 -0
- package/dist/types/src/js/pds-live-manager/template-service.d.ts +17 -0
- package/dist/types/src/js/pds-live-manager/template-service.d.ts.map +1 -0
- package/dist/types/src/js/pds-manager.d.ts +4 -0
- package/dist/types/src/js/pds-toast.d.ts +2 -0
- package/dist/types/src/js/pds-toast.d.ts.map +1 -0
- package/dist/types/src/js/pds.d.ts.map +1 -1
- package/package.json +11 -5
- package/packages/pds-cli/README.md +60 -0
- package/packages/pds-cli/bin/pds-import.js +176 -0
- package/packages/pds-cli/bin/pds-static.js +27 -1
- package/packages/pds-cli/bin/postinstall.mjs +17 -8
- package/packages/pds-cli/bin/templates/bootstrap/pds.config.js +1 -5
- package/packages/pds-cli/bin/templates/bootstrap/public/index.html +2 -1
- package/packages/pds-cli/bin/templates/starter-templates.js +1 -3
- package/public/assets/js/app.js +9 -163
- package/public/assets/js/pds-ask.js +25 -0
- package/public/assets/js/pds-auto-definer.js +1 -0
- package/public/assets/js/pds-autocomplete.js +7 -0
- package/public/assets/js/pds-enhancers.js +1 -0
- package/public/assets/js/pds-manager.js +370 -267
- package/public/assets/js/pds-toast.js +1 -0
- package/public/assets/js/pds.js +2 -32
- package/public/assets/pds/components/pds-calendar.js +2 -2
- package/public/assets/pds/components/pds-drawer.js +1 -1
- package/public/assets/pds/components/pds-form.js +7 -6
- package/public/assets/pds/components/pds-icon.js +12 -9
- package/public/assets/pds/components/pds-live-converter.js +47 -0
- package/public/assets/pds/components/pds-live-edit.js +758 -44
- package/public/assets/pds/components/pds-live-importer.js +773 -0
- package/public/assets/pds/components/pds-live-template-canvas.js +172 -0
- package/public/assets/pds/components/pds-omnibox.js +147 -3
- package/public/assets/pds/components/pds-richtext.js +2 -0
- package/public/assets/pds/components/pds-scrollrow.js +61 -2
- package/public/assets/pds/components/pds-splitpanel.js +3 -1
- package/public/assets/pds/components/pds-theme.js +2 -0
- package/public/assets/pds/components/pds-toaster.js +52 -5
- package/public/assets/pds/components/pds-treeview.js +974 -0
- package/public/assets/pds/components/pds-upload.js +2 -0
- package/public/assets/pds/core/pds-ask.js +25 -0
- package/public/assets/pds/core/pds-auto-definer.js +1 -0
- package/public/assets/pds/core/pds-autocomplete.js +7 -0
- package/public/assets/pds/core/pds-enhancers.js +1 -0
- package/public/assets/pds/core/pds-manager.js +3646 -0
- package/public/assets/pds/core/pds-toast.js +1 -0
- package/public/assets/pds/core.js +2 -0
- package/public/assets/pds/custom-elements.json +803 -16
- package/public/assets/pds/pds-css-complete.json +7 -2
- package/public/assets/pds/templates/commerce-scroll-explorer.html +115 -0
- package/public/assets/pds/templates/content-brand-showcase.html +110 -0
- package/public/assets/pds/templates/feedback-ops-dashboard.html +91 -0
- package/public/assets/pds/templates/release-readiness-radar.html +69 -0
- package/public/assets/pds/templates/support-command-center.html +92 -0
- package/public/assets/pds/templates/templates.json +53 -0
- package/public/assets/pds/templates/workspace-settings-lab.html +131 -0
- package/public/assets/pds/vscode-custom-data.json +54 -4
- package/readme.md +38 -1
- package/src/js/pds-core/pds-config.js +9 -9
- package/src/js/pds-core/pds-enhancers.js +146 -0
- package/src/js/pds-core/pds-generator.js +170 -29
- package/src/js/pds-core/pds-live.js +456 -13
- package/src/js/pds-core/pds-start-helpers.js +5 -1
- package/src/js/pds-live-manager/conversion-service.js +3135 -0
- package/src/js/pds-live-manager/import-contract.js +57 -0
- package/src/js/pds-live-manager/import-history-service.js +145 -0
- package/src/js/pds-live-manager/import-service.js +255 -0
- package/src/js/pds-live-manager/tailwind-conversion-rules.json +383 -0
- package/src/js/pds-live-manager/template-service.js +170 -0
- package/src/js/pds.d.ts +1 -0
- package/src/js/pds.js +192 -12
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
import { PDS } from "#pds";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accessible, form-associated treeview with nested UL output.
|
|
5
|
+
*
|
|
6
|
+
* @element pds-treeview
|
|
7
|
+
* @formAssociated
|
|
8
|
+
*
|
|
9
|
+
* @attr {string} name - Form field name.
|
|
10
|
+
* @attr {string} value - Selected node value.
|
|
11
|
+
* @attr {boolean} disabled - Disables interaction.
|
|
12
|
+
* @attr {boolean} required - Requires a selected node for form validity.
|
|
13
|
+
* @attr {boolean} display-only - Renders as browse-only tree without selection.
|
|
14
|
+
* @attr {boolean} expanded-all - Expands all branch nodes on load.
|
|
15
|
+
* @attr {string} src - Optional URL source for tree JSON.
|
|
16
|
+
*
|
|
17
|
+
* @property {object} settings - Data and behavior settings object.
|
|
18
|
+
* @property {object} options - Alias for settings.
|
|
19
|
+
* @property {Function} settings.getChildren - Optional async child loader invoked on first expand per node.
|
|
20
|
+
*
|
|
21
|
+
* @fires treeview-load Fired after root data has been loaded and indexed.
|
|
22
|
+
* @fires treeview-error Fired when root source loading fails.
|
|
23
|
+
* @fires node-toggle Fired when a branch node is expanded or collapsed.
|
|
24
|
+
* @fires node-select Fired when a node is selected.
|
|
25
|
+
* @fires node-load Fired when lazy children for an expanded node are loaded.
|
|
26
|
+
* @fires node-load-error Fired when lazy children loading fails for a node.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const LAYERS = ["tokens", "primitives", "components", "utilities"];
|
|
30
|
+
const ROOT_SELECTOR = ".tv-tree";
|
|
31
|
+
|
|
32
|
+
export class PdsTreeview extends HTMLElement {
|
|
33
|
+
static formAssociated = true;
|
|
34
|
+
|
|
35
|
+
static get observedAttributes() {
|
|
36
|
+
return ["name", "value", "disabled", "required", "display-only", "expanded-all", "src"];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#root;
|
|
40
|
+
#internals;
|
|
41
|
+
#settings = {};
|
|
42
|
+
#nodes = [];
|
|
43
|
+
#expandedIds = new Set();
|
|
44
|
+
#nodeById = new Map();
|
|
45
|
+
#parentById = new Map();
|
|
46
|
+
#selectedId = null;
|
|
47
|
+
#focusedId = null;
|
|
48
|
+
#defaultValue = "";
|
|
49
|
+
#loadToken = 0;
|
|
50
|
+
#childrenLoadPromises = new Map();
|
|
51
|
+
|
|
52
|
+
constructor() {
|
|
53
|
+
super();
|
|
54
|
+
this.#root = this.attachShadow({ mode: "open" });
|
|
55
|
+
this.#internals = this.attachInternals();
|
|
56
|
+
this.#renderShell();
|
|
57
|
+
void this.#adoptStyles();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
connectedCallback() {
|
|
61
|
+
this.#defaultValue = this.getAttribute("value") || "";
|
|
62
|
+
this.#syncAttributes();
|
|
63
|
+
void this.refresh();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
67
|
+
if (oldValue === newValue) return;
|
|
68
|
+
if (name === "src") {
|
|
69
|
+
void this.refresh();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.#syncAttributes();
|
|
73
|
+
if (name === "value" && this.#nodes.length) {
|
|
74
|
+
this.selectByValue(newValue ?? "");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
formAssociatedCallback() {}
|
|
79
|
+
|
|
80
|
+
formDisabledCallback(disabled) {
|
|
81
|
+
this.toggleAttribute("disabled", Boolean(disabled));
|
|
82
|
+
this.#syncAttributes();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
formResetCallback() {
|
|
86
|
+
this.value = this.#defaultValue;
|
|
87
|
+
this.selectByValue(this.#defaultValue);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
formStateRestoreCallback(state) {
|
|
91
|
+
this.value = state ?? "";
|
|
92
|
+
this.selectByValue(this.value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get settings() {
|
|
96
|
+
return this.#settings;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
set settings(value) {
|
|
100
|
+
this.#settings = value && typeof value === "object" ? value : {};
|
|
101
|
+
void this.refresh();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get options() {
|
|
105
|
+
return this.settings;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
set options(value) {
|
|
109
|
+
this.settings = value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get name() {
|
|
113
|
+
return this.getAttribute("name") || "";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
set name(value) {
|
|
117
|
+
if (value == null || value === "") this.removeAttribute("name");
|
|
118
|
+
else this.setAttribute("name", value);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get value() {
|
|
122
|
+
return this.getAttribute("value") || "";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
set value(value) {
|
|
126
|
+
const next = value == null ? "" : String(value);
|
|
127
|
+
if (next === "") this.removeAttribute("value");
|
|
128
|
+
else this.setAttribute("value", next);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get disabled() {
|
|
132
|
+
return this.hasAttribute("disabled");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
set disabled(value) {
|
|
136
|
+
if (value) this.setAttribute("disabled", "");
|
|
137
|
+
else this.removeAttribute("disabled");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
get required() {
|
|
141
|
+
return this.hasAttribute("required");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
set required(value) {
|
|
145
|
+
if (value) this.setAttribute("required", "");
|
|
146
|
+
else this.removeAttribute("required");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get displayOnly() {
|
|
150
|
+
return this.hasAttribute("display-only");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
set displayOnly(value) {
|
|
154
|
+
if (value) this.setAttribute("display-only", "");
|
|
155
|
+
else this.removeAttribute("display-only");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get expandedAll() {
|
|
159
|
+
return this.hasAttribute("expanded-all");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
set expandedAll(value) {
|
|
163
|
+
if (value) this.setAttribute("expanded-all", "");
|
|
164
|
+
else this.removeAttribute("expanded-all");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get selectedNode() {
|
|
168
|
+
return this.#selectedId ? this.#nodeById.get(this.#selectedId) || null : null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getSelectedNode() {
|
|
172
|
+
return this.selectedNode;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async refresh() {
|
|
176
|
+
const loadToken = ++this.#loadToken;
|
|
177
|
+
const host = this.#root.querySelector(".tv-host");
|
|
178
|
+
if (host) host.dataset.state = "loading";
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
const sourceData = await this.#resolveSource();
|
|
182
|
+
if (loadToken !== this.#loadToken) return;
|
|
183
|
+
this.#ingestData(sourceData);
|
|
184
|
+
this.#renderTree();
|
|
185
|
+
this.#restoreSelection();
|
|
186
|
+
this.dispatchEvent(
|
|
187
|
+
new CustomEvent("treeview-load", {
|
|
188
|
+
detail: { nodes: this.#nodes },
|
|
189
|
+
bubbles: true,
|
|
190
|
+
composed: true,
|
|
191
|
+
}),
|
|
192
|
+
);
|
|
193
|
+
if (host) host.dataset.state = "ready";
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (loadToken !== this.#loadToken) return;
|
|
196
|
+
console.error("pds-treeview: failed to load data", error);
|
|
197
|
+
this.#nodes = [];
|
|
198
|
+
this.#nodeById.clear();
|
|
199
|
+
this.#parentById.clear();
|
|
200
|
+
this.#expandedIds.clear();
|
|
201
|
+
this.#selectedId = null;
|
|
202
|
+
this.#focusedId = null;
|
|
203
|
+
this.#renderTree();
|
|
204
|
+
if (host) host.dataset.state = "error";
|
|
205
|
+
this.dispatchEvent(
|
|
206
|
+
new CustomEvent("treeview-error", {
|
|
207
|
+
detail: { error },
|
|
208
|
+
bubbles: true,
|
|
209
|
+
composed: true,
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
expandAll() {
|
|
216
|
+
for (const [id, node] of this.#nodeById.entries()) {
|
|
217
|
+
if (node.children?.length) this.#expandedIds.add(id);
|
|
218
|
+
}
|
|
219
|
+
this.#renderTree();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
collapseAll() {
|
|
223
|
+
this.#expandedIds.clear();
|
|
224
|
+
this.#renderTree();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
selectById(id) {
|
|
228
|
+
if (!id || !this.#nodeById.has(id)) return false;
|
|
229
|
+
this.#selectNode(id, { user: false, focus: true });
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
selectByValue(value) {
|
|
234
|
+
const normalized = value == null ? "" : String(value);
|
|
235
|
+
if (!normalized) {
|
|
236
|
+
this.#selectedId = null;
|
|
237
|
+
this.#syncValue();
|
|
238
|
+
this.#renderTree();
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
for (const [id, node] of this.#nodeById.entries()) {
|
|
242
|
+
if (String(node.value) === normalized) {
|
|
243
|
+
this.#expandAncestors(id);
|
|
244
|
+
this.#selectNode(id, { user: false, focus: false });
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
checkValidity() {
|
|
252
|
+
this.#syncValidity();
|
|
253
|
+
return this.#internals.checkValidity();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
reportValidity() {
|
|
257
|
+
this.#syncValidity();
|
|
258
|
+
return this.#internals.reportValidity();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
#renderShell() {
|
|
262
|
+
this.#root.innerHTML = `
|
|
263
|
+
<div class="tv-host" data-state="ready">
|
|
264
|
+
<ul class="tv-tree" role="tree" aria-label="Treeview"></ul>
|
|
265
|
+
</div>
|
|
266
|
+
`;
|
|
267
|
+
|
|
268
|
+
this.#root.addEventListener("click", (event) => {
|
|
269
|
+
const target = event.target;
|
|
270
|
+
if (!(target instanceof Element) || this.disabled) return;
|
|
271
|
+
|
|
272
|
+
const toggle = target.closest(".tv-toggle");
|
|
273
|
+
if (toggle) {
|
|
274
|
+
const id = toggle.getAttribute("data-node-id");
|
|
275
|
+
if (id) void this.#toggleNode(id, true);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const row = target.closest(".tv-row");
|
|
280
|
+
if (!row) return;
|
|
281
|
+
const id = row.getAttribute("data-node-id");
|
|
282
|
+
if (!id) return;
|
|
283
|
+
|
|
284
|
+
if (event instanceof MouseEvent && event.detail === 2) {
|
|
285
|
+
void this.#toggleNode(id, true);
|
|
286
|
+
if (!this.displayOnly) {
|
|
287
|
+
this.#selectNode(id, { user: true, focus: true });
|
|
288
|
+
} else {
|
|
289
|
+
this.#focusRow(id);
|
|
290
|
+
}
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!this.displayOnly) {
|
|
295
|
+
this.#selectNode(id, { user: true, focus: true });
|
|
296
|
+
} else {
|
|
297
|
+
this.#focusRow(id);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
this.#root.addEventListener("focusin", (event) => {
|
|
302
|
+
const target = event.target;
|
|
303
|
+
if (!(target instanceof Element)) return;
|
|
304
|
+
const row = target.closest(".tv-row");
|
|
305
|
+
if (!row) return;
|
|
306
|
+
const id = row.getAttribute("data-node-id");
|
|
307
|
+
if (!id) return;
|
|
308
|
+
this.#focusedId = id;
|
|
309
|
+
this.#syncTabStops();
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
this.#root.addEventListener("keydown", (event) => {
|
|
313
|
+
if (this.disabled) return;
|
|
314
|
+
const handled = this.#handleKeydown(event);
|
|
315
|
+
if (handled) {
|
|
316
|
+
event.preventDefault();
|
|
317
|
+
event.stopPropagation();
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async #adoptStyles() {
|
|
323
|
+
const componentStyles = PDS.createStylesheet(/*css*/ `
|
|
324
|
+
@layer treeview {
|
|
325
|
+
:host {
|
|
326
|
+
display: block;
|
|
327
|
+
--tv-indent: var(--spacing-5);
|
|
328
|
+
--tv-toggle-size: 30px;
|
|
329
|
+
--tv-row-height: var(--tv-toggle-size);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
.tv-host {
|
|
333
|
+
color: var(--color-text-primary);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
.tv-tree,
|
|
337
|
+
.tv-group {
|
|
338
|
+
list-style: none;
|
|
339
|
+
margin: var(--spacing-0);
|
|
340
|
+
padding: var(--spacing-0);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
.tv-group {
|
|
344
|
+
margin-inline-start: var(--tv-indent);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
.tv-item {
|
|
348
|
+
margin: var(--spacing-0);
|
|
349
|
+
padding: var(--spacing-0);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
.tv-row {
|
|
353
|
+
min-height: var(--tv-row-height);
|
|
354
|
+
display: grid;
|
|
355
|
+
grid-template-columns: var(--tv-toggle-size) auto 1fr;
|
|
356
|
+
align-items: center;
|
|
357
|
+
gap: var(--spacing-1);
|
|
358
|
+
border-radius: var(--radius-sm);
|
|
359
|
+
cursor: default;
|
|
360
|
+
outline: none;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
.tv-row[aria-selected="true"] {
|
|
364
|
+
background: var(--color-surface-hover);
|
|
365
|
+
color: var(--color-text-primary);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
.tv-row:focus-visible {
|
|
369
|
+
box-shadow: inset 0 0 0 1px var(--color-primary-500);
|
|
370
|
+
background: var(--color-surface-hover);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
.tv-toggle,
|
|
374
|
+
.tv-toggle-gap {
|
|
375
|
+
width: var(--tv-toggle-size);
|
|
376
|
+
height: var(--tv-toggle-size);
|
|
377
|
+
display: inline-grid;
|
|
378
|
+
place-items: center;
|
|
379
|
+
font-weight: var(--font-weight-semibold);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
.tv-toggle {
|
|
383
|
+
min-width: var(--tv-toggle-size);
|
|
384
|
+
min-height: var(--tv-toggle-size);
|
|
385
|
+
aspect-ratio: 1 / 1;
|
|
386
|
+
border: 1px solid transparent;
|
|
387
|
+
border-radius: var(--radius-sm);
|
|
388
|
+
background: transparent;
|
|
389
|
+
color: var(--color-text-muted);
|
|
390
|
+
padding: var(--spacing-0);
|
|
391
|
+
line-height: 1;
|
|
392
|
+
cursor: pointer;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
.tv-toggle:hover {
|
|
396
|
+
border-color: var(--color-primary-500);
|
|
397
|
+
color: var(--color-text-primary);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
:host([disabled]) .tv-row,
|
|
401
|
+
:host([disabled]) .tv-toggle,
|
|
402
|
+
.tv-host[data-state="loading"] .tv-row {
|
|
403
|
+
opacity: 0.65;
|
|
404
|
+
cursor: not-allowed;
|
|
405
|
+
pointer-events: none;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
.tv-prefix {
|
|
409
|
+
display: inline-flex;
|
|
410
|
+
align-items: center;
|
|
411
|
+
justify-content: center;
|
|
412
|
+
width: var(--spacing-5);
|
|
413
|
+
height: var(--spacing-5);
|
|
414
|
+
color: var(--color-text-muted);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
.tv-prefix img {
|
|
418
|
+
width: 100%;
|
|
419
|
+
height: 100%;
|
|
420
|
+
object-fit: cover;
|
|
421
|
+
border-radius: var(--radius-sm);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
.tv-label {
|
|
425
|
+
min-width: 0;
|
|
426
|
+
white-space: nowrap;
|
|
427
|
+
overflow: hidden;
|
|
428
|
+
text-overflow: ellipsis;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
.tv-label-link {
|
|
432
|
+
color: inherit;
|
|
433
|
+
text-decoration: none;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
.tv-label-link:hover,
|
|
437
|
+
.tv-label-link:focus-visible {
|
|
438
|
+
text-decoration: underline;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
`);
|
|
442
|
+
|
|
443
|
+
await PDS.adoptLayers(this.#root, LAYERS, [componentStyles]);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
#syncAttributes() {
|
|
447
|
+
this.#syncValue();
|
|
448
|
+
this.#syncTabStops();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async #resolveSource() {
|
|
452
|
+
const options = this.#settings || {};
|
|
453
|
+
const getItems =
|
|
454
|
+
typeof options.getItems === "function" ? options.getItems : null;
|
|
455
|
+
|
|
456
|
+
let source =
|
|
457
|
+
options.source ??
|
|
458
|
+
options.items ??
|
|
459
|
+
options.data ??
|
|
460
|
+
this.getAttribute("src") ??
|
|
461
|
+
null;
|
|
462
|
+
|
|
463
|
+
if (!source && getItems) {
|
|
464
|
+
source = await getItems({ host: this, options, settings: options });
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
source = await this.#resolveDynamicSource(source, { options, settings: options, host: this }, options.fetch);
|
|
468
|
+
|
|
469
|
+
if (typeof options.transform === "function") {
|
|
470
|
+
source = await options.transform(source, { host: this, options, settings: options });
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return source;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async #fetchJson(url, fetchOptions) {
|
|
477
|
+
const response = await fetch(url, fetchOptions || {});
|
|
478
|
+
if (!response.ok) {
|
|
479
|
+
throw new Error(`Failed to load tree data (${response.status})`);
|
|
480
|
+
}
|
|
481
|
+
return response.json();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async #resolveDynamicSource(source, context, fetchOptions) {
|
|
485
|
+
if (typeof source === "function") {
|
|
486
|
+
source = await source(context);
|
|
487
|
+
} else if (typeof source === "string") {
|
|
488
|
+
source = await this.#fetchJson(source, fetchOptions);
|
|
489
|
+
} else if (source && typeof source === "object" && !Array.isArray(source) && typeof source.url === "string") {
|
|
490
|
+
const response = await this.#fetchJson(source.url, source.fetch || fetchOptions);
|
|
491
|
+
source = typeof source.mapResponse === "function" ? source.mapResponse(response) : response;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return source;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
#ingestData(source) {
|
|
498
|
+
const settings = this.#settings || {};
|
|
499
|
+
const list = Array.isArray(source) ? source : source ? [source] : [];
|
|
500
|
+
const roots = list.map((node, index) => this.#normalizeNode(node, [], index)).filter(Boolean);
|
|
501
|
+
|
|
502
|
+
this.#nodes = roots;
|
|
503
|
+
this.#reindexNodes();
|
|
504
|
+
|
|
505
|
+
if (this.expandedAll || settings.expandedAll) {
|
|
506
|
+
this.#expandedIds = new Set(
|
|
507
|
+
Array.from(this.#nodeById.values())
|
|
508
|
+
.filter((node) => node.children.length)
|
|
509
|
+
.map((node) => node.id),
|
|
510
|
+
);
|
|
511
|
+
} else {
|
|
512
|
+
this.#expandedIds.clear();
|
|
513
|
+
const defaultExpanded = settings.defaultExpanded ?? [];
|
|
514
|
+
for (const id of defaultExpanded) {
|
|
515
|
+
if (this.#nodeById.has(id)) this.#expandedIds.add(id);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
#normalizeNode(rawNode, path, index) {
|
|
521
|
+
if (!rawNode || typeof rawNode !== "object") return null;
|
|
522
|
+
|
|
523
|
+
const rawId = rawNode.id ?? rawNode.value ?? rawNode.key;
|
|
524
|
+
const fallbackId = [...path, index + 1].join(".");
|
|
525
|
+
const id = String(rawId ?? fallbackId);
|
|
526
|
+
|
|
527
|
+
const text = String(rawNode.text ?? rawNode.label ?? rawNode.title ?? id);
|
|
528
|
+
const value = String(rawNode.value ?? rawNode.id ?? id);
|
|
529
|
+
const link = this.#sanitizeLink(rawNode.link ?? rawNode.href ?? null);
|
|
530
|
+
const icon = rawNode.icon ?? rawNode.prefixIcon ?? rawNode.prefix?.icon ?? null;
|
|
531
|
+
const image = rawNode.image ?? rawNode.prefixImage ?? rawNode.prefix?.image ?? null;
|
|
532
|
+
const childrenRaw =
|
|
533
|
+
rawNode.children ?? rawNode.subnodes ?? rawNode.nodes ?? rawNode.items ?? [];
|
|
534
|
+
const childArray = Array.isArray(childrenRaw) ? childrenRaw : [];
|
|
535
|
+
const nextPath = [...path, id];
|
|
536
|
+
const children = childArray
|
|
537
|
+
.map((child, childIndex) => this.#normalizeNode(child, nextPath, childIndex))
|
|
538
|
+
.filter(Boolean);
|
|
539
|
+
const explicitHasChildren = rawNode.hasChildren ?? rawNode.hasChildNodes ?? rawNode.branch;
|
|
540
|
+
const hasChildren =
|
|
541
|
+
typeof explicitHasChildren === "boolean"
|
|
542
|
+
? explicitHasChildren || children.length > 0
|
|
543
|
+
: children.length > 0;
|
|
544
|
+
const explicitLoaded = rawNode.childrenLoaded;
|
|
545
|
+
const childrenLoaded =
|
|
546
|
+
typeof explicitLoaded === "boolean"
|
|
547
|
+
? explicitLoaded
|
|
548
|
+
: childArray.length > 0 || !hasChildren;
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
id,
|
|
552
|
+
text,
|
|
553
|
+
value,
|
|
554
|
+
link,
|
|
555
|
+
icon,
|
|
556
|
+
image,
|
|
557
|
+
data: rawNode,
|
|
558
|
+
hasChildren,
|
|
559
|
+
childrenLoaded,
|
|
560
|
+
loadingChildren: false,
|
|
561
|
+
children,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
#reindexNodes() {
|
|
566
|
+
this.#nodeById.clear();
|
|
567
|
+
this.#parentById.clear();
|
|
568
|
+
|
|
569
|
+
const visit = (node, parentId) => {
|
|
570
|
+
this.#nodeById.set(node.id, node);
|
|
571
|
+
if (parentId) this.#parentById.set(node.id, parentId);
|
|
572
|
+
for (const child of node.children) visit(child, node.id);
|
|
573
|
+
};
|
|
574
|
+
for (const node of this.#nodes) visit(node, null);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
#renderTree() {
|
|
578
|
+
const tree = this.#root.querySelector(ROOT_SELECTOR);
|
|
579
|
+
if (!tree) return;
|
|
580
|
+
tree.innerHTML = this.#renderNodes(this.#nodes, 1, this.#canRenderLinks());
|
|
581
|
+
|
|
582
|
+
if (!this.#focusedId) {
|
|
583
|
+
this.#focusedId = this.#selectedId || this.#firstVisibleId() || null;
|
|
584
|
+
}
|
|
585
|
+
this.#syncTabStops();
|
|
586
|
+
this.#syncValue();
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
#renderNodes(nodes, level, linksEnabled) {
|
|
590
|
+
if (!nodes.length) return "";
|
|
591
|
+
|
|
592
|
+
return nodes
|
|
593
|
+
.map((node) => {
|
|
594
|
+
const expanded = this.#expandedIds.has(node.id);
|
|
595
|
+
const hasChildren = Boolean(node.hasChildren);
|
|
596
|
+
const selected = this.#selectedId === node.id;
|
|
597
|
+
const toggleGlyph = node.loadingChildren ? "…" : expanded ? "−" : "+";
|
|
598
|
+
const toggle = hasChildren
|
|
599
|
+
? `<button type="button" class="tv-toggle icon-only" data-node-id="${this.#escapeAttribute(node.id)}" aria-label="${expanded ? "Collapse" : "Expand"} ${this.#escapeAttribute(node.text)}" ${node.loadingChildren ? "disabled" : ""}>${toggleGlyph}</button>`
|
|
600
|
+
: `<span class="tv-toggle-gap" aria-hidden="true"></span>`;
|
|
601
|
+
const prefix = this.#renderPrefix(node);
|
|
602
|
+
const label = linksEnabled && node.link
|
|
603
|
+
? `<a class="tv-label tv-label-link" href="${this.#escapeAttribute(node.link)}">${this.#escapeHtml(node.text)}</a>`
|
|
604
|
+
: `<span class="tv-label">${this.#escapeHtml(node.text)}</span>`;
|
|
605
|
+
const childGroup =
|
|
606
|
+
hasChildren && expanded
|
|
607
|
+
? `<ul class="tv-group" role="group">${this.#renderNodes(node.children, level + 1, linksEnabled)}</ul>`
|
|
608
|
+
: "";
|
|
609
|
+
|
|
610
|
+
return `
|
|
611
|
+
<li class="tv-item" role="none">
|
|
612
|
+
<div
|
|
613
|
+
class="tv-row"
|
|
614
|
+
role="treeitem"
|
|
615
|
+
aria-level="${level}"
|
|
616
|
+
${hasChildren ? `aria-expanded="${expanded ? "true" : "false"}"` : ""}
|
|
617
|
+
${node.loadingChildren ? "aria-busy=\"true\"" : ""}
|
|
618
|
+
aria-selected="${selected ? "true" : "false"}"
|
|
619
|
+
data-node-id="${this.#escapeAttribute(node.id)}"
|
|
620
|
+
tabindex="-1"
|
|
621
|
+
>
|
|
622
|
+
${toggle}
|
|
623
|
+
${prefix}
|
|
624
|
+
${label}
|
|
625
|
+
</div>
|
|
626
|
+
${childGroup}
|
|
627
|
+
</li>
|
|
628
|
+
`;
|
|
629
|
+
})
|
|
630
|
+
.join("");
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
#canRenderLinks() {
|
|
634
|
+
return !this.closest("form");
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
#renderPrefix(node) {
|
|
638
|
+
if (node.image) {
|
|
639
|
+
return `<span class="tv-prefix"><img src="${this.#escapeAttribute(node.image)}" alt="" loading="lazy" /></span>`;
|
|
640
|
+
}
|
|
641
|
+
if (node.icon) {
|
|
642
|
+
return `<span class="tv-prefix"><pds-icon icon="${this.#escapeAttribute(node.icon)}"></pds-icon></span>`;
|
|
643
|
+
}
|
|
644
|
+
return `<span class="tv-prefix" aria-hidden="true"></span>`;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
#handleKeydown(event) {
|
|
648
|
+
const key = event.key;
|
|
649
|
+
const visibleIds = this.#getVisibleIds();
|
|
650
|
+
if (!visibleIds.length) return false;
|
|
651
|
+
|
|
652
|
+
const activeId = this.#focusedId && visibleIds.includes(this.#focusedId)
|
|
653
|
+
? this.#focusedId
|
|
654
|
+
: visibleIds[0];
|
|
655
|
+
const currentIndex = visibleIds.indexOf(activeId);
|
|
656
|
+
const currentNode = this.#nodeById.get(activeId);
|
|
657
|
+
|
|
658
|
+
if (!currentNode) return false;
|
|
659
|
+
|
|
660
|
+
if (key === "ArrowDown") {
|
|
661
|
+
const nextId = visibleIds[Math.min(visibleIds.length - 1, currentIndex + 1)];
|
|
662
|
+
this.#focusRow(nextId);
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
if (key === "ArrowUp") {
|
|
667
|
+
const prevId = visibleIds[Math.max(0, currentIndex - 1)];
|
|
668
|
+
this.#focusRow(prevId);
|
|
669
|
+
return true;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (key === "Home") {
|
|
673
|
+
this.#focusRow(visibleIds[0]);
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (key === "End") {
|
|
678
|
+
this.#focusRow(visibleIds[visibleIds.length - 1]);
|
|
679
|
+
return true;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (key === "ArrowRight") {
|
|
683
|
+
if (currentNode.hasChildren && !this.#expandedIds.has(activeId)) {
|
|
684
|
+
void this.#toggleNode(activeId, true);
|
|
685
|
+
return true;
|
|
686
|
+
}
|
|
687
|
+
if (currentNode.hasChildren && this.#expandedIds.has(activeId) && currentNode.children.length) {
|
|
688
|
+
this.#focusRow(currentNode.children[0].id);
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
return false;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (key === "ArrowLeft") {
|
|
695
|
+
if (currentNode.hasChildren && this.#expandedIds.has(activeId)) {
|
|
696
|
+
void this.#toggleNode(activeId, true);
|
|
697
|
+
return true;
|
|
698
|
+
}
|
|
699
|
+
const parentId = this.#parentById.get(activeId);
|
|
700
|
+
if (parentId) {
|
|
701
|
+
this.#focusRow(parentId);
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
return false;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (key === "Enter" || key === " ") {
|
|
708
|
+
if (!this.displayOnly) {
|
|
709
|
+
this.#selectNode(activeId, { user: true, focus: true });
|
|
710
|
+
} else if (currentNode.hasChildren) {
|
|
711
|
+
void this.#toggleNode(activeId, true);
|
|
712
|
+
}
|
|
713
|
+
return true;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
async #toggleNode(id, user) {
|
|
720
|
+
const node = this.#nodeById.get(id);
|
|
721
|
+
if (!node || !node.hasChildren) return;
|
|
722
|
+
|
|
723
|
+
const nextExpanded = !this.#expandedIds.has(id);
|
|
724
|
+
if (nextExpanded) {
|
|
725
|
+
this.#expandedIds.add(id);
|
|
726
|
+
this.#renderTree();
|
|
727
|
+
this.#focusRow(id);
|
|
728
|
+
await this.#ensureNodeChildrenLoaded(id, { user: Boolean(user) });
|
|
729
|
+
} else {
|
|
730
|
+
this.#expandedIds.delete(id);
|
|
731
|
+
}
|
|
732
|
+
this.#renderTree();
|
|
733
|
+
this.#focusRow(id);
|
|
734
|
+
|
|
735
|
+
this.dispatchEvent(
|
|
736
|
+
new CustomEvent("node-toggle", {
|
|
737
|
+
detail: { node, expanded: nextExpanded, user: Boolean(user) },
|
|
738
|
+
bubbles: true,
|
|
739
|
+
composed: true,
|
|
740
|
+
}),
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
async #ensureNodeChildrenLoaded(id, { user } = {}) {
|
|
745
|
+
const node = this.#nodeById.get(id);
|
|
746
|
+
if (!node || node.childrenLoaded || node.loadingChildren || !node.hasChildren) return;
|
|
747
|
+
|
|
748
|
+
if (this.#childrenLoadPromises.has(id)) {
|
|
749
|
+
await this.#childrenLoadPromises.get(id);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
const task = (async () => {
|
|
754
|
+
node.loadingChildren = true;
|
|
755
|
+
this.#renderTree();
|
|
756
|
+
this.#focusRow(id);
|
|
757
|
+
|
|
758
|
+
try {
|
|
759
|
+
const childSource = await this.#resolveNodeChildren(node);
|
|
760
|
+
const childList = Array.isArray(childSource)
|
|
761
|
+
? childSource
|
|
762
|
+
: childSource
|
|
763
|
+
? [childSource]
|
|
764
|
+
: [];
|
|
765
|
+
node.children = childList
|
|
766
|
+
.map((child, childIndex) => this.#normalizeNode(child, [node.id], childIndex))
|
|
767
|
+
.filter(Boolean);
|
|
768
|
+
node.childrenLoaded = true;
|
|
769
|
+
node.hasChildren = node.children.length > 0;
|
|
770
|
+
this.#reindexNodes();
|
|
771
|
+
|
|
772
|
+
this.dispatchEvent(
|
|
773
|
+
new CustomEvent("node-load", {
|
|
774
|
+
detail: { node, children: node.children, user: Boolean(user) },
|
|
775
|
+
bubbles: true,
|
|
776
|
+
composed: true,
|
|
777
|
+
}),
|
|
778
|
+
);
|
|
779
|
+
} catch (error) {
|
|
780
|
+
node.childrenLoaded = false;
|
|
781
|
+
console.error("pds-treeview: failed to load child nodes", error);
|
|
782
|
+
this.dispatchEvent(
|
|
783
|
+
new CustomEvent("node-load-error", {
|
|
784
|
+
detail: { node, error, user: Boolean(user) },
|
|
785
|
+
bubbles: true,
|
|
786
|
+
composed: true,
|
|
787
|
+
}),
|
|
788
|
+
);
|
|
789
|
+
} finally {
|
|
790
|
+
node.loadingChildren = false;
|
|
791
|
+
}
|
|
792
|
+
})();
|
|
793
|
+
|
|
794
|
+
this.#childrenLoadPromises.set(id, task);
|
|
795
|
+
try {
|
|
796
|
+
await task;
|
|
797
|
+
} finally {
|
|
798
|
+
this.#childrenLoadPromises.delete(id);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
async #resolveNodeChildren(node) {
|
|
803
|
+
const options = this.#settings || {};
|
|
804
|
+
const getChildren = typeof options.getChildren === "function" ? options.getChildren : null;
|
|
805
|
+
let source = null;
|
|
806
|
+
|
|
807
|
+
if (getChildren) {
|
|
808
|
+
source = await getChildren({
|
|
809
|
+
host: this,
|
|
810
|
+
node,
|
|
811
|
+
nodeId: node.id,
|
|
812
|
+
options,
|
|
813
|
+
settings: options,
|
|
814
|
+
});
|
|
815
|
+
} else {
|
|
816
|
+
const raw = node.data || {};
|
|
817
|
+
source = raw.childrenSource ?? raw.childrenSrc ?? raw.childrenUrl ?? null;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
source = await this.#resolveDynamicSource(
|
|
821
|
+
source,
|
|
822
|
+
{ host: this, node, nodeId: node.id, options, settings: options },
|
|
823
|
+
options.fetch,
|
|
824
|
+
);
|
|
825
|
+
|
|
826
|
+
if (typeof options.transformChildren === "function") {
|
|
827
|
+
source = await options.transformChildren(source, {
|
|
828
|
+
host: this,
|
|
829
|
+
node,
|
|
830
|
+
nodeId: node.id,
|
|
831
|
+
options,
|
|
832
|
+
settings: options,
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
return source;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
#selectNode(id, { user, focus }) {
|
|
840
|
+
const node = this.#nodeById.get(id);
|
|
841
|
+
if (!node) return;
|
|
842
|
+
|
|
843
|
+
this.#selectedId = id;
|
|
844
|
+
this.#expandAncestors(id);
|
|
845
|
+
this.#renderTree();
|
|
846
|
+
if (focus) this.#focusRow(id);
|
|
847
|
+
this.#syncValue();
|
|
848
|
+
|
|
849
|
+
this.dispatchEvent(
|
|
850
|
+
new CustomEvent("node-select", {
|
|
851
|
+
detail: { node, value: node.value, user: Boolean(user) },
|
|
852
|
+
bubbles: true,
|
|
853
|
+
composed: true,
|
|
854
|
+
}),
|
|
855
|
+
);
|
|
856
|
+
this.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
|
|
857
|
+
this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
|
|
858
|
+
|
|
859
|
+
if (user && typeof this.#settings?.onSelect === "function") {
|
|
860
|
+
this.#settings.onSelect(node, this);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
#restoreSelection() {
|
|
865
|
+
const preferred = this.getAttribute("value") || this.#defaultValue || "";
|
|
866
|
+
if (preferred) {
|
|
867
|
+
if (!this.selectByValue(preferred)) {
|
|
868
|
+
this.#selectedId = null;
|
|
869
|
+
}
|
|
870
|
+
} else if (this.#selectedId && this.#nodeById.has(this.#selectedId)) {
|
|
871
|
+
this.#expandAncestors(this.#selectedId);
|
|
872
|
+
this.#renderTree();
|
|
873
|
+
} else {
|
|
874
|
+
this.#selectedId = null;
|
|
875
|
+
this.#syncValue();
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
#expandAncestors(id) {
|
|
880
|
+
let current = this.#parentById.get(id);
|
|
881
|
+
while (current) {
|
|
882
|
+
this.#expandedIds.add(current);
|
|
883
|
+
current = this.#parentById.get(current);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
#syncValue() {
|
|
888
|
+
const selected = this.selectedNode;
|
|
889
|
+
const nextValue = this.displayOnly ? "" : selected?.value ? String(selected.value) : "";
|
|
890
|
+
|
|
891
|
+
if (nextValue) {
|
|
892
|
+
if (this.getAttribute("value") !== nextValue) {
|
|
893
|
+
this.setAttribute("value", nextValue);
|
|
894
|
+
}
|
|
895
|
+
this.#internals.setFormValue(nextValue);
|
|
896
|
+
} else {
|
|
897
|
+
if (this.hasAttribute("value")) this.removeAttribute("value");
|
|
898
|
+
this.#internals.setFormValue("");
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
this.#syncValidity();
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
#syncValidity() {
|
|
905
|
+
if (this.required && !this.displayOnly && !this.selectedNode) {
|
|
906
|
+
this.#internals.setValidity(
|
|
907
|
+
{ valueMissing: true },
|
|
908
|
+
"Please select a node.",
|
|
909
|
+
this,
|
|
910
|
+
);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
this.#internals.setValidity({});
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
#focusRow(id) {
|
|
917
|
+
if (!id) return;
|
|
918
|
+
this.#focusedId = id;
|
|
919
|
+
this.#syncTabStops();
|
|
920
|
+
const row = this.#root.querySelector(`.tv-row[data-node-id="${CSS.escape(id)}"]`);
|
|
921
|
+
row?.focus();
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
#syncTabStops() {
|
|
925
|
+
const rows = this.#root.querySelectorAll(".tv-row");
|
|
926
|
+
if (!rows.length) return;
|
|
927
|
+
|
|
928
|
+
const fallbackId = this.#selectedId || this.#firstVisibleId();
|
|
929
|
+
if (!this.#focusedId || !this.#root.querySelector(`.tv-row[data-node-id="${CSS.escape(this.#focusedId)}"]`)) {
|
|
930
|
+
this.#focusedId = fallbackId || null;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
for (const row of rows) {
|
|
934
|
+
const id = row.getAttribute("data-node-id");
|
|
935
|
+
row.setAttribute("tabindex", id === this.#focusedId ? "0" : "-1");
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
#firstVisibleId() {
|
|
940
|
+
const first = this.#root.querySelector(".tv-row");
|
|
941
|
+
return first?.getAttribute("data-node-id") || null;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
#getVisibleIds() {
|
|
945
|
+
return Array.from(this.#root.querySelectorAll(".tv-row"))
|
|
946
|
+
.map((row) => row.getAttribute("data-node-id"))
|
|
947
|
+
.filter(Boolean);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
#sanitizeLink(link) {
|
|
951
|
+
if (!link) return null;
|
|
952
|
+
const value = String(link).trim();
|
|
953
|
+
if (!value) return null;
|
|
954
|
+
if (/^javascript\s*:/i.test(value)) return null;
|
|
955
|
+
return value;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
#escapeHtml(value) {
|
|
959
|
+
return String(value)
|
|
960
|
+
.replaceAll("&", "&")
|
|
961
|
+
.replaceAll("<", "<")
|
|
962
|
+
.replaceAll(">", ">")
|
|
963
|
+
.replaceAll('"', """)
|
|
964
|
+
.replaceAll("'", "'");
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
#escapeAttribute(value) {
|
|
968
|
+
return this.#escapeHtml(value);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
if (!customElements.get("pds-treeview")) {
|
|
973
|
+
customElements.define("pds-treeview", PdsTreeview);
|
|
974
|
+
}
|