-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex2.html
77 lines (66 loc) · 1.54 KB
/
index2.html
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
<!doctype html>
<html lang="en" manifest="mani.manifest">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
<ul id="todos">
</ul>
<input type="text" id="newentry">
<button id="send">Abschicken</button>
<script src="jquery.min.js"></script>
<script>
var mytodos;
var offlinedata = JSON.parse(localStorage.getItem("offlinetodo")) || [];
function loadData() {
$.get('/api', function(data){
mytodos = data;
if(offlinedata.length) {
for(var i = 0; i < offlinedata.length; i++) {
mytodos.entries.push(offlinedata[i]);
}
offlinedata = [];
localStorage.setItem("offlinetodo", JSON.stringify(offlinedata));
saveData();
}
localStorage.setItem("todos", JSON.stringify(mytodos));
writeData();
});
}
function writeData() {
$('#todos').html('');
var todos = mytodos;
for(var i = 0; i < todos.entries.length; i++) {
var el = $("<li>" + todos.entries[i] + "</li>");
$('#todos').append(el);
}
}
function saveData() {
$.post('/api', JSON.stringify(mytodos), function() {
loadData();
});
}
$(window).on('online', function(){
saveData();
});
if(!navigator.onLine) {
mytodos = JSON.parse(localStorage.getItem("todos"));
writeData();
}
$('#send').on('click', function(e) {
var text = $('#newentry').val();
mytodos.entries.push(text);
if(navigator.onLine) {
saveData();
} else {
offlinedata.push(text);
localStorage.setItem("offlinetodo", JSON.stringify(offlinedata));
}
writeData();
});
loadData();
</script>
</body>
</html>