page size and unsaved changes contr. refactor
This commit is contained in:
parent
9b521c1f2b
commit
5bd0cf7ae0
2 changed files with 12 additions and 16 deletions
|
|
@ -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?")) {
|
||||
event.preventDefault()
|
||||
}
|
||||
} else {
|
||||
if (!window.confirm("Es gibt ungespeicherte Änderungen! Wirklich verlassen?")) {
|
||||
event.preventDefault()
|
||||
}
|
||||
return event.returnValue;
|
||||
if (!window.confirm(LEAVE_ALERT)) {
|
||||
event.preventDefault()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue