Sha256: 0b142e65ab35135458d66d97318c1d8469f6102e8a149b64c327ac0f659f82c6

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 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();
      this.morph(new DOMParser().parseFromString(html, "text/html"));
    },
    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.5.0 app/assets/lookbook/js/components/page.js
lookbook-0.5.0.beta.2 app/assets/lookbook/js/components/page.js
lookbook-0.5.0.beta.1 app/assets/lookbook/js/components/page.js
lookbook-0.5.0.beta.0 app/assets/lookbook/js/components/page.js