This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
forked from pietervdvn/MapComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.ts
90 lines (70 loc) · 2.54 KB
/
Helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {OsmConnection} from "./Logic/OsmConnection";
import {Changes} from "./Logic/Changes";
import {UIEventSource} from "./UI/UIEventSource";
export class Helpers {
static DoEvery(millis: number, f: (() => void)) {
window.setTimeout(
function () {
f();
Helpers.DoEvery(millis, f);
}
, millis)
}
static SetupAutoSave(changes: Changes, millisTillChangesAreSaved: UIEventSource<number>, saveAfterXMillis: number) {
changes.pendingChangesES.addCallback(function () {
var c = changes.pendingChangesES.data;
if (c > 10) {
millisTillChangesAreSaved.setData(0);
changes.uploadAll(undefined);
return;
}
if (c > 0) {
millisTillChangesAreSaved.setData(saveAfterXMillis);
}
});
millisTillChangesAreSaved.addCallback((time) => {
if (time <= 0 && changes.pendingChangesES.data > 0) {
changes.uploadAll(undefined);
}
}
)
Helpers.DoEvery(
1000,
() => {
millisTillChangesAreSaved
.setData(millisTillChangesAreSaved.data - 1000)
});
}
/*
* 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!
if (changes.pendingChangesES.data == 0) {
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()
});
})
}
}