Sha256: f98c6a2c6fdeb99085e1400779e7fbba76ae0e1d21dc1e8aedd98a8e09902fc7

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

export default function navItemComponent({ id, matchers }) {
  matchers = matchers.map((matcher) =>
    matcher.replace(/\s/g, "").toLowerCase()
  );

  return {
    filteredOut: false,

    active: false,

    get open() {
      return this.isCollection && this.isOpen(id);
    },

    get children() {
      return this.$refs.items ? Array.from(this.$refs.items.children) : [];
    },

    get isCollection() {
      return !this.$refs.link;
    },

    checkActive() {
      this.active =
        this.$refs.link &&
        window.location.pathname === this.$refs.link.getAttribute("href");
    },

    toggle() {
      this.toggleOpen(id);
    },

    async filter(text) {
      if (this.isCollection) {
        this.filteredOut = true;
        this.children.forEach(async (child) => {
          const data = Alpine.$data(child);
          await data.filter(text);
          if (!data.filteredOut) {
            this.filteredOut = false;
          }
        });
      } else {
        this.filteredOut = !this.match(text);
      }
      return this;
    },

    match(text) {
      if (text.length) {
        const matched = (matchers || []).map((m) => m.includes(text));
        return matched.filter((m) => m).length;
      }
      return true;
    },

    bindings: {
      toggle: {
        ["x-on:click.stop"]: "toggle",
        ["x-ref"]: "toggle",
      },
      link: {
        [":class"]: "{'!bg-lookbook-nav-item-active':active}",
        ["x-ref"]: "link",
      },
    },
  };
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-2.3.5 app/components/lookbook/nav/item/component.js
lookbook-2.3.4 app/components/lookbook/nav/item/component.js
lookbook-2.3.3 app/components/lookbook/nav/item/component.js
lookbook-2.3.2 app/components/lookbook/nav/item/component.js