-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
141 lines (126 loc) · 4.53 KB
/
script.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
const itemsArray = localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')) : [];
document.querySelector("#enter").addEventListener("click", () => {
const item = document.querySelector("#item");
createItem(item);
item.value = ""; // Clear the input after creating the item
});
document.querySelector("#item").addEventListener("keypress", (e) => {
if (e.key === "Enter") {
const item = document.querySelector("#item");
createItem(item);
item.value = ""; // Clear the input after creating the item
}
});
function displayDate() {
let date = new Date();
date = date.toString().split(" ");
date = date[1] + " " + date[2] + " " + date[3];
const dateElement = document.querySelector("#date");
const characters = date.split("");
let index = 0;
const typingInterval = setInterval(() => {
dateElement.textContent += characters[index];
index++;
if (index === characters.length) {
clearInterval(typingInterval);
}
}, 100); // Adjust typing speed as needed
}
function displayItems() {
let items = "";
for (let i = 0; i < itemsArray.length; i++) {
items += `<div class="item">
<div class="input-controller">
<textarea disabled>${itemsArray[i]}</textarea>
<div class="edit-controller">
<i class="fa-solid fa-xmark deleteBtn"></i>
<i class="fa-solid fa-pen-to-square editBtn"></i>
</div>
</div>
<div class="update-controller">
<button class="saveBtn">Save</button>
<button class="cancelBtn">Cancel</button>
</div>
<span class="task-time">${formatDate(new Date())}</span> <!-- Add task creation time -->
</div>`;
}
document.querySelector(".to-do-list").innerHTML = items;
activateDeleteListeners();
activateEditListeners();
activateSaveListeners();
activateCancelListeners();
}
function activateDeleteListeners() {
let deleteBtn = document.querySelectorAll(".deleteBtn");
deleteBtn.forEach((dB, i) => {
dB.addEventListener("click", () => {
deleteItem(i);
});
});
}
function activateEditListeners() {
const editBtn = document.querySelectorAll(".editBtn");
const updateController = document.querySelectorAll(".update-controller");
const inputs = document.querySelectorAll(".input-controller textarea");
editBtn.forEach((eB, i) => {
eB.addEventListener("click", () => {
updateController[i].style.display = "block";
inputs[i].disabled = false;
});
});
}
function activateSaveListeners() {
const saveBtn = document.querySelectorAll(".saveBtn");
const inputs = document.querySelectorAll(".input-controller textarea");
saveBtn.forEach((sB, i) => {
sB.addEventListener("click", () => {
updateItem(inputs[i].value, i);
});
});
}
function activateCancelListeners() {
const cancelBtn = document.querySelectorAll(".cancelBtn");
const updateController = document.querySelectorAll(".update-controller");
const inputs = document.querySelectorAll(".input-controller textarea");
cancelBtn.forEach((cB, i) => {
cB.addEventListener("click", () => {
updateController[i].style.display = "none";
inputs[i].disabled = true;
inputs[i].style.border = "none";
});
});
}
function createItem(item) {
itemsArray.push(item.value);
localStorage.setItem('items', JSON.stringify(itemsArray));
displayItems(); // Update the displayed items without reloading the page
}
function deleteItem(i) {
itemsArray.splice(i, 1);
localStorage.setItem('items', JSON.stringify(itemsArray));
displayItems(); // Update the displayed items without reloading the page
}
function updateItem(text, i) {
itemsArray[i] = text;
localStorage.setItem('items', JSON.stringify(itemsArray));
displayItems(); // Update the displayed items without reloading the page
}
// Function to format date as "Mon DD YYYY"
function formatDate(date) {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const month = months[date.getMonth()];
const day = date.getDate();
const year = date.getFullYear();
return `${month} ${day} ${year}`;
}
// Add event listener to dynamically adjust text area height
document.querySelectorAll('.input-controller textarea').forEach(textarea => {
textarea.addEventListener('input', () => {
textarea.style.height = 'auto';
textarea.style.height = textarea.scrollHeight + 'px';
});
});
window.onload = function () {
displayDate();
displayItems();
};