From 1a02b77f9c22f0e9641da44649f4980d01abe36e Mon Sep 17 00:00:00 2001 From: Alex Bogdanovski Date: Thu, 30 Jan 2020 00:51:50 +0200 Subject: [PATCH] added numeric pagination with option para.numeric_pagination_enabled = true, closes #147 --- README.md | 28 ++++--- .../utils/ScooldRequestInterceptor.java | 2 + .../com/erudika/scoold/utils/ScooldUtils.java | 13 +++- src/main/resources/templates/macro.vm | 73 ++++++++++++++++--- 4 files changed, 93 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a461be04..dbc8cbf7 100755 --- a/README.md +++ b/README.md @@ -131,11 +131,11 @@ The most important settings are `para.endpoint` - the URL of the Para server, as Copy the Scoold example configuration below to your **`application.conf`** and edit it if necessary: ```ini +### Minimal configuration ### +# the name of the application para.app_name = "Scoold" # the port for Scoold para.port = 8000 -# Session cookie name -para.auth_cookie = "scoold-auth" # change this to "production" later para.env = "development" # the URL where Scoold is hosted, or http://localhost:8000 @@ -146,8 +146,22 @@ para.endpoint = "https://paraio.com" para.access_key = "app:scoold" # secret key for your Para app para.secret_key = "" +# the identifier of admin user - check Para user object +para.admins = "admin@domain.com" +############################## + +####### Authentication ####### # enable or disable email and password authentication para.password_auth_enabled = true +# Session cookie name +para.auth_cookie = "scoold-auth" +# Facebook - create your own Facebook app first! +para.fb_app_id = "123456789" +# Google - create your own Google app first! +para.google_client_id = "123-abcd.apps.googleusercontent.com" +############################### + +### Misc. ### # if false, commenting is allowed after 100+ reputation para.new_users_can_comment = true # if true, posts by new users require approval from moderator @@ -158,16 +172,10 @@ para.posts_rep_threshold = 100 para.gmaps_api_key = "" # Enable/disable near me feature (geolocation) para.nearme_feature_enabled = false -# the identifier of admin user - check Para user object -para.admins = "admin@domain.com" # GA code para.google_analytics_id = "UA-123456-7" # enables syntax highlighting in posts para.code_highlighting_enabled = true -# Facebook - create your own Facebook app first! -para.fb_app_id = "123456789" -# Google - create your own Google app first! -para.google_client_id = "123-abcd.apps.googleusercontent.com" # If true, the default space will be accessible by everyone para.is_default_space_public = true # If true, users can change their profile pictures @@ -185,6 +193,8 @@ para.max_comment_length = 255 para.max_post_length = 20000 # Sets the default tag for new questions para.default_question_tag = "question" +# Enable/disable numeric pagination (< 1 2 3...N >) +para.numeric_pagination_enabled = false ``` On startup, Scoold will try to connect to Para 10 times, with a 10 second interval between retries. After that it will @@ -869,7 +879,7 @@ para.file_uploads_dir = "uploads" ``` To upload an image just **drag & drop** the file you want to upload onto the post editor. A link will automatically appear -when the upload is finished. +when the upload is finished. Profile pictures (avatars) can also be changed by dragging a new image on top of the existing profile picture on a user's `/profile` page. For best results, use a square image here. diff --git a/src/main/java/com/erudika/scoold/utils/ScooldRequestInterceptor.java b/src/main/java/com/erudika/scoold/utils/ScooldRequestInterceptor.java index 40e7e25d..fe418aba 100755 --- a/src/main/java/com/erudika/scoold/utils/ScooldRequestInterceptor.java +++ b/src/main/java/com/erudika/scoold/utils/ScooldRequestInterceptor.java @@ -113,6 +113,7 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response, modelAndView.addObject("reportTypes", ReportType.values()); modelAndView.addObject("returnto", StringUtils.removeStart(request.getRequestURI(), CONTEXT_PATH)); // Configurable constants + modelAndView.addObject("MAX_PAGES", Config.MAX_PAGES); modelAndView.addObject("MAX_TEXT_LENGTH", MAX_TEXT_LENGTH); modelAndView.addObject("MAX_TAGS_PER_POST", MAX_TAGS_PER_POST); modelAndView.addObject("MAX_REPLIES_PER_POST", MAX_REPLIES_PER_POST); @@ -190,6 +191,7 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response, modelAndView.addObject("lang", utils.getLang(currentLocale)); modelAndView.addObject("langDirection", utils.isLanguageRTL(currentLocale.getLanguage()) ? "RTL" : "LTR"); // Pagination + modelAndView.addObject("numericPaginationEnabled", Config.getConfigBoolean("numeric_pagination_enabled", false)); // check for AJAX pagination requests if (utils.isAjaxRequest(request) && (utils.param(request, "page") || utils.param(request, "page1") || utils.param(request, "page2"))) { diff --git a/src/main/java/com/erudika/scoold/utils/ScooldUtils.java b/src/main/java/com/erudika/scoold/utils/ScooldUtils.java index 4a3452a1..e3499168 100755 --- a/src/main/java/com/erudika/scoold/utils/ScooldUtils.java +++ b/src/main/java/com/erudika/scoold/utils/ScooldUtils.java @@ -565,16 +565,23 @@ public Set getCoreScooldTypes() { } public Pager getPager(String pageParamName, HttpServletRequest req) { - return new Pager(NumberUtils.toInt(req.getParameter(pageParamName), 1), Config.MAX_ITEMS_PER_PAGE); + return pagerFromParams(pageParamName, req); } public Pager pagerFromParams(HttpServletRequest req) { + return pagerFromParams("page", req); + } + + public Pager pagerFromParams(String pageParamName, HttpServletRequest req) { Pager p = new Pager(); - p.setPage(NumberUtils.toLong(req.getParameter("page"), 1)); - p.setDesc(Boolean.parseBoolean(req.getParameter("desc"))); + p.setPage(Math.min(NumberUtils.toLong(req.getParameter(pageParamName), 1), Config.MAX_PAGES)); p.setLimit(NumberUtils.toInt(req.getParameter("limit"), Config.MAX_ITEMS_PER_PAGE)); String lastKey = req.getParameter("lastKey"); String sort = req.getParameter("sort"); + String desc = req.getParameter("desc"); + if (!StringUtils.isBlank(desc)) { + p.setDesc(Boolean.parseBoolean(desc)); + } if (!StringUtils.isBlank(lastKey)) { p.setLastKey(lastKey); } diff --git a/src/main/resources/templates/macro.vm b/src/main/resources/templates/macro.vm index cc012537..50840a02 100755 --- a/src/main/resources/templates/macro.vm +++ b/src/main/resources/templates/macro.vm @@ -157,7 +157,12 @@ ################# PAGINATION ##################### #macro(paginate $macroCode $pager $_rpath $pageparam) #if($pager && $pager.count > 0) - #if ($pager && $pager.page > 1000) #set($next = 1) #else #set($next = $pager.page + 1) #end + #set($pageCount = ($pager.count / $pager.limit)) + #if($pageCount > $MAX_PAGES)#set($pageCount = $MAX_PAGES)#end + + #set($next = $pager.page + 1) + #if ($pager.page <= 1) #set($prev = 1) #else #set($prev = $pager.page - 1) #end + #if(!$_rpath || $_rpath.isEmpty()) #if($request.queryString) #set($_rpath = "$!{request.requestURI}?$!{request.queryString}") @@ -165,17 +170,63 @@ #set($_rpath = "$!{request.requestURI}") #end #end + #set($rexp = "${pageparam}=(\d+|&)+") + #set($_rpath = $_rpath.replaceAll($rexp, "")) + #set($_rpath = $_rpath.replaceAll("(&|&|\?)$", "")) + #if ($_rpath.contains("?"))#set($paramseparator = "&")#else #set($paramseparator = "?")#end
#evaluate($macroCode)
- #if ($pager.count && $pager.count > $pager.limit && $macroCode && !$macroCode.trim().isEmpty()) -
- #if ($_rpath.contains("?")) - $!lang.get("more") - #else - $!lang.get("more") - #end - $!macroCode - -
+ #if ($pager.count > $pager.limit && $macroCode && !$macroCode.trim().isEmpty()) +
+ #if($numericPaginationEnabled) + #set($maxPageLinks = 7) + #if ($pager.page >= $pageCount) #set($next = 1) #else #set($next = $pager.page + 1) #end + #if($pager.count % $pager.limit > 0) #set($pageCount = $pageCount + 1) #end +
    +
  • + #if($pager.page <= $maxPageLinks) + #set($startPage = 1) + #set($endPage = $maxPageLinks) + #foreach($page in [$startPage..$endPage]) + #if($page == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • $!page
  • + #end + #if($pageCount > $maxPageLinks) + + #if($pageCount == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • $pageCount
  • + #end + #elseif($pager.page > $maxPageLinks && $pager.page < ($pageCount - $maxPageLinks)) + #set($startPage = $pager.page - ($maxPageLinks / 2)) + #set($endPage = $pager.page + ($maxPageLinks / 2)) + #if(1 == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • 1
  • + + #foreach($page in [$startPage..$endPage]) + #if($page == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • $!page
  • + #end + + #if($pageCount == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • $pageCount
  • + #else + #if(1 == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • 1
  • + + #set($startPage = $pageCount - $maxPageLinks) + #set($endPage = $pageCount) + #foreach($page in [$startPage..$endPage]) + #if($page == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end +
  • $!page
  • + #end + #end +
  • +
+ #else + $!lang.get("more") + $!macroCode + + #end +
#end #end #end