本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 line
4.3KB

  1. // Copyright 2015 Unknwon
  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 paginater is a helper module for custom pagination calculation.
  15. package paginater
  16. // Paginater represents a set of results of pagination calculations.
  17. type Paginater struct {
  18. total int
  19. pagingNum int
  20. current int
  21. numPages int
  22. }
  23. // New initialize a new pagination calculation and returns a Paginater as result.
  24. func New(total, pagingNum, current, numPages int) *Paginater {
  25. if pagingNum <= 0 {
  26. pagingNum = 1
  27. }
  28. if current <= 0 {
  29. current = 1
  30. }
  31. p := &Paginater{total, pagingNum, current, numPages}
  32. if p.current > p.TotalPages() {
  33. p.current = p.TotalPages()
  34. }
  35. return p
  36. }
  37. // IsFirst returns true if current page is the first page.
  38. func (p *Paginater) IsFirst() bool {
  39. return p.current == 1
  40. }
  41. // HasPrevious returns true if there is a previous page relative to current page.
  42. func (p *Paginater) HasPrevious() bool {
  43. return p.current > 1
  44. }
  45. func (p *Paginater) Previous() int {
  46. if !p.HasPrevious() {
  47. return p.current
  48. }
  49. return p.current - 1
  50. }
  51. // HasNext returns true if there is a next page relative to current page.
  52. func (p *Paginater) HasNext() bool {
  53. return p.total > p.current*p.pagingNum
  54. }
  55. func (p *Paginater) Next() int {
  56. if !p.HasNext() {
  57. return p.current
  58. }
  59. return p.current + 1
  60. }
  61. // IsLast returns true if current page is the last page.
  62. func (p *Paginater) IsLast() bool {
  63. if p.total == 0 {
  64. return true
  65. }
  66. return p.total > (p.current-1)*p.pagingNum && !p.HasNext()
  67. }
  68. // Total returns number of total rows.
  69. func (p *Paginater) Total() int {
  70. return p.total
  71. }
  72. // TotalPage returns number of total pages.
  73. func (p *Paginater) TotalPages() int {
  74. if p.total == 0 {
  75. return 1
  76. }
  77. if p.total%p.pagingNum == 0 {
  78. return p.total / p.pagingNum
  79. }
  80. return p.total/p.pagingNum + 1
  81. }
  82. // Current returns current page number.
  83. func (p *Paginater) Current() int {
  84. return p.current
  85. }
  86. // Page presents a page in the paginater.
  87. type Page struct {
  88. num int
  89. isCurrent bool
  90. }
  91. func (p *Page) Num() int {
  92. return p.num
  93. }
  94. func (p *Page) IsCurrent() bool {
  95. return p.isCurrent
  96. }
  97. func getMiddleIdx(numPages int) int {
  98. if numPages%2 == 0 {
  99. return numPages / 2
  100. }
  101. return numPages/2 + 1
  102. }
  103. // Pages returns a list of nearby page numbers relative to current page.
  104. // If value is -1 means "..." that more pages are not showing.
  105. func (p *Paginater) Pages() []*Page {
  106. if p.numPages == 0 {
  107. return []*Page{}
  108. } else if p.numPages == 1 && p.TotalPages() == 1 {
  109. // Only show current page.
  110. return []*Page{{1, true}}
  111. }
  112. // Total page number is less or equal.
  113. if p.TotalPages() <= p.numPages {
  114. pages := make([]*Page, p.TotalPages())
  115. for i := range pages {
  116. pages[i] = &Page{i + 1, i+1 == p.current}
  117. }
  118. return pages
  119. }
  120. numPages := p.numPages
  121. maxIdx := numPages - 1
  122. offsetIdx := 0
  123. hasMoreNext := false
  124. // Check more previous and next pages.
  125. previousNum := getMiddleIdx(p.numPages) - 1
  126. if previousNum > p.current-1 {
  127. previousNum -= previousNum - (p.current - 1)
  128. }
  129. nextNum := p.numPages - previousNum - 1
  130. if p.current+nextNum > p.TotalPages() {
  131. delta := nextNum - (p.TotalPages() - p.current)
  132. nextNum -= delta
  133. previousNum += delta
  134. }
  135. offsetVal := p.current - previousNum
  136. if offsetVal > 1 {
  137. numPages++
  138. maxIdx++
  139. offsetIdx = 1
  140. }
  141. if p.current+nextNum < p.TotalPages() {
  142. numPages++
  143. hasMoreNext = true
  144. }
  145. pages := make([]*Page, numPages)
  146. // There are more previous pages.
  147. if offsetIdx == 1 {
  148. pages[0] = &Page{-1, false}
  149. }
  150. // There are more next pages.
  151. if hasMoreNext {
  152. pages[len(pages)-1] = &Page{-1, false}
  153. }
  154. // Check previous pages.
  155. for i := 0; i < previousNum; i++ {
  156. pages[offsetIdx+i] = &Page{i + offsetVal, false}
  157. }
  158. pages[offsetIdx+previousNum] = &Page{p.current, true}
  159. // Check next pages.
  160. for i := 1; i <= nextNum; i++ {
  161. pages[offsetIdx+previousNum+i] = &Page{p.current + i, false}
  162. }
  163. return pages
  164. }
上海开阖软件有限公司 沪ICP备12045867号-1