Show a treeview in the admin menus, to select between the different elements.

This commit is contained in:
Jan Böhmer 2019-04-05 17:49:02 +02:00
parent 928b574d8c
commit 650b388a1d
16 changed files with 269 additions and 25 deletions

View file

@ -508,6 +508,11 @@ span.highlight {
line-height: 1.428;
}
.treeview-sm .list-group-item {
padding-top: 3px;
padding-bottom: 3px;
}
/*****************************
* Pagination bar
*****************************/

View file

@ -24,7 +24,7 @@ import 'datatables.net-select-bs4/css/select.bootstrap4.css'
import 'bootstrap-select/dist/css/bootstrap-select.css'
import 'awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css'
import "bootstrap-treeview/src/css/bootstrap-treeview.css"
import "patternfly-bootstrap-treeview/src/css/bootstrap-treeview.css"
//require( 'jszip' );
//#require( 'pdfmake' );
@ -39,12 +39,11 @@ require( 'datatables.net-select-bs4' );
require('bootstrap-select');
require('jquery-form');
//require('bootstrap-treeview/src/js/bootstrap-treeview');
//Define jquery globally
window.$ = window.jQuery = require("jquery");
require('bootstrap-treeview/src/js/bootstrap-treeview');
require('patternfly-bootstrap-treeview/src/js/bootstrap-treeview')
require('./datatables.js');

View file

@ -217,7 +217,10 @@ class AjaxUI {
}
},
//onNodeContextmenu: contextmenu_handler,
expandIcon: "fas fa-plus fa-fw fa-treeview", collapseIcon: "fas fa-minus fa-fw fa-treeview"}).treeview('collapseAll', { silent: true });
expandIcon: "fas fa-plus fa-fw fa-treeview", collapseIcon: "fas fa-minus fa-fw fa-treeview"})
.on('initialized', function() {
$(this).treeview('collapseAll', { silent: true });
});
});
}

View file

@ -28,6 +28,8 @@
*
*/
import {ajaxUI} from "./ajax_ui";
/************************************
*
* In this file all the functions that has to be called using AjaxUIoperation are registered.
@ -123,10 +125,36 @@ $(document).on("ajaxUI:reload", function () {
$(document).on("ajaxUI:start ajaxUI:reload", function () {
$(".tooltip").remove();
$('a[title], button[title], span[title], h6[title]')
//@ts-ignore
//@ts-ignore
.tooltip("hide").tooltip({container: "body", placement: "auto", boundary: 'window'});
});
// Add bootstrap treeview on divs with data-tree-data attribute
$(document).on("ajaxUI:start ajaxUI:reload", function() {
$("[data-tree-data]").each(function(index, element) {
let data = $(element).data('treeData');
//@ts-ignore
$(element).treeview({
data: data,
enableLinks: false,
showIcon: false,
showBorder: true,
expandIcon: "fas fa-plus fa-fw fa-treeview", collapseIcon: "fas fa-minus fa-fw fa-treeview",
onNodeSelected: function(event, data) {
if(data.href) {
ajaxUI.navigateTo(data.href);
}
}
}).on('initialized', function() {
$(this).treeview('collapseAll', { silent: true });
let selected = $(this).treeview('getSelected');
$(this).treeview('revealNode', [ selected, {silent: true } ]);
});
});
});
/**
* Register the button, to jump to the top of the page.

View file

@ -24,7 +24,6 @@
"@types/js-cookie": "^2.2.1",
"awesome-bootstrap-checkbox": "^1.0.1",
"bootstrap-select": "^1.13.8",
"bootstrap-treeview": "jbtronics/bootstrap-treeview",
"datatables.net-bs4": "^1.10.19",
"datatables.net-buttons-bs4": "^1.5.4",
"datatables.net-fixedheader-bs4": "^3.1.5",
@ -32,6 +31,7 @@
"jquery-form": "^4.2.2",
"js-cookie": "^2.2.0",
"jszip": "^3.2.0",
"patternfly-bootstrap-treeview": "^2.1.8",
"pdfmake": "^0.1.53",
"ts-loader": "^5.3.3",
"typescript": "^3.3.4000"

View file

@ -67,7 +67,7 @@ class AttachmentTypeController extends AbstractController
}
/**
* @Route("/new")
* @Route("/new", name="attachment_type_new")
*
* @return \Symfony\Component\HttpFoundation\Response
*/

View file

@ -91,7 +91,7 @@ class Attachment extends NamedDBElement
*
* @return DBElement The associated Element.
*/
public function getElement(): AttachmentContainingDBElement
public function getElement(): ?AttachmentContainingDBElement
{
return $this->element;
}

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ -33,9 +34,9 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
/**
* @var
* //TODO
* //@ORM\OneToMany(targetEntity="Attachment", mappedBy="element")
* @ORM\OneToMany(targetEntity="Attachment", mappedBy="element")
*/
protected $attachment;
protected $attachments;
//TODO
protected $attachmentTypes;
@ -54,7 +55,7 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*
* @throws Exception if there was an error
*/
public function getAttachmentTypes(): array
public function getAttachmentTypes(): ?array
{
return $this->attachmentTypes;
}
@ -70,7 +71,7 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*
* @throws Exception if there was an error
*/
public function getAttachments($type_id = null, bool $only_table_attachements = false): array
public function getAttachments($type_id = null, bool $only_table_attachements = false) : Collection
{
if ($only_table_attachements || $type_id) {
$attachements = $this->attachments;

View file

@ -25,6 +25,7 @@ namespace App\Entity;
use App\Validator\Constraints\NoneOfItsChildren;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ -58,7 +59,7 @@ class AttachmentType extends StructuralDBElement
* @return Attachment[] all attachements with this type, as a one-dimensional array of Attachement-objects
* (sorted by their names)
*/
public function getAttachementsForType(): ArrayCollection
public function getAttachementsForType(): Collection
{
// the attribute $this->attachements is used from class "AttachementsContainingDBELement"
if (null === $this->attachments) {

View file

@ -40,6 +40,8 @@ class TreeViewNode
protected $href;
protected $nodes;
protected $state;
/**
* Creates a new TreeView node with the given parameters.
* @param string $text The text that is shown in the node. (e.g. the name of the node)
@ -53,6 +55,8 @@ class TreeViewNode
$this->text = $text;
$this->href = $href;
$this->nodes = $nodes;
$this->state = new TreeViewNodeState();
}
/**
@ -115,4 +119,29 @@ class TreeViewNode
return $this;
}
public function getState() : TreeViewNodeState
{
return $this->state;
}
public function setState(TreeViewNodeState $state) : self
{
$this->state = $state;
return $this;
}
public function setDisabled(?bool $disabled) : self
{
$this->state->setDisabled($disabled);
return $this;
}
public function setSelected(?bool $selected) : self
{
$this->state->setSelected($selected);
return $this;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Helpers;
class TreeViewNodeState
{
/** @var null|bool */
protected $checked = null;
/** @var null|bool */
protected $disabled = null;
/** @var null|bool */
protected $expanded = null;
/** @var null|bool */
protected $selected = null;
/**
* @return bool|null
*/
public function getDisabled(): ?bool
{
return $this->disabled;
}
/**
* @param bool|null $disabled
*/
public function setDisabled(?bool $disabled): void
{
$this->disabled = $disabled;
}
/**
* @return bool|null
*/
public function getExpanded(): ?bool
{
return $this->expanded;
}
/**
* @param bool|null $expanded
*/
public function setExpanded(?bool $expanded): void
{
$this->expanded = $expanded;
}
/**
* @return bool|null
*/
public function getSelected(): ?bool
{
return $this->selected;
}
/**
* @param bool|null $selected
*/
public function setSelected(?bool $selected): void
{
$this->selected = $selected;
}
}

View file

@ -29,6 +29,7 @@
namespace App\Services;
use App\Entity\AttachmentType;
use App\Entity\Category;
use App\Entity\NamedDBElement;
use App\Entity\Part;
@ -107,6 +108,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
}
if($entity instanceof AttachmentType) {
return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]);
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -124,6 +129,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('part_new');
}
if($entity instanceof AttachmentType) {
return $this->urlGenerator->generate('attachment_type_new');
}
throw new EntityNotSupported('The given entity is not supported yet!');
}

View file

@ -31,10 +31,12 @@
namespace App\Services;
use App\Entity\DBElement;
use App\Entity\StructuralDBElement;
use App\Helpers\TreeViewNode;
use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This service gives you multiple possibilities to generate trees.
@ -45,11 +47,13 @@ class TreeBuilder
{
protected $url_generator;
protected $em;
protected $translator;
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em)
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->url_generator = $URLGenerator;
$this->em = $em;
$this->translator = $translator;
}
/**
@ -57,15 +61,18 @@ class TreeBuilder
* @param StructuralDBElement $element The element for which the tree should be generated.
* @param string $href_type The type of the links that should be used for the links. Set to null, to disable links.
* See EntityURLGenerator::getURL for possible types.
* @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in
* the resulting tree. When $selectedElement is not existing in the tree, then nothing happens.
* @return TreeViewNode The Node for the given Element.
* @throws \App\Exceptions\EntityNotSupported
*/
public function elementToTreeNode(StructuralDBElement $element, ?string $href_type = 'list_parts') : TreeViewNode
public function elementToTreeNode(StructuralDBElement $element, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : TreeViewNode
{
$children = $element->getSubelements();
$children_nodes = null;
foreach ($children as $child) {
$children_nodes[] = $this->elementToTreeNode($child, $href_type);
$children_nodes[] = $this->elementToTreeNode($child, $href_type, $selectedElement);
}
//Check if we need to generate a href type
@ -75,7 +82,14 @@ class TreeBuilder
$href = $this->url_generator->getURL($element, $href_type);
}
return new TreeViewNode($element->getName(), $href, $children_nodes);
$tree_node = new TreeViewNode($element->getName(), $href, $children_nodes);
//Check if we need to select the current part
if ($selectedElement !== null && $element->getID() === $selectedElement->getID()) {
$tree_node->setSelected(true);
}
return $tree_node;
}
/**
@ -84,9 +98,12 @@ class TreeBuilder
* be generated.
* @param string $href_type The type of the links that should be used for the links. Set to null, to disable links.
* See EntityURLGenerator::getURL for possible types.
* @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in
* the resulting tree. When $selectedElement is not existing in the tree, then nothing happens.
* @return TreeViewNode[] Returns an array, containing all nodes. It is empty if the given class has no elements.
* @throws \App\Exceptions\EntityNotSupported
*/
public function typeToTree(string $class_name, ?string $href_type = 'list_parts') : array
public function typeToTree(string $class_name, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : array
{
/**
* @var $repo StructuralDBElementRepository
@ -95,8 +112,27 @@ class TreeBuilder
$root_nodes = $repo->findRootNodes();
$array = array();
//When we use the newEdit type, add the New Element node.
if ($href_type === 'newEdit') {
//Generate the url for the new node
$href = $this->url_generator->createURL(new $class_name());
$new_node = new TreeViewNode($this->translator->trans('entity.tree.new'), $href);
//When the id of the selected element is null, then we have a new element, and we need to select "new" node
if ($selectedElement != null && $selectedElement->getID() == null) {
$new_node->setSelected(true);
}
$array[] = $new_node;
//Add spacing
$array[] = (new TreeViewNode(''))->setDisabled(true);
//Every other treeNode will be used for edit
$href_type = "edit";
}
foreach ($root_nodes as $node) {
$array[] = $this->elementToTreeNode($node, $href_type);
$array[] = $this->elementToTreeNode($node, $href_type, $selectedElement);
}
return $array;

View file

@ -29,22 +29,31 @@
namespace App\Twig;
use App\Entity\Attachment;
use App\Entity\DBElement;
use App\Services\EntityURLGenerator;
use App\Services\TreeBuilder;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
protected $cache;
protected $serializer;
protected $treeBuilder;
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache)
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
SerializerInterface $serializer, TreeBuilder $treeBuilder)
{
$this->entityURLGenerator = $entityURLGenerator;
$this->cache = $cache;
$this->serializer = $serializer;
$this->treeBuilder = $treeBuilder;
}
public function getFilters()
@ -55,6 +64,19 @@ class AppExtension extends AbstractExtension
];
}
public function getFunctions()
{
return [
new TwigFunction('generateTreeData', [$this, 'treeData'])
];
}
public function treeData(DBElement $element, string $type = 'newEdit') : string
{
$tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element);
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
}
public function generateEntityURL(DBElement $entity, string $method = 'info'): string
{
return $this->entityURLGenerator->getURL($entity, $method);

View file

@ -4,6 +4,10 @@
<div class="row">
<div class="col-4">
<div class="treeview-sm" id="tree" data-tree-data="{{ generateTreeData(entity) }}">
</div>
</div>
<div class="col-8">

View file

@ -1234,9 +1234,10 @@ bootstrap-select@^1.13.8:
resolved "https://registry.yarnpkg.com/bootstrap-select/-/bootstrap-select-1.13.8.tgz#c47047410511f6d8a8917a7738d1b03c95717d5d"
integrity sha512-WF6GJPgb98cv75X6/zoCWUNwM/HPoYKP6JDBryTNDCHMPV9YyMqU7xXncC7Se2M5VAt7kZQ/i2GSYwaf5Q/Z3w==
bootstrap-treeview@jbtronics/bootstrap-treeview:
version "1.2.1"
resolved "https://codeload.github.com/jbtronics/bootstrap-treeview/tar.gz/0855394d4fa06c99289d2ba9a164a987228456c5"
bootstrap@3.4.x:
version "3.4.1"
resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-3.4.1.tgz#c3a347d419e289ad11f4033e3c4132b87c081d72"
integrity sha512-yN5oZVmRCwe5aKwzRj6736nSmKDX7pLYwsXiCj/EYmo16hODaBiT4En5btW/jhBF/seV+XMx3aYwukYC3A49DA==
bootstrap@^4.3.1:
version "4.3.1"
@ -3664,7 +3665,7 @@ jquery-form@^4.2.2:
dependencies:
jquery ">=1.7.2"
jquery@>=1.7, jquery@>=1.7.2, jquery@^3.3.1:
"jquery@>= 2.1.x", jquery@>=1.7, jquery@>=1.7.2, jquery@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.3.1.tgz#958ce29e81c9790f31be7792df5d4d95fc57fbca"
integrity sha512-Ubldcmxp5np52/ENotGxlLe6aGMvmF4R8S6tZjsP6Knsaxd/xp3Zrh50cG93lR6nPXyUFwzN3ZSOQI0wRJNdGg==
@ -4809,6 +4810,14 @@ path-type@^2.0.0:
dependencies:
pify "^2.0.0"
patternfly-bootstrap-treeview@^2.1.8:
version "2.1.8"
resolved "https://registry.yarnpkg.com/patternfly-bootstrap-treeview/-/patternfly-bootstrap-treeview-2.1.8.tgz#4a79e245e8421578b6ce89a52c94596f523ae16f"
integrity sha512-Z3v+zJ0AEhMiySyj7qgUs4yGM8afJwjUUWgwSosWI9xpMsyJV+B+I+GzgRoGiukzh14EOl3EiX4qqMBC+nwqXQ==
dependencies:
bootstrap "3.4.x"
jquery ">= 2.1.x"
pbkdf2@^3.0.3:
version "3.0.17"
resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.0.17.tgz#976c206530617b14ebb32114239f7b09336e93a6"