in web_ui/src/applications/bistro/view/BistroPagerView.php [104:207]
public function producePhutilSafeHTML() {
if (!$this->uri) {
throw new Exception(
pht("You must call setURI() before you can call render()."));
}
require_celerity_resource('bistro-pager-view-css');
$page = (int)floor($this->getOffset() / $this->getPageSize());
$last = ((int)ceil($this->computeCount() / $this->getPageSize())) - 1;
$near = $this->surroundingPages;
$min = $page - $near;
$max = $page + $near;
// Limit the window size to no larger than the number of available pages.
if ($max - $min > $last) {
$max = $min + $last;
if ($max == $min) {
return phabricator_tag(
'div', array('class' => 'bistro-pager-view'), '');
}
}
// Slide the window so it is entirely over displayable pages.
if ($min < 0) {
$max += 0 - $min;
$min += 0 - $min;
}
if ($max > $last) {
$min -= $max - $last;
$max -= $max - $last;
}
// Build up a list of <index, label, css-class> tuples which describe the
// links we'll display, then render them all at once.
$links = array();
$prev_index = null;
$next_index = null;
if ($min > 0) {
$links[] = array(0, pht('First'), null);
}
if ($page > 0) {
$links[] = array($page - 1, pht('Prev'), null);
$prev_index = $page - 1;
}
for ($ii = $min; $ii <= $max; $ii++) {
$links[] = array($ii, $ii + 1, ($ii == $page) ? 'current' : null);
}
if ($page < $last && $last > 0) {
$links[] = array($page + 1, pht('Next'), null);
$next_index = $page + 1;
}
if ($max < ($last - 1)) {
$links[] = array($last, pht('Last'), null);
}
$base_uri = $this->uri;
$parameter = $this->pagingParameter;
if ($this->enableKeyboardShortcuts) {
$pager_links = array();
$pager_index = array(
'prev' => $prev_index,
'next' => $next_index);
foreach ($pager_index as $key => $index) {
if ($index !== null) {
$display_index = $this->getDisplayIndex($index);
$pager_links[$key] = (string)$base_uri->alter(
$parameter,
$display_index);
}
}
Javelin::initBehavior('phabricator-keyboard-pager', $pager_links);
}
// Convert tuples into rendered nodes.
$rendered_links = array();
foreach ($links as $link) {
list($index, $label, $class) = $link;
$display_index = $this->getDisplayIndex($index);
$link = $base_uri->alter($parameter, $display_index);
$rendered_links[] = phabricator_tag(
'a',
array(
'href' => $link,
'class' => $class),
$label);
}
return phabricator_tag(
'div',
array('class' => 'bistro-pager-view'),
$rendered_links);
}