Skip to content

Commit

Permalink
added numeric pagination with option para.numeric_pagination_enabled …
Browse files Browse the repository at this point in the history
…= true, closes #147
  • Loading branch information
albogdano committed Jan 29, 2020
1 parent eacd78c commit 1a02b77
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 23 deletions.
28 changes: 19 additions & 9 deletions README.md
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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"))) {
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Expand Up @@ -565,16 +565,23 @@ public Set<String> 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);
}
Expand Down
73 changes: 62 additions & 11 deletions src/main/resources/templates/macro.vm
Expand Up @@ -157,25 +157,76 @@
################# 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}")
#else
#set($_rpath = "$!{request.requestURI}")
#end
#end
#set($rexp = "${pageparam}=(\d+|&amp;)+")
#set($_rpath = $_rpath.replaceAll($rexp, ""))
#set($_rpath = $_rpath.replaceAll("(&amp;|&|\?)$", ""))
#if ($_rpath.contains("?"))#set($paramseparator = "&")#else #set($paramseparator = "?")#end
<div class="page-content">#evaluate($macroCode)</div>
#if ($pager.count && $pager.count > $pager.limit && $macroCode && !$macroCode.trim().isEmpty())
<div class="pages center mtl">
#if ($_rpath.contains("?"))
<a href="${_rpath}&${pageparam}=$!next" class="more-link pagelink chip" title="Load more">$!lang.get("more")</a>
#else
<a href="${_rpath}?${pageparam}=$!next" class="more-link pagelink chip" title="Load more">$!lang.get("more")</a>
#end
<span class="hide page-macro-code">$!macroCode</span>
<span class="$!{next}"></span>
</div>
#if ($pager.count > $pager.limit && $macroCode && !$macroCode.trim().isEmpty())
<div class="pages center mtl">
#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
<ul class="pagination ptl">
<li><a href="${_rpath}${paramseparator}${pageparam}=$!prev"><i class="fa fa-chevron-left grey-text"></i></a></li>
#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
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=$!page">$!page</a></li>
#end
#if($pageCount > $maxPageLinks)
<i class="fa fa-ellipsis-h pvm phm"></i>
#if($pageCount == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=$pageCount">$pageCount</a></li>
#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
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=1">1</a></li>
<i class="fa fa-ellipsis-h pvm phm"></i>
#foreach($page in [$startPage..$endPage])
#if($page == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=$!page">$!page</a></li>
#end
<i class="fa fa-ellipsis-h pvm phm"></i>
#if($pageCount == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=$pageCount">$pageCount</a></li>
#else
#if(1 == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=1">1</a></li>
<i class="fa fa-ellipsis-h pvm phm"></i>
#set($startPage = $pageCount - $maxPageLinks)
#set($endPage = $pageCount)
#foreach($page in [$startPage..$endPage])
#if($page == $pager.page)#set($currPage = "active")#else#set($currPage = "")#end
<li class="waves-effect ${currPage}"><a href="${_rpath}${paramseparator}${pageparam}=$!page">$!page</a></li>
#end
#end
<li><a href="${_rpath}${paramseparator}${pageparam}=$!next"><i class="fa fa-chevron-right grey-text"></i></a></li>
</ul>
#else
<a href="${_rpath}${paramseparator}${pageparam}=$!next" class="more-link pagelink chip" title="Load more">$!lang.get("more")</a>
<span class="hide page-macro-code">$!macroCode</span>
<span class="$!{next}"></span>
#end
</div>
#end
#end
#end
Expand Down

0 comments on commit 1a02b77

Please sign in to comment.