paginator.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2013 wetalk authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package utils
  15. import (
  16. "math"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. )
  21. type Paginator struct {
  22. Request *http.Request
  23. PerPageNums int
  24. MaxPages int
  25. nums int64
  26. pageRange []int
  27. pageNums int
  28. page int
  29. }
  30. func (p *Paginator) PageNums() int {
  31. if p.pageNums != 0 {
  32. return p.pageNums
  33. }
  34. pageNums := math.Ceil(float64(p.nums) / float64(p.PerPageNums))
  35. if p.MaxPages > 0 {
  36. pageNums = math.Min(pageNums, float64(p.MaxPages))
  37. }
  38. p.pageNums = int(pageNums)
  39. return p.pageNums
  40. }
  41. func (p *Paginator) Nums() int64 {
  42. return p.nums
  43. }
  44. func (p *Paginator) SetNums(nums int64) {
  45. p.nums = nums
  46. }
  47. func (p *Paginator) Page() int {
  48. if p.page != 0 {
  49. return p.page
  50. }
  51. if p.Request.Form == nil {
  52. p.Request.ParseForm()
  53. }
  54. p.page, _ = strconv.Atoi(p.Request.Form.Get("page"))
  55. if p.page > p.PageNums() {
  56. p.page = p.PageNums()
  57. }
  58. if p.page <= 0 {
  59. p.page = 1
  60. }
  61. return p.page
  62. }
  63. func (p *Paginator) Pages() []int {
  64. if p.pageRange == nil && p.nums > 0 {
  65. var pages []int
  66. pageNums := p.PageNums()
  67. page := p.Page()
  68. switch {
  69. case page >= pageNums-4 && pageNums > 9:
  70. start := pageNums - 9 + 1
  71. pages = make([]int, 9)
  72. for i, _ := range pages {
  73. pages[i] = start + i
  74. }
  75. case page >= 5 && pageNums > 9:
  76. start := page - 5 + 1
  77. pages = make([]int, int(math.Min(9, float64(page+4+1))))
  78. for i, _ := range pages {
  79. pages[i] = start + i
  80. }
  81. default:
  82. pages = make([]int, int(math.Min(9, float64(pageNums))))
  83. for i, _ := range pages {
  84. pages[i] = i + 1
  85. }
  86. }
  87. p.pageRange = pages
  88. }
  89. return p.pageRange
  90. }
  91. func (p *Paginator) PageLink(page int) string {
  92. link, _ := url.ParseRequestURI(p.Request.RequestURI)
  93. values := link.Query()
  94. if page == 1 {
  95. values.Del("page")
  96. } else {
  97. values.Set("page", strconv.Itoa(page))
  98. }
  99. link.RawQuery = values.Encode()
  100. return link.String()
  101. }
  102. func (p *Paginator) PageParams() url.Values {
  103. link, _ := url.ParseRequestURI(p.Request.RequestURI)
  104. values := link.Query()
  105. values.Del("page")
  106. return values
  107. }
  108. func (p *Paginator) BasePageLink() string {
  109. link, _ := url.ParseRequestURI(p.Request.RequestURI)
  110. values := link.Query()
  111. values.Del("page")
  112. link.RawQuery = values.Encode()
  113. return link.String()
  114. }
  115. func (p *Paginator) PageLinkPrev() (link string) {
  116. if p.HasPrev() {
  117. link = p.PageLink(p.Page() - 1)
  118. }
  119. return
  120. }
  121. func (p *Paginator) PageLinkNext() (link string) {
  122. if p.HasNext() {
  123. link = p.PageLink(p.Page() + 1)
  124. }
  125. return
  126. }
  127. func (p *Paginator) PageLinkFirst() (link string) {
  128. return p.PageLink(1)
  129. }
  130. func (p *Paginator) PageLinkLast() (link string) {
  131. return p.PageLink(p.PageNums())
  132. }
  133. func (p *Paginator) HasPrev() bool {
  134. return p.Page() > 1
  135. }
  136. func (p *Paginator) HasNext() bool {
  137. return p.Page() < p.PageNums()
  138. }
  139. func (p *Paginator) IsActive(page int) bool {
  140. return p.Page() == page
  141. }
  142. func (p *Paginator) Offset() int {
  143. return (p.Page() - 1) * p.PerPageNums
  144. }
  145. func (p *Paginator) HasPages() bool {
  146. return p.PageNums() > 1
  147. }
  148. func (p *Paginator) Total() int {
  149. return p.PageNums()
  150. }
  151. func (p *Paginator) TotalSubOne() int {
  152. return p.PageNums() - 1
  153. }
  154. func NewPaginator(req *http.Request, per int, nums int64) *Paginator {
  155. p := Paginator{}
  156. p.Request = req
  157. if per <= 0 {
  158. per = 10
  159. }
  160. p.PerPageNums = per
  161. p.SetNums(nums)
  162. return &p
  163. }
  164. func FakePaginator(currentPage int, per int, nums int64) *Paginator {
  165. fakeReq, _ := http.NewRequest("GET", "/?page="+strconv.Itoa(currentPage), nil)
  166. return NewPaginator(fakeReq, per, nums)
  167. }