From 3d23c96166fc3b6c2b14b16e99b42ede9a594bee Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Wed, 5 Jul 2023 18:34:46 -0400 Subject: [PATCH] load more button --- main.go | 2 +- public/style.css | 14 ++++++++++++-- public/utils.js | 18 ++++++++++++++++++ routes.go | 11 +++++++++-- state.go | 12 ++---------- templates/frontpage.html | 7 ++++++- templates/main.html | 9 +++++++-- templates/post.html | 4 ++-- templates/xhr.html | 2 +- 9 files changed, 58 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index cdd4cdc..75fce11 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ func init() { md = goldmark.New(goldmark.WithExtensions(extension.Linkify)) templates = make(map[string]*template.Template) if !*watch { - for _, name := range []string{"index.html", "login.html", "frontpage.html", "root.html", "settings.html"} { + for _, name := range []string{"index.html", "login.html", "frontpage.html", "root.html", "settings.html", "xhr.html"} { t := template.New(name).Funcs(funcMap) glob, err := t.ParseGlob("templates/*") if err != nil { diff --git a/public/style.css b/public/style.css index c72b7e0..4aaddd3 100644 --- a/public/style.css +++ b/public/style.css @@ -121,6 +121,9 @@ code { [disabled] { cursor: not-allowed; } +#loadmore [disabled] { + cursor: wait; +} .post .title { color: #888; font-size: 10px; @@ -357,6 +360,7 @@ form.nsfw div { } .pager { margin: 10px; + display: none; } .pager a { padding: 1px 4px; @@ -367,6 +371,9 @@ form.nsfw div { text-decoration: none; color: #369; } +#loadmore { + margin: 10px 0px; +} .buttons li { display: inline; } @@ -457,14 +464,14 @@ form.nsfw div { display: inline-block; overflow: hidden; resize: both; - width: 100%; + max-width: 100%; background-repeat: no-repeat; background-size: contain; background-position: top left; } .expando .image img { visibility: hidden; - width: 100%; + max-width: 100%; } .expando .md { background-color: #fafafa; @@ -712,6 +719,9 @@ nav .communities a.more { position: absolute; right: 0; } +.dark nav .communities a.more { + background-color: #cccccc; +} .orangered, .orangered b { color: orangered !important; } diff --git a/public/utils.js b/public/utils.js index 3e469e0..b8d57d3 100644 --- a/public/utils.js +++ b/public/utils.js @@ -91,6 +91,24 @@ function commentClick(e) { return false } } +function loadMore(e) { + e.preventDefault() + page = e.target.getAttribute("data-page") + e.target.disabled="disabled" + e.target.value="loading" + var urlParams = new URLSearchParams(window.location.search); + urlParams.set("xhr", "1") + urlParams.set("page", page) + request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "", function(res){ + if (res.trim()) { + e.target.outerHTML = res + '
' + } + else { + e.target.outerHTML = "" + } + }) + return false; +} function hideAllChildComments(e) { e.preventDefault() var comments = document.getElementsByClassName("comment") diff --git a/routes.go b/routes.go index 7664c15..1c465d2 100644 --- a/routes.go +++ b/routes.go @@ -351,7 +351,11 @@ func GetFrontpage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) if state.Op == "" { state.GetPosts() } - Render(w, "frontpage.html", state) + if state.XHR { + Render(w, "xhr.html", state) + } else { + Render(w, "frontpage.html", state) + } } func GetPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -508,10 +512,11 @@ func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { setCookie(w, state.Host, name, r.FormValue(name)) } if r.FormValue("darkmode") != "" { - setCookie(w, state.Host, "Dark", "1") + setCookie(w, "", "Dark", "1") state.Dark = true } else { deleteCookie(w, state.Host, "Dark") + deleteCookie(w, "", "Dark") state.Dark = false } if r.FormValue("shownsfw") != "" { @@ -934,6 +939,8 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { state.Client.CreatePostLike(context.Background(), post) if r.FormValue("xhr") != "" { state.GetPost(postid) + state.PostID = 0 + state.Op = "vote_post" state.XHR = true Render(w, "index.html", state) return diff --git a/state.go b/state.go index e33caef..1692c56 100644 --- a/state.go +++ b/state.go @@ -235,7 +235,7 @@ func (state *State) GetComment(commentid int) { ParentID: types.NewOptional(state.CommentID), Sort: types.NewOptional(types.CommentSortType(state.Sort)), Type: types.NewOptional(types.ListingType("All")), - Limit: types.NewOptional(int64(200)), + Limit: types.NewOptional(int64(50)), }) if err != nil { fmt.Println(err) @@ -266,17 +266,9 @@ func (state *State) GetComments() { PostID: types.NewOptional(state.PostID), Sort: types.NewOptional(types.CommentSortType(state.Sort)), Type: types.NewOptional(types.ListingType("All")), - Limit: types.NewOptional(int64(200)), + Limit: types.NewOptional(int64(50)), Page: types.NewOptional(int64(state.Page)), }) - if err != nil && strings.Contains(fmt.Sprintf("%v", err), "couldnt_get_comments") { - cresp, err = state.Client.Comments(context.Background(), types.GetComments{ - PostID: types.NewOptional(state.PostID), - Sort: types.NewOptional(types.CommentSortType(state.Sort)), - Type: types.NewOptional(types.ListingType("All")), - Page: types.NewOptional(int64(state.Page)), - }) - } if err != nil { state.Status = http.StatusInternalServerError fmt.Println(err) diff --git a/templates/frontpage.html b/templates/frontpage.html index b603641..93b37c6 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -10,9 +10,13 @@