From b9cf0c4835fc5e63c1ca7f22e8e3656248057926 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Tue, 11 Jul 2023 11:30:20 -0400 Subject: [PATCH] seperated comment paging logic. fixes #29 fixes #30 --- public/style.css | 3 +++ public/utils.js | 44 ++++++++++++++++++++++++++++++++-------- templates/frontpage.html | 4 ++-- templates/main.html | 13 ++++++++---- templates/xhr.html | 2 ++ 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/public/style.css b/public/style.css index 2b8d148..b61b969 100644 --- a/public/style.css +++ b/public/style.css @@ -352,6 +352,9 @@ form.nsfw div { .gray { color: #808080; } +.loading { + color: red !important; +} .error { color: red; font-size: 13px; diff --git a/public/utils.js b/public/utils.js index f1ca97d..be30024 100644 --- a/public/utils.js +++ b/public/utils.js @@ -105,6 +105,28 @@ function commentClick(e) { return false } } + +function loadMoreComments(e) { + e.preventDefault() + page = e.target.getAttribute("data-page") + var urlParams = new URLSearchParams(window.location.search); + urlParams.set("xhr", "1") + urlParams.set("page", page) + e.target.innerHTML = "loading" + e.target.className = "loading" + request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "", + function(res){ + if (res.trim()) { + e.target.parentNode.outerHTML = res + '
load more comments
' + setup() + } else { + e.target.parentNode.outerHTML = "" + } + }, function() { + e.target.innerHTML = "loading failed" + }) + return false; +} function loadMore(e) { e.preventDefault() page = e.target.getAttribute("data-page") @@ -281,6 +303,9 @@ function setup() { if (hidechildren = document.getElementById("hidechildren")){ hidechildren.addEventListener("click", hideAllChildComments) } + if (lmc = document.getElementById("lmc")){ + lmc.addEventListener("click", loadMoreComments) + } var posts = document.getElementsByClassName("post") for (var i = 0; i < posts.length; i++) { posts[i].addEventListener("click", postClick) @@ -313,17 +338,20 @@ if (localStorage.getItem("endlessScrolling") == "true") { loadmore.className = "show" loadmore.addEventListener("click", loadMore) } - - if (localStorage.getItem("autoLoad") == "true") { - window.onscroll = function(e) { - if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) { - var loadmore = document.getElementById("loadmore") - if (loadmore) { +} +if (localStorage.getItem("autoLoad") == "true") { + window.onscroll = function(e) { + if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) { + if (localStorage.getItem("endlessScrolling") == "true") { + if (loadmore = document.getElementById("loadmore")) { loadmore.click() } } - }; - } + if (lmc = document.getElementById("lmc")) { + lmc.click() + } + } + }; } // delete cookies without HTTPOnly diff --git a/templates/frontpage.html b/templates/frontpage.html index 39ec210..26509ad 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -2,7 +2,7 @@ {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - + @@ -56,6 +56,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index 7b3b97d..b6596b6 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,7 +3,7 @@ {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - +