Sha256: d400936431d4cc1022ea5aec37095b6e475b253ee646d8eee88c2434e1c9ca98

Contents?: true

Size: 1.43 KB

Versions: 25

Compression:

Stored size: 1.43 KB

Contents

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

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

export default function app() {
  return {
    init() {
      if (window.SOCKET_PATH) {
        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) {
      let path;
      if (loc instanceof Event) {
        path = loc.currentTarget.href;
        loc.preventDefault();
      } else {
        path = loc;
      }
      history.pushState({}, null, path);
      this.$dispatch("popstate");
    },
    refresh() {
      this.$dispatch("refresh");
    },
    morph(dom) {
      const pageHtml = dom.getElementById(this.$root.id).outerHTML;
      Alpine.morph(this.$root, pageHtml, morphOpts);
      this.$dispatch("page:morphed");
    },
  };
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
lookbook-0.7.2.beta.2 app/assets/lookbook/js/components/app.js
lookbook-0.7.2.beta.1 app/assets/lookbook/js/components/app.js
lookbook-0.7.2.beta.0 app/assets/lookbook/js/components/app.js
lookbook-0.7.1 app/assets/lookbook/js/components/app.js
lookbook-0.7.0 app/assets/lookbook/js/components/app.js