From e7fa1ebcb83b679c7139e6324e4dd4524a6ce180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 5 Aug 2022 00:24:28 +0200 Subject: [PATCH] Added an parameter to decide, if the root tree node should be expanded or not Related to issue #158, but it does not work due to a very buggy treeview... --- config/parameters.yaml | 1 + config/services.yaml | 7 +++++++ src/Helpers/Trees/TreeViewNode.php | 4 ++-- src/Services/Trees/TreeViewGenerator.php | 8 ++++++-- templates/components/tree_macros.html.twig | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/parameters.yaml b/config/parameters.yaml index 36d56906..a78d989e 100644 --- a/config/parameters.yaml +++ b/config/parameters.yaml @@ -47,6 +47,7 @@ parameters: - categories - devices - tools + partdb.sidebar.root_expanded: true # If this is set to true, the root node of the sidebar is expanded by default ###################################################################################################################### # Miscellaneous diff --git a/config/services.yaml b/config/services.yaml index 9d492f15..90f44739 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -199,6 +199,13 @@ services: tags: - { name: 'app.label_placeholder_provider', priority: 10} + #################################################################################################################### + # Trees + #################################################################################################################### + App\Services\Trees\TreeViewGenerator: + arguments: + $rootNodeExpandedByDefault: '%partdb.sidebar.root_expanded%' + #################################################################################################################### # Symfony overrides #################################################################################################################### diff --git a/src/Helpers/Trees/TreeViewNode.php b/src/Helpers/Trees/TreeViewNode.php index 41b652f5..4366b733 100644 --- a/src/Helpers/Trees/TreeViewNode.php +++ b/src/Helpers/Trees/TreeViewNode.php @@ -204,14 +204,14 @@ final class TreeViewNode implements JsonSerializable return $this; } - public function setExpanded(?bool $selected): self + public function setExpanded(?bool $selected = true): self { //Lazy loading of state, so it does not need to get serialized and transfered, when it is empty. if (null === $this->state) { $this->state = new TreeViewNodeState(); } - $this->state->setExpanded(true); + $this->state->setExpanded($selected); return $this; } diff --git a/src/Services/Trees/TreeViewGenerator.php b/src/Services/Trees/TreeViewGenerator.php index 721e8b59..310d5cdc 100644 --- a/src/Services/Trees/TreeViewGenerator.php +++ b/src/Services/Trees/TreeViewGenerator.php @@ -64,14 +64,18 @@ class TreeViewGenerator protected $keyGenerator; protected $translator; + protected $rootNodeExpandedByDefault; + public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em, - TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator, TranslatorInterface $translator) + TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator, TranslatorInterface $translator, bool $rootNodeExpandedByDefault) { $this->urlGenerator = $URLGenerator; $this->em = $em; $this->cache = $treeCache; $this->keyGenerator = $keyGenerator; $this->translator = $translator; + + $this->rootNodeExpandedByDefault = $rootNodeExpandedByDefault; } /** @@ -142,7 +146,7 @@ class TreeViewGenerator if ($mode === 'list_parts_root' ||$mode === 'devices') { $root_node = new TreeViewNode($this->translator->trans('tree.root_node.text'), null, $generic); - $root_node->setExpanded(true); + $root_node->setExpanded($this->rootNodeExpandedByDefault); $generic = [$root_node]; } diff --git a/templates/components/tree_macros.html.twig b/templates/components/tree_macros.html.twig index 37e50be3..f6b45054 100644 --- a/templates/components/tree_macros.html.twig +++ b/templates/components/tree_macros.html.twig @@ -35,7 +35,7 @@ -
+
{% endmacro %}