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 {
|
|
|
|
private lastChange: Date
|
|
|
|
|
2023-03-28 05:13:48 +02:00
|
|
|
constructor(changes: Changes, selectedFeature: UIEventSource<Feature>) {
|
2021-02-20 22:18:42 +01:00
|
|
|
const self = this
|
|
|
|
this.lastChange = new Date()
|
2021-07-15 20:47:28 +02:00
|
|
|
changes.pendingChanges.addCallback(() => {
|
2021-02-20 22:18:42 +01:00
|
|
|
self.lastChange = new Date()
|
2021-07-03 14:48:07 +02:00
|
|
|
|
2021-02-20 22:18:42 +01:00
|
|
|
window.setTimeout(() => {
|
|
|
|
const diff = (new Date().getTime() - self.lastChange.getTime()) / 1000
|
2021-07-03 14:48:07 +02:00
|
|
|
if (Constants.updateTimeoutSec >= diff - 1) {
|
2021-02-20 22:18:42 +01:00
|
|
|
changes.flushChanges("Flushing changes due to timeout")
|
|
|
|
}
|
|
|
|
}, Constants.updateTimeoutSec * 1000)
|
|
|
|
})
|
2021-07-03 14:48:07 +02:00
|
|
|
|
2021-07-03 14:36:26 +02:00
|
|
|
selectedFeature.stabilized(10000).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
|
|
|
}
|
|
|
|
}
|