page size and unsaved changes contr. refactor
Some checks failed
/ Run tests (push) Failing after 1m14s
/ Run system tests (push) Failing after 1m19s
/ Build, push and deploy image (push) Has been skipped

This commit is contained in:
david 2024-10-29 01:20:21 +01:00
parent 9b521c1f2b
commit 5bd0cf7ae0
2 changed files with 12 additions and 16 deletions

View file

@ -1,36 +1,32 @@
import { Controller } from "@hotwired/stimulus" import { Controller } from "@hotwired/stimulus"
const LEAVE_ALERT = "Es gibt ungespeicherte Änderungen! Wirklich verlassen?"
// Connects to data-controller="unsaved-changes" // Connects to data-controller="unsaved-changes"
export default class extends Controller { export default class extends Controller {
originalFormData = null initialState = null
connect() { connect() {
this.originalFormData = this.formState() this.initialState = this.formState()
window.addEventListener("beforeunload", (event) => this.leavingPage(event)) window.addEventListener("beforeunload", (event) => this.leavingPage(event))
document.addEventListener('turbo:before-visit', (e) => this.leavingPage(e)); document.addEventListener('turbo:before-visit', (e) => this.leavingPage(e))
this.element.addEventListener("submit", _ => this.originalFormData = this.formState()) this.element.addEventListener("submit", (_) => this.initialState = this.formState())
} }
formState() { formState() {
return JSON.stringify(Array.from(new FormData(this.element).entries()) return JSON.stringify(Array.from(new FormData(this.element).entries())
.filter(x => x[1] != "")) .filter(x => x[1] != "")
.sort(x => x[0]))
} }
leavingPage(event) { leavingPage(event) {
if(this.originalFormData == this.formState()) { if(this.initialState == this.formState()) {
return return
} }
if (event.type == "turbo:before-visit") { if (!window.confirm(LEAVE_ALERT)) {
if (!window.confirm("Es gibt ungespeicherte Änderungen! Wirklich verlassen?")) { event.preventDefault()
event.preventDefault()
}
} else {
if (!window.confirm("Es gibt ungespeicherte Änderungen! Wirklich verlassen?")) {
event.preventDefault()
}
return event.returnValue;
} }
} }
} }

View file

@ -10,7 +10,7 @@
# You can set any pagy variable as a Pagy::DEFAULT. They can also be overridden per instance by just passing them to # You can set any pagy variable as a Pagy::DEFAULT. They can also be overridden per instance by just passing them to
# Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods # Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods
# Here are the few that make more sense as DEFAULTs: # Here are the few that make more sense as DEFAULTs:
# Pagy::DEFAULT[:limit] = 20 # default Pagy::DEFAULT[:limit] = 100 # default
# Pagy::DEFAULT[:size] = 7 # default # Pagy::DEFAULT[:size] = 7 # default
# Pagy::DEFAULT[:ends] = true # default # Pagy::DEFAULT[:ends] = true # default
# Pagy::DEFAULT[:page_param] = :page # default # Pagy::DEFAULT[:page_param] = :page # default