aboutsummaryrefslogtreecommitdiff
path: root/src/static/comments.js
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2020-03-25 05:12:40 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2020-03-25 05:12:40 +0100
commited161e7fb20a697ecba070ef7db4c231d700f245 (patch)
tree5a415f8e758c6b7001ade8003ca2aa26b476b58c /src/static/comments.js
parentfb94d7aef76a5996892ef82938524cb0c2e0d285 (diff)
downloadmitjafelicijan.com-ed161e7fb20a697ecba070ef7db4c231d700f245.tar.gz
Move to my own static generator
Diffstat (limited to 'src/static/comments.js')
-rw-r--r--src/static/comments.js65
1 files changed, 0 insertions, 65 deletions
diff --git a/src/static/comments.js b/src/static/comments.js
deleted file mode 100644
index 3f373e5..0000000
--- a/src/static/comments.js
+++ /dev/null
@@ -1,65 +0,0 @@
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 = window.location.hostname.replace('.', '-') + '/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
24if (submit) {
25 ref.on("value", function (snapshot) {
26 comments.innerHTML = '';
27 var commentList = Array();
28
29 // generating normal array
30 snapshot.forEach(function (item) {
31 commentList.push(item.val())
32 });
33
34 // rendering html
35 commentList.reverse().forEach(function (item) {
36 var liItem = `<li>
37 <div><b>${encodeHTML(item.name)}</b> - ${encodeHTML(item.published)}</div>
38 <div>${encodeHTML(item.comment)}</div>
39 </li>`;
40 comments.innerHTML += liItem;
41 });
42
43 }, function (errorObject) {
44 console.log("The read failed: " + errorObject.code);
45 });
46
47 submit.addEventListener('click', function (evt) {
48 if (textName.value && textComment.value) {
49 submit.disabled = true;
50 firebase.database().ref(docPath + '/' + Date.now()).set({
51 name: textName.value,
52 comment: textComment.value,
53 published: new Date().toISOString().slice(0, 16).replace('T', ' '),
54 }, function (error) {
55 if (error) {
56 alert('Data could not be saved.' + error);
57 } else {
58 textName.value = '';
59 textComment.value = '';
60 submit.disabled = false;
61 }
62 });
63 }
64 });
65}