Move pane related codes from Dashboard -> DashboardHome class

This commit is contained in:
Yonas Habteab 2022-06-01 14:39:49 +02:00
parent d277c5ba3b
commit d0d7e4ebd1
2 changed files with 47 additions and 59 deletions

View File

@ -153,64 +153,6 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
return $this->tabs;
}
/**
* Activate the default pane of this dashboard and returns its name
*
* @return ?string
*/
private function setDefaultPane()
{
$active = $this->getActiveHome()->rewindEntries();
if ($active) {
$active = $active->getName();
$this->activate($active);
}
return $active;
}
/**
* @see determineActivePane()
*/
public function getActivePane()
{
return $this->determineActivePane();
}
/**
* Determine the active pane either by the selected tab or the current request
*
* @return Pane The currently active pane
* @throws \Icinga\Exception\ProgrammingError
*
* @throws \Icinga\Exception\ConfigurationError
*/
public function determineActivePane()
{
$active = $this->getTabs()->getActiveTab();
$activeHome = $this->getActiveHome();
if (! $active) {
if ($active = Url::fromRequest()->getParam($this->tabParam)) {
if ($activeHome->hasEntry($active)) {
$this->activate($active);
} else {
throw new ProgrammingError('Try to get an inexistent pane.');
}
} else {
$active = $this->setDefaultPane();
}
} else {
$active = $active->getName();
}
if ($activeHome->hasEntry($active)) {
return $activeHome->getEntry($active);
}
throw new ConfigurationError('Could not determine active pane');
}
protected function assemble()
{
$activeHome = $this->getActiveHome();
@ -238,7 +180,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
} elseif (! $activeHome->hasEntries()) {
$this->addHtml(HtmlElement::create('h1', null, t('No dashboard added to this dashboard home.')));
} else {
$activePane = $this->getActivePane();
$activePane = $activeHome->getActivePane($this->getTabs());
if (! $activePane->hasEntries()) {
$this->addHtml(HtmlElement::create('h1', null, t('No dashlet added to this pane.')));

View File

@ -4,6 +4,7 @@
namespace Icinga\Web\Dashboard;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError;
use Icinga\Model\Home;
use Icinga\Web\Dashboard\Common\BaseDashboard;
@ -13,6 +14,8 @@ use Icinga\Util\DBUtils;
use Icinga\Web\Dashboard\Common\WidgetState;
use ipl\Stdlib\Filter;
use ipl\Web\Url;
use ipl\Web\Widget\Tabs;
use function ipl\Stdlib\get_php_type;
class DashboardHome extends BaseDashboard implements Sortable
@ -118,6 +121,49 @@ class DashboardHome extends BaseDashboard implements Sortable
return $this->type;
}
/**
* @see determineActivePane()
*/
public function getActivePane(Tabs $tabs): Pane
{
return $this->determineActivePane($tabs);
}
/**
* Determine the active pane either by the selected tab or the current request
*
* @param Tabs $tabs
*
* @return Pane
*/
public function determineActivePane(Tabs $tabs): Pane
{
$activeTab = $tabs->getActiveTab();
if ($activeTab) {
$pane = $activeTab->getName();
} else {
if (! ($pane = Url::fromRequest()->getParam('pane'))) {
if (($firstPane = $this->rewindEntries())) {
$tabs->activate($firstPane->getName());
$pane = $firstPane->getName();
}
} else {
if ($this->hasEntry($pane)) {
$tabs->activate($pane);
} else {
throw new ProgrammingError('Try to get an inexistent pane.');
}
}
}
if ($pane && $this->hasEntry($pane)) {
return $this->getEntry($pane);
}
throw new ConfigurationError('Could not determine active pane');
}
public function removeEntry($pane)
{
$name = $pane instanceof Pane ? $pane->getName() : $pane;