-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotesView.js
98 lines (80 loc) · 3.66 KB
/
NotesView.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
export default class NotesView {
constructor(root, { onNoteSelect, onNoteAdd, onNoteEdit, onNoteDelete } = {}) {
this.root = root;
this.onNoteSelect = onNoteSelect;
this.onNoteAdd = onNoteAdd;
this.onNoteEdit = onNoteEdit;
this.onNoteDelete = onNoteDelete;
this.root.innerHTML = `
<div class="notes__sidebar">
<button class="notes__add" type="button">Add Note</button>
<div class="notes__list"></div>
</div>
<div class="notes__preview">
<input class="notes__title" type="text" placeholder="New Note...">
<textarea class="notes__body">Take Note...</textarea>
</div>
`;
const btnAddNote = this.root.querySelector(".notes__add");
const inpTitle = this.root.querySelector(".notes__title");
const inpBody = this.root.querySelector(".notes__body");
btnAddNote.addEventListener("click", () => {
this.onNoteAdd();
});
[inpTitle, inpBody].forEach(inputField => {
inputField.addEventListener("blur", () => {
const updatedTitle = inpTitle.value.trim();
const updatedBody = inpBody.value.trim();
this.onNoteEdit(updatedTitle, updatedBody);
});
});
this.updateNotePreviewVisibility(false);
}
_createListItemHTML(id, title, body, updated) {
const MAX_BODY_LENGTH = 60;
return `
<div class="notes__list-item" data-note-id="${id}">
<div class="notes__small-title">${title}</div>
<div class="notes__small-body">
${body.substring(0, MAX_BODY_LENGTH)}
${body.length > MAX_BODY_LENGTH ? "..." : ""}
</div>
<div class="notes__small-updated">
${updated.toLocaleString(undefined, { dateStyle: "full", timeStyle: "short" })}
</div>
</div>
`;
}
updateNoteList(notes) {
const notesListContainer = this.root.querySelector(".notes__list");
// Empty list
notesListContainer.innerHTML = "";
for (const note of notes) {
const html = this._createListItemHTML(note.id, note.title, note.body, new Date(note.updated));
notesListContainer.insertAdjacentHTML("beforeend", html);
}
// Add select/delete events for each list item
notesListContainer.querySelectorAll(".notes__list-item").forEach(noteListItem => {
noteListItem.addEventListener("click", () => {
this.onNoteSelect(noteListItem.dataset.noteId);
});
noteListItem.addEventListener("dblclick", () => {
const doDelete = confirm("Are you sure you want to delete this note?");
if (doDelete) {
this.onNoteDelete(noteListItem.dataset.noteId);
}
});
});
}
updateActiveNote(note) {
this.root.querySelector(".notes__title").value = note.title;
this.root.querySelector(".notes__body").value = note.body;
this.root.querySelectorAll(".notes__list-item").forEach(noteListItem => {
noteListItem.classList.remove("notes__list-item--selected");
});
this.root.querySelector(`.notes__list-item[data-note-id="${note.id}"]`).classList.add("notes__list-item--selected");
}
updateNotePreviewVisibility(visible) {
this.root.querySelector(".notes__preview").style.visibility = visible ? "visible" : "hidden";
}
}