From 97e6c39402ef2c6a01f362b1e3b35ee7554a837e Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Wed, 3 Jul 2024 20:15:35 -0700 Subject: [PATCH] Load movies in iframe for new item page preview Hey hey, it's working! Still stuff to add like pause/play, but yeah! --- app/assets/javascripts/outfit-viewer.js | 46 ++++++++++++++++++----- app/assets/javascripts/swf_assets/show.js | 29 +++++++++----- app/assets/stylesheets/items/_show.sass | 6 ++- app/views/items/_outfit_viewer.html.haml | 5 ++- 4 files changed, 64 insertions(+), 22 deletions(-) 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