forked from fixin.me/fixin.me
Stimulus controllers were reaching into Turbo.StreamElement.prototype to call disableElement/enableElement — tight coupling to Turbo internals. Extract both functions to app/javascript/element_helpers.js and import from there in application.js (which still assigns them to the Turbo prototype for server-driven Turbo Stream actions), details_controller, and readout_unit_controller. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
66 lines
2.1 KiB
JavaScript
66 lines
2.1 KiB
JavaScript
// Configure your import map in config/importmap.rb. Read more:
|
|
// https://github.com/rails/importmap-rails
|
|
import "@hotwired/turbo-rails"
|
|
import "controllers"
|
|
import { disableElement, enableElement } from "element_helpers"
|
|
|
|
|
|
/* Hide page before loaded for testing purposes */
|
|
function showPage() {
|
|
document.documentElement.style.visibility = "visible"
|
|
}
|
|
document.addEventListener('turbo:load', showPage)
|
|
|
|
|
|
/* Turbo stream actions */
|
|
Turbo.StreamElement.prototype.disableElement = disableElement
|
|
Turbo.StreamElement.prototype.enableElement = enableElement
|
|
Turbo.StreamActions.disable = function() {
|
|
this.targetElements.forEach(disableElement)
|
|
}
|
|
Turbo.StreamActions.enable = function() {
|
|
this.targetElements.forEach(enableElement)
|
|
}
|
|
|
|
/* TODO: change to visibility = collapse to avoid width change? */
|
|
Turbo.StreamActions.hide = function() {
|
|
this.targetElements.forEach((e) => { e.style.display = "none" })
|
|
}
|
|
|
|
Turbo.StreamActions.show = function() {
|
|
this.targetElements.forEach((e) => { e.style.removeProperty("display") })
|
|
}
|
|
|
|
/*
|
|
Turbo.StreamActions.collapse = function() {
|
|
this.targetElements.forEach((e) => { e.style.visibility = "collapse" })
|
|
}
|
|
*/
|
|
|
|
Turbo.StreamActions.close_form = function() {
|
|
this.targetElements.forEach((e) => {
|
|
/* Move focus if there's no focus or focus inside form being closed */
|
|
const focused = document.activeElement
|
|
if (!focused || (focused == document.body) || e.contains(focused)) {
|
|
let nextForm = e.parentElement.querySelector(`#${e.id} ~ tr:has([autofocus])`)
|
|
nextForm ??= e.parentElement.querySelector("tr:has([autofocus])")
|
|
nextForm?.querySelector("[autofocus]").focus()
|
|
}
|
|
document.getElementById(e.getAttribute("data-form")).remove()
|
|
if (e.hasAttribute("data-link")) {
|
|
this.enableElement(document.getElementById(e.getAttribute("data-link")))
|
|
}
|
|
if (e.hasAttribute("data-hidden-row")) {
|
|
document.getElementById(e.getAttribute("data-hidden-row")).removeAttribute("style")
|
|
}
|
|
e.remove()
|
|
})
|
|
}
|
|
|
|
Turbo.StreamActions.unselect = function() {
|
|
this.targetElements.forEach((e) => {
|
|
e.checked = false
|
|
this.enableElement(e)
|
|
})
|
|
}
|