-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
239 lines (215 loc) · 6.7 KB
/
script.js
File metadata and controls
239 lines (215 loc) · 6.7 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
let notes = [];
let notetitlezs = [];
let trash = [];
let archive = [];
function renderPage() {
load();
let newNote = document.getElementById("newNote");
let archiveDiv = document.getElementById("archiveDiv");
let cardContainer = document.getElementById("cardContainer");
let trashCan = document.getElementById("trashCan");
generateCardContainer();
generateArchive();
generateTrashCan();
save();
}
// add new note to array
function add() {
let titlez = document.getElementById("titlezInput").value;
let note = document.getElementById("noteInput").value;
if (titlez === "" || note === "") {
alert("Both Title and Note must be filled out!");
return;
}
notetitlezs.push(titlez);
notes.push(note);
save();
renderPage();
}
// move to archive
function moveToArchive(i) {
let archiveTitlez = notetitlezs[i];
let archiveNote = notes[i];
notetitlezs.splice(i, 1);
notes.splice(i, 1);
archive.push({ title: archiveTitlez, note: archiveNote });
save();
renderPage();
}
// move from archive to bin
function moveToTrash(i) {
let archiveTrash = [];
archiveTrash = archive[i];
trash.push(archiveTrash);
archive.splice(i, 1);
save();
renderPage();
}
// restore to notes
function restoreToNotes(i) {
let archiveItem = archive[i];
notetitlezs.push(archiveItem.title);
notes.push(archiveItem.note);
archive.splice(i, 1);
save();
renderPage();
}
// restore from trash to notes
function restoreFromTrash(i) {
let archiveItem = trash[i];
notetitlezs.push(archiveItem.title);
notes.push(archiveItem.note);
trash.splice(i, 1);
save();
renderPage();
}
// delete note from archive//trash
function deleteNote(i) {
trash.splice(i, 1);
save();
renderPage();
}
// save note in localStorage
function save() {
let noteAsText = JSON.stringify(notes);
let notetitlezAsText = JSON.stringify(notetitlezs);
let archiveAsText = JSON.stringify(archive);
let trashAsText = JSON.stringify(trash);
localStorage.setItem("notes", noteAsText);
localStorage.setItem("notetitlezs", notetitlezAsText);
localStorage.setItem("archive", archiveAsText);
localStorage.setItem("trash", trashAsText);
}
// load from localStorage
function load() {
let noteAsText = localStorage.getItem("notes");
let notetitlezAsText = localStorage.getItem("notetitlezs");
let archiveAsText = localStorage.getItem("archive");
let trashAsText = localStorage.getItem("trash");
if (noteAsText && notetitlezAsText) {
notes = JSON.parse(noteAsText);
notetitlezs = JSON.parse(notetitlezAsText);
}
if (archiveAsText) {
archive = JSON.parse(archiveAsText);
}
if (trashAsText) {
trash = JSON.parse(trashAsText);
} else {
trash = [];
}
}
// Note open as Popup
function openNote(i) {
document.getElementById("openCard").classList.remove("d-none");
document.getElementById("openNoteText").innerHTML = `
<h2>${notetitlezs[i]}</h2>
<p>${notes[i]}</p>`;
}
// close popup note
function closeNote() {
document.getElementById("openCard").classList.add("d-none");
}
// getting the input field
function addButtonFuction() {
newNote.innerHTML += `
<div class="inputBox">
<input id="titlezInput" type="text" placeholder="Title...">
<textarea id="noteInput" type="text" placeholder="Note..." "></textarea>
<div id="inputButtonDiv"><button id="inputButtonAdd" onclick="add()">Add</button></div>
</div>
`;
let checking = document.querySelector("textarea");
checking.addEventListener("keyup", (e) => {
if (e.keyCode === 13) {
add();
}
});
document.getElementById("addButton").classList.add("d-none");
}
// generate to container containing the note cards
function generateCardContainer() {
newNote.innerHTML = "";
newNote.innerHTML += `<img id="addButton" src="./img/add.svg" alt="Add Icon" onclick="addButtonFuction()"></img>`;
cardContainer.innerHTML = "";
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
const titlez = notetitlezs[i];
cardContainer.innerHTML += `
<div class="card" onclick="openNote(${i})">
<div class="left-card">
<b>${notetitlezs[i]}</b>
<p class="d-none">${notes[i]}</p>
</div>
<button id="archiveButton" class="" onclick="event.stopPropagation(); moveToArchive(${i})">Archive</button>
</div>
<div id="openCard" class="d-none" onclick="closeNote(${i})">
<div class="popUp-bg" >
<div id="openNoteText" class="notePopUp">
<h2>${notetitlezs[i]}</h2>
<p>${notes[i]}</p>
</div>
</div>
</div>
`;
}
}
// generate the archive
function generateArchive() {
archiveDiv.innerHTML = "";
archiveDiv.innerHTML += `<h2><img id="archiveIcon" src="./img/archiveIcon.svg" alt="archiveIcon" onclick="showHideArchive()"></h2>`;
archiveDiv.innerHTML += `<div id="archiveContent" class="archiveContent d-none"></div>`;
for (let i = 0; i < archive.length; i++) {
const archivedItem = archive[i];
const archivedTitle = archivedItem.title;
const archivedNote = archivedItem.note;
archiveContent.innerHTML += `
<div class="card">
<b>${archivedTitle}</b> <br />
<p class="d-none">${archivedNote}</p>
<button onclick="moveToTrash(${i})">
<img src="./img/trash.svg" alt="trash can" />
</button>
<button onclick="restoreToNotes(${i})">Restore</button>
</div>
`;
}
}
// generate the trash can
function generateTrashCan() {
trashCan.innerHTML = "";
trashCan.innerHTML += `<h2><img id="mainDivTrashIcon" src="./img/trash.svg" alt="trashCan" onclick="showHideTrash()"></h2>`;
trashCan.innerHTML += `<div id="trashContent" class="trashContent d-none"></div>`;
trashContent.innerHTML += `<div id="deleteAllButton"><button onclick="deleteAllTrash()">Empty Trash</button></div>`;
for (let i = 0; i < trash.length; i++) {
const archivedItem = trash[i];
const archivedTitle = archivedItem.title;
const archivedNote = archivedItem.note;
trashContent.innerHTML += `
<div class="card">
<b>${archivedTitle}</b> <br />
<p class="d-none">${archivedNote}</p>
<button onclick="deleteNote(${i})">
<img src="./img/trash.svg" alt="trash can" />
</button>
<button onclick="restoreFromTrash(${i})">Restore</button>
</div>
`;
}
}
// show and hide content of trash onclick on icon
function showHideTrash() {
let HideAndShow = document.getElementById("trashContent");
HideAndShow.classList.toggle("d-none");
}
// show and hide content of archive onclick on icon
function showHideArchive() {
let HideAndShow = document.getElementById("archiveContent");
HideAndShow.classList.toggle("d-none");
}
// empty trash
function deleteAllTrash() {
trash = [];
save();
renderPage();
}