aboutsummaryrefslogtreecommitdiff
path: root/src/static
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2019-10-25 01:49:59 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2019-10-25 01:49:59 +0200
commit52dd381304bf920eeced8985e8ab1d4cb1bc40a4 (patch)
tree90df088dfc7523e4ad0742d6773cea00366a6c08 /src/static
parentbc4ea1bd4874860f1abb22f0a6a1b4c305cbc77e (diff)
downloadmitjafelicijan.com-52dd381304bf920eeced8985e8ab1d4cb1bc40a4.tar.gz
Added simple commenting system with Firebase Database integration
Diffstat (limited to 'src/static')
-rw-r--r--src/static/comments.js63
-rw-r--r--src/static/style.css40
2 files changed, 100 insertions, 3 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});
diff --git a/src/static/style.css b/src/static/style.css
index b7fe7f7..f76fea0 100644
--- a/src/static/style.css
+++ b/src/static/style.css
@@ -1,3 +1,7 @@
1* {
2 box-sizing: border-box;
3}
4
1body { 5body {
2 line-height: 150%; 6 line-height: 150%;
3 margin-bottom: 100px; 7 margin-bottom: 100px;
@@ -39,14 +43,19 @@ img {
39} 43}
40 44
41ul.article-list li { 45ul.article-list li {
42 margin-bottom: 10px; 46 margin-bottom: 10px;
47}
48
49ul.article-list li div {
50 display: flex;
43} 51}
44 52
45ul.article-list time { 53ul.article-list time {
46 display: inline-block; 54 display: inline-block;
47 min-width: 120px; 55 min-width: 130px;
48} 56}
49 57
58
50article .info { 59article .info {
51 font-style: oblique; 60 font-style: oblique;
52} 61}
@@ -84,6 +93,31 @@ p.modified {
84 color: #000; 93 color: #000;
85} 94}
86 95
96
97.comments {
98 margin-top: 50px;
99}
100.comments ul {
101 margin-top: 30px;
102 padding-left: 15px;
103}
104.comments ul li {
105 margin-bottom: 20px;
106}
107.comments input,
108.comments textarea {
109 width: 100%;
110 padding: 10px;
111 margin-bottom: 10px;
112 font-family: inherit;
113 font-size: inherit;
114 border: 1px solid lightgray;
115}
116.comments textarea {
117 resize: vertical;
118 height: 100px;
119}
120
87@media only screen and (max-width:480px) { 121@media only screen and (max-width:480px) {
88 main { 122 main {
89 padding: 20px; 123 padding: 20px;