2021-02-20 22:18:42 +01:00
|
|
|
import { Changes } from "../Osm/Changes"
|
|
|
|
import Constants from "../../Models/Constants"
|
|
|
|
import { UIEventSource } from "../UIEventSource"
|
2021-10-20 00:09:40 +02:00
|
|
|
import { Utils } from "../../Utils"
|
2023-03-28 05:13:48 +02:00
|
|
|
import { Feature } from "geojson"
|
2021-02-20 22:18:42 +01:00
|
|
|
|
2021-07-03 14:48:07 +02:00
|
|
|
export default class PendingChangesUploader {
|
2023-03-28 05:13:48 +02:00
|
|
|
constructor(changes: Changes, selectedFeature: UIEventSource<Feature>) {
|
2023-10-16 14:27:05 +02:00
|
|
|
changes.pendingChanges
|
|
|
|
.stabilized(Constants.updateTimeoutSec * 1000)
|
|
|
|
.addCallback(() => changes.flushChanges("Flushing changes due to timeout"))
|
2021-07-03 14:48:07 +02:00
|
|
|
|
2023-10-16 13:38:11 +02:00
|
|
|
selectedFeature.stabilized(1000).addCallback((feature) => {
|
2021-07-03 14:48:07 +02:00
|
|
|
if (feature === undefined) {
|
|
|
|
// The popup got closed - we flush
|
|
|
|
changes.flushChanges("Flushing changes due to popup closed")
|
|
|
|
}
|
|
|
|
})
|
2021-07-03 14:36:26 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
if (Utils.runningFromConsole) {
|
2021-10-20 00:09:40 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-07-03 14:36:26 +02:00
|
|
|
document.addEventListener("mouseout", (e) => {
|
|
|
|
// @ts-ignore
|
|
|
|
if (!e.toElement && !e.relatedTarget) {
|
|
|
|
changes.flushChanges("Flushing changes due to focus lost")
|
|
|
|
}
|
|
|
|
})
|
2021-07-03 14:48:07 +02:00
|
|
|
|
2021-07-03 14:36:26 +02:00
|
|
|
document.onfocus = () => {
|
|
|
|
changes.flushChanges("OnFocus")
|
|
|
|
}
|
|
|
|
|
|
|
|
document.onblur = () => {
|
|
|
|
changes.flushChanges("OnFocus")
|
|
|
|
}
|
2021-07-03 14:48:07 +02:00
|
|
|
try {
|
2021-07-03 14:36:26 +02:00
|
|
|
document.addEventListener(
|
|
|
|
"visibilitychange",
|
|
|
|
() => {
|
|
|
|
changes.flushChanges("Visibility change")
|
|
|
|
},
|
|
|
|
false
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-07-03 14:48:07 +02:00
|
|
|
} catch (e) {
|
2021-07-03 14:36:26 +02:00
|
|
|
console.warn("Could not register visibility change listener", e)
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:48:07 +02:00
|
|
|
function onunload(e) {
|
2021-09-09 00:05:51 +02:00
|
|
|
if (changes.pendingChanges.data.length == 0) {
|
2021-02-20 22:18:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
changes.flushChanges("onbeforeunload - probably closing or something similar")
|
|
|
|
e.preventDefault()
|
|
|
|
return "Saving your last changes..."
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:48:07 +02:00
|
|
|
window.onbeforeunload = onunload
|
|
|
|
// https://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad#4824156
|
|
|
|
window.addEventListener("pagehide", onunload)
|
2021-02-20 22:18:42 +01:00
|
|
|
}
|
|
|
|
}
|