Files
fizzy/app/javascript/controllers/knob_controller.js
T
Jorge Manrubia b9dd7fe808 Tidy up code
2025-06-05 08:54:31 +02:00

39 lines
805 B
JavaScript

import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "field", "option", "slider" ]
connect() {
this.#index = this.#selectedOption.dataset.index
}
optionChanged({ target }) {
this.#index = target.dataset.index
}
sliderChanged({ target }) {
this.#index = target.value
}
get #selectedOption() {
return this.optionTargets.find(option => {
return option.checked
})
}
set #index(index) {
this.fieldTarget.style.setProperty("--knob-index", `${index}`);
this.sliderTarget.value = index
}
set #value(index) {
this.#optionForIndex(index).checked = true
}
#optionForIndex(index) {
return this.optionTargets.find(option => {
return option.dataset.index === index;
})
}
}