Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 2c2a77b

Browse files
committed
Added custom icons feature
1 parent f5a88ad commit 2c2a77b

File tree

7 files changed

+133
-4
lines changed

7 files changed

+133
-4
lines changed

DependencyInjection/CmfTreeBrowserExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Cmf\Bundle\TreeBrowserBundle\DependencyInjection;
1313

14+
use Symfony\Component\Config\FileLocator;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
17+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1618
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1719

1820
class CmfTreeBrowserExtension extends Extension implements PrependExtensionInterface
@@ -40,5 +42,10 @@ public function prepend(ContainerBuilder $container)
4042
*/
4143
public function load(array $configs, ContainerBuilder $container)
4244
{
45+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46+
$loader->load('services.xml');
47+
48+
$config = $this->processConfiguration(new Configuration(), $configs);
49+
$container->setParameter('cmf_tree_browser.description.icon_map', $config['icons']);
4350
}
4451
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\TreeBrowserBundle\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
17+
class Configuration implements ConfigurationInterface
18+
{
19+
public function getConfigTreeBuilder()
20+
{
21+
$treeBuilder = new TreeBuilder();
22+
$rootNode = $treeBuilder->root('cmf_tree_browser');
23+
24+
$rootNode
25+
->children()
26+
->arrayNode('icons')
27+
->useAttributeAsKey('class')
28+
->prototype('scalar')->end()
29+
->info('A mapping of classes/interfaces and icon classes or icon URLs used by the tree browser.')
30+
->end()
31+
->end()
32+
;
33+
34+
return $treeBuilder;
35+
}
36+
}

Description/IconEnhancer.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\TreeBrowserBundle\Description;
13+
14+
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
15+
use Symfony\Cmf\Component\Resource\Description\Description;
16+
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
17+
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource;
18+
19+
/**
20+
* A description enhancer to add custom tree node icons.
21+
*
22+
* @author Wouter de Jong <[email protected]>
23+
*/
24+
class IconEnhancer implements DescriptionEnhancerInterface
25+
{
26+
private $iconMap;
27+
private $classMap = [];
28+
29+
public function __construct(array $iconMap)
30+
{
31+
$this->iconMap = $iconMap;
32+
}
33+
34+
public function enhance(Description $description)
35+
{
36+
$class = get_class($description->getResource()->getPayload());
37+
if (isset($this->classMap[$class])) {
38+
$class = $this->classMap[$class];
39+
}
40+
41+
$description->set('icon', $this->iconMap[$class]);
42+
}
43+
44+
public function supports(PuliResource $resource)
45+
{
46+
if (!$resource instanceof CmfResource) {
47+
return false;
48+
}
49+
50+
$payload = $resource->getPayload();
51+
$payloadClass = get_class($payload);
52+
53+
if (isset($this->iconMap[$payloadClass]) || isset($this->classMap[$payloadClass])) {
54+
return true;
55+
}
56+
57+
foreach (array_keys($this->iconMap) as $class) {
58+
if (is_a($payload, $class) || is_subclass_of($payload, $class)) {
59+
$this->classMap[$payloadClass] = $class;
60+
61+
return true;
62+
}
63+
}
64+
65+
return false;
66+
}
67+
}

Resources/assets/js/adapter/fancytree.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class FancytreeAdapter {
7171

7272
this.$tree = $elem;
7373
var actions = this.actions;
74-
var requestNode = this.requestNode;
7574
var requestNodeToFancytreeNode = (requestNode) => {
7675
if (requestNode.length === 0) {
7776
return;
@@ -92,6 +91,10 @@ export class FancytreeAdapter {
9291

9392
this.pathKeyMap[fancytreeNode.refPath] = key;
9493

94+
if (requestNode.descriptors.hasOwnProperty('icon')) {
95+
fancytreeNode.icon = requestNode.descriptors.icon;
96+
}
97+
9598
for (let actionName in actions) {
9699
var action = actions[actionName];
97100
var url = action.url;

Resources/config/services.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="cmf_tree_browser.description.icon_enhancer"
8+
class="Symfony\Cmf\Bundle\TreeBrowserBundle\Description\IconEnhancer"
9+
public="false">
10+
<argument>%cmf_tree_browser.description.icon_map%</argument>
11+
12+
<tag name="cmf_resource.description.enhancer" alias="cmf_tree_icons"/>
13+
</service>
14+
</services>
15+
16+
</container>

Resources/public/js/cmf_tree_browser.fancytree.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"require": {
1515
"php": "^5.6|^7.0",
16-
"symfony-cmf/resource-rest-bundle": "~1.0"
16+
"symfony-cmf/resource-rest-bundle": "1.0.*"
1717
},
1818
"require-dev": {
1919
"symfony/form": "^2.8|^3.0"

0 commit comments

Comments
 (0)