ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Renderer.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Alexander Killing <killing@leifos.de> Extended GPL, see docs/LICENSE */
4 
6 
11 
13 {
17  public function render(Component\Component $component, RendererInterface $default_renderer)
18  {
19  $this->checkComponent($component);
20 
21  return $this->renderDropdown($component, $default_renderer);
22  }
23 
24  protected function renderDropdown(Component\Dropdown\Dropdown $component, RendererInterface $default_renderer)
25  {
26 
27  // get template
28  $tpl_name = "tpl.standard.html";
29  $tpl = $this->getTemplate($tpl_name, true, true);
30 
31  // render items
32  $items = $component->getItems();
33  if (count($items) == 0) {
34  return "";
35  }
36  $this->renderItems($items, $tpl, $default_renderer);
37 
38  // render trigger button
39  $label = $component->getLabel();
40  if ($label !== null) {
41  $tpl->setVariable("LABEL", $component->getLabel());
42  } else {
43  $tpl->setVariable("LABEL", "");
44  }
45 
46  // ensure that a) a separate aria label may be provided and
47  // b) that an empty label and empty aria-label will use the "actions" fallback
48  if ($component->getLabel() == "" || $component->getAriaLabel() != "") {
49  $aria_label = ($component->getAriaLabel() != "")
50  ? $component->getAriaLabel()
51  : $this->txt("actions");
52  $tpl->setCurrentBlock("aria_label");
53  $tpl->setVariable("ARIA_LABEL", $aria_label);
54  $tpl->parseCurrentBlock();
55  }
56 
57  $this->maybeRenderId($component, $tpl, "with_id", "ID");
58 
59  return $tpl->get();
60  }
61 
66  protected function renderItems($items, $tpl, $default_renderer)
67  {
68  foreach ($items as $item) {
69  $tpl->setCurrentBlock("item");
70  $tpl->setVariable("ITEM", $default_renderer->render($item));
71  $tpl->parseCurrentBlock();
72  }
73  }
74 
75 
76  protected function maybeRenderId(Component\Component $component, $tpl, $block, $template_var)
77  {
78  $id = $this->bindJavaScript($component);
79  if ($id !== null) {
80  $tpl->setCurrentBlock($block);
81  $tpl->setVariable($template_var, $id);
82  $tpl->parseCurrentBlock();
83  }
84  }
85 
86 
94  public function withBlocksToBeTouched($block)
95  {
96  assert(is_string($block));
97  $clone = clone $this;
98  $clone->touch_blocks[] = $block;
99  return $clone;
100  }
101 
102 
106  public function registerResources(ResourceRegistry $registry)
107  {
108  parent::registerResources($registry);
109  $registry->register('./src/UI/templates/js/Dropdown/dropdown.js');
110  }
111 
115  protected function getComponentInterfaceName()
116  {
117  return array(Component\Dropdown\Standard::class
118  );
119  }
120 }
withBlocksToBeTouched($block)
Append a block to touch during rendering and return cloned instance.
Definition: Renderer.php:94
renderDropdown(Component\Dropdown\Dropdown $component, RendererInterface $default_renderer)
Definition: Renderer.php:24
Registry for resources required by rendered output like Javascript or CSS.
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.null
Definition: Renderer.php:106
$tpl
Definition: ilias.php:10
if(!array_key_exists('StateId', $_REQUEST)) $id
renderItems($items, $tpl, $default_renderer)
Definition: Renderer.php:66
maybeRenderId(Component\Component $component, $tpl, $block, $template_var)
Definition: Renderer.php:76
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:17
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
This implements commonalities between different types of Dropdowns.
Definition: Dropdown.php:16
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.