aboutsummaryrefslogtreecommitdiff
path: root/src/static/comments.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/static/comments.js')
-rw-r--r--src/static/comments.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/static/comments.js b/src/static/comments.js
new file mode 100644
index 0000000..c8234d2
--- /dev/null
+++ b/src/static/comments.js
@@ -0,0 +1,63 @@
1var firebaseConfig = {
2 apiKey: "AIzaSyD3E0XtiUJI4-JIxcIPZziNLGVaTdojz20",
3 authDomain: "mitja-felicijan-blog.firebaseapp.com",
4 databaseURL: "https://mitja-felicijan-blog.firebaseio.com",
5 projectId: "mitja-felicijan-blog",
6 storageBucket: "mitja-felicijan-blog.appspot.com",
7 messagingSenderId: "41650892882",
8 appId: "1:41650892882:web:b308f0a9c47198bdf7ef8b"
9};
10firebase.initializeApp(firebaseConfig);
11
12var database = firebase.database();
13var docPath = 'comments' + window.location.pathname.replace('.html', '');
14var submit = document.querySelector('#submit');
15var comments = document.querySelector('.comments ul');
16var textName = document.querySelector('#name');
17var textComment = document.querySelector('#comment');
18var ref = firebase.database().ref(docPath);
19
20function encodeHTML(s) {
21 return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
22}
23
24ref.on("value", function (snapshot) {
25 comments.innerHTML = '';
26 var commentList = Array();
27
28 // generating normal array
29 snapshot.forEach(function (item) {
30 commentList.push(item.val())
31 });
32
33 // rendering html
34 commentList.reverse().forEach(function (item) {
35 var liItem = `<li>
36 <div><b>${encodeHTML(item.name)}</b> - ${encodeHTML(item.published)}</div>
37 <div>${encodeHTML(item.comment)}</div>
38 </li>`;
39 comments.innerHTML += liItem;
40 });
41
42}, function (errorObject) {
43 console.log("The read failed: " + errorObject.code);
44});
45
46submit.addEventListener('click', function (evt) {
47 if (textName.value && textComment.value) {
48 submit.disabled = true;
49 firebase.database().ref(docPath + '/' + Date.now()).set({
50 name: textName.value,
51 comment: textComment.value,
52 published: new Date().toISOString().slice(0, 16).replace('T', ' '),
53 }, function (error) {
54 if (error) {
55 alert('Data could not be saved.' + error);
56 } else {
57 textName.value = '';
58 textComment.value = '';
59 submit.disabled = false;
60 }
61 });
62 }
63});