Sha256: 9e6718358f2f44fab875029bbc95459230b71f932f0a7182f92b614865cbdfef

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

import createSocket from "../lib/socket";

const morphOpts = {
  key(el) {
    return el.getAttribute("key") ? el.getAttribute("key") : el.id;
  },
  updating(el, toEl, childrenOnly, skip) {
    if (
      el.getAttribute &&
      el.getAttribute("data-morph-strategy") === "replace"
    ) {
      el.innerHTML = toEl.innerHTML;
      return skip();
    }
  },
  lookahead: true,
};

export default function page() {
  return {
    init() {
      const socket = createSocket(window.SOCKET_PATH);
      socket.addListener("Lookbook::ReloadChannel", () => this.refresh());
    },
    async update() {
      const response = await fetch(window.document.location);
      if (!response.ok) return window.location.reload();
      const html = await response.text();
      const newDoc = new DOMParser().parseFromString(html, "text/html");
      this.morph(newDoc);
      document.title = newDoc.title;
    },
    setLocation(loc) {
      const path = loc instanceof Event ? loc.currentTarget.href : loc;
      history.pushState({}, null, path);
      this.$dispatch("popstate");
    },
    refresh() {
      this.$dispatch("popstate");
    },
    morph(dom) {
      const pageHtml = dom.getElementById(this.$root.id).outerHTML;
      Alpine.morph(this.$root, pageHtml, morphOpts);
      this.$dispatch("page:morphed");
    },
  };
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-0.6.1 app/assets/lookbook/js/components/page.js
lookbook-0.6.0 app/assets/lookbook/js/components/page.js
lookbook-0.5.2 app/assets/lookbook/js/components/page.js
lookbook-0.5.1 app/assets/lookbook/js/components/page.js