From 3034722b471ba6f6a9c690d2f1570455c048a815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20Cora=C3=A7=C3=A3o?= Date: Mon, 12 Dec 2022 22:34:28 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Taxonomy=20add=20likes=20and=20views?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/page.js | 106 ++++++++++++++++++++ config/_default/params.toml | 3 +- exampleSite/config/_default/params.toml | 1 + layouts/_default/taxonomy.html | 12 ++- layouts/partials/article-meta-taxonomy.html | 36 +++++++ 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 assets/js/page.js create mode 100644 layouts/partials/article-meta-taxonomy.html diff --git a/assets/js/page.js b/assets/js/page.js new file mode 100644 index 00000000..714b5d20 --- /dev/null +++ b/assets/js/page.js @@ -0,0 +1,106 @@ +var liked_page = false + +if (typeof auth !== 'undefined') { + var id = oid ? oid.replaceAll("/", "-") : oid + var viewed = localStorage.getItem(id); + + if (!viewed) { + auth.signInAnonymously() + .then(() => { + var docRef = db.collection('views').doc(id) + localStorage.setItem(id, true); + docRef.get().then((doc) => { + if (doc.exists) { + db.collection('views').doc(id).update({ + views: firebase.firestore.FieldValue.increment(1) + }); + } else { + db.collection('views').doc(id).set({ views: 1 }) + } + }).catch((error) => { + console.log("Error getting document:", error); + }); + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + console.error(errorCode, errorMessage) + }); + } + + var id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes + var liked = localStorage.getItem(id_likes); + + if (liked) { + liked_page = true + document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = "" + document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = "none" + document.querySelectorAll("span[id='likes_button_text']")[0].innerText = "" + } + +} + +function like_article(id_likes) { + auth.signInAnonymously() + .then(() => { + var docRef = db.collection('likes').doc(id_likes) + docRef.get().then((doc) => { + liked_page = true + localStorage.setItem(id_likes, true); + document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = "" + document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = "none" + document.querySelectorAll("span[id='likes_button_text']")[0].innerText = "" + if (doc.exists) { + db.collection('likes').doc(id_likes).update({ + likes: firebase.firestore.FieldValue.increment(1) + }); + } else { + db.collection('likes').doc(id_likes).set({ likes: 1 }) + } + }).catch((error) => { + console.log("Error getting document:", error); + }); + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + console.error(errorCode, errorMessage) + }); +} + +function remove_like_article(id_likes) { + auth.signInAnonymously() + .then(() => { + var docRef = db.collection('likes').doc(id_likes) + docRef.get().then((doc) => { + liked_page = false + localStorage.removeItem(id_likes); + document.querySelectorAll("span[id='likes_button_heart']")[0].style.display = "none" + document.querySelectorAll("span[id='likes_button_emtpty_heart']")[0].style.display = "" + document.querySelectorAll("span[id='likes_button_text']")[0].innerText = "\xa0Like" + if (doc.exists) { + db.collection('likes').doc(id_likes).update({ + likes: firebase.firestore.FieldValue.increment(-1) + }); + } else { + db.collection('likes').doc(id_likes).set({ likes: 0 }) + } + }).catch((error) => { + console.log("Error getting document:", error); + }); + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + console.error(errorCode, errorMessage) + }); +} + +function process_article() { + var id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes + if (!liked_page) { + like_article(id_likes) + } else { + remove_like_article(id_likes) + } +} \ No newline at end of file diff --git a/config/_default/params.toml b/config/_default/params.toml index c24a7cae..dd43b62c 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -82,11 +82,12 @@ enableCodeCopy = true excludedKinds = [] [taxonomy] - #showTermCount = true + showTermCount = true #showHero = true #heroStyle = "background" # valid options: basic, big, background, thumbAndBackground #layoutBackgroundBlur = true # only used when heroStyle equals background #layoutBackgroundHeaderSpace = false + showBreadcrumbs = false #showViews = false #showLikes = false #showTableOfContents = true diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 5fdc6350..bd63ad47 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -86,6 +86,7 @@ mainSections = ["docs"] heroStyle = "background" # valid options: basic, big, background, thumbAndBackground layoutBackgroundBlur = true # only used when heroStyle equals background layoutBackgroundHeaderSpace = false + showBreadcrumbs = false showViews = true showLikes = true showTableOfContents = true diff --git a/layouts/_default/taxonomy.html b/layouts/_default/taxonomy.html index 75bd293f..8c523d1d 100644 --- a/layouts/_default/taxonomy.html +++ b/layouts/_default/taxonomy.html @@ -1,15 +1,25 @@ {{ define "main" }}
- {{ if .Params.showBreadcrumbs | default (.Site.Params.list.showBreadcrumbs | default false) }} + {{ if .Params.showBreadcrumbs | default (.Site.Params.taxonomy.showBreadcrumbs | default false) }} {{ partial "breadcrumbs.html" . }} {{ end }}

{{ .Title }}

+
+ {{ partial "article-meta-taxonomy.html" (dict "context" . "scope" "single") }} +
{{ if .Content }}
{{ .Content | emojify }}
+ + {{ $jsPage := resources.Get "js/page.js" }} + {{ $jsPage = $jsPage | resources.Minify | resources.Fingerprint "sha512" }} +
{{ end }}
diff --git a/layouts/partials/article-meta-taxonomy.html b/layouts/partials/article-meta-taxonomy.html new file mode 100644 index 00000000..d8d0142e --- /dev/null +++ b/layouts/partials/article-meta-taxonomy.html @@ -0,0 +1,36 @@ +{{/* Determine the correct context and scope */}} +{{/* This allows for different logic depending on where the partial is called */}} +{{ $context := . }} +{{ $scope := default nil }} + +{{ if (reflect.IsMap . ) }} +{{ $context = .context }} +{{ $scope = cond (not .scope) nil .scope }} +{{ end }} + +{{ with $context }} +{{ $meta := newScratch }} + +{{/* Gather partials for this context */}} + +{{ if (.Params.showViews | default (.Site.Params.taxonomy.showViews | default false)) }} +{{ $meta.Add "partials" (slice (partial "meta/views.html" .)) }} +{{ end }} + +{{ if (.Params.showLikes | default (.Site.Params.taxonomy.showLikes | default false)) }} +{{ $meta.Add "partials" (slice (partial "meta/likes.html" .)) }} +{{ end }} + +{{ if and (eq $scope "single") (.Params.showLikes | default (.Site.Params.taxonomy.showLikes | default false)) }} +{{ $meta.Add "partials" (slice (partial "meta/likes_button.html" .)) }} +{{ end }} + + +
+ {{/* Output partials */}} + {{ with ($meta.Get "partials") }} + {{ delimit . "·" }} + {{ end }} +
+ +{{ end }} \ No newline at end of file