forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAphrontCursorPagerView.php
189 lines (153 loc) · 4.11 KB
/
AphrontCursorPagerView.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
<?php
final class AphrontCursorPagerView extends AphrontView {
private $afterID;
private $beforeID;
private $pageSize = 100;
private $nextPageID;
private $prevPageID;
private $moreResults;
private $uri;
public function setPageSize($page_size) {
$this->pageSize = max(1, $page_size);
return $this;
}
public function getPageSize() {
return $this->pageSize;
}
public function setURI(PhutilURI $uri) {
$this->uri = $uri;
return $this;
}
public function readFromRequest(AphrontRequest $request) {
$this->uri = $request->getRequestURI();
$this->afterID = $request->getStr('after');
$this->beforeID = $request->getStr('before');
return $this;
}
public function setAfterID($after_id) {
$this->afterID = $after_id;
return $this;
}
public function getAfterID() {
return $this->afterID;
}
public function setBeforeID($before_id) {
$this->beforeID = $before_id;
return $this;
}
public function getBeforeID() {
return $this->beforeID;
}
public function setNextPageID($next_page_id) {
$this->nextPageID = $next_page_id;
return $this;
}
public function getNextPageID() {
return $this->nextPageID;
}
public function setPrevPageID($prev_page_id) {
$this->prevPageID = $prev_page_id;
return $this;
}
public function getPrevPageID() {
return $this->prevPageID;
}
public function sliceResults(array $results) {
if (count($results) > $this->getPageSize()) {
$offset = ($this->beforeID ? count($results) - $this->getPageSize() : 0);
$results = array_slice($results, $offset, $this->getPageSize(), true);
$this->moreResults = true;
}
return $results;
}
public function getHasMoreResults() {
return $this->moreResults;
}
public function willShowPagingControls() {
return $this->prevPageID ||
$this->nextPageID ||
$this->afterID ||
($this->beforeID && $this->moreResults);
}
public function getFirstPageURI() {
if (!$this->uri) {
throw new PhutilInvalidStateException('setURI');
}
if (!$this->afterID && !($this->beforeID && $this->moreResults)) {
return null;
}
return $this->uri
->alter('before', null)
->alter('after', null);
}
public function getPrevPageURI() {
if (!$this->uri) {
throw new PhutilInvalidStateException('getPrevPageURI');
}
if (!$this->prevPageID) {
return null;
}
return $this->uri
->alter('after', null)
->alter('before', $this->prevPageID);
}
public function getNextPageURI() {
if (!$this->uri) {
throw new PhutilInvalidStateException('setURI');
}
if (!$this->nextPageID) {
return null;
}
return $this->uri
->alter('after', $this->nextPageID)
->alter('before', null);
}
public function render() {
if (!$this->uri) {
throw new PhutilInvalidStateException('setURI');
}
$links = array();
$first_uri = $this->getFirstPageURI();
if ($first_uri) {
$icon = id(new PHUIIconView())
->setIcon('fa-fast-backward');
$links[] = id(new PHUIButtonView())
->setTag('a')
->setHref($first_uri)
->setIcon($icon)
->addClass('mml')
->setColor(PHUIButtonView::GREY)
->setText(pht('First'));
}
$prev_uri = $this->getPrevPageURI();
if ($prev_uri) {
$icon = id(new PHUIIconView())
->setIcon('fa-backward');
$links[] = id(new PHUIButtonView())
->setTag('a')
->setHref($prev_uri)
->setIcon($icon)
->addClass('mml')
->setColor(PHUIButtonView::GREY)
->setText(pht('Prev'));
}
$next_uri = $this->getNextPageURI();
if ($next_uri) {
$icon = id(new PHUIIconView())
->setIcon('fa-forward');
$links[] = id(new PHUIButtonView())
->setTag('a')
->setHref($next_uri)
->setIcon($icon, false)
->addClass('mml')
->setColor(PHUIButtonView::GREY)
->setText(pht('Next'));
}
return phutil_tag(
'div',
array(
'class' => 'phui-pager-view',
),
$links);
}
}