Sha256: 517fe92d526ebadc031119bb1a0c79baf5233afdd4f2f5e3f95c2220eab1f9ad

Contents?: true

Size: 1.91 KB

Versions: 17

Compression:

Stored size: 1.91 KB

Contents

import createSocket from "./lib/socket";
import { morph } from "./helpers/dom";
import { fetchHTML } from "./helpers/request";
import { isExternalLink } from "./helpers/dom";

export default function app() {
  return {
    version: Alpine.$persist("").as("lookbook-version"),

    location: window.location,

    init() {
      if (window.SOCKET_PATH) {
        const socket = createSocket(window.SOCKET_PATH);
        socket.addListener("Lookbook::ReloadChannel", () => this.updateDOM());
      }
    },

    navigateTo(path) {
      this.debug(`Navigating to ${path}`);
      history.pushState({}, null, path);
      this.$dispatch("popstate");
    },

    async handleNavigation() {
      this.debug("Navigating to ", window.location.pathname);
      this.$dispatch("navigation:start");
      this.location = window.location;
      await this.updateDOM();
      this.$dispatch("navigation:complete");
    },

    hijax(evt) {
      const link = evt.target.closest("a[href]");
      if (link && !isExternalLink(link)) {
        evt.preventDefault();
        this.navigateTo(link.href);
      }
    },

    async updateDOM() {
      this.debug("Starting DOM update");
      this.$dispatch("dom:update-start");
      try {
        const { fragment, title } = await fetchHTML(
          window.location,
          `#${this.$root.id}`
        );
        morph(this.$root, fragment);
        document.title = title;
        this.$dispatch("dom:update-complete");
        this.debug("DOM update complete");
      } catch (err) {
        this.error(err);
        window.location.reload();
      }
    },

    toggleSidebar() {
      this.$store.layout.sidebar.hidden = !this.$store.layout.sidebar.hidden;
    },

    closeMobileSidebar() {
      if (this.$store.layout.mobile && !this.sidebarHidden) {
        this.toggleSidebar();
      }
    },

    get sidebarHidden() {
      return this.$store.layout.sidebar.hidden;
    },

    ...Alpine.$log,
  };
}

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
lookbook-1.0.4 app/assets/lookbook/js/app.js
lookbook-1.0.3 app/assets/lookbook/js/app.js
lookbook-1.0.2 app/assets/lookbook/js/app.js
lookbook-1.0.1 app/assets/lookbook/js/app.js
lookbook-1.0.0 app/assets/lookbook/js/app.js
lookbook-1.0.0.rc.3 app/assets/lookbook/js/app.js
lookbook-1.0.0.rc.2 app/assets/lookbook/js/app.js
lookbook-1.0.0.rc.1 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.8 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.7 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.6 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.5 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.4 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.3 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.2 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.1 app/assets/lookbook/js/app.js
lookbook-1.0.0.beta.0 app/assets/lookbook/js/app.js