-
Notifications
You must be signed in to change notification settings - Fork 1
/
lite-model-viewer.js
105 lines (87 loc) · 3.25 KB
/
lite-model-viewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const styles = `
lite-model-viewer {
background-image: var(--poster);
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: grid;
height: var(--height, 300px);
place-items: center;
width: var(--width, 100%);
}
lite-model-viewer.is-active {
display: contents;
}
model-viewer {
height: var(--height, 300px);
width: var(--width, 100%);
}
`;
class LiteModelViewer extends HTMLElement {
connectedCallback() {
// Inject the styles if they haven't been yet
if (!document.querySelector("#lite-model-viewer-styles")) {
const stylesheet = document.createElement("style");
stylesheet.id = "lite-model-viewer-styles";
stylesheet.textContent = styles;
document.head.append(stylesheet);
}
// Check for styling attributes, to be set as custom properties
const poster = this.getAttribute("poster");
const width = this.getAttribute("width");
const height = this.getAttribute("height");
if (poster) this.style.setProperty("--poster", `url(${poster})`);
if (width) this.style.setProperty("--width", width);
if (height) this.style.setProperty("--height", height);
// Add a click listener to load the model-viewer script
this.addEventListener("click", this._loadScript);
}
_loadScript() {
// Load the model-viewer script if it hasn't been loaded yet
if (!document.querySelector("#model-viewer-script")) {
const script = document.createElement("script");
script.id = "model-viewer-script";
script.type = "module";
script.src =
"https://ajax.googleapis.com/ajax/libs/model-viewer/3.3.0/model-viewer.min.js";
document.body.append(script);
}
// Remove click listener so it doesn't conflict with model-viewer
this.removeEventListener("click", this._loadScript);
// Once `model-viewer` script is available, activate the element
customElements.whenDefined("model-viewer").then(() => this._activate());
}
_activate() {
// Create the actual model-viewer
const modelViewer = document.createElement("model-viewer");
// Reproduce all of lite-model-viewer's attributes on the model-viewer
for (const attr of this.attributes) {
modelViewer.setAttribute(attr.name, attr.value);
}
// The actual model-viewer should use eager loading,
// since we're handling the lazy loading ourselves
modelViewer.setAttribute("loading", "eager");
// Query for an inner template (if any)
const template = this.querySelector("template");
// If it exists, insert that into the model-viewer
if (template) {
modelViewer.insertAdjacentHTML("beforeend", template.innerHTML);
}
// Start listening for loading progress
// so we can update a custom property for styling
modelViewer.addEventListener("progress", (event) => {
event.target.style.setProperty("--progress", event.detail.totalProgress);
event.target.classList.toggle(
"is-loaded",
event.detail.totalProgress === 1
);
});
// Wipe the previous contents
this.innerHTML = "";
// Append the model-viewer
this.append(modelViewer);
// Add a style hook for that state
this.classList.add("is-active");
}
}
customElements.define("lite-model-viewer", LiteModelViewer);