Skip to content

Commit aa6480e

Browse files
committed
Forward DataViewer attributes from DatasetSelector
Add automatic forwarding of attributes from DatasetSelector to the inner DataViewer component. This allows setting DataViewer attributes directly on the DatasetSelector without manually implementing each one. - Import DataViewer to access its observedAttributes - Add forwardInitialAttributes method to pass attributes when viewer is created - Skip 'src' attribute to prevent unwanted fetch operations
1 parent 0b5fc95 commit aa6480e

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/components/dataset-selector.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { DataViewer } from "../viewer.js"
2+
3+
14
export class DatasetSelector extends HTMLElement {
25
static get defaults() {
36
return {
@@ -22,7 +25,9 @@ export class DatasetSelector extends HTMLElement {
2225

2326
// MARK: setup
2427
static get observedAttributes() {
25-
return ["src"]
28+
// Combine our attributes with DataViewer's
29+
const ownAttributes = ["src"]
30+
return [...ownAttributes, ...DataViewer.observedAttributes]
2631
}
2732

2833
connectedCallback() {
@@ -34,6 +39,13 @@ export class DatasetSelector extends HTMLElement {
3439
attributeChangedCallback(name, oldValue, newValue) {
3540
if (name === "src" && oldValue !== newValue) {
3641
this.loadSpecFromSrc(newValue)
42+
} else if (this.dataViewer && DataViewer.observedAttributes.includes(name)) {
43+
// Forward DataViewer attributes
44+
if (newValue !== null) {
45+
this.dataViewer.setAttribute(name, newValue)
46+
} else {
47+
this.dataViewer.removeAttribute(name)
48+
}
3749
}
3850
}
3951

@@ -43,6 +55,17 @@ export class DatasetSelector extends HTMLElement {
4355
}
4456
}
4557

58+
forwardInitialAttributes() {
59+
if (!this.dataViewer) return
60+
61+
DataViewer.observedAttributes.forEach(attr => {
62+
if (attr === 'src') return
63+
if (this.hasAttribute(attr)) {
64+
this.dataViewer.setAttribute(attr, this.getAttribute(attr))
65+
}
66+
})
67+
}
68+
4669
// Load spec and initialize
4770
async loadSpecFromSrc(src) {
4871
try {
@@ -436,6 +459,7 @@ export class DatasetSelector extends HTMLElement {
436459
const viewer = document.createElement("data-viewer")
437460
this.shadowRoot.querySelector(".viewer-container").appendChild(viewer)
438461
this.dataViewer = viewer
462+
this.forwardInitialAttributes()
439463
}
440464
}
441465

0 commit comments

Comments
 (0)