1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Pagination;
|
4
|
|
5
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract;
|
6
|
|
7
|
class UrlWindow
|
8
|
{
|
9
|
/**
|
10
|
* The paginator implementation.
|
11
|
*
|
12
|
* @var \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
13
|
*/
|
14
|
protected $paginator;
|
15
|
|
16
|
/**
|
17
|
* Create a new URL window instance.
|
18
|
*
|
19
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
|
20
|
* @return void
|
21
|
*/
|
22
|
public function __construct(PaginatorContract $paginator)
|
23
|
{
|
24
|
$this->paginator = $paginator;
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* Create a new URL window instance.
|
29
|
*
|
30
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
|
31
|
* @param int $onEachSide
|
32
|
* @return array
|
33
|
*/
|
34
|
public static function make(PaginatorContract $paginator, $onEachSide = 3)
|
35
|
{
|
36
|
return (new static($paginator))->get($onEachSide);
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Get the window of URLs to be shown.
|
41
|
*
|
42
|
* @param int $onEachSide
|
43
|
* @return array
|
44
|
*/
|
45
|
public function get($onEachSide = 3)
|
46
|
{
|
47
|
if ($this->paginator->lastPage() < ($onEachSide * 2) + 6) {
|
48
|
return $this->getSmallSlider();
|
49
|
}
|
50
|
|
51
|
return $this->getUrlSlider($onEachSide);
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Get the slider of URLs there are not enough pages to slide.
|
56
|
*
|
57
|
* @return array
|
58
|
*/
|
59
|
protected function getSmallSlider()
|
60
|
{
|
61
|
return [
|
62
|
'first' => $this->paginator->getUrlRange(1, $this->lastPage()),
|
63
|
'slider' => null,
|
64
|
'last' => null,
|
65
|
];
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Create a URL slider links.
|
70
|
*
|
71
|
* @param int $onEachSide
|
72
|
* @return array
|
73
|
*/
|
74
|
protected function getUrlSlider($onEachSide)
|
75
|
{
|
76
|
$window = $onEachSide * 2;
|
77
|
|
78
|
if (! $this->hasPages()) {
|
79
|
return [
|
80
|
'first' => null,
|
81
|
'slider' => null,
|
82
|
'last' => null,
|
83
|
];
|
84
|
}
|
85
|
|
86
|
// If the current page is very close to the beginning of the page range, we will
|
87
|
// just render the beginning of the page range, followed by the last 2 of the
|
88
|
// links in this list, since we will not have room to create a full slider.
|
89
|
if ($this->currentPage() <= $window) {
|
90
|
return $this->getSliderTooCloseToBeginning($window);
|
91
|
}
|
92
|
|
93
|
// If the current page is close to the ending of the page range we will just get
|
94
|
// this first couple pages, followed by a larger window of these ending pages
|
95
|
// since we're too close to the end of the list to create a full on slider.
|
96
|
elseif ($this->currentPage() > ($this->lastPage() - $window)) {
|
97
|
return $this->getSliderTooCloseToEnding($window);
|
98
|
}
|
99
|
|
100
|
// If we have enough room on both sides of the current page to build a slider we
|
101
|
// will surround it with both the beginning and ending caps, with this window
|
102
|
// of pages in the middle providing a Google style sliding paginator setup.
|
103
|
return $this->getFullSlider($onEachSide);
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Get the slider of URLs when too close to beginning of window.
|
108
|
*
|
109
|
* @param int $window
|
110
|
* @return array
|
111
|
*/
|
112
|
protected function getSliderTooCloseToBeginning($window)
|
113
|
{
|
114
|
return [
|
115
|
'first' => $this->paginator->getUrlRange(1, $window + 2),
|
116
|
'slider' => null,
|
117
|
'last' => $this->getFinish(),
|
118
|
];
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Get the slider of URLs when too close to ending of window.
|
123
|
*
|
124
|
* @param int $window
|
125
|
* @return array
|
126
|
*/
|
127
|
protected function getSliderTooCloseToEnding($window)
|
128
|
{
|
129
|
$last = $this->paginator->getUrlRange(
|
130
|
$this->lastPage() - ($window + 2),
|
131
|
$this->lastPage()
|
132
|
);
|
133
|
|
134
|
return [
|
135
|
'first' => $this->getStart(),
|
136
|
'slider' => null,
|
137
|
'last' => $last,
|
138
|
];
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Get the slider of URLs when a full slider can be made.
|
143
|
*
|
144
|
* @param int $onEachSide
|
145
|
* @return array
|
146
|
*/
|
147
|
protected function getFullSlider($onEachSide)
|
148
|
{
|
149
|
return [
|
150
|
'first' => $this->getStart(),
|
151
|
'slider' => $this->getAdjacentUrlRange($onEachSide),
|
152
|
'last' => $this->getFinish(),
|
153
|
];
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Get the page range for the current page window.
|
158
|
*
|
159
|
* @param int $onEachSide
|
160
|
* @return array
|
161
|
*/
|
162
|
public function getAdjacentUrlRange($onEachSide)
|
163
|
{
|
164
|
return $this->paginator->getUrlRange(
|
165
|
$this->currentPage() - $onEachSide,
|
166
|
$this->currentPage() + $onEachSide
|
167
|
);
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Get the starting URLs of a pagination slider.
|
172
|
*
|
173
|
* @return array
|
174
|
*/
|
175
|
public function getStart()
|
176
|
{
|
177
|
return $this->paginator->getUrlRange(1, 2);
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Get the ending URLs of a pagination slider.
|
182
|
*
|
183
|
* @return array
|
184
|
*/
|
185
|
public function getFinish()
|
186
|
{
|
187
|
return $this->paginator->getUrlRange(
|
188
|
$this->lastPage() - 1,
|
189
|
$this->lastPage()
|
190
|
);
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* Determine if the underlying paginator being presented has pages to show.
|
195
|
*
|
196
|
* @return bool
|
197
|
*/
|
198
|
public function hasPages()
|
199
|
{
|
200
|
return $this->paginator->lastPage() > 1;
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* Get the current page from the paginator.
|
205
|
*
|
206
|
* @return int
|
207
|
*/
|
208
|
protected function currentPage()
|
209
|
{
|
210
|
return $this->paginator->currentPage();
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Get the last page from the paginator.
|
215
|
*
|
216
|
* @return int
|
217
|
*/
|
218
|
protected function lastPage()
|
219
|
{
|
220
|
return $this->paginator->lastPage();
|
221
|
}
|
222
|
}
|