From f9aba0c7ffad02c0579cd6e54d6e2b69cdef3e1a Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 8 Jun 2022 12:57:32 +0200 Subject: [PATCH] Disable drag&drop events for the default home --- .../Web/Dashboard/Common/DashboardManager.php | 2 +- .../Web/Dashboard/Common/ItemListControl.php | 15 ++++++++++++++- library/Icinga/Web/Dashboard/Dashboard.php | 2 +- library/Icinga/Web/Dashboard/DashboardHome.php | 10 ++++++++++ .../Web/Dashboard/ItemList/DashboardHomeList.php | 14 ++++++++++++++ library/Icinga/Web/Dashboard/Settings.php | 2 +- public/js/icinga/behavior/dashboards.js | 5 ++++- 7 files changed, 45 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Web/Dashboard/Common/DashboardManager.php b/library/Icinga/Web/Dashboard/Common/DashboardManager.php index a2d6e5b47..f97c84b78 100644 --- a/library/Icinga/Web/Dashboard/Common/DashboardManager.php +++ b/library/Icinga/Web/Dashboard/Common/DashboardManager.php @@ -170,7 +170,7 @@ trait DashboardManager 'user_id' => self::getUser()->getAdditional('id'), 'name' => $home->getName(), 'label' => $home->getTitle(), - 'priority' => $home->getName() === DashboardHome::DEFAULT_HOME ? 0 : $priority++, + 'priority' => $home->isDefaultHome() ? 0 : $priority++, 'type' => $home->getType() !== Dashboard::SYSTEM ? $home->getType() : Dashboard::PRIVATE_DS ]); diff --git a/library/Icinga/Web/Dashboard/Common/ItemListControl.php b/library/Icinga/Web/Dashboard/Common/ItemListControl.php index a26870070..bd2d64d14 100644 --- a/library/Icinga/Web/Dashboard/Common/ItemListControl.php +++ b/library/Icinga/Web/Dashboard/Common/ItemListControl.php @@ -44,6 +44,16 @@ abstract class ItemListControl extends BaseHtmlElement */ abstract protected function createItemList(): BaseHtmlElement; + /** + * Get whether to render the drag initiator icon bars + * + * @return bool + */ + protected function shouldRenderDragInitiator(): bool + { + return true; + } + /** * Get a drag initiator for this widget item * @@ -76,7 +86,10 @@ abstract class ItemListControl extends BaseHtmlElement ])); $header->addHtml(HtmlElement::create('div', ['class' => 'spacer'])); - $header->addHtml(self::createDragInitiator()); + if ($this->shouldRenderDragInitiator()) { + $header->addHtml(self::createDragInitiator()); + } + $this->addHtml($header); } diff --git a/library/Icinga/Web/Dashboard/Dashboard.php b/library/Icinga/Web/Dashboard/Dashboard.php index 95f47987e..101d63b0b 100644 --- a/library/Icinga/Web/Dashboard/Dashboard.php +++ b/library/Icinga/Web/Dashboard/Dashboard.php @@ -152,7 +152,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry protected function assemble() { $activeHome = $this->getActiveHome(); - if (! $activeHome || (! $activeHome->hasEntries() && $activeHome->getName() === DashboardHome::DEFAULT_HOME)) { + if (! $activeHome || (! $activeHome->hasEntries() && $activeHome->isDefaultHome())) { $this->setAttribute('class', 'dashboard-introduction'); $this->addHtml(HtmlElement::create('h2', null, t('Welcome to Icinga Web 2!'))); diff --git a/library/Icinga/Web/Dashboard/DashboardHome.php b/library/Icinga/Web/Dashboard/DashboardHome.php index aa675f212..d6e256137 100644 --- a/library/Icinga/Web/Dashboard/DashboardHome.php +++ b/library/Icinga/Web/Dashboard/DashboardHome.php @@ -63,6 +63,16 @@ class DashboardHome extends BaseDashboard implements Sortable return $self; } + /** + * Get whether this home is the default one + * + * @return bool + */ + public function isDefaultHome(): bool + { + return $this->getName() === self::DEFAULT_HOME; + } + /** * Set the type of this dashboard home * diff --git a/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php b/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php index af0f467ee..6787f3659 100644 --- a/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php +++ b/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php @@ -26,6 +26,12 @@ class DashboardHomeList extends ItemListControl public function __construct(DashboardHome $home) { $this->home = $home; + + $this->init(); + } + + protected function init() + { if (! $this->home->hasEntries()) { // Just retry to load dashboards $this->home->loadDashboardEntries(); @@ -34,6 +40,9 @@ class DashboardHomeList extends ItemListControl $this->getAttributes() ->registerAttributeCallback('data-icinga-home', function () { return $this->home->getName(); + }) + ->registerAttributeCallback('data-disable-widget-sorting', function () { + return $this->home->isDefaultHome(); }); } @@ -61,6 +70,11 @@ class DashboardHomeList extends ItemListControl return $this->home->isActive(); } + protected function shouldRenderDragInitiator(): bool + { + return ! $this->home->isDefaultHome(); + } + protected function createItemList(): BaseHtmlElement { if (! $this->headerDisabled) { diff --git a/library/Icinga/Web/Dashboard/Settings.php b/library/Icinga/Web/Dashboard/Settings.php index a1db39b1e..9f627e0aa 100644 --- a/library/Icinga/Web/Dashboard/Settings.php +++ b/library/Icinga/Web/Dashboard/Settings.php @@ -24,7 +24,7 @@ class Settings extends BaseHtmlElement protected function assemble() { $activeHome = $this->dashboard->getActiveHome(); - if (count($this->dashboard->getEntries()) === 1 && $activeHome->getName() === DashboardHome::DEFAULT_HOME) { + if (count($this->dashboard->getEntries()) === 1 && $activeHome->isDefaultHome()) { $this->setAttribute('data-icinga-home', DashboardHome::DEFAULT_HOME); $this->addFrom((new DashboardHomeList($activeHome))->setHeaderDisabled()); } else { diff --git a/public/js/icinga/behavior/dashboards.js b/public/js/icinga/behavior/dashboards.js index 0201b0e59..2136d2360 100644 --- a/public/js/icinga/behavior/dashboards.js +++ b/public/js/icinga/behavior/dashboards.js @@ -166,7 +166,10 @@ draggable : draggable, handle : '.widget-drag-initiator', group : { name : groupName }, - chosenClass : 'draggable-widget-chosen' + chosenClass : 'draggable-widget-chosen', + onMove : function (evt, orgEvt) { + return ! evt.related.matches('[data-disable-widget-sorting]'); + } }; _this.Sortable.create(sortable, options);