-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlength_aware_paginator.go
142 lines (115 loc) · 3.55 KB
/
length_aware_paginator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gopager
import (
"strconv"
)
type TLengthAwarePaginator struct {
TPaginator
onEachSide int
}
type TElement struct {
Show bool
IsDots bool
Items map[int]string
}
func NewLengthAwarePaginator(items interface{}, total int, perPage int, currentPage int, options map[string]string) *TLengthAwarePaginator {
paginator := NewPaginator(items, total, perPage, currentPage, options)
lengthAwarePaginator := &TLengthAwarePaginator{TPaginator: *paginator}
if options != nil {
for optionKey, optionValue := range options {
if optionKey == "onEachSide" {
i, _ := strconv.Atoi(optionValue)
lengthAwarePaginator.onEachSide = i
}
}
}
if lengthAwarePaginator.onEachSide == 0 {
lengthAwarePaginator.onEachSide = 3
}
return lengthAwarePaginator
}
func (p *TLengthAwarePaginator) getSmallSlider() map[string]map[int]string {
return map[string]map[int]string{
"first": p.getUrlRange(1, p.LastPage),
"slider": nil,
"last": nil,
}
}
func (p *TLengthAwarePaginator) getUrlSlider(onEachSide int) map[string]map[int]string {
window := onEachSide * 2
if ! p.HasPage() {
return map[string]map[int]string{"first": nil, "slider": nil, "last": nil,}
}
if p.CurrentPage <= window {
return p.getSliderTooCloseToBeginning(window)
} else if p.CurrentPage > (p.LastPage - window) {
return p.getSliderTooCloseToEnding(window)
}
return p.getFullSlider(onEachSide)
}
func (p *TLengthAwarePaginator) getSliderTooCloseToBeginning(window int) map[string]map[int]string {
return map[string]map[int]string{
"first": p.getUrlRange(1, window+2),
"slider": nil,
"last": p.getFinish(),
}
}
func (p *TLengthAwarePaginator) getSliderTooCloseToEnding(window int) map[string]map[int]string {
last := p.getUrlRange(p.LastPage-(window+2), p.LastPage)
return map[string]map[int]string{"first": p.getStart(), "slider": nil, "last": last,}
}
func (p *TLengthAwarePaginator) getFullSlider(onEachSide int) map[string]map[int]string {
return map[string]map[int]string{
"first": p.getStart(),
"slider": p.getAdjacentUrlRange(onEachSide),
"last": p.getFinish(),
}
}
func (p *TLengthAwarePaginator) getAdjacentUrlRange(onEachSide int) map[int]string {
return p.getUrlRange(p.CurrentPage-onEachSide, p.CurrentPage+onEachSide)
}
func (p *TLengthAwarePaginator) getStart() map[int]string {
return p.getUrlRange(1, 2)
}
func (p *TLengthAwarePaginator) getFinish() map[int]string {
return p.getUrlRange(p.LastPage-1, p.LastPage)
}
func (p *TLengthAwarePaginator) Elements() []TElement {
window := p.makeWindow()
sliderDots := TElement{false, false, nil}
lastDots := TElement{false, false, nil}
if window["slider"] != nil {
sliderDots = TElement{true, true, nil}
}
if window["last"] != nil {
lastDots = TElement{true, true, nil}
}
return []TElement{
{true, false, window["first"]},
sliderDots,
{true, false, window["slider"]},
lastDots,
{true, false, window["last"]},
}
}
func (p *TLengthAwarePaginator) makeWindow() map[string]map[int]string {
if p.LastPage < (p.onEachSide*2)+6 {
return p.getSmallSlider()
}
return p.getUrlSlider(p.onEachSide)
}
func (p *TLengthAwarePaginator) GetStringMap() map[string]interface{} {
return map[string]interface{}{
"current_page": p.CurrentPage,
"data": p.Items,
"first_page_url": p.url(1),
"from": p.firstItem(),
"last_page": p.lastPage(),
"last_page_url": p.url(p.lastPage()),
"next_page_url": p.NextPageUrl(),
"path": p.Path,
"per_page": p.PerPage,
"prev_page_url": p.PreviousPageUrl(),
"to": p.lastItem(),
"total": p.Total,
}
}