ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\UI\Renderer as RendererInterface;
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
44 $component = $this->addTriggererOnLoadCode($component);
45
46 $tpl = $this->getTemplate("tpl.image.html", true, true);
47
48 if (($sources = $component->getAdditionalHighResSources()) !== []) {
49 $component = $component->withAdditionalOnLoadCode(
50 fn(string $id): string => "
51 const imageElement = il.UI.image.getImageElement('$id');
52 if (imageElement === null) {
53 throw new Error(`Image '$id' not found.`);
54 }
55 il.UI.image.loadHighResolutionSource(
56 imageElement,
57 {$this->getHighResSourceMapForJs($sources)}
58 );
59 "
60 );
61 }
62
63 $id = $this->bindJavaScript($component);
64 if (!empty($component->getAction())) {
65 $tpl->touchBlock("action_begin");
66
67 if (is_string($component->getAction())) {
68 $tpl->setCurrentBlock("with_href");
69 $tpl->setVariable("HREF", $component->getAction());
70 $tpl->parseCurrentBlock();
71 }
72
73 if (is_array($component->getAction())) {
74 $tpl->setCurrentBlock("with_href");
75 $tpl->setVariable("HREF", "#");
76 $tpl->parseCurrentBlock();
77 $tpl->setCurrentBlock("with_id");
78 $tpl->setVariable("ID", $id);
79 $tpl->parseCurrentBlock();
80 }
81 }
82
83 if (!is_array($component->getAction()) && $id !== null) {
84 $tpl->setVariable("IMG_ID", " id='" . $id . "' ");
85 }
86
87 $tpl->setCurrentBlock($component->getType());
88 $tpl->setVariable("SOURCE", $component->getSource());
89 $tpl->setVariable("ALT", htmlspecialchars($component->getAlt()));
90 $tpl->parseCurrentBlock();
91
92 if (!empty($component->getAction())) {
93 $tpl->touchBlock("action_end");
94 }
95
96 return $tpl->get();
97 }
98
102 public function registerResources(ResourceRegistry $registry): void
103 {
104 parent::registerResources($registry);
105 $registry->register('assets/js/image.min.js');
106 }
107
111 protected function getHighResSourceMapForJs(array $resources): string
112 {
113 $map_entries = '';
114 foreach ($resources as $min_width => $source) {
115 $map_entries .= "[$min_width, '$source'],\n";
116 }
117
118 return "new Map([\n$map_entries])";
119 }
120}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:102
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.
addTriggererOnLoadCode(Triggerer $triggerer)
Add onload-code for triggerer.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
render(Component $component, Renderer $default_renderer)
Render the component if possible and delegate additional rendering to the default_renderer.
Registry for resources required by rendered output like Javascript or CSS.
register(string $name)
Add a dependency.
An entity that renders components to a string output.
Definition: Renderer.php:31
$resources
Definition: ltiservices.php:68