mapcomplete/Helpers.ts

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
import {OsmConnection} from "./Logic/OsmConnection";
import {Changes} from "./Logic/Changes";
import {UIEventSource} from "./UI/UIEventSource";
export class Helpers {
2020-06-28 23:33:48 +02:00
static DoEvery(millis: number, f: (() => void)) {
window.setTimeout(
function () {
f();
Helpers.DoEvery(millis, f);
2020-06-24 00:35:19 +02:00
}
2020-06-28 23:33:48 +02:00
, millis)
}
static SetupAutoSave(changes: Changes, millisTillChangesAreSaved: UIEventSource<number>, saveAfterXMillis: number) {
2020-06-24 00:35:19 +02:00
changes.pendingChangesES.addCallback(function () {
var c = changes.pendingChangesES.data;
2020-06-24 00:35:19 +02:00
if (c > 10) {
millisTillChangesAreSaved.setData(0);
2020-06-24 00:35:19 +02:00
changes.uploadAll(undefined);
return;
}
if (c > 0) {
millisTillChangesAreSaved.setData(saveAfterXMillis);
2020-06-24 00:35:19 +02:00
}
});
2020-06-28 23:33:48 +02:00
millisTillChangesAreSaved.addCallback((time) => {
if (time <= 0 && changes.pendingChangesES.data > 0) {
changes.uploadAll(undefined);
}
}
)
Helpers.DoEvery(
1000,
() => {
millisTillChangesAreSaved
.setData(millisTillChangesAreSaved.data - 1000)
});
2020-06-24 00:35:19 +02:00
}
2020-06-28 23:33:48 +02:00
2020-06-24 00:35:19 +02:00
/*
* Registers an action that:
* -> Upload everything to OSM
* -> Asks the user not to close. The 'not to close' dialog should profide enough time to upload
* -> WHen uploading is done, the window is closed anyway
*/
2020-06-28 23:33:48 +02:00
static LastEffortSave(changes: Changes) {
2020-06-24 00:35:19 +02:00
window.addEventListener("beforeunload", function (e) {
// Quickly save everyting!
if (changes.pendingChangesES.data == 0) {
2020-06-24 00:35:19 +02:00
return "";
}
changes.uploadAll(function () {
window.close()
});
var confirmationMessage = "Nog even geduld - je laatset wijzigingen worden opgeslaan!";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
document.addEventListener('visibilitychange',() => {
if(document.visibilityState === "visible"){
return;
}
if (changes.pendingChangesES.data == 0) {
return;
}
console.log("Upmoading: loss of focus")
changes.uploadAll(function () {
window.close()
});
})
2020-06-24 00:35:19 +02:00
}
2020-06-29 03:12:44 +02:00
2020-06-24 00:35:19 +02:00
}