diff --git a/app/assets/javascripts/outfit-viewer.js b/app/assets/javascripts/outfit-viewer.js index 3a7ba6de..2c35eb32 100644 --- a/app/assets/javascripts/outfit-viewer.js +++ b/app/assets/javascripts/outfit-viewer.js @@ -7,19 +7,47 @@ class OutfitLayer extends HTMLElement { } connectedCallback() { - setTimeout(() => this.#initializeImage(), 0); + setTimeout(() => this.#connectToChildren(), 0); } - #initializeImage() { - this.image = this.querySelector("img"); - if (!this.image) { - throw new Error(` must contain an tag`); + disconnectedCallback() { + window.removeEventListener("message", this.#onMessage); + } + + #connectToChildren() { + const image = this.querySelector("img"); + const iframe = this.querySelector("iframe"); + + if (image) { + image.addEventListener("load", () => this.#setStatus("loaded")); + image.addEventListener("error", () => this.#setStatus("error")); + this.#setStatus(image.complete ? "loaded" : "loading"); + } else if (iframe) { + this.iframe = iframe; + window.addEventListener("message", (m) => this.#onMessage(m)); + this.#setStatus("loading"); + } else { + throw new Error( + ` must contain an or