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...
This commit is contained in:
Jan Böhmer 2022-08-05 00:24:28 +02:00
parent cb16819340
commit e7fa1ebcb8
5 changed files with 17 additions and 5 deletions

View file

@ -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

View file

@ -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
####################################################################################################################

View file

@ -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;
}

View file

@ -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];
}

View file

@ -35,7 +35,7 @@
<input type="search" class="form-control bg-light border-0" placeholder="{% trans %}search.placeholder{% endtrans %}" {{ stimulus_action('elements/sidebar_tree', 'searchInput') }}>
</div>
<div id="tree-categories" {{ stimulus_target('elements/sidebar_tree', 'tree') }}></div>
<div id="{{ id }}Tree" {{ stimulus_target('elements/sidebar_tree', 'tree') }}></div>
</div>
{% endmacro %}