2014-10-02 09:40:46 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pagination
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2014-10-07 09:02:07 +00:00
|
|
|
// Paginator within the state of a http request.
|
2014-10-02 09:40:46 +00:00
|
|
|
type Paginator struct {
|
|
|
|
Request *http.Request
|
|
|
|
PerPageNums int
|
|
|
|
MaxPages int
|
|
|
|
|
|
|
|
nums int64
|
|
|
|
pageRange []int
|
|
|
|
pageNums int
|
|
|
|
page int
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageNums Returns the total number of pages.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageNums() int {
|
|
|
|
if p.pageNums != 0 {
|
|
|
|
return p.pageNums
|
|
|
|
}
|
|
|
|
pageNums := math.Ceil(float64(p.nums) / float64(p.PerPageNums))
|
|
|
|
if p.MaxPages > 0 {
|
|
|
|
pageNums = math.Min(pageNums, float64(p.MaxPages))
|
|
|
|
}
|
|
|
|
p.pageNums = int(pageNums)
|
|
|
|
return p.pageNums
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// Nums Returns the total number of items (e.g. from doing SQL count).
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) Nums() int64 {
|
|
|
|
return p.nums
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// SetNums Sets the total number of items.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) SetNums(nums interface{}) {
|
2015-09-14 15:17:33 +00:00
|
|
|
p.nums, _ = toInt64(nums)
|
2014-10-02 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// Page Returns the current page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) Page() int {
|
|
|
|
if p.page != 0 {
|
|
|
|
return p.page
|
|
|
|
}
|
|
|
|
if p.Request.Form == nil {
|
|
|
|
p.Request.ParseForm()
|
|
|
|
}
|
|
|
|
p.page, _ = strconv.Atoi(p.Request.Form.Get("p"))
|
|
|
|
if p.page > p.PageNums() {
|
|
|
|
p.page = p.PageNums()
|
|
|
|
}
|
|
|
|
if p.page <= 0 {
|
|
|
|
p.page = 1
|
|
|
|
}
|
|
|
|
return p.page
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// Pages Returns a list of all pages.
|
2014-10-07 09:02:07 +00:00
|
|
|
//
|
|
|
|
// Usage (in a view template):
|
|
|
|
//
|
|
|
|
// {{range $index, $page := .paginator.Pages}}
|
|
|
|
// <li{{if $.paginator.IsActive .}} class="active"{{end}}>
|
|
|
|
// <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
|
|
|
|
// </li>
|
|
|
|
// {{end}}
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) Pages() []int {
|
|
|
|
if p.pageRange == nil && p.nums > 0 {
|
|
|
|
var pages []int
|
|
|
|
pageNums := p.PageNums()
|
|
|
|
page := p.Page()
|
|
|
|
switch {
|
|
|
|
case page >= pageNums-4 && pageNums > 9:
|
|
|
|
start := pageNums - 9 + 1
|
|
|
|
pages = make([]int, 9)
|
2015-03-20 05:29:01 +00:00
|
|
|
for i := range pages {
|
2014-10-02 09:40:46 +00:00
|
|
|
pages[i] = start + i
|
|
|
|
}
|
|
|
|
case page >= 5 && pageNums > 9:
|
|
|
|
start := page - 5 + 1
|
|
|
|
pages = make([]int, int(math.Min(9, float64(page+4+1))))
|
2015-03-20 05:29:01 +00:00
|
|
|
for i := range pages {
|
2014-10-02 09:40:46 +00:00
|
|
|
pages[i] = start + i
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
pages = make([]int, int(math.Min(9, float64(pageNums))))
|
2015-03-20 05:29:01 +00:00
|
|
|
for i := range pages {
|
2014-10-02 09:40:46 +00:00
|
|
|
pages[i] = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.pageRange = pages
|
|
|
|
}
|
|
|
|
return p.pageRange
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageLink Returns URL for a given page index.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageLink(page int) string {
|
2014-12-25 03:23:04 +00:00
|
|
|
link, _ := url.ParseRequestURI(p.Request.URL.String())
|
2014-10-02 09:40:46 +00:00
|
|
|
values := link.Query()
|
|
|
|
if page == 1 {
|
|
|
|
values.Del("p")
|
|
|
|
} else {
|
|
|
|
values.Set("p", strconv.Itoa(page))
|
|
|
|
}
|
|
|
|
link.RawQuery = values.Encode()
|
|
|
|
return link.String()
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageLinkPrev Returns URL to the previous page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageLinkPrev() (link string) {
|
|
|
|
if p.HasPrev() {
|
|
|
|
link = p.PageLink(p.Page() - 1)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageLinkNext Returns URL to the next page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageLinkNext() (link string) {
|
|
|
|
if p.HasNext() {
|
|
|
|
link = p.PageLink(p.Page() + 1)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageLinkFirst Returns URL to the first page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageLinkFirst() (link string) {
|
|
|
|
return p.PageLink(1)
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// PageLinkLast Returns URL to the last page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) PageLinkLast() (link string) {
|
|
|
|
return p.PageLink(p.PageNums())
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// HasPrev Returns true if the current page has a predecessor.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) HasPrev() bool {
|
|
|
|
return p.Page() > 1
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// HasNext Returns true if the current page has a successor.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) HasNext() bool {
|
|
|
|
return p.Page() < p.PageNums()
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// IsActive Returns true if the given page index points to the current page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) IsActive(page int) bool {
|
|
|
|
return p.Page() == page
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// Offset Returns the current offset.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) Offset() int {
|
|
|
|
return (p.Page() - 1) * p.PerPageNums
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// HasPages Returns true if there is more than one page.
|
2014-10-02 09:40:46 +00:00
|
|
|
func (p *Paginator) HasPages() bool {
|
|
|
|
return p.PageNums() > 1
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:17:33 +00:00
|
|
|
// NewPaginator Instantiates a paginator struct for the current http request.
|
2014-10-02 09:40:46 +00:00
|
|
|
func NewPaginator(req *http.Request, per int, nums interface{}) *Paginator {
|
|
|
|
p := Paginator{}
|
|
|
|
p.Request = req
|
|
|
|
if per <= 0 {
|
|
|
|
per = 10
|
|
|
|
}
|
|
|
|
p.PerPageNums = per
|
|
|
|
p.SetNums(nums)
|
|
|
|
return &p
|
|
|
|
}
|