-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
261 lines (224 loc) · 8.01 KB
/
scripts.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
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
window.entryAmount = 0;
document.addEventListener("DOMContentLoaded", function() {
document.querySelector(".entry-list-add").addEventListener("click", newEntry);
loadEntries()
const entries = document.querySelectorAll(".entry");
entries.forEach(entry => {
entry.addEventListener("click", selectEntry);
})
document.querySelector(".entry-delete").addEventListener("click", deleteEntry)
document.querySelector(".diary-tittle").addEventListener("input", changeEntryName)
document.querySelector(".diary-text").addEventListener("input", handleInputContent)
setupThemes()
});
let themes = [
"theme-gruvbox",
"theme-gruvbox-light",
"theme-dracula",
"theme-solarized-dark",
"theme-solarized-light",
"theme-nord",
"theme-material",
"theme-jone"
]
function setupThemes(){
const local_theme = localStorage.getItem("theme")
if (local_theme) {
changeTheme(local_theme)
}
const selector_list = document.getElementById("themes-selector-list")
themes.forEach(theme => {
const _li = document.createElement("li");
_li.innerHTML = theme;
_li.className = "themes-selector-item"
_li.addEventListener("click", changeThemeClick)
selector_list.appendChild(_li);
})
}
function changeTheme(theme) {
const body = document.getElementById("body")
body.className = theme
localStorage.setItem("theme", theme)
}
function changeThemeClick(){
const theme = this.innerHTML;
changeTheme(theme)
}
function newEntry() {
var entryList = document.querySelector(".entry-list")
const entry = document.createElement("div")
entry.className = "entry"
entry.addEventListener("click", selectEntry)
entry.setAttribute("data-index", entryAmount)
entryAmount++
const entryName = document.createElement("p")
entryName.className = "entry-name"
entryName.innerHTML = "New Entry"
const entryDate = document.createElement("p")
entryDate.className = "entry-date"
let date = new Date().toLocaleDateString()
entryDate.innerHTML = date
const entryDateLast = document.createElement("p")
entryDateLast.className = "entry-date-last"
entryDateLast.innerHTML = "Last modified: " + date
entry.appendChild(entryName);
entry.appendChild(entryDate);
entry.appendChild(entryDateLast);
entryList.appendChild(entry);
addEntryStorage(date);
}
function selectEntry() {
var entries = document.querySelectorAll(".entry");
entries.forEach(entry => {
entry.classList.remove("entry-selected");
});
this.classList.add("entry-selected");
const entryName = this.querySelector(".entry-name").innerHTML;
const entryIndex = this.getAttribute("data-index");
const entryContent = loadEntryContent(entryIndex);
const diaryTittle = document.querySelector(".diary-tittle");
diaryTittle.innerHTML = entryName;
const diaryText = document.querySelector(".diary-text");
diaryText.value = entryContent;
}
function deleteEntry() {
if (confirm("Are you sure you want to delete this entry?")) {
var selectedEntry = document.querySelector(".entry-selected");
deleteEntryStorage(selectedEntry.getAttribute("data-index"));
selectedEntry.remove();
const diaryTittle = document.querySelector(".diary-tittle");
diaryTittle.innerHTML = "";
const diaryText = document.querySelector(".diary-text");
diaryText.value = "";
}
entryAmount--;
var entries = document.querySelectorAll(".entry");
entries.forEach((entry, index) => {
entry.setAttribute("data-index", index);
});
}
function changeEntryName() {
const selectedEntry = document.querySelector(".entry-selected");
if (!selectedEntry) return;
const entryName = selectedEntry.querySelector(".entry-name");
const diaryTittle = document.querySelector(".diary-tittle");
entryName.innerHTML = diaryTittle.innerHTML;
const entryDate = selectedEntry.querySelector(".entry-date-last");
var date = new Date().toLocaleDateString();
entryDate.innerHTML = "Last modified: " + date;
saveEntry();
}
function handleInputContent() {
const selectedEntry = document.querySelector(".entry-selected");
if (!selectedEntry) return;
const entryDateLast = selectedEntry.querySelector(".entry-date-last");
var date = new Date().toLocaleDateString();
entryDateLast.innerHTML = "Last modified: " + date;
saveEntry();
}
function loadEntries() {
try {
const entries = JSON.parse(localStorage.getItem("entries"));
if (entries) {
entries.forEach(entry => {
var entryList = document.querySelector(".entry-list");
const newEntry = document.createElement("div");
newEntry.className = "entry";
newEntry.addEventListener("click", selectEntry);
newEntry.setAttribute("data-index", entryAmount);
const entryName = document.createElement("p");
entryName.className = "entry-name";
entryName.innerHTML = entry.name;
const entryDate = document.createElement("p");
entryDate.className = "entry-date";
entryDate.innerHTML = entry.date;
const entryDateLast = document.createElement("p");
entryDateLast.className = "entry-date-last";
entryDateLast.innerHTML = entry.datelast;
newEntry.appendChild(entryName);
newEntry.appendChild(entryDate);
newEntry.appendChild(entryDateLast);
entryList.appendChild(newEntry);
entryAmount++;
});
}
} catch (error) {
console.log(error);
return false;
}
}
function saveEntry() {
const selectedEntry = document.querySelector(".entry-selected");
const entryIndex = selectedEntry.getAttribute("data-index");
const entryName = selectedEntry.querySelector(".entry-name").innerHTML;
const entryDate = selectedEntry.querySelector(".entry-date").innerHTML;
const entryDateLast = selectedEntry.querySelector(".entry-date-last").innerHTML;
const entryContent = document.querySelector(".diary-text").value;
const entry = {
"index": entryIndex,
"name": entryName,
"date": entryDate,
"datelast": entryDateLast,
"content": entryContent
}
try {
var entries = JSON.parse(localStorage.getItem("entries"));
if (entries) {
entries[entryIndex] = entry;
localStorage.setItem("entries", JSON.stringify(entries));
} else {
localStorage.setItem("entries", JSON.stringify([entry]));
}
return true;
} catch (error) {
console.log(error);
return false;
}
}
function addEntryStorage(date) {
const entry = {
"name": "empty",
"date": date,
"datelast": "no modification",
"content": ""
}
try {
const entries = JSON.parse(localStorage.getItem("entries"));
if (entries) {
entries.push(entry);
localStorage.setItem("entries", JSON.stringify(entries));
} else {
localStorage.setItem("entries", JSON.stringify([entry]));
}
return true;
} catch (error) {
console.log(error);
return false;
}
}
function loadEntryContent(index) {
try {
const entries = JSON.parse(localStorage.getItem("entries"));
if (entries) {
return entries[index]["content"];
} else {
console.log("No entries found");
return "no content";
}
} catch (error) {
console.log(error);
}
}
function deleteEntryStorage(index) {
try {
const entries = JSON.parse(localStorage.getItem("entries"));
if (entries) {
entries.splice(index, 1);
localStorage.setItem("entries", JSON.stringify(entries));
} else {
console.log("No entries found");
}
} catch (error) {
console.log(error);
}
}