Merge pull request #17078 from lrb2/consolidate-nav-groups

Fix #17003: Consolidate nested groups in navigation tree
This commit is contained in:
Maurício Meneghini Fauth 2021-09-11 09:37:41 -03:00 committed by GitHub
commit 3be84cf9b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -835,11 +835,13 @@ class NavigationTree
unset($prefixes[$key]);
}
$numChildren = count($node->children);
// rfe #1634 Don't group if there's only one group and no other items
if (count($prefixes) === 1) {
$keys = array_keys($prefixes);
$key = $keys[0];
if ($prefixes[$key] == count($node->children) - 1) {
if ($prefixes[$key] == $numChildren - 1) {
unset($prefixes[$key]);
}
}
@ -864,33 +866,7 @@ class NavigationTree
$this->largeGroupWarning = true;
}
$groups[$key] = new Node(
htmlspecialchars((string) $key),
Node::CONTAINER,
true
);
$groups[$key]->separator = $node->separator;
$groups[$key]->separatorDepth = $node->separatorDepth - 1;
$groups[$key]->icon = ['image' => 'b_group', 'title' => __('Groups')];
$groups[$key]->pos2 = $node->pos2;
$groups[$key]->pos3 = $node->pos3;
if (
$node instanceof NodeTableContainer
|| $node instanceof NodeViewContainer
) {
$groups[$key]->links = [
'text' => [
'route' => $node->links['text']['route'],
'params' => array_merge($node->links['text']['params'], ['tbl_group' => $key]),
],
'icon' => [
'route' => $node->links['icon']['route'],
'params' => array_merge($node->links['icon']['params'], ['tbl_group' => $key]),
],
];
}
$node->addChild($groups[$key]);
$newChildren = [];
foreach ($separators as $separator) {
$separatorLength = strlen($separator);
// FIXME: this could be more efficient
@ -932,19 +908,77 @@ class NavigationTree
$newChild->links = $child->links;
$newChild->pos2 = $child->pos2;
$newChild->pos3 = $child->pos3;
$groups[$key]->addChild($newChild);
foreach ($child->children as $elm) {
$newChild->addChild($elm);
}
$node->removeChild($child->name);
$newChildren[] = [
'node' => $newChild,
'replaces_name' => $child->name,
];
}
}
if (count($newChildren) === 0) {
continue;
}
// If the current node is a standard group (not NodeTableContainer, etc.)
// and the new group contains all of the current node's children, combine them
$class = get_class($node);
if (
count($newChildren) === $numChildren
&& substr($class, strrpos($class, '\\') + 1) === 'Node'
) {
$node->name .= $separators[0] . htmlspecialchars((string) $key);
$node->realName .= $separators[0] . htmlspecialchars((string) $key);
$node->separatorDepth--;
foreach ($newChildren as $newChild) {
$node->removeChild($newChild['replaces_name']);
$node->addChild($newChild['node']);
}
} else {
$groups[$key] = new Node(
htmlspecialchars((string) $key),
Node::CONTAINER,
true
);
$groups[$key]->separator = $node->separator;
$groups[$key]->separatorDepth = $node->separatorDepth - 1;
$groups[$key]->icon = ['image' => 'b_group', 'title' => __('Groups')];
$groups[$key]->pos2 = $node->pos2;
$groups[$key]->pos3 = $node->pos3;
if (
$node instanceof NodeTableContainer
|| $node instanceof NodeViewContainer
) {
$groups[$key]->links = [
'text' => [
'route' => $node->links['text']['route'],
'params' => array_merge($node->links['text']['params'], ['tbl_group' => $key]),
],
'icon' => [
'route' => $node->links['icon']['route'],
'params' => array_merge($node->links['icon']['params'], ['tbl_group' => $key]),
],
];
}
foreach ($newChildren as $newChild) {
$node->removeChild($newChild['replaces_name']);
$groups[$key]->addChild($newChild['node']);
}
}
}
foreach ($prefixes as $key => $value) {
$this->groupNode($groups[$key]);
$groups[$key]->classes = 'navGroup';
foreach ($groups as $group) {
if (count($group->children) === 0) {
continue;
}
$node->addChild($group);
$this->groupNode($group);
$group->classes = 'navGroup';
}
}