Sha256: b0f5b10633d473fcc8c0086be19561b938b3862ccb61d1bc23fa9acf4990a6e5

Contents?: true

Size: 1.14 KB

Versions: 16

Compression:

Stored size: 1.14 KB

Contents

export class IngredientGroup extends HTMLDetailsElement {
  #localStorageKey = "Alchemy.expanded_ingredient_groups"

  constructor() {
    super()

    this.addEventListener("toggle", this)

    if (this.isInLocalStorage) {
      this.open = true
    }
  }

  /**
   * Toggle visibility of the ingredient fields in this group
   */
  handleEvent() {
    let expanded_ingredient_groups = this.localStorageItem

    if (this.open) {
      if (!this.isInLocalStorage) expanded_ingredient_groups.push(this.id)
    } else {
      expanded_ingredient_groups = expanded_ingredient_groups.filter(
        (value) => value !== this.id
      )
    }

    localStorage.setItem(
      this.#localStorageKey,
      JSON.stringify(expanded_ingredient_groups)
    )
  }

  get isInLocalStorage() {
    return this.localStorageItem.includes(this.id)
  }

  get localStorageItem() {
    const item = localStorage.getItem(this.#localStorageKey)

    if (!item) return []

    try {
      return JSON.parse(item)
    } catch (error) {
      console.error(error)
      return []
    }
  }
}

customElements.define("alchemy-ingredient-group", IngredientGroup, {
  extends: "details"
})

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
alchemy_cms-7.1.12 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.11 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.10 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.9 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.8 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.7 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.6 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.5 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.4 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.3 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.2 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.1 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.0 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.0.pre.rc1 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.0.pre.b2 app/javascript/alchemy_admin/components/ingredient_group.js
alchemy_cms-7.1.0.pre.b1 app/javascript/alchemy_admin/components/ingredient_group.js