-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (116 loc) · 4.46 KB
/
script.js
File metadata and controls
147 lines (116 loc) · 4.46 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
/*************************************************************************
* Create Note Popup Logic
**************************************************************************/
function popup() {
const popupContainer = document.createElement("div");
popupContainer.innerHTML = `
<div id="popupContainer">
<h1>New Note</h1>
<textarea id="note-text" placeholder="Enter your note..."></textarea>
<div id="btn-container">
<button id="submitBtn" onclick="createNote()">Create Note</button>
<button id="closeBtn" onclick="closePopup()">Close</button>
</div>
</div>
`;
document.body.appendChild(popupContainer);
}
function closePopup() {
const popupContainer = document.getElementById("popupContainer");
if(popupContainer) {
popupContainer.remove();
}
}
function createNote() {
const popupContainer = document.getElementById('popupContainer');
const noteText = document.getElementById('note-text').value;
if (noteText.trim() !== '') {
const note = {
id: new Date().getTime(),
text: noteText
};
const existingNotes = JSON.parse(localStorage.getItem('notes')) || [];
existingNotes.push(note);
localStorage.setItem('notes', JSON.stringify(existingNotes));
document.getElementById('note-text').value = '';
popupContainer.remove();
displayNotes();
}
}
/*************************************************************************
* Display Notes Logic
**************************************************************************/
function displayNotes() {
const notesList = document.getElementById('notes-list');
notesList.innerHTML = '';
v
const notes = JSON.parse(localStorage.getItem('notes')) || [];
notes.forEach(note => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<span>${note.text}</span>
<div id="noteBtns-container">
<button id="editBtn" onclick="editNote(${note.id})"><img src="bxs-pencil.svg"></button>
<button id="deleteBtn" onclick="deleteNote(${note.id})"><img src="bxs-trash.svg"></button>
</div>
`;
notesList.appendChild(listItem);
});
}
/*************************************************************************
* Edit Note Popup Logic
**************************************************************************/
function editNote(noteId) {
const notes = JSON.parse(localStorage.getItem('notes')) || [];
const noteToEdit = notes.find(note => note.id == noteId);
const noteText = noteToEdit ? noteToEdit.text : '';
const editingPopup = document.createElement("div");
editingPopup.innerHTML = `
<div id="editing-container" data-note-id="${noteId}">
<h1>Edit Note</h1>
<textarea id="note-text">${noteText}</textarea>
<div id="btn-container">
<button id="submitBtn" onclick="updateNote()">Done</button>
<button id="closeBtn" onclick="closeEditPopup()">Cancel</button>
</div>
</div>
`;
document.body.appendChild(editingPopup);
}
function closeEditPopup() {
const editingPopup = document.getElementById("editing-container");
if(editingPopup) {
editingPopup.remove();
}
}
function updateNote() {
const noteText = document.getElementById('note-text').value.trim();
const editingPopup = document.getElementById('editing-container');
if (noteText !== '') {
const noteId = editingPopup.getAttribute('data-note-id');
let notes = JSON.parse(localStorage.getItem('notes')) || [];
// Find the note to update
const updatedNotes = notes.map(note => {
if (note.id == noteId) {
return { id: note.id, text: noteText };
}
return note;
});
// Update the notes in local storage
localStorage.setItem('notes', JSON.stringify(updatedNotes));
// Close the editing popup
editingPopup.remove();
// Refresh the displayed notes
displayNotes();
}
}
/*************************************************************************
* Delete Note Logic
**************************************************************************/
function deleteNote(noteId) {
let notes = JSON.parse(localStorage.getItem('notes')) || [];
notes = notes.filter(note => note.id !== noteId);
localStorage.setItem('notes', JSON.stringify(notes));
displayNotes();
}
displayNotes();