-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratchpad.js
227 lines (194 loc) · 6.28 KB
/
scratchpad.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
// - Figure out how to update the web page quickly during development
// - Tweak timeouts (e.g. service worker staling) for production
// - Figure out if we what prod and dev to be any different
// - Somehow indicate if offline mode is properly installed
// - favicon
// - Shortcut for New/Delete buttons (or make them focusable or something)
let itemIds = [];
let selectedItem = null;
let selectedId = null;
let changeTimeoutId = null;
let UPDATE_TIMEOUT = 2000;
let WHITESPACE_REGEX = /^\s*$/;
function log(s) {
console.log(new Date().toISOString() + " " + s);
}
function generateId() {
let digits = "0123456789abcdef";
let result = "";
for (let i = 0; i < 32; i++) {
result = result + digits[Math.floor(Math.random() * 16)];
}
return result;
}
function updateDatabaseFromChange() {
log("update");
markChangesAsCommitted();
selectedItem.title = document.querySelector(".title").value;
selectedItem.content = document.querySelector(".content").value;
localStorage.setItem("item:" + selectedId, JSON.stringify(selectedItem));
refreshItemList();
}
function itemChanged() {
if (changeTimeoutId !== null) {
clearTimeout(changeTimeoutId);
}
changeTimeoutId = setTimeout(updateDatabaseFromChange, UPDATE_TIMEOUT);
document.querySelector(".changed_marker").style.visibility = "visible";
}
function itemKeyDown(id, e) {
let currentIndex = itemIds.indexOf(id);
let newIndex;
if (e.keyCode == 13) {
log("enter 3 " + id);
selectItem(id);
e.preventDefault();
}
if (e.keyCode == 40) {
log("down " + id);
newIndex = currentIndex + 1;
} else if (e.keyCode == 38) {
log("up " + id);
newIndex = currentIndex - 1;
} else {
return;
}
if (newIndex >= 0 && newIndex < itemIds.length) {
log(newIndex);
document.querySelector(".items_pane").childNodes[newIndex].focus();
}
}
function createListItem(id, item, tabIndex) {
let result = document.createElement("div");
// Theoretically, we should check if there are any
result.textContent = item.title.match(WHITESPACE_REGEX) ? "(empty)" : item.title;
result.className = id == selectedId ? "item selected_item" : "item";
result.setAttribute("tabindex", tabIndex);
result.addEventListener("click", () => { selectItem(id); });
result.addEventListener("keydown", e => { itemKeyDown(id, e); });
return result;
}
function getListElementById(id) {
return document.querySelector(".items_pane").childNodes[itemIds.indexOf(id)];
}
function markChangesAsCommitted() {
clearTimeout(changeTimeoutId);
changeTimeoutId = null;
document.querySelector(".changed_marker").style.visibility = "hidden";
}
function selectItem(id) {
if (id === selectedId) {
let contentElement = document.querySelector(".content");
contentElement.select();
contentElement.focus();
return;
}
if (changeTimeoutId !== null) {
updateDatabaseFromChange();
}
if (selectedId !== null) {
let currentElement = getListElementById(selectedId);
currentElement.className = "item";
}
if (id !== null && itemIds.indexOf(id) >= 0) {
let newElement = document.querySelector(".items_pane").childNodes[itemIds.indexOf(id)];
newElement.className = "item selected_item";
selectedItem = JSON.parse(localStorage.getItem("item:" + id));
selectedId = id;
updateContent();
let contentElement = document.querySelector(".content");
contentElement.select();
contentElement.focus();
} else {
selectedItem = null;
selectedId = null;
updateContent();
}
}
function updateContent() {
if (changeTimeoutId !== null) {
markChangesAsCommitted();
}
let contentElement = document.querySelector(".content");
let titleElement = document.querySelector(".title");
if (selectedItem === null) {
contentElement.value = "";
contentElement.disabled = true;
titleElement.value = "";
titleElement.disabled = true;
} else {
contentElement.value = selectedItem.content;
contentElement.disabled = false;
titleElement.value = selectedItem.title;
titleElement.disabled = false;
}
}
function initializeDatabase() {
log("initializing database on first startup");
let initialId = generateId();
localStorage.setItem("items", JSON.stringify([initialId]));
localStorage.setItem("item:" + initialId, JSON.stringify({
title: "First item",
content: "Type something here."}));
}
function refreshItemList() {
itemIds = JSON.parse(localStorage.getItem("items"));
let itemsNode = document.querySelector(".items_pane");
let focusedIndex = null;
if (document.activeElement !== null && document.activeElement.parentNode === itemsNode) {
focusedIndex = Array.prototype.indexOf.call(itemsNode.childNodes, document.activeElement);
}
while (itemsNode.firstChild) {
itemsNode.removeChild(itemsNode.firstChild);
}
for (let i = 0; i < itemIds.length; i++) {
let id = itemIds[i];
let element = createListItem(id, JSON.parse(localStorage.getItem("item:" + id)), i + 3);
itemsNode.appendChild(element);
}
if (focusedIndex !== null) {
log("fi " + focusedIndex + " " + itemsNode.childNodes.length);
itemsNode.childNodes[focusedIndex].focus();
}
}
function newItem() {
let id = generateId();
let item = { title: "New", content: "" };
localStorage.setItem("item:" + id, JSON.stringify(item));
itemIds.push(id);
localStorage.setItem("items", JSON.stringify(itemIds));
refreshItemList();
selectItem(id);
let titleElement = document.querySelector(".title");
titleElement.select();
titleElement.focus();
}
function deleteCurrentItem() {
if (selectedId === null) {
return;
}
let deleteId = selectedId;
selectItem(null);
let idx = itemIds.indexOf(deleteId);
itemIds.splice(idx, 1);
localStorage.removeItem("item:" + deleteId);
localStorage.setItem("items", JSON.stringify(itemIds));
refreshItemList();
}
function onLoad() {
if (localStorage.getItem("items") === null) {
// First startup. Initialize the local storage with something reasonable.
initializeDatabase();
}
navigator.serviceWorker.register("service-worker.js", { scope: "./" })
.then(r => {
log("service worker installed: " + r);
})
.catch(e => {
log("could not install service worker: " + e);
});
refreshItemList();
selectItem(itemIds[0]);
}
window.addEventListener("load", onLoad);