-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhome.js
More file actions
281 lines (246 loc) · 9.67 KB
/
home.js
File metadata and controls
281 lines (246 loc) · 9.67 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var firebaseConfig = {
apiKey: "AIzaSyBFkmspKoi-RDOMZfL-k9__70YIaS7vLn0",
authDomain: "notedemo-a1e15.firebaseapp.com",
projectId: "notedemo-a1e15",
storageBucket: "notedemo-a1e15.appspot.com",
messagingSenderId: "641128485554",
appId: "1:641128485554:web:db3794ada96d08b1ea2fab"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
const dbRef = firebase.database()
const auth = firebase.auth();
let fsignout = 0;
let flagEdit = 0;
var editIndex = -1;
//Authentication function to check if any user is logged in
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("Welcome to notes app. This is app.js");
getNotesForThisUser().then(notes => display(notes)).catch(err => handleError(err))
} else if (fsignout == 0) {
window.alert("You need to LogIn First.Redirecting to Home Page.");
window.location = "index.html";
}
});
//dipslay
//Clears the contents
let addBtn = document.getElementById("addBtn");
let clearBtn = document.getElementById("clearAll")
clearBtn.addEventListener("click", e => {
window.location.reload();
})
// If user adds a note, add it to the Firebase
addBtn.addEventListener("click", function (e) {
document.getElementById("addTxt").value = CKEDITOR.instances["addTxt"].getData(); //getting data from editor
if (document.getElementById("addTxt").value != "" && document.getElementById("addTitle").value != "" && flagEdit === 0) {
saveNoteWithTitle(document.getElementById("addTitle").value, document.getElementById("addTxt").value)
.then(async res => {
let notes = await getNotesForThisUser()
display(notes)
document.getElementById("addTxt").value = "";
document.getElementById("addTitle").value = "";
CKEDITOR.instances["addTxt"].setData('');
})
.catch(err => console.log(err))
} else if (flagEdit === 1) {
if (document.getElementById("addTxt").value != "" && document.getElementById("addTitle").value != "") {
updateNote(localStorage.getItem("uid"), editIndex, document.getElementById("addTxt").value, document.getElementById("addTitle").value);
getNotesForThisUser().then(notes => display(notes)).catch(err => handleError(err));
}
else {
if (document.getElementById("addTitle").value == "")
window.alert("Warning: Empty Note Title")
else
window.alert("Warning: Empty Note Content")
}
document.getElementById("addTxt").value = '';
document.getElementById("addTitle").value = '';
CKEDITOR.instances["addTxt"].setData('');
flagEdit = 0;
editIndex = -1;
replaceButtonText('addBtn', 'Add Note');
}
else {
if (document.getElementById("addTitle").value == "")
window.alert("Warning: Empty Note Title")
else
window.alert("Warning: Empty Note Content")
}
});
// Function to show elements from Firebase
function display(notes) {
let i = 1;
let html = "";
Object.keys(notes).forEach(function (k, i) {
if (notes[k].title != undefined) {
html += `
<div class="noteCard my-2 mx-2 card" content="centre" style="width:100%;" >
<div class="card-body" id="note-card">
<h5 class="card-title">${notes[k].title}</h5>
<p class="card-text" > ${notes[k].content}</p>
<button id="${k}" onclick="deleteNote(this.id)" class="btn btn-danger">Delete Note</button>
<button id="${k}" onclick="editNote(this.id)" class="btn btn-info">Edit Note</button>
</div>
<div class="card-footer text-muted" style="font: italics;">
Last Updated: ${notes[k].lastUpdated}
</div>
</div>`;
}
else {
html += `
<div class="noteCard my-2 mx-2 card" content="centre" style="width:100%;" >
<div class="card-body" id="note-card">
<h5 class="card-title">Note ${i + 1} (You can Now edit Title)</h5>
<p class="card-text" > ${notes[k]}</p>
<button id="${k}" onclick="deleteNote(this.id)" class="btn btn-danger">Delete Note</button>
<button id="${k}" onclick="editNote(this.id)" class="btn btn-info">Edit Note</button>
</div>
</div>`;
}
i++
});
let notesElm = document.getElementById("notes");
if (i > 0)
notesElm.innerHTML = html;
else
notesElm.innerHTML = `Nothing to show! Use "Add a Note" section above to add notes.`;
return;
}
//function to write data
function updateNote(userId, noteId, newBody, newTitle) {
var d = new Date(Date.now())
var lastUpdated = d.getDate() + '.' + (d.getMonth() + 1) + '.' + d.getFullYear() + ',' + d.getHours() + ':' + d.getMinutes();
let updates = {
['/notes/' + userId + '/' + noteId + '/content']: newBody,
['/notes/' + userId + '/' + noteId + '/title']: newTitle,
['/notes/' + userId + '/' + noteId + '/lastUpdated']: lastUpdated
}
return firebase.database().ref().update(updates, (e) => {
if (e) console.log(e)
})
}
//Function to edit a note
function editNote(index) {
const uid = localStorage.getItem("uid")
window.scrollTo(0, 0);
var adaRef = firebase.database().ref('notes/' + uid + '/' + index);
adaRef.get().then((snapshot) => {
if (snapshot.exists()) {
var crrntNote = snapshot.val();
document.getElementById("addTitle").value = crrntNote.title;
document.getElementById("addTxt").value = crrntNote.content;
CKEDITOR.instances["addTxt"].setData(crrntNote);
replaceButtonText('addBtn', 'Update Note');
flagEdit = 1;
editIndex = index;
}
}).catch((error) => {
console.error(error);
});
}
// Function to delete a note
function deleteNote(index) {
const uid = localStorage.getItem("uid")
var adaRef = firebase.database().ref('notes/' + uid + '/' + index);
adaRef.remove()
.then(function () {
getNotesForThisUser().then(notes => display(notes)).catch(err => handleError(err))
CKEDITOR.instances["addTxt"].setData('');
})
.catch(function (error) {
console.log("Remove failed: " + error.message)
});
}
function replaceButtonText(buttonId, text) {
if (document.getElementById) {
var button = document.getElementById(buttonId);
if (button) {
if (button.childNodes[0]) {
button.childNodes[0].nodeValue = text;
}
else if (button.value) {
button.value = text;
}
else //if (button.innerHTML)
{
button.innerHTML = text;
}
}
}
}
let search = document.getElementById('searchTxt');
search.addEventListener("input", function () {
let inputVal = search.value.toLowerCase();
// console.log('Input event fired!', inputVal);
let noteCards = document.getElementsByClassName('noteCard');
Array.from(noteCards).forEach(function (element) {
let cardTxt = element.getElementsByClassName("card-title")[0].innerText;
if (cardTxt.toLowerCase().includes(inputVal)) {
element.style.display = "block";
} else {
element.style.display = "none";
}
// console.log(cardTxt);
})
})
//Saving Note with title
async function saveNoteWithTitle(title, content) {
const uid = localStorage.getItem("uid")
var d = new Date(Date.now())
var lastUpdated = d.getDate() + '.' + (d.getMonth() + 1) + '.' + d.getFullYear() + ',' + d.getHours() + ':' + d.getMinutes();
const userNoteRef = dbRef.ref('notes/' + uid)
let upd =
{
title,
content,
lastUpdated
}
return await userNoteRef.update({
[Date.now()]: upd
})
}
/*
async function saveNoteToFirebase(content) {
const uid = localStorage.getItem("uid")
//window.alert("Successfully Added");
const userNoteRef = dbRef.ref('notes/' + uid)
let notes = await getNotesForThisUser()
return await userNoteRef.set({
...notes,
[Date.now()]: content
})
}*/
async function getNotesForThisUser() {
const uid = localStorage.getItem("uid")
const userNotesRef = dbRef.ref('notes/' + uid)
const ss = await userNotesRef.get()
if (ss.exists())
return ss.val()
else {
let notesElm = document.getElementById("notes");
notesElm.innerHTML = `Nothing to show! Use "Add a Note" section above to add notes.`;
return;
}
}
//LogOut Function
document.querySelector('#signout').addEventListener('click', function () {
auth.signOut()
.then(() => {
// Sign-out successful.
window.alert("You have Logged Out Succesfully.");
window.location = "index.html";
fsignout = 1;
}).catch((error) => {
// An error happened.
alert(error.message);
});
});
/*
TODO: Further Features:
1. Add Title
2. Mark a note as Important
3. Separate notes by user
4. Sync and host to web server
*/