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-27 03:06:51 +02:00
|
|
|
static SetupAutoSave(changes: Changes, millisTillChangesAreSaved : UIEventSource<number>, saveAfterXMillis : number) {
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
// This little function triggers the actual upload:
|
|
|
|
// Either when more then three answers are selected, or when no new answer has been added for the last 20s
|
|
|
|
// @ts-ignore
|
|
|
|
window.decreaseTime = function () {
|
2020-06-27 03:06:51 +02:00
|
|
|
var time = millisTillChangesAreSaved.data;
|
2020-06-24 00:35:19 +02:00
|
|
|
if (time <= 0) {
|
2020-06-27 03:06:51 +02:00
|
|
|
if (changes.pendingChangesES.data > 0) {
|
2020-06-24 00:35:19 +02:00
|
|
|
changes.uploadAll(undefined);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-27 03:06:51 +02:00
|
|
|
millisTillChangesAreSaved.setData(time - 1000);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
window.setTimeout('decreaseTime()', 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
changes.pendingChangesES.addCallback(function () {
|
|
|
|
|
2020-06-27 03:06:51 +02:00
|
|
|
var c = changes.pendingChangesES.data;
|
2020-06-24 00:35:19 +02:00
|
|
|
if (c > 10) {
|
2020-06-27 03:06:51 +02:00
|
|
|
millisTillChangesAreSaved.setData(0);
|
2020-06-24 00:35:19 +02:00
|
|
|
changes.uploadAll(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c > 0) {
|
2020-06-27 03:06:51 +02:00
|
|
|
millisTillChangesAreSaved.setData(saveAfterXMillis);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
window.decreaseTime(); // The timer keeps running...
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
static LastEffortSave(changes : Changes){
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", function (e) {
|
|
|
|
// Quickly save everyting!
|
2020-06-27 03:06:51 +02:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|