-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-functions.js
More file actions
108 lines (95 loc) · 2.71 KB
/
notes-functions.js
File metadata and controls
108 lines (95 loc) · 2.71 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
"use strict";
// Read existing notes from localStorage
const getSavedNotes = () => {
const notesJSON = localStorage.getItem("notes");
try {
return notesJSON ? JSON.parse(notesJSON) : [];
} catch (e) {
return [];
}
};
// Save the notes to localStorage
const saveNotes = (notes) => {
localStorage.setItem("notes", JSON.stringify(notes));
};
//Remove note from the list
const removeNote = (id) => {
const noteIndex = notes.findIndex((note) => note.id === id);
if (noteIndex > -1) {
notes.splice(noteIndex, 1);
}
};
// Generate the DOM structure for a note
const generateNoteDOM = (note) => {
const noteEl = document.createElement("div");
noteEl.classList.add("app-content__notes-item");
const textEl = document.createElement("a");
textEl.classList.add("app-content__notes-item__name");
const button = document.createElement("button");
button.classList.add("app-content__notes-item__button-remove");
button.addEventListener("click", () => {
removeNote(note.id);
saveNotes(notes);
renderNotes(notes, filters);
});
// Setup the remove note button
// Setup the note title text
if (note.title.length > 0) {
textEl.textContent = note.title;
} else {
textEl.textContent = "Unnamed note";
}
textEl.setAttribute("href", `/notes-app/edit.html#${note.id}`);
noteEl.appendChild(textEl);
noteEl.appendChild(button);
return noteEl;
};
const sortNotes = (notes, sortBy) => {
if (sortBy === "byEdited") {
return notes.sort((a, b) => {
if (a.updatedAt > b.updatedAt) {
return -1;
} else if (a.updatedAt < b.updatedAt) {
return 1;
} else {
return 0;
}
});
} else if (sortBy === "byCreated") {
return notes.sort((a, b) => {
if (a.createdAt > b.createdAt) {
return -1;
} else if (a.createdAt < b.createdAt) {
return 1;
} else {
return 0;
}
});
} else if (sortBy === "alphabetical") {
return notes.sort((a, b) => {
if (a.title.toLowerCase() < b.title.toLowerCase()) {
return -1;
} else if (a.title.toLowerCase() > b.title.toLowerCase()) {
return 1;
} else {
return 0;
}
});
} else {
return notes;
}
};
// Render application notes
const renderNotes = (notes, filters) => {
notes = sortNotes(notes, filters.sortBy);
const filteredNotes = notes.filter((note) =>
note.title.toLowerCase().includes(filters.searchText.toLowerCase())
);
document.querySelector("#notes").innerHTML = "";
filteredNotes.forEach((note) => {
const noteEl = generateNoteDOM(note);
document.querySelector("#notes").appendChild(noteEl);
});
};
const generateLastEdited = (timeStamp) =>
`Last edited ${moment(timeStamp).fromNow()}`;