|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Copyright 2015 Unknwon. Licensed under the Apache License, Version 2.0 |
| 6 | + |
| 7 | +package paginator |
| 8 | + |
| 9 | +/* |
| 10 | +In template: |
| 11 | +
|
| 12 | +```html |
| 13 | +{{if not .Page.IsFirst}}[First](1){{end}} |
| 14 | +{{if .Page.HasPrevious}}[Previous]({{.Page.Previous}}){{end}} |
| 15 | +
|
| 16 | +{{range .Page.Pages}} |
| 17 | + {{if eq .Num -1}} |
| 18 | + ... |
| 19 | + {{else}} |
| 20 | + {{.Num}}{{if .IsCurrent}}(current){{end}} |
| 21 | + {{end}} |
| 22 | +{{end}} |
| 23 | +
|
| 24 | +{{if .Page.HasNext}}[Next]({{.Page.Next}}){{end}} |
| 25 | +{{if not .Page.IsLast}}[Last]({{.Page.TotalPages}}){{end}} |
| 26 | +``` |
| 27 | +
|
| 28 | +Output: |
| 29 | +
|
| 30 | +``` |
| 31 | +[First](1) [Previous](2) ... 2 3(current) 4 ... [Next](4) [Last](5) |
| 32 | +``` |
| 33 | +*/ |
| 34 | + |
| 35 | +// Paginator represents a set of results of pagination calculations. |
| 36 | +type Paginator struct { |
| 37 | + total int // total rows count |
| 38 | + pagingNum int // how many rows in one page |
| 39 | + current int // current page number |
| 40 | + numPages int // how many pages to show on the UI |
| 41 | +} |
| 42 | + |
| 43 | +// New initialize a new pagination calculation and returns a Paginator as result. |
| 44 | +func New(total, pagingNum, current, numPages int) *Paginator { |
| 45 | + if pagingNum <= 0 { |
| 46 | + pagingNum = 1 |
| 47 | + } |
| 48 | + if current <= 0 { |
| 49 | + current = 1 |
| 50 | + } |
| 51 | + p := &Paginator{total, pagingNum, current, numPages} |
| 52 | + if p.current > p.TotalPages() { |
| 53 | + p.current = p.TotalPages() |
| 54 | + } |
| 55 | + return p |
| 56 | +} |
| 57 | + |
| 58 | +// IsFirst returns true if current page is the first page. |
| 59 | +func (p *Paginator) IsFirst() bool { |
| 60 | + return p.current == 1 |
| 61 | +} |
| 62 | + |
| 63 | +// HasPrevious returns true if there is a previous page relative to current page. |
| 64 | +func (p *Paginator) HasPrevious() bool { |
| 65 | + return p.current > 1 |
| 66 | +} |
| 67 | + |
| 68 | +func (p *Paginator) Previous() int { |
| 69 | + if !p.HasPrevious() { |
| 70 | + return p.current |
| 71 | + } |
| 72 | + return p.current - 1 |
| 73 | +} |
| 74 | + |
| 75 | +// HasNext returns true if there is a next page relative to current page. |
| 76 | +func (p *Paginator) HasNext() bool { |
| 77 | + return p.total > p.current*p.pagingNum |
| 78 | +} |
| 79 | + |
| 80 | +func (p *Paginator) Next() int { |
| 81 | + if !p.HasNext() { |
| 82 | + return p.current |
| 83 | + } |
| 84 | + return p.current + 1 |
| 85 | +} |
| 86 | + |
| 87 | +// IsLast returns true if current page is the last page. |
| 88 | +func (p *Paginator) IsLast() bool { |
| 89 | + if p.total == 0 { |
| 90 | + return true |
| 91 | + } |
| 92 | + return p.total > (p.current-1)*p.pagingNum && !p.HasNext() |
| 93 | +} |
| 94 | + |
| 95 | +// Total returns number of total rows. |
| 96 | +func (p *Paginator) Total() int { |
| 97 | + return p.total |
| 98 | +} |
| 99 | + |
| 100 | +// TotalPages returns number of total pages. |
| 101 | +func (p *Paginator) TotalPages() int { |
| 102 | + if p.total == 0 { |
| 103 | + return 1 |
| 104 | + } |
| 105 | + return (p.total + p.pagingNum - 1) / p.pagingNum |
| 106 | +} |
| 107 | + |
| 108 | +// Current returns current page number. |
| 109 | +func (p *Paginator) Current() int { |
| 110 | + return p.current |
| 111 | +} |
| 112 | + |
| 113 | +// PagingNum returns number of page size. |
| 114 | +func (p *Paginator) PagingNum() int { |
| 115 | + return p.pagingNum |
| 116 | +} |
| 117 | + |
| 118 | +// Page presents a page in the paginator. |
| 119 | +type Page struct { |
| 120 | + num int |
| 121 | + isCurrent bool |
| 122 | +} |
| 123 | + |
| 124 | +func (p *Page) Num() int { |
| 125 | + return p.num |
| 126 | +} |
| 127 | + |
| 128 | +func (p *Page) IsCurrent() bool { |
| 129 | + return p.isCurrent |
| 130 | +} |
| 131 | + |
| 132 | +func getMiddleIdx(numPages int) int { |
| 133 | + return (numPages + 1) / 2 |
| 134 | +} |
| 135 | + |
| 136 | +// Pages returns a list of nearby page numbers relative to current page. |
| 137 | +// If value is -1 means "..." that more pages are not showing. |
| 138 | +func (p *Paginator) Pages() []*Page { |
| 139 | + if p.numPages == 0 { |
| 140 | + return []*Page{} |
| 141 | + } else if p.numPages == 1 && p.TotalPages() == 1 { |
| 142 | + // Only show current page. |
| 143 | + return []*Page{{1, true}} |
| 144 | + } |
| 145 | + |
| 146 | + // Total page number is less or equal. |
| 147 | + if p.TotalPages() <= p.numPages { |
| 148 | + pages := make([]*Page, p.TotalPages()) |
| 149 | + for i := range pages { |
| 150 | + pages[i] = &Page{i + 1, i+1 == p.current} |
| 151 | + } |
| 152 | + return pages |
| 153 | + } |
| 154 | + |
| 155 | + numPages := p.numPages |
| 156 | + offsetIdx := 0 |
| 157 | + hasMoreNext := false |
| 158 | + |
| 159 | + // Check more previous and next pages. |
| 160 | + previousNum := getMiddleIdx(p.numPages) - 1 |
| 161 | + if previousNum > p.current-1 { |
| 162 | + previousNum -= previousNum - (p.current - 1) |
| 163 | + } |
| 164 | + nextNum := p.numPages - previousNum - 1 |
| 165 | + if p.current+nextNum > p.TotalPages() { |
| 166 | + delta := nextNum - (p.TotalPages() - p.current) |
| 167 | + nextNum -= delta |
| 168 | + previousNum += delta |
| 169 | + } |
| 170 | + |
| 171 | + offsetVal := p.current - previousNum |
| 172 | + if offsetVal > 1 { |
| 173 | + numPages++ |
| 174 | + offsetIdx = 1 |
| 175 | + } |
| 176 | + |
| 177 | + if p.current+nextNum < p.TotalPages() { |
| 178 | + numPages++ |
| 179 | + hasMoreNext = true |
| 180 | + } |
| 181 | + |
| 182 | + pages := make([]*Page, numPages) |
| 183 | + |
| 184 | + // There are more previous pages. |
| 185 | + if offsetIdx == 1 { |
| 186 | + pages[0] = &Page{-1, false} |
| 187 | + } |
| 188 | + // There are more next pages. |
| 189 | + if hasMoreNext { |
| 190 | + pages[len(pages)-1] = &Page{-1, false} |
| 191 | + } |
| 192 | + |
| 193 | + // Check previous pages. |
| 194 | + for i := 0; i < previousNum; i++ { |
| 195 | + pages[offsetIdx+i] = &Page{i + offsetVal, false} |
| 196 | + } |
| 197 | + |
| 198 | + pages[offsetIdx+previousNum] = &Page{p.current, true} |
| 199 | + |
| 200 | + // Check next pages. |
| 201 | + for i := 1; i <= nextNum; i++ { |
| 202 | + pages[offsetIdx+previousNum+i] = &Page{p.current + i, false} |
| 203 | + } |
| 204 | + |
| 205 | + return pages |
| 206 | +} |
0 commit comments