paginater.go 3.8 KB

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