-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
348 lines (293 loc) · 9.86 KB
/
Copy pathscript.js
File metadata and controls
348 lines (293 loc) · 9.86 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
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
/**
* Main application logic for the List Maker App.
*
* This file initializes the application, handles user input for creating
* and managing lists, and renders the lists to the DOM.
*
* @author Zafer Yilmaz <136199553+zaferyilmaznet@users.noreply.github.com>
* @version 1.0.0
* @since 2025-09-19
* @copyright 2025 Zafer Yilmaz
* @license MIT
*/
/*
Structure
1. handleCreateList → creates a list object, pushes it to lists, calls renderList.
2. renderList → creates a list container (div with title, buttons, input for new items, <ul> for items).
3. renderListItem → creates a single <li> with text, inline edit, and delete.
4. Data model → each list is { id, title, items: [ { id, text } ] }.
*/
// DOM References
let newListTitleInput = document.querySelector("#new-list-title-input");
const createNewListBtn = document.querySelector("#create-new-list-btn");
createNewListBtn.classList.add("primary");
const listsView = document.querySelector("#lists-view");
// Create a wrapper for controls (counter + clear all)
const controlsBar = document.createElement("div");
controlsBar.classList.add("flex-between");
controlsBar.id = "controls-bar";
// Counter
const counter = document.createElement("span");
controlsBar.appendChild(counter);
// Clear All button
const clearAllBtn = document.createElement("button");
clearAllBtn.textContent = "Clear All";
clearAllBtn.classList.add("danger");
clearAllBtn.addEventListener("click", handleClearAll);
controlsBar.appendChild(clearAllBtn);
// Insert controls before lists view
listsView.parentNode.insertBefore(controlsBar, listsView);
// Data Model
let lists = [];
const LIST_LIMIT = 100;
// Event Listeners
createNewListBtn.addEventListener("click", handleCreateList);
newListTitleInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
handleCreateList();
}
});
// Handlers
function handleCreateList() {
if (lists.length >= LIST_LIMIT) {
alert(`You can only create up to ${LIST_LIMIT} lists.`);
return;
}
const title = newListTitleInput.value.trim();
if (!title) {
alert("Please enter a title.");
return;
}
const newList = { id: Date.now(), title, items: [] };
lists.push(newList);
renderList(newList);
saveToLocalStorage();
updateCounter();
newListTitleInput.value = "";
newListTitleInput.blur(); // remove focus from the title input
// Move focus to the "Add item" input in the newly created list
const newListContainer = document.querySelector(
`.list-container[data-id="${newList.id}"]`
);
newListContainer?.querySelector('input[type="text"]')?.focus();
}
function handleClearAll() {
if (confirm("Are you sure you want to delete all lists?")) {
lists = [];
listsView.innerHTML = "";
saveToLocalStorage();
updateCounter();
}
}
// Rendering
function renderList(listObj) {
const container = document.createElement("div");
container.classList.add("list-container");
container.dataset.id = listObj.id;
// Title row
const titleRow = document.createElement("div");
titleRow.classList.add("flex-between");
const titleSpan = document.createElement("h3");
titleSpan.textContent = listObj.title;
titleSpan.style.color = getRandomPaletteColor(); // random vibe from curated palette
// Inline edit for title
const editTitleBtn = document.createElement("button");
editTitleBtn.textContent = "Edit Title";
editTitleBtn.classList.add("secondary");
editTitleBtn.addEventListener("click", () => {
if (editTitleBtn.textContent === "Edit Title") {
const input = document.createElement("input");
input.type = "text";
input.value = listObj.title;
titleRow.replaceChild(input, titleSpan);
editTitleBtn.textContent = "Save Title";
editTitleBtn.classList.remove("secondary");
editTitleBtn.classList.add("primary");
input.focus();
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") editTitleBtn.click();
});
} else {
const input = titleRow.querySelector("input");
const newTitle = input.value.trim();
if (newTitle) listObj.title = newTitle;
titleSpan.textContent = listObj.title;
titleRow.replaceChild(titleSpan, input);
editTitleBtn.textContent = "Edit Title";
editTitleBtn.classList.remove("primary");
editTitleBtn.classList.add("secondary");
}
saveToLocalStorage();
});
// Delete list
const deleteListBtn = document.createElement("button");
deleteListBtn.textContent = "Delete List";
deleteListBtn.classList.add("danger");
deleteListBtn.addEventListener("click", () => {
// Remove from data model
lists = lists.filter((l) => l.id !== listObj.id);
// Remove from DOM
container.remove();
saveToLocalStorage();
updateCounter();
});
const titleActions = document.createElement("div");
titleActions.appendChild(editTitleBtn);
titleActions.appendChild(deleteListBtn);
titleRow.appendChild(titleSpan);
titleRow.appendChild(titleActions);
container.appendChild(titleRow);
// Ul for items
const ul = document.createElement("ul");
container.appendChild(ul);
// Render existing items (important for localStorage load)
listObj.items.forEach((itemObj) => {
renderListItem(listObj.id, itemObj, ul);
});
// Add item input + button
const input = document.createElement("input");
input.type = "text";
input.placeholder = "Add item...";
container.appendChild(input);
const addItemBtn = document.createElement("button");
addItemBtn.type = "button";
addItemBtn.textContent = "Add Item";
addItemBtn.classList.add("primary");
container.appendChild(addItemBtn);
// Events for adding items
addItemBtn.addEventListener("click", () => {
handleAddItem(listObj.id, input, ul);
});
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
handleAddItem(listObj.id, input, ul);
}
});
// Append container
listsView.insertBefore(container, listsView.firstChild);
}
function handleAddItem(listId, input, ul) {
const itemText = input.value.trim();
if (!itemText) return;
// Update data model
const list = lists.find((l) => l.id === listId);
const newItem = { id: Date.now(), text: itemText };
list.items.push(newItem);
renderListItem(listId, newItem, ul);
saveToLocalStorage();
input.value = "";
}
// Render single item
function renderListItem(listId, itemObj, ul) {
const li = document.createElement("li");
li.dataset.id = itemObj.id;
const row = document.createElement("div");
row.classList.add("flex-between");
const span = document.createElement("span");
span.textContent = itemObj.text;
row.appendChild(span);
const actions = document.createElement("div");
// Edit item inline
const editBtn = document.createElement("button");
editBtn.textContent = "Edit";
editBtn.classList.add("secondary");
editBtn.addEventListener("click", () => {
if (editBtn.textContent === "Edit") {
const input = document.createElement("input");
input.type = "text";
input.value = itemObj.text;
row.replaceChild(input, span);
editBtn.textContent = "Save";
editBtn.classList.remove("secondary");
editBtn.classList.add("primary");
input.focus();
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") editBtn.click();
});
} else {
const input = row.querySelector("input");
const newText = input.value.trim();
if (newText) itemObj.text = newText;
span.textContent = itemObj.text;
row.replaceChild(span, input);
editBtn.textContent = "Edit";
editBtn.classList.remove("primary");
editBtn.classList.add("secondary");
}
saveToLocalStorage();
});
actions.appendChild(editBtn);
// Delete item
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.classList.add("danger");
deleteBtn.addEventListener("click", () => {
const list = lists.find((l) => l.id === listId);
list.items = list.items.filter((it) => it.id !== itemObj.id);
li.remove();
saveToLocalStorage();
});
actions.appendChild(deleteBtn);
row.appendChild(actions);
li.appendChild(row);
ul.appendChild(li);
}
// Helpers for saving to and loading from localStorage
function saveToLocalStorage() {
localStorage.setItem("lists", JSON.stringify(lists));
}
function loadFromLocalStorage() {
const stored = localStorage.getItem("lists");
if (stored) {
lists = JSON.parse(stored);
}
}
function updateCounter() {
const footer = document.querySelector("#app-footer");
if (lists.length > 0) {
controlsBar.style.display = "flex";
counter.textContent = `${lists.length} / ${LIST_LIMIT} lists`;
footer.hidden = false; // show footer
} else {
controlsBar.style.display = "none";
footer.hidden = true; // hide footer
}
}
// Page Load
loadFromLocalStorage();
listsView.innerHTML = ""; // clear old UI before rendering
lists.forEach((listObj) => renderList(listObj));
updateCounter();
// Random color generator
function getRandomPaletteColor() {
const palette = [
"#1e57e1", // primary blue
"#2a9d8f", // teal
"#264653", // deep green/blue
"#457b9d", // soft blue
"#8a2be2", // muted violet
"#6a4c93", // dusty purple
"#e76f51", // terracotta
"#f4a261", // muted orange
"#e63946", // soft red
"#2c3e50", // deep navy
"#7d8ca3", // gray-blue
"#b56576", // muted rose
"#bc6c25", // earthy brown/orange
"#118ab2", // calm cyan
];
return palette[Math.floor(Math.random() * palette.length)];
}
/* function getRandomColor() {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
// Convert each to hex and pad with 0 if needed
let hexR = r.toString(16).padStart(2, "0");
let hexG = g.toString(16).padStart(2, "0");
let hexB = b.toString(16).padStart(2, "0");
let hexColor = "#" + hexR + hexG + hexB;
return hexColor;
} */