Add 'pagehide' to ask confirmation on ipads

This commit is contained in:
pietervdvn 2021-07-03 14:48:07 +02:00
parent d686a756fb
commit b6d9a3127c

View file

@ -2,33 +2,33 @@ import {Changes} from "../Osm/Changes";
import Constants from "../../Models/Constants"; import Constants from "../../Models/Constants";
import {UIEventSource} from "../UIEventSource"; import {UIEventSource} from "../UIEventSource";
export default class PendingChangesUploader{ export default class PendingChangesUploader {
private lastChange : Date; private lastChange: Date;
constructor(changes: Changes, selectedFeature: UIEventSource<any>) { constructor(changes: Changes, selectedFeature: UIEventSource<any>) {
const self = this; const self = this;
this.lastChange = new Date(); this.lastChange = new Date();
changes.pending.addCallback(() => { changes.pending.addCallback(() => {
self.lastChange = new Date(); self.lastChange = new Date();
window.setTimeout(() => { window.setTimeout(() => {
const diff = (new Date().getTime() - self.lastChange.getTime()) / 1000; const diff = (new Date().getTime() - self.lastChange.getTime()) / 1000;
if(Constants.updateTimeoutSec >= diff - 1){ if (Constants.updateTimeoutSec >= diff - 1) {
changes.flushChanges("Flushing changes due to timeout"); changes.flushChanges("Flushing changes due to timeout");
} }
}, Constants.updateTimeoutSec * 1000); }, Constants.updateTimeoutSec * 1000);
}); });
selectedFeature selectedFeature
.stabilized(10000) .stabilized(10000)
.addCallback(feature => { .addCallback(feature => {
if(feature === undefined){ if (feature === undefined) {
// The popup got closed - we flush // The popup got closed - we flush
changes.flushChanges("Flushing changes due to popup closed"); changes.flushChanges("Flushing changes due to popup closed");
} }
}); });
document.addEventListener('mouseout', e => { document.addEventListener('mouseout', e => {
// @ts-ignore // @ts-ignore
@ -36,7 +36,7 @@ export default class PendingChangesUploader{
changes.flushChanges("Flushing changes due to focus lost"); changes.flushChanges("Flushing changes due to focus lost");
} }
}); });
document.onfocus = () => { document.onfocus = () => {
changes.flushChanges("OnFocus") changes.flushChanges("OnFocus")
} }
@ -44,18 +44,17 @@ export default class PendingChangesUploader{
document.onblur = () => { document.onblur = () => {
changes.flushChanges("OnFocus") changes.flushChanges("OnFocus")
} }
try{ try {
document.addEventListener("visibilitychange", () => { document.addEventListener("visibilitychange", () => {
changes.flushChanges("Visibility change") changes.flushChanges("Visibility change")
}, false); }, false);
}catch(e){ } catch (e) {
console.warn("Could not register visibility change listener", e) console.warn("Could not register visibility change listener", e)
} }
window.onbeforeunload = function(e){ function onunload(e) {
if (changes.pending.data.length == 0) {
if(changes.pending.data.length == 0){
return; return;
} }
changes.flushChanges("onbeforeunload - probably closing or something similar"); changes.flushChanges("onbeforeunload - probably closing or something similar");
@ -63,8 +62,11 @@ export default class PendingChangesUploader{
return "Saving your last changes..." return "Saving your last changes..."
} }
window.onbeforeunload = onunload
// https://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad#4824156
window.addEventListener("pagehide", onunload)
} }
} }