lib: Use navigation classes in the limiter control

refs #5543
This commit is contained in:
Eric Lippmann 2015-09-25 13:36:28 +02:00
parent 823a2cc8ea
commit 7ff74ae36a
1 changed files with 85 additions and 72 deletions

View File

@ -3,98 +3,111 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Navigation\NavigationRenderer;
use Icinga\Web\Url; use Icinga\Web\Url;
/** /**
* Limiter * Limiter control
*/ */
class Limiter extends AbstractWidget class Limiter extends AbstractWidget
{ {
/** /**
* The url * CSS class for the limiter widget
* *
* @var Url * @var string
*/ */
private $url; const CSS_CLASS_LIMITER = 'limiter-control';
private $max; /**
* Default limit
*
* @var int
*/
const DEFAULT_LIMIT = 50;
private $pages; /**
* Selectable limits
private $default; *
* @var int[]
public function setUrl(Url $url) */
{ public static $limits = array(
$this->url = $url;
return $this;
}
public function setCurrentPageCount($pages)
{
$this->pages = $pages;
return $this;
}
public function setMaxLimit($max)
{
$this->max = $max;
return $this;
}
public function setDefaultLimit($limit)
{
$this->default = $limit;
return $this;
}
public function render()
{
if ($this->url === null) {
$this->url = Url::fromRequest();
}
$currentLimit = (int) $this->url->getParam('limit', $this->default);
$availableLimits = array(
10 => '10', 10 => '10',
25 => '25', 25 => '25',
50 => '50', 50 => '50',
100 => '100', 100 => '100',
500 => '500' 500 => '500'
); );
if ($currentLimit === 0) {
$availableLimits[0] = t('all'); /**
* Default limit for this instance
*
* @var int|null
*/
protected $defaultLimit;
/**
* Get the default limit
*
* @return int
*/
public function getDefaultLimit()
{
return $this->defaultLimit !== null ? $this->defaultLimit : static::DEFAULT_LIMIT;
} }
// if ($this->pages === 1 && $currentLimit === 10) return ''; /**
* Set the default limit
*
* @param int $defaultLimit
*
* @return $this
*/
public function setDefaultLimit($defaultLimit)
{
$this->defaultLimit = (int) $defaultLimit;
return $this;
}
$limits = array(); /**
$view = $this->view(); * {@inheritdoc}
$gotCurrent = false; */
foreach ($availableLimits as $limit => $caption) { public function render()
if ($gotCurrent) { {
if ($this->pages === 1) { $url = Url::fromRequest();
// break; $activeLimit = (int) $url->getParam('limit', $this->getDefaultLimit());
} $navigation = new Navigation();
} $navigation->setLayout(Navigation::LAYOUT_TABS);
if ($this->max !== null && ($limit === 0 || $limit > $this->max)) { foreach (static::$limits as $limit => $label) {
//echo "$limit > $this->max"; break; $navigationItem = new NavigationItem();
} $navigationItem
if ($limit === $currentLimit) { ->setActive($activeLimit === $limit)
$gotCurrent = true; ->setAttribute(
$limits[] = $caption; 'title',
} else { sprintf(
$limits[] = $view->qlink( t('Show %u rows on this page'),
$caption, $limit
$this->url->setParam('limit', $limit),
null,
array(
'title' => sprintf($view->translate('Limit each page to a maximum of %u rows'), $caption)
) )
); )
->setId($limit)
->setLabel($label)
->setUrl($url->with(array('limit' => $limit)));
$navigation->addItem($navigationItem);
} }
if ($activeLimit === 0) {
$navigationItem = new NavigationItem();
$navigationItem
->setActive(true)
->setAttribute('title', t('Show all items on this page'))
->setId(0)
->setLabel(t('all'));
$navigation->addItem($navigationItem);
} }
$navigationRenderer = new NavigationRenderer($navigation);
if (empty($limits)) return ''; $navigationRenderer
return '<span class="widgetLimiter">' . implode(' ', $limits) . '</span>'; ->setCssClass(static::CSS_CLASS_LIMITER)
->setHeading(t('Limiter'));
return $navigationRenderer->render();
} }
} }