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 
27 
33 {
37  public function render(Component\Component $component, RendererInterface $default_renderer): string
38  {
39  if (!$component instanceof Component\Image\Image) {
40  $this->cannotHandleComponent($component);
41  }
42 
43  $tpl = $this->getTemplate("tpl.image.html", true, true);
44 
45  if (($sources = $component->getAdditionalHighResSources()) !== []) {
46  $component = $component->withAdditionalOnLoadCode(
47  fn(string $id): string => "
48  const imageElement = il.UI.image.getImageElement('$id');
49  if (imageElement === null) {
50  throw new Error(`Image '$id' not found.`);
51  }
52  il.UI.image.loadHighResolutionSource(
53  imageElement,
54  {$this->getHighResSourceMapForJs($sources)}
55  );
56  "
57  );
58  }
59 
60  $id = $this->bindJavaScript($component);
61  if (!empty($component->getAction())) {
62  $tpl->touchBlock("action_begin");
63 
64  if (is_string($component->getAction())) {
65  $tpl->setCurrentBlock("with_href");
66  $tpl->setVariable("HREF", $component->getAction());
67  $tpl->parseCurrentBlock();
68  }
69 
70  if (is_array($component->getAction())) {
71  $tpl->setCurrentBlock("with_href");
72  $tpl->setVariable("HREF", "#");
73  $tpl->parseCurrentBlock();
74  $tpl->setCurrentBlock("with_id");
75  $tpl->setVariable("ID", $id);
76  $tpl->parseCurrentBlock();
77  }
78  }
79 
80  if (!is_array($component->getAction()) && $id !== null) {
81  $tpl->setVariable("IMG_ID", " id='" . $id . "' ");
82  }
83 
84  $tpl->setCurrentBlock($component->getType());
85  $tpl->setVariable("SOURCE", $component->getSource());
86  $tpl->setVariable("ALT", htmlspecialchars($component->getAlt()));
87  $tpl->parseCurrentBlock();
88 
89  if (!empty($component->getAction())) {
90  $tpl->touchBlock("action_end");
91  }
92 
93  return $tpl->get();
94  }
95 
99  public function registerResources(ResourceRegistry $registry): void
100  {
101  parent::registerResources($registry);
102  $registry->register('assets/js/image.min.js');
103  }
104 
108  protected function getHighResSourceMapForJs(array $resources): string
109  {
110  $map_entries = '';
111  foreach ($resources as $min_width => $source) {
112  $map_entries .= "[$min_width, '$source'],\n";
113  }
114 
115  return "new Map([\n$map_entries])";
116  }
117 }
Registry for resources required by rendered output like Javascript or CSS.
$resources
Definition: ltiservices.php:65
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:99
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
register(string $name)
Add a dependency.
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:37
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.