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