forked from fixin.me/fixin.me
Migrate all inline JS to Stimulus controllers
Add stimulus-rails gem and wire up 7 controllers: - measurements_view_controller: view toggle (compact/wide) via localStorage - measurements_controller: grouped rows MutationObserver - charts_controller: Plotly chart rendering - form_controller: keyboard shortcuts (Escape/Enter) and submit validation - details_controller: quantity picker state, focusout close, MutationObserver - readout_unit_controller: default unit button enable/disable + PATCH submission - drag_controller: drag-and-drop for quantity reparenting and unit rebasing Remove all inline onclick/onkeydown/ondrag*/onsubmit handlers from templates. Remove all window.* global exports from application.js. Remove bare <script> block from measurements/_form.html.erb. Remove turbo:load listeners for behavior now in controller connect(). application.js now only contains: Turbo Stream custom action definitions and the showPage visibility listener. Document Stimulus conventions in CLAUDE.md. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
55
app/javascript/controllers/details_controller.js
Normal file
55
app/javascript/controllers/details_controller.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["countLabel", "submitButton", "list"]
|
||||
|
||||
connect() {
|
||||
this._observer = new MutationObserver(() => {
|
||||
this.element.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
})
|
||||
this._observer.observe(this.listTarget, { subtree: true, attributeFilter: ['disabled'] })
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this._observer?.disconnect()
|
||||
}
|
||||
|
||||
change() {
|
||||
const count = this.element.querySelectorAll('input:checked:not([disabled])').length
|
||||
if (count > 0) {
|
||||
this.countLabelTarget.textContent = count + ' selected'
|
||||
Turbo.StreamElement.prototype.enableElement(this.submitButtonTarget)
|
||||
} else {
|
||||
this.countLabelTarget.textContent = this.countLabelTarget.dataset.prompt
|
||||
Turbo.StreamElement.prototype.disableElement(this.submitButtonTarget)
|
||||
}
|
||||
}
|
||||
|
||||
close(event) {
|
||||
if (!event.relatedTarget ||
|
||||
event.relatedTarget.closest("details") != this.element) {
|
||||
this.element.removeAttribute("open")
|
||||
}
|
||||
}
|
||||
|
||||
processKey(event) {
|
||||
switch (event.key) {
|
||||
case "Escape":
|
||||
if (this.element.hasAttribute("open")) {
|
||||
this.element.removeAttribute("open")
|
||||
event.stopPropagation()
|
||||
}
|
||||
break
|
||||
case "Enter": {
|
||||
const button = this.element.querySelector("button:not([disabled])")
|
||||
if (button) {
|
||||
button.click()
|
||||
event.target.blur()
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user