ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
30 {
34  public function render(Component\Component $component, RendererInterface $default_renderer): string
35  {
36  if (!$component instanceof Tree\Expandable) {
37  $this->cannotHandleComponent($component);
38  }
39 
40  $tpl_name = "tpl.tree.html";
41  $tpl = $this->getTemplate($tpl_name, true, true);
42 
43  $tpl->setVariable("ARIA_LABEL", $component->getLabel());
44 
45  $nodes = [];
46  foreach ($component->getData() as $record) {
47  $nodes[] = $this->buildNode(
48  $component->getRecursion(),
49  $record,
50  $component->getEnvironment()
51  );
52  }
53 
54  $nodes_html = $default_renderer->render($nodes);
55 
56  if ($component->isSubTree()) {
57  return $nodes_html;
58  }
59 
60  $tpl->setVariable('NODES', $nodes_html);
61 
62  $highlight_node_on_click = $component->getHighlightOnNodeClick();
63  $component = $component->withAdditionalOnLoadCode(
64  fn($id) => "il.UI.tree.init('$id', $highlight_node_on_click)"
65  );
66 
67  $id = $this->bindJavaScript($component);
68  $tpl->setVariable("ID", $id);
69 
70 
71  return $tpl->get();
72  }
73 
78  protected function buildNode(
79  Tree\TreeRecursion $recursion,
80  $record,
81  $environment
82  ): Tree\Node\Node {
83  $node = $recursion->build(
84  $this->getUIFactory()->tree()->node(),
85  $record,
86  $environment
87  );
88 
89  foreach ($recursion->getChildren($record, $environment) as $sub_record) {
90  $node = $node->withAdditionalSubnode(
91  $this->buildNode($recursion, $sub_record, $environment)
92  );
93  }
94 
95  return $node;
96  }
97 
101  public function registerResources(ResourceRegistry $registry): void
102  {
103  parent::registerResources($registry);
104  $registry->register('assets/js/tree.js');
105  }
106 }
Registry for resources required by rendered output like Javascript or CSS.
This describes a Tree Control.
Definition: Expandable.php:28
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:34
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
register(string $name)
Add a dependency.
This describes a Tree Control.
Definition: Tree.php:28
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Interface for mapping data-structures to the Tree.
buildNode(Tree\TreeRecursion $recursion, $record, $environment)
Trigger TreeRecursion::build and recurse into hierarchy by checking for further children of the recor...
Definition: Renderer.php:78
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:101
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.