-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.html
More file actions
144 lines (130 loc) · 5.07 KB
/
Copy pathbackup.html
File metadata and controls
144 lines (130 loc) · 5.07 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=240, height=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Backup & Restore</title>
<link rel="icon" type="image/png" href="icon.png">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>Backup & Restore</header>
<section id="app">
<div class="section-title">Backup</div>
<p class="hint">Save all your notes to a backup file.</p>
<button type="button" class="action-btn" id="backup-btn">Save Backup File</button>
<div class="section-title">Restore</div>
<p class="hint">Choose a previously saved backup file to restore your notes. This replaces all current notes.</p>
<button type="button" class="action-btn" id="restore-btn">Choose Backup File</button>
<input type="file" id="restore-file" accept="application/json,.json" style="display:none;">
<p class="hint" id="restore-status"></p>
</section>
<footer>
<span id="softkey-left" class="softkey"></span>
<span id="softkey-center" class="softkey"></span>
<span id="softkey-right" class="softkey"></span>
</footer>
<script src="app.js"></script>
<script>
setupCommon();
var backupBtn = document.getElementById('backup-btn');
var restoreBtn = document.getElementById('restore-btn');
var fileInput = document.getElementById('restore-file');
var statusEl = document.getElementById('restore-status');
function buildBackupPayload() {
return JSON.stringify({
app: 'CloudPhone Notes',
version: APP_VERSION,
exportedAt: nowTs(),
notes: loadNotes()
});
}
function cleanNotes(notes) {
return notes.filter(function (n) { return n && typeof n === 'object'; }).map(function (n) {
return {
id: n.id || uid(),
title: typeof n.title === 'string' ? n.title : '',
body: typeof n.body === 'string' ? n.body : '',
createdAt: n.createdAt || nowTs(),
updatedAt: n.updatedAt || nowTs()
};
});
}
/* ---------- Backup: save to file ---------- */
backupBtn.addEventListener('click', function () {
var json = buildBackupPayload();
try {
var blob = new Blob([json], { type: 'application/json' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'notes-backup.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(function () { URL.revokeObjectURL(url); }, 2000);
showModal('Backup file saved.', [{ label: 'OK', primary: true }]);
} catch (e) {
showModal('Could not save the backup file on this device.', [{ label: 'OK', primary: true }]);
}
});
/* ---------- Restore: load from file ---------- */
restoreBtn.addEventListener('click', function () {
fileInput.click();
});
fileInput.addEventListener('change', function (e) {
var file = e.target.files && e.target.files[0];
fileInput.value = '';
if (!file) return;
statusEl.textContent = 'Selected: ' + file.name;
var reader = new FileReader();
reader.onerror = function () {
showModal('Could not read that file.', [{ label: 'OK', primary: true }]);
};
reader.onload = function () {
var parsed;
try {
parsed = JSON.parse(String(reader.result));
} catch (err) {
showModal('That file is not valid backup data.', [{ label: 'OK', primary: true }]);
return;
}
var notes = Array.isArray(parsed) ? parsed : parsed.notes;
if (!Array.isArray(notes)) {
showModal('That file is not valid backup data.', [{ label: 'OK', primary: true }]);
return;
}
showModal('Replace all current notes with "' + file.name + '"?', [
{ label: 'Cancel' },
{
label: 'Restore', primary: true, action: function () {
var cleaned = cleanNotes(notes);
saveNotes(cleaned);
statusEl.textContent = 'Restored ' + cleaned.length + ' note(s) from ' + file.name;
showModal('Restore complete. ' + cleaned.length + ' note(s) loaded.', [
{ label: 'OK', primary: true }
]);
}
}
]);
};
reader.readAsText(file);
});
/* Up/Down d-pad navigation between the two action buttons */
var focusables = [backupBtn, restoreBtn];
var focusIdx = 0;
function setFocus(i) {
focusIdx = (i + focusables.length) % focusables.length;
focusables[focusIdx].focus();
}
window.addEventListener('keydown', function (e) {
if (e.key === 'ArrowDown') { e.preventDefault(); setFocus(focusIdx + 1); }
else if (e.key === 'ArrowUp') { e.preventDefault(); setFocus(focusIdx - 1); }
});
setFocus(0);
bindRightSoftKeyBack('index.html', 'Back');
document.getElementById('softkey-left').textContent = '';
document.getElementById('softkey-center').textContent = '';
</script>
</body>
</html>