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
27use ILIAS\UI\Renderer as RendererInterface;
29
34{
35 public function registerResources(ResourceRegistry $registry): void
36 {
37 $registry->register('./assets/js/progress.min.js');
38 parent::registerResources($registry);
39 }
40
41 public function render(Component $component, RendererInterface $default_renderer): string
42 {
43 if ($component instanceof \ILIAS\UI\Component\Triggerer) {
44 $component = $this->addTriggererOnLoadCode($component);
45 }
46 if ($component instanceof Bar) {
47 return $this->renderProgressBar($component, $default_renderer);
48 }
49
50 $this->cannotHandleComponent($component);
51 }
52
53 protected function renderProgressBar(Bar $component, RendererInterface $default_renderer): string
54 {
55 $template = $this->getTemplate('tpl.progress_bar.html', true, true);
56
57 $template->setVariable('LABEL', $component->getLabel());
58
59 $template->setVariable('SUCCESS_GLYPH', $default_renderer->render(
60 $this->getUIFactory()->symbol()->glyph()->apply(),
61 ));
62
63 $template->setVariable('FAILURE_GLYPH', $default_renderer->render(
64 $this->getUIFactory()->symbol()->glyph()->close(),
65 ));
66
67 $async_url = $component->getAsyncUrl();
68 if (null !== $async_url) {
69 $template->setVariable('ASYNC_URL', (string) $async_url);
70 }
71
72 if (null !== $component->getAsyncUrl()) {
73 $enriched_component = $component->withAdditionalOnLoadCode(
74 static fn(string $id): string => "
75 const progressbar = il.UI.Progress.Bar.createAsync(
76 document.getElementById('$id'),
77 '{$component->getUpdateSignal()->getId()}',
78 {$component->getAsyncRefreshInterval()->getRefreshIntervalInMs()},
79 );
80
81 $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
82 ",
83 );
84 } else {
85 $enriched_component = $component->withAdditionalOnLoadCode(
86 static fn(string $id): string => "
87 const progressBar = il.UI.Progress.Bar.create(
88 document.getElementById('$id'),
89 '{$component->getUpdateSignal()->getId()}',
90 );
91
92 $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
93 ",
94 );
95 }
96
97 $this->maybeApplyProgressBarValue($template, 0);
98 $this->applyProgressBarMaxValue($template);
99
100 $id = $this->bindJavaScript($enriched_component) ?? $this->createId();
101 $template->setVariable('ID', $id);
102
103 return $template->get();
104 }
105
106 protected function applyProgressBarMaxValue(Template $template, int $max = Bar::MAX_VALUE): void
107 {
108 $template->setVariable('MAX_VALUE', (string) $max);
109 }
110
111 protected function maybeApplyProgressBarValue(Template $template, ?int $value = null): void
112 {
113 if (null !== $value) {
114 $template->setVariable('VALUE', (string) $value);
115 }
116 }
117}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderProgressBar(Bar $component, RendererInterface $default_renderer)
Definition: Renderer.php:53
maybeApplyProgressBarValue(Template $template, ?int $value=null)
Definition: Renderer.php:111
applyProgressBarMaxValue(Template $template, int $max=Bar::MAX_VALUE)
Definition: Renderer.php:106
render(Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:41
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:35
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.
Definition: UI.php:24
A component is the most general form of an entity in the UI.
Definition: Component.php:28
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
Registry for resources required by rendered output like Javascript or CSS.
register(string $name)
Add a dependency.
Interface to templating as it is used in the UI framework.
Definition: Template.php:29
setVariable(string $name, $value)
Set a variable in the current block.
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.