Files
eminuxandCursor 4e03fa58ab
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / scan_ruby (push) Has been cancelled
Aggiorna lo stato dell'invio email senza ricaricare la pagina.
Così la lista destinatari si aggiorna in corso d'opera senza riportare lo scroll in cima.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-18 23:03:35 +02:00

50 lines
1.2 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { interval: { type: Number, default: 10000 } }
connect() {
this.timer = setInterval(() => this.refresh(), this.intervalValue)
}
disconnect() {
clearInterval(this.timer)
}
refresh() {
if (document.hidden) return
const frame = this.element.closest("turbo-frame")
if (frame && typeof frame.reload === "function") {
this.reloadFrame(frame)
return
}
this.refreshPreservingScroll()
}
reloadFrame(frame) {
if (frame.hasAttribute("busy")) return
const url = window.location.href
if (frame.getAttribute("src") === url) {
frame.reload()
} else {
frame.setAttribute("src", url)
}
}
refreshPreservingScroll() {
const { scrollX, scrollY } = window
const restore = () => window.scrollTo(scrollX, scrollY)
document.addEventListener("turbo:render", restore, { once: true })
document.addEventListener("turbo:load", restore, { once: true })
if (window.Turbo?.visit) {
window.Turbo.visit(window.location.href, { action: "replace" })
} else {
window.location.reload()
}
}
}