-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
492 lines (441 loc) · 19.2 KB
/
index.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Listits v1</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f5f5f5;
}
#app {
max-width: 800px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.group {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
}
.group-title {
font-size: 1.2em;
font-weight: bold;
display: flex;
justify-content: space-between;
align-items: center;
}
.list-title {
font-weight: bold;
margin-top: 10px;
}
.item {
padding: 5px;
margin: 5px 0;
display: flex;
justify-content: space-between;
align-items: center;
background-color: rgba(0, 0, 0, 0.1);
border-radius: 4px;
}
.item.done {
background-color: #e0ffe0;
}
button {
margin: 5px;
padding: 5px 10px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
button:not([disabled]):hover {
background-color: #0056b3;
}
button[disabled] {
background-color: #ccc;
cursor: not-allowed;
}
.btn-danger {
background-color: #dc3545;
}
.btn-danger:not([disabled]):hover {
background-color: #c82333;
}
.filter-buttons, .import-export-buttons, .undo-redo-buttons {
display: flex;
justify-content: flex-end;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="app">
<h1>Listits v1</h1>
<a href="https://github.com/viveret/listits" target="_blank">View Source</a>
<div>
<div class="import-export-buttons">
<button onclick="importData()">Import Data</button>
<button onclick="exportData()">Export Data</button>
</div>
<div class="undo-redo-buttons">
<button onclick="undo()">Undo</button>
<button onclick="redo()">Redo</button>
</div>
</div>
<div class="filter-buttons">
<button onclick="setFilter('all')">Show All</button>
<button onclick="setFilter('done')">Show Only Done</button>
<button onclick="setFilter('not-done')">Show Only Not Done</button>
</div>
<button onclick="createNewGroup()">Add New Group</button>
<div id="groups-container"></div>
<div class="other-buttons">
<button onclick="clearData()" class="btn-danger">Clear Data</button>
</div>
</div>
<script>
const generateId = () => '_' + Math.random().toString(36).substr(2, 9);
const loadData = () => {
const json = localStorage.getItem('listitsGroups');
if (json && json !== 'undefined') {
const data = JSON.parse(json);
if (data && typeof data === "object" && data.groups && data.groups.length !== 0) {
return data;
}
}
return initializeDefaultData();
};
const initializeDefaultData = () => {
const defaultGroups = {
historyMessage: 'Initial state (default)',
groups: [
{
id: generateId(),
name: 'Favorite Reads',
lists: [
{ id: generateId(), name: 'Books', items: [] },
{ id: generateId(), name: 'Zines', items: [] }
]
},
{
id: generateId(),
name: 'To Do',
lists: [
{ id: generateId(), name: 'At Home', items: [] },
{ id: generateId(), name: 'Outside', items: [] }
]
}
]
};
return defaultGroups;
};
let data = loadData();
let filter = 'all';
const promptInput = (message, defaultValue = '') => prompt(message, defaultValue) || '';
const setFilter = (newFilter) => {
filter = newFilter;
render();
};
const addToHistory = (historyMessage) => {
console.log('Adding to history:', historyMessage);
data.historyMessage = historyMessage;
addToUndoHistory(data, true);
// clear redo history
localStorage.setItem('listitsRedoHistory', JSON.stringify([]));
};
const addToUndoHistory = (state, clipHistory) => {
const history = JSON.parse(localStorage.getItem('listitsHistory')) || [];
history.push(state);
if (clipHistory && history.length > 100) {
history.shift();
}
localStorage.setItem('listitsHistory', JSON.stringify(history));
};
const addToRedoHistory = (state) => {
const redoHistory = JSON.parse(localStorage.getItem('listitsRedoHistory')) || [];
redoHistory.push(state);
localStorage.setItem('listitsRedoHistory', JSON.stringify(redoHistory));
};
const undo = () => {
const history = JSON.parse(localStorage.getItem('listitsHistory')) || [];
if (history.length) {
let historyItem = history.pop();
// check if this is the same as the current state
if (JSON.stringify(historyItem) === JSON.stringify(data)) {
// add current state to redo history
addToRedoHistory(historyItem);
historyItem = history.pop();
} else {
addToRedoHistory(data);
}
console.log('Undoing:', data.historyMessage);
if (!historyItem) {
historyItem = initializeDefaultData();
}
console.log('New state:', historyItem.historyMessage);
data = historyItem;
localStorage.setItem('listitsHistory', JSON.stringify(history));
saveAndRender();
}
};
const redo = () => {
const history = JSON.parse(localStorage.getItem('listitsRedoHistory')) || [];
if (history.length) {
const redoItem = history.pop();
data = redoItem;
console.log('Redoing:', redoItem.historyMessage);
// add current state to history
addToUndoHistory(redoItem, false);
localStorage.setItem('listitsRedoHistory', JSON.stringify(history));
saveAndRender();
}
};
const setCanUndo = () => {
const history = JSON.parse(localStorage.getItem('listitsHistory')) || [];
if (history.length === 0) {
document.querySelector('button[onclick="undo()"]').disabled = true;
} else {
document.querySelector('button[onclick="undo()"]').removeAttribute('disabled');
}
};
const setCanRedo = () => {
const history = JSON.parse(localStorage.getItem('listitsRedoHistory')) || [];
if (history.length === 0) {
document.querySelector('button[onclick="redo()"]').disabled = true;
} else {
document.querySelector('button[onclick="redo()"]').removeAttribute('disabled');
}
};
const saveChangesToHistoryAndRender = (historyMessage) => {
addToHistory(historyMessage);
saveAndRender();
};
const saveAndRender = () => {
// add to history
localStorage.setItem('listitsGroups', JSON.stringify(data));
render();
};
const createItem = (parent, itemName, itemType) => {
const name = promptInput(`Enter ${itemName} name:`);
if (name) {
switch (itemType) {
case 'item': {
const description = promptInput(`Enter ${itemName} description:`)
parent.push({ id: generateId(), name, description, done: false, notes: '' });
} break;
case 'group':{
parent.push({ id: generateId(), name, lists: [] });
} break;
case 'list': {
parent.push({ id: generateId(), name, items: [] });
} break;
default: {
throw new Error(`unknown type ${itemType}`);
}
}
saveChangesToHistoryAndRender(`Created new ${itemName}: ${name}`);
}
};
const deleteItem = (parent, itemId) => {
const index = parent.findIndex(item => item.id === itemId);
if (index !== -1) {
parent.splice(index, 1);
saveChangesToHistoryAndRender(`Deleted ${parent[index].name}`);
}
};
const toggleDoneStatus = (item) => {
item.done = !item.done;
saveChangesToHistoryAndRender(`Toggled done status for ${item.name}`);
};
const editDescription = (item) => {
const description = promptInput('Edit description:', item.description);
if (description) {
item.description = description;
saveChangesToHistoryAndRender(`Edited description for ${item.name}`);
}
};
const editNotes = (item) => {
const notes = promptInput('Edit notes:', item.notes);
if (notes) {
item.notes = notes;
saveChangesToHistoryAndRender(`Edited notes for ${item.name}`);
}
};
// Function to save a new checklist template
const saveTemplate = (templateName, lists) => {
const templates = JSON.parse(localStorage.getItem('listitsTemplates')) || [];
const newTemplate = { id: generateId(), name: templateName, lists };
templates.push(newTemplate);
localStorage.setItem('listitsTemplates', JSON.stringify(templates));
};
// Function to apply a template to a group
const applyTemplateToGroup = (groupId, templateId) => {
const templates = JSON.parse(localStorage.getItem('listitsTemplates')) || [];
const template = templates.find(t => t.id === templateId);
if (template) {
const group = findGroup(groupId);
if (group) {
group.lists = template.lists.map(list => ({
...list,
id: generateId(),
items: list.items.map(item => ({ ...item, id: generateId() }))
}));
saveChangesToHistoryAndRender(`Applied template ${template.name} to group ${group.name}`);
}
}
};
// Function to list all templates (for the UI)
const getTemplates = () => JSON.parse(localStorage.getItem('listitsTemplates')) || [];
// Function to create a new template from a group
const createTemplateFromGroup = (groupId) => {
const group = findGroup(groupId);
if (group) {
const templateName = promptInput('Enter template name:');
if (templateName) {
saveTemplate(templateName, group.lists);
}
}
};
// Function to export data from localStorage
const exportData = () => {
const groupsData = localStorage.getItem('listitsGroups');
if (groupsData) {
prompt('Copy this data:', groupsData);
}
};
// Function to import data into localStorage
const importData = () => {
try {
const jsonData = prompt('Paste your data:');
localStorage.setItem('listitsGroups', jsonData);
data = loadData();
saveChangesToHistoryAndRender('Imported data');
} catch (error) {
console.error('Error importing data:', error);
}
};
const clearData = () => {
if (confirm('Are you sure you want to clear all data?')) {
localStorage.removeItem('listitsGroups');
localStorage.removeItem('listitsTemplates');
data = initializeDefaultData();
saveChangesToHistoryAndRender('Cleared all data');
}
};
// Add the ability to apply a template to the UI
const renderTemplateOptions = (groupId) => {
const templates = getTemplates();
if (templates.length) {
const container = document.createElement('div');
templates.forEach(template => {
const button = document.createElement('button');
button.textContent = `Apply ${template.name}`;
button.onclick = () => applyTemplateToGroup(groupId, template.id);
container.appendChild(button);
});
document.getElementById(`group-${groupId}`).appendChild(container);
}
};
const updateStaticElements = () => {
setCanUndo();
setCanRedo();
};
const render = () => {
updateStaticElements();
const container = document.getElementById('groups-container');
container.innerHTML = '';
data.groups.forEach(group => {
const groupDiv = document.createElement('div');
groupDiv.id = `group-${group.id}`;
groupDiv.className = 'group';
groupDiv.innerHTML = `
<div class="group-title">
${group.name}
<button class="btn-danger" onclick="deleteGroup('${group.id}')">Delete Group</button>
<button onclick="createTemplateFromGroup('${group.id}')">Save as Template</button>
<button onclick="renderTemplateOptions('${group.id}')">Apply Template</button>
</div>
<button onclick="createList('${group.id}')">Add New List</button>
`;
// renderTemplateOptions(group.id);
group.lists.forEach(list => {
const listDiv = document.createElement('div');
listDiv.className = 'list';
listDiv.innerHTML = `
<div class="list-title">
${list.name}
<button class="btn-danger" onclick="deleteList('${group.id}', '${list.id}')">Delete List</button>
</div>
<button onclick="createItemInList('${group.id}', '${list.id}')">Add New Item</button>
`;
list.items.forEach(item => {
if (filter === 'all' || (filter === 'done' && item.done) || (filter === 'not-done' && !item.done)) {
const itemDiv = document.createElement('div');
const notes = item.notes ? `<div>Your notes: <i>"${item.notes}"</i></div>` : '';
itemDiv.className = `item ${item.done ? 'done' : ''}`;
itemDiv.innerHTML = `
<div>
<div>
<strong>${item.name}</strong> - ${item.description}
</div>
${notes}
</div>
<div>
<button onclick="toggleDoneStatusInList('${group.id}', '${list.id}', '${item.id}')">
${item.done ? 'Not Done' : 'Done'}
</button>
<button onclick="editNotesInList('${group.id}', '${list.id}', '${item.id}')">Edit Notes</button>
<button onclick="editDescriptionInList('${group.id}', '${list.id}', '${item.id}')">Edit Description</button>
<button class="btn-danger" onclick="deleteItemInList('${group.id}', '${list.id}', '${item.id}')">Delete</button>
</div>
`;
listDiv.appendChild(itemDiv);
}
});
groupDiv.appendChild(listDiv);
});
container.appendChild(groupDiv);
});
};
// query data functions
const queryData = (query) => {
const groups = data.groups;
const group = groups.find(query.group);
if (!group) {
return null;
}
const list = group.lists.find(query.list);
if (!list) {
return null;
}
const item = list.items.find(query.item);
if (!item) {
return null;
}
return { group, list, item };
};
const findGroup = (groupId) => queryData({ group: g => g.id === groupId })?.group;
const findGroupList = (groupId, listId) => queryData({ group: g => g.id === groupId, list: l => l.id === listId })?.list;
const findGroupListItem = (groupId, listId, itemId) => queryData({ group: g => g.id === groupId, list: l => l.id === listId, item: i => i.id === itemId })?.item;
// UI functions
const createNewGroup = () => createItem(data.groups, 'group', 'group');
const deleteGroup = (groupId) => deleteItem(data.groups, groupId);
const createList = (groupId) => createItem(findGroup(groupId).lists, 'list', 'list');
const deleteList = (groupId, listId) => deleteItem(findGroup(groupId).lists, listId);
const createItemInList = (groupId, listId) => createItem(findGroupList(groupId, listId).items, 'item', 'item');
const deleteItemInList = (groupId, listId, itemId) => deleteItem(findGroupList(groupId, listId).items, itemId);
const toggleDoneStatusInList = (groupId, listId, itemId) => toggleDoneStatus(findGroupListItem(groupId, listId, itemId));
const editDescriptionInList = (groupId, listId, itemId) => editDescription(findGroupListItem(groupId, listId, itemId));
const editNotesInList = (groupId, listId, itemId) => editNotes(findGroupListItem(groupId, listId, itemId));
document.addEventListener('DOMContentLoaded', render);
</script>
</body>
</html>