-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (119 loc) · 3.41 KB
/
index.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
var $noteTitle = $(".note-title");
var $noteText = $(".note-textarea");
var $saveNoteBtn = $(".save-note");
var $newNoteBtn = $(".new-note");
var $noteList = $(".list-container .list-group");
// activeNote is used to keep track of the note in the textarea
var activeNote = {};
// A function for getting all notes from the db
var getNotes = function() {
return $.ajax({
url: "/api/notes",
method: "GET"
});
};
// A function for saving a note to the db
var saveNote = function(note) {
return $.ajax({
url: "/api/notes",
data: note,
method: "POST"
});
};
// A function for deleting a note from the db
var deleteNote = function(id) {
return $.ajax({
url: "api/notes/" + id,
method: "DELETE"
});
};
// If there is an activeNote, display it, otherwise render empty inputs
var renderActiveNote = function() {
$saveNoteBtn.hide();
if (activeNote.id) {
$noteTitle.attr("readonly", true);
$noteText.attr("readonly", true);
$noteTitle.val(activeNote.title);
$noteText.val(activeNote.text);
} else {
$noteTitle.attr("readonly", false);
$noteText.attr("readonly", false);
$noteTitle.val("");
$noteText.val("");
}
};
// Get the note data from the inputs, save it to the db and update the view
var handleNoteSave = function() {
var newNote = {
title: $noteTitle.val(),
text: $noteText.val()
};
saveNote(newNote).then(function(data) {
getAndRenderNotes();
renderActiveNote();
});
};
// Delete the clicked note
var handleNoteDelete = function(event) {
// prevents the click listener for the list from being called when the button inside of it is clicked
event.stopPropagation();
var note = $(this)
.parent(".list-group-item")
.data();
if (activeNote.id === note.id) {
activeNote = {};
}
deleteNote(note.id).then(function() {
getAndRenderNotes();
renderActiveNote();
});
};
// Sets the activeNote and displays it
var handleNoteView = function() {
activeNote = $(this).data();
renderActiveNote();
};
// Sets the activeNote to and empty object and allows the user to enter a new note
var handleNewNoteView = function() {
activeNote = {};
renderActiveNote();
};
// If a note's title or text are empty, hide the save button
// Or else show it
var handleRenderSaveBtn = function() {
if (!$noteTitle.val().trim() || !$noteText.val().trim()) {
$saveNoteBtn.hide();
} else {
$saveNoteBtn.show();
}
};
// Render's the list of note titles
var renderNoteList = function(notes) {
$noteList.empty();
var noteListItems = [];
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
var $li = $("<li class='list-group-item'>").data(note);
var $span = $("<span>").text(note.title);
var $delBtn = $(
"<i class='fas fa-trash-alt float-right text-danger delete-note'>"
);
$li.append($span, $delBtn);
noteListItems.push($li);
}
$noteList.append(noteListItems);
};
// Gets notes from the db and renders them to the sidebar
var getAndRenderNotes = function() {
return getNotes().then(function(data) {
renderNoteList(data);
});
};
$saveNoteBtn.on("click", handleNoteSave);
$noteList.on("click", ".list-group-item", handleNoteView);
$newNoteBtn.on("click", handleNewNoteView);
$noteList.on("click", ".delete-note", handleNoteDelete);
$noteTitle.on("keyup", handleRenderSaveBtn);
$noteText.on("keyup", handleRenderSaveBtn);
// Gets and renders the initial list of notes
getAndRenderNotes();