This repository has been archived by the owner on May 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathDouyasiPresenter.php
221 lines (195 loc) · 6.08 KB
/
DouyasiPresenter.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Douyasi\Extensions;
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
use Illuminate\Contracts\Pagination\Presenter as PresenterContract;
/**
* DouyasiPresenter
* 扩展分页样式(Laravel 5 中已经移除了分页模版)
*
* @author raoyc <[email protected]>
*/
class DouyasiPresenter implements PresenterContract
{
/**
* The paginator implementation.
*
* @var \Illuminate\Contracts\Pagination\Paginator
*/
protected $paginator;
/**
* Create a DouyasiPresenter.
*
* @param \Illuminate\Contracts\Pagination\Paginator $paginator
* @return void
*/
public function __construct(PaginatorContract $paginator)
{
$this->paginator = $paginator;
}
/**
* 是否需要分页
*
* @return bool
*/
public function hasPages()
{
return $this->paginator->hasPages() && count($this->paginator->items()) > 0;
}
/*
<div class="yas_page">
<span class="current">1</span>
<a href="http://yascmf.dev/admin/article?page=2">2</a>
<a href="http://yascmf.dev/admin/article?page=3">3</a>
<a href="http://yascmf.dev/admin/article?page=4">4</a>
</div>
*/
/**
* 渲染出分页
*
* @return string
*/
public function render()
{
if ($this->hasPages()) {
return $this->getLinks();
}
return '';
}
/**
* 当前激活页
*
* @param string $text
* @return string
*/
public function getActivePageWrapper($text)
{
return '<span class="current">'.$text.'</span>';
}
/**
* 灰色不可点击页(如 省略页)
*
* @param string $text
* @return string
*/
public function getDisabledTextWrapper($text)
{
return '<span class="ellipsis">'.$text.'</span>';
}
/**
* 其它可点击页
*
* @param string $url
* @param int $page
* @param string|null $rel
* @return string
*/
protected function getAvailablePageWrapper($url, $page, $rel = null)
{
$rel = is_null($rel) ? '' : ' rel="'.$rel.'"';
return '<a href="'.$url.'"'.$rel.'>'.$page.'</a>';
}
/**
* 省略页
*
* @return string
*/
protected function getDots()
{
return $this->getDisabledTextWrapper("...");
}
/**
* 获取当前页
*
* @return int
*/
protected function currentPage()
{
return $this->paginator->currentPage();
}
/**
* 获取最后页
*
* @return int
*/
protected function lastPage()
{
return $this->paginator->lastPage();
}
/**
* 生成特定页面URL
*
* @return string
*/
protected function getUrl($page)
{
return $this->paginator->url($page);
}
/**
* 生成分页链接[这里的分页算法逻辑可能令人迷惑,请自行琢磨]
*
* @return $string
*/
protected function getLinks()
{
$html = '<div class="yas_page">';
$cp = $this->currentPage(); //当前页
$lp = $tp = $this->lastPage(); //最后页(总页数)
if ($tp <=6) {
//总页数小于等于6,全部展示
for ($i = 1;$i <= $tp; $i++) {
if ($cp == $i) {
$html .= $this->getActivePageWrapper($cp);
} else {
$html .= $this->getAvailablePageWrapper($this->getUrl($i), $i, null);
}
}
} else {
if ($cp <= 4) {
//优先满足前区5页
/*
<[1] 2 {3} 4 5> ... 7 | <[1] 2 {3} 4 5> ... 8 | <[1] 2 {3} 4 5> ... 9
<1 [2] {3} 4 5> ... 7 | <1 [2] {3} 4 5> ... 8 | <1 [2] {3} 4 5> ...9
<1 2 {[3]} 4 5> ... 7 | <1 2 {[3]} 4 5> ... 8 | <1 2 {[3]} 4 5> ... 9
<1 2 {3} [4] 5> ... 7 | <1 2 {3} [4] 5> ... 8 | <1 2 {3} [4] 5> ... 9
*/
for ($i = 1; $i <= 5; $i++) {
if ($cp == $i) {
$html .= $this->getActivePageWrapper($cp);
} else {
$html .= $this->getAvailablePageWrapper($this->getUrl($i), $i, null);
}
}
$html .= $this->getDots().''.$this->getAvailablePageWrapper($this->getUrl($lp), $lp, null);
} else {
//优先满足后区5页
/*
1 ... <3 4 {[5]} 6 7> | 1 ... <4 [5] {6} 7 8> | 1 ... <[5] 6 {7} 8 9> | 1 ... <3 4 {[5]} 6 7> ... 10 | 1 ... <3 4 {[5]} 6 7> ... 11
1 ... <3 4 {5} [6] 7> | 1 ... <4 5 {[6]} 7 8> | 1 ... <5 [6] {7} 8 9> | 1 ... <[6] 7 {8} 9 10> | 1 ... <4 5 {[6]} 7 8> ... 11
1 ... <3 4 {5} 6 [7]> | 1 ... <4 5 {6} [7] 8> | 1 ... <5 6 {[7]} 8 9> | 1 ... <6 [7] {8} 9 10> | 1 ... <5 6 {[7]} 8 9> ... 11
*/
if ($tp <= ($cp+4)) {
$html .= $this->getAvailablePageWrapper($this->getUrl(1), 1, null).''.$this->getDots();
for ($i=$tp-4;$i<=$tp;$i++) {
if ($cp == $i) {
$html .= $this->getActivePageWrapper($cp);
} else {
$html .= $this->getAvailablePageWrapper($this->getUrl($i), $i, null);
}
}
} else {
$html .= $this->getAvailablePageWrapper($this->getUrl(1), 1, null).''.$this->getDots();
for ($i=($cp-2);$i<=($cp+2);$i++) {
if ($cp == $i) {
$html .= $this->getActivePageWrapper($cp);
} else {
$html .= $this->getAvailablePageWrapper($this->getUrl($i), $i, null);
}
}
$html .= $this->getDots().''.$this->getAvailablePageWrapper($this->getUrl($lp), $lp, null);
}
}
}
$html .= '</div>';
return $html;
}
}