diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2019-10-30 00:32:50 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2019-10-30 00:32:50 +0100 |
| commit | 5471d723a030705c349c1633faf134b137bc9a5a (patch) | |
| tree | 54942790b44a79e5a21bf95cb23da3de24cc660d /src/static/comments.js | |
| parent | 408e00616d219601db45776409e54ffa579e431c (diff) | |
| download | mitjafelicijan.com-5471d723a030705c349c1633faf134b137bc9a5a.tar.gz | |
Added comment curation tool available at /comments.html
Diffstat (limited to 'src/static/comments.js')
| -rw-r--r-- | src/static/comments.js | 76 |
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 = { | |||
| 10 | firebase.initializeApp(firebaseConfig); | 10 | firebase.initializeApp(firebaseConfig); |
| 11 | 11 | ||
| 12 | var database = firebase.database(); | 12 | var database = firebase.database(); |
| 13 | var docPath = 'comments' + window.location.pathname.replace('.html', ''); | 13 | var docPath = window.location.hostname.replace('.', '-') + '/comments' + window.location.pathname.replace('.html', ''); |
| 14 | var submit = document.querySelector('#submit'); | 14 | var submit = document.querySelector('#submit'); |
| 15 | var comments = document.querySelector('.comments ul'); | 15 | var comments = document.querySelector('.comments ul'); |
| 16 | var textName = document.querySelector('#name'); | 16 | var textName = document.querySelector('#name'); |
| @@ -21,43 +21,45 @@ function encodeHTML(s) { | |||
| 21 | return s.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"'); | 21 | return s.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"'); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | ref.on("value", function (snapshot) { | 24 | if (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 | ||
| 46 | submit.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 | } | ||
