forked from b3log/pipe
/
paginations.go
107 lines (94 loc) · 2.94 KB
/
paginations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2019, b3log.org & hacpai.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package util
import (
"math"
"strconv"
"github.com/gin-gonic/gin"
)
// Pagination represents pagination info.
type Pagination struct {
CurrentPageNum int `json:"currentPageNum"`
PageSize int `json:"pageSize"`
PageCount int `json:"pageCount"`
WindowSize int `json:"windowSize"`
RecordCount int `json:"recordCount"`
PageNums []int `json:"pageNums"`
NextPageNum int `json:"nextPageNum"`
PreviousPageNum int `json:"previousPageNum"`
FirstPageNum int `josn:"firstPageNum"`
LastPageNum int `json:"lastPageNum"`
PageURL string `json:"pageURL"`
}
// GetPage returns paging parameter.
func GetPage(c *gin.Context) int {
ret, _ := strconv.Atoi(c.Query("p"))
if 1 > ret {
ret = 1
}
return ret
}
// NewPagination creates a new pagination with the specified current page num, page size, window size and record count.
func NewPagination(currentPageNum, pageSize, windowSize, recordCount int) *Pagination {
pageCount := int(math.Ceil(float64(recordCount) / float64(pageSize)))
previousPageNum := currentPageNum - 1
if 1 > previousPageNum {
previousPageNum = 0
}
nextPageNum := currentPageNum + 1
if nextPageNum > pageCount {
nextPageNum = 0
}
pageNums := paginate(currentPageNum, pageSize, pageCount, windowSize)
firstPageNum := 0
lastPageNum := 0
if 0 < len(pageNums) {
firstPageNum = pageNums[0]
lastPageNum = pageNums[len(pageNums)-1]
}
return &Pagination{
CurrentPageNum: currentPageNum,
NextPageNum: nextPageNum,
PreviousPageNum: previousPageNum,
PageSize: pageSize,
PageCount: pageCount,
WindowSize: windowSize,
RecordCount: recordCount,
PageNums: pageNums,
FirstPageNum: firstPageNum,
LastPageNum: lastPageNum,
}
}
func paginate(currentPageNum, pageSize, pageCount, windowSize int) []int {
var ret []int
if pageCount < windowSize {
for i := 0; i < pageCount; i++ {
ret = append(ret, i+1)
}
} else {
first := currentPageNum + 1 - windowSize/2
if first < 1 {
first = 1
}
if first+windowSize > pageCount {
first = pageCount - windowSize + 1
}
for i := 0; i < windowSize; i++ {
ret = append(ret, first+i)
}
}
return ret
}