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"
const LEAVE_ALERT = "Es gibt ungespeicherte Änderungen! Wirklich verlassen?"
// Connects to data-controller="unsaved-changes"
export default class extends Controller {
originalFormData = null
initialState = null
connect() {
this.originalFormData = this.formState()
this.initialState = this.formState()
window.addEventListener("beforeunload", (event) => this.leavingPage(event))
document.addEventListener('turbo:before-visit', (e) => this.leavingPage(e));
this.element.addEventListener("submit", _ => this.originalFormData = this.formState())
document.addEventListener('turbo:before-visit', (e) => this.leavingPage(e))
this.element.addEventListener("submit", (_) => this.initialState = this.formState())
}
formState() {
return JSON.stringify(Array.from(new FormData(this.element).entries())
.filter(x => x[1] != ""))
.filter(x => x[1] != "")
.sort(x => x[0]))
}
leavingPage(event) {
if(this.originalFormData == this.formState()) {
if(this.initialState == this.formState()) {
return
}
if (event.type == "turbo:before-visit") {
if (!window.confirm("Es gibt ungespeicherte Änderungen! Wirklich verlassen?")) {
if (!window.confirm(LEAVE_ALERT)) {
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
# 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:
# Pagy::DEFAULT[:limit] = 20 # default
Pagy::DEFAULT[:limit] = 100 # default
# Pagy::DEFAULT[:size] = 7 # default
# Pagy::DEFAULT[:ends] = true # default
# Pagy::DEFAULT[:page_param] = :page # default