47 lines
978 B
JavaScript
47 lines
978 B
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
|
|
export default class extends Controller {
|
|
static values = { key: String }
|
|
static targets = [ "input", "section" ]
|
|
|
|
sectionTargetConnected() {
|
|
this.#restoreToggles()
|
|
}
|
|
|
|
toggle(event) {
|
|
const section = event.target
|
|
|
|
const key = this.#localStorageKeyFor(section)
|
|
if (section.open) {
|
|
localStorage.removeItem(key)
|
|
} else {
|
|
localStorage.setItem(key, true)
|
|
}
|
|
}
|
|
|
|
showWhileFiltering() {
|
|
if (this.inputTarget.value) {
|
|
this.#expandAll();
|
|
} else {
|
|
this.#restoreToggles()
|
|
}
|
|
}
|
|
|
|
#expandAll() {
|
|
this.sectionTargets.forEach(section => {
|
|
section.open = true
|
|
})
|
|
}
|
|
|
|
#restoreToggles() {
|
|
this.sectionTargets.forEach(section => {
|
|
const key = this.#localStorageKeyFor(section)
|
|
section.open = !localStorage.getItem(key)
|
|
})
|
|
}
|
|
|
|
#localStorageKeyFor(section) {
|
|
return section.getAttribute("data-nav-section-expander-key-value")
|
|
}
|
|
}
|