mirror of https://github.com/halo-dev/halo
✨ 新增评论分页特性
parent
dff55aca9e
commit
42ee51cfff
|
@ -0,0 +1,128 @@
|
|||
package cc.ryanc.halo.model.dto;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* List分页
|
||||
* </pre>
|
||||
*
|
||||
* @author : RYAN0UP
|
||||
* @date : 2018/8/27
|
||||
*/
|
||||
public class ListPage<T> {
|
||||
|
||||
/**
|
||||
* 原集合
|
||||
*/
|
||||
private List<T> data;
|
||||
|
||||
/**
|
||||
* 上一页页码
|
||||
*/
|
||||
private int prePage;
|
||||
|
||||
/**
|
||||
* 是否有上一页
|
||||
*/
|
||||
private boolean hasPrevious;
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
*/
|
||||
private int nowPage;
|
||||
|
||||
/**
|
||||
* 下一页页码
|
||||
*/
|
||||
private int nextPage;
|
||||
|
||||
/**
|
||||
* 是否有下一页
|
||||
*/
|
||||
private boolean hasNext;
|
||||
|
||||
/**
|
||||
* 每页条数
|
||||
*/
|
||||
private int pageSize;
|
||||
|
||||
/**
|
||||
* 总页数
|
||||
*/
|
||||
private int totalPage;
|
||||
|
||||
/**
|
||||
* 总数据条数
|
||||
*/
|
||||
private int totalCount;
|
||||
|
||||
public ListPage(List<T> data, int nowPage, int pageSize) {
|
||||
this.data = data;
|
||||
this.pageSize = pageSize;
|
||||
this.nowPage = nowPage;
|
||||
this.totalCount = data.size();
|
||||
this.totalPage = (totalCount + pageSize - 1) / pageSize;
|
||||
this.prePage = nowPage-1>1? nowPage-1:1;
|
||||
this.nextPage = nowPage>=totalPage? totalPage: nowPage + 1;
|
||||
this.hasPrevious = nowPage!=prePage;
|
||||
this.hasNext = nowPage!=nextPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前页数据
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public List<T> getPageList() {
|
||||
int fromIndex = (nowPage - 1) * pageSize;
|
||||
if (fromIndex >= data.size()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
if(fromIndex<0){
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int toIndex = nowPage * pageSize;
|
||||
if (toIndex >= data.size()) {
|
||||
toIndex = data.size();
|
||||
}
|
||||
return data.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getPrePage() {
|
||||
return prePage;
|
||||
}
|
||||
|
||||
public boolean isHasPrevious() {
|
||||
return hasPrevious;
|
||||
}
|
||||
|
||||
public int getNowPage() {
|
||||
return nowPage;
|
||||
}
|
||||
|
||||
public int getNextPage() {
|
||||
return nextPage;
|
||||
}
|
||||
|
||||
public boolean isHasNext() {
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
public int getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public int getTotalPage() {
|
||||
return totalPage;
|
||||
}
|
||||
|
||||
public int getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
}
|
|
@ -30,6 +30,11 @@ public enum BlogPropertiesEnum {
|
|||
*/
|
||||
INDEX_POSTS("index_posts"),
|
||||
|
||||
/**
|
||||
* 每页评论条数
|
||||
*/
|
||||
INDEX_COMMENTS("index_comments"),
|
||||
|
||||
/**
|
||||
* 是否已经安装
|
||||
*/
|
||||
|
|
|
@ -3,6 +3,7 @@ package cc.ryanc.halo.utils;
|
|||
import cc.ryanc.halo.model.domain.Comment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -33,6 +34,8 @@ public class CommentUtil {
|
|||
for (Comment comment : commentsResult) {
|
||||
comment.setChildComments(getChild(comment.getCommentId(), commentsRoot));
|
||||
}
|
||||
//集合倒序,最新的评论在最前面
|
||||
Collections.reverse(commentsResult);
|
||||
return commentsResult;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Comment;
|
|||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.model.dto.ListPage;
|
||||
import cc.ryanc.halo.model.enums.*;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
|
@ -22,6 +23,7 @@ import org.springframework.ui.Model;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
@ -109,7 +111,9 @@ public class FrontArchiveController extends BaseController {
|
|||
* @return 模板路径/themes/{theme}/post
|
||||
*/
|
||||
@GetMapping(value = "{postUrl}")
|
||||
public String getPost(@PathVariable String postUrl, Model model) {
|
||||
public String getPost(@PathVariable String postUrl,
|
||||
@RequestParam(value = "cp",defaultValue = "1") Integer cp,
|
||||
Model model) {
|
||||
Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_POST.getDesc());
|
||||
if (null == post || !post.getPostStatus().equals(PostStatusEnum.PUBLISHED.getCode())) {
|
||||
return this.renderNotFound();
|
||||
|
@ -141,9 +145,17 @@ public class FrontArchiveController extends BaseController {
|
|||
tagWords.add(tag.getTagName());
|
||||
}
|
||||
}
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//获取每页评论条数
|
||||
if (!StringUtils.isBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
|
||||
}
|
||||
//评论分页
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments),cp, size);
|
||||
model.addAttribute("is_post",true);
|
||||
model.addAttribute("post", post);
|
||||
model.addAttribute("comments", CommentUtil.getComments(comments));
|
||||
model.addAttribute("comments", commentsPage);
|
||||
model.addAttribute("commentsCount", comments.size());
|
||||
model.addAttribute("tagWords", CollUtil.join(tagWords, ","));
|
||||
postService.updatePostView(post);
|
||||
|
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Comment;
|
|||
import cc.ryanc.halo.model.domain.Gallery;
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.model.dto.ListPage;
|
||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||
import cc.ryanc.halo.model.enums.CommentStatusEnum;
|
||||
import cc.ryanc.halo.model.enums.PostTypeEnum;
|
||||
|
@ -19,6 +20,7 @@ import org.springframework.stereotype.Controller;
|
|||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -72,7 +74,9 @@ public class FrontPageController extends BaseController {
|
|||
* @return 模板路径/themes/{theme}/post
|
||||
*/
|
||||
@GetMapping(value = "/p/{postUrl}")
|
||||
public String getPage(@PathVariable(value = "postUrl") String postUrl, Model model) {
|
||||
public String getPage(@PathVariable(value = "postUrl") String postUrl,
|
||||
@RequestParam(value = "cp",defaultValue = "1") Integer cp,
|
||||
Model model) {
|
||||
Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_PAGE.getDesc());
|
||||
if (null == post) {
|
||||
return this.renderNotFound();
|
||||
|
@ -83,9 +87,17 @@ public class FrontPageController extends BaseController {
|
|||
} else {
|
||||
comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatusEnum.RECYCLE.getCode());
|
||||
}
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//获取每页评论条数
|
||||
if (!StringUtils.isBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
|
||||
}
|
||||
//评论分页
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments),cp, size);
|
||||
model.addAttribute("is_page",true);
|
||||
model.addAttribute("post", post);
|
||||
model.addAttribute("comments", CommentUtil.getComments(comments));
|
||||
model.addAttribute("comments", commentsPage);
|
||||
model.addAttribute("commentsCount", comments.size());
|
||||
postService.updatePostView(post);
|
||||
return this.render("page");
|
||||
|
|
|
@ -287,6 +287,16 @@
|
|||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="indexComments" class="col-lg-2 col-sm-4 control-label">每页显示条数:
|
||||
<span data-toggle="tooltip" data-placement="top" title="默认为10条" style="cursor: pointer">
|
||||
<i class="fa fa-question-circle" aria-hidden="true"></i>
|
||||
</span>
|
||||
</label>
|
||||
<div class="col-lg-4 col-sm-8">
|
||||
<input type="number" class="form-control" id="indexComments" name="index_comments" value="${options.index_comments?default('10')}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="nativeCommentPlaceholder" class="col-lg-2 col-sm-4 control-label">占位提示:</label>
|
||||
<div class="col-lg-4 col-sm-8">
|
||||
|
|
|
@ -213,16 +213,20 @@
|
|||
}
|
||||
|
||||
.native-nav{
|
||||
padding: 10px 0;
|
||||
padding: 10px 0!important;;
|
||||
}
|
||||
.page-nav{
|
||||
margin: 20px 0;
|
||||
padding: 0 10px;
|
||||
list-style: none;
|
||||
text-align: center;
|
||||
margin: 20px 0!important;;
|
||||
padding: 0 10px!important;;
|
||||
list-style: none!important;;
|
||||
text-align: center!important;;
|
||||
}
|
||||
.page-nav li{
|
||||
display: inline-block;
|
||||
display: inline-block!important;
|
||||
padding: 0 10px!important;
|
||||
}
|
||||
.page-nav li a{
|
||||
text-decoration: #0a001f!important;
|
||||
}
|
||||
${options.native_css?if_exists}
|
||||
@media screen and (max-width: 560px) {
|
||||
|
@ -292,9 +296,9 @@
|
|||
</#if>
|
||||
</ul>
|
||||
</#macro>
|
||||
<ul class="native-list">
|
||||
<#if comments?? && comments?size gt 0>
|
||||
<#list comments?sort_by("commentDate")?reverse as comment>
|
||||
<ul class="native-list" id="comments-list">
|
||||
<#if comments?? && comments.getPageList()?size gt 0>
|
||||
<#list comments.getPageList()?sort_by("commentDate")?reverse as comment>
|
||||
<li class="native-list-one" id="comment-id-${comment.commentId?c}">
|
||||
<img class="native-list-one-img" src="//gravatar.loli.net/avatar/${comment.commentAuthorAvatarMd5?if_exists}?s=256&d=${options.native_comment_avatar?default('mm')}">
|
||||
<section>
|
||||
|
@ -320,12 +324,25 @@
|
|||
</#list>
|
||||
</#if>
|
||||
</ul>
|
||||
<#--<div class="native-nav" id="comment-nav">-->
|
||||
<#--<ol class="page-nav">-->
|
||||
<#--<li><</li>-->
|
||||
<#--<li>></li>-->
|
||||
<#--</ol>-->
|
||||
<#--</div>-->
|
||||
<div class="native-nav" id="comment-nav">
|
||||
<#if comments.totalPage gt 1>
|
||||
<ol class="page-nav">
|
||||
<#if comments.hasPrevious>
|
||||
<li>
|
||||
<a href="?cp=${comments.nowPage-1}#comments-list" title="上一页">←</a>
|
||||
</li>
|
||||
</#if>
|
||||
<li>
|
||||
<span title="当前页">${comments.nowPage}</span>
|
||||
</li>
|
||||
<#if comments.hasNext>
|
||||
<li>
|
||||
<a href="?cp=${comments.nowPage+1}#comments-list" title="下一页">→</a>
|
||||
</li>
|
||||
</#if>
|
||||
</ol>
|
||||
</#if>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/plugins/jquery/jquery.min.js"></script>
|
||||
<script src="/static/plugins/md5/md5.min.js"></script>
|
||||
|
|
Loading…
Reference in New Issue