ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
Renderer.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
28 
33 {
34  public function registerResources(ResourceRegistry $registry): void
35  {
36  $registry->register('./assets/js/progress.min.js');
37  parent::registerResources($registry);
38  }
39 
40  public function render(Component $component, RendererInterface $default_renderer): string
41  {
42  if ($component instanceof Bar) {
43  return $this->renderProgressBar($component, $default_renderer);
44  }
45 
46  $this->cannotHandleComponent($component);
47  }
48 
49  protected function renderProgressBar(Bar $component, RendererInterface $default_renderer): string
50  {
51  $template = $this->getTemplate('tpl.progress_bar.html', true, true);
52 
53  $template->setVariable('LABEL', $component->getLabel());
54 
55  $template->setVariable('SUCCESS_GLYPH', $default_renderer->render(
56  $this->getUIFactory()->symbol()->glyph()->apply(),
57  ));
58 
59  $template->setVariable('FAILURE_GLYPH', $default_renderer->render(
60  $this->getUIFactory()->symbol()->glyph()->close(),
61  ));
62 
63  $async_url = $component->getAsyncUrl();
64  if (null !== $async_url) {
65  $template->setVariable('ASYNC_URL', (string) $async_url);
66  }
67 
68  if (null !== $component->getAsyncUrl()) {
69  $enriched_component = $component->withAdditionalOnLoadCode(
70  static fn(string $id): string => "
71  const progressbar = il.UI.Progress.Bar.createAsync(
72  document.getElementById('$id'),
73  '{$component->getUpdateSignal()->getId()}',
74  {$component->getAsyncRefreshInterval()->getRefreshIntervalInMs()},
75  );
76 
77  $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
78  ",
79  );
80  } else {
81  $enriched_component = $component->withAdditionalOnLoadCode(
82  static fn(string $id): string => "
83  const progressBar = il.UI.Progress.Bar.create(
84  document.getElementById('$id'),
85  '{$component->getUpdateSignal()->getId()}',
86  );
87 
88  $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
89  ",
90  );
91  }
92 
93  $this->maybeApplyProgressBarValue($template, 0);
94  $this->applyProgressBarMaxValue($template);
95 
96  $id = $this->bindJavaScript($enriched_component) ?? $this->createId();
97  $template->setVariable('ID', $id);
98 
99  return $template->get();
100  }
101 
102  protected function applyProgressBarMaxValue(Template $template, int $max = Bar::MAX_VALUE): void
103  {
104  $template->setVariable('MAX_VALUE', (string) $max);
105  }
106 
107  protected function maybeApplyProgressBarValue(Template $template, ?int $value = null): void
108  {
109  if (null !== $value) {
110  $template->setVariable('VALUE', (string) $value);
111  }
112  }
113 }
Registry for resources required by rendered output like Javascript or CSS.
render(Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
applyProgressBarMaxValue(Template $template, int $max=Bar::MAX_VALUE)
Definition: Renderer.php:102
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVariable(string $name, $value)
Set a variable in the current block.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
maybeApplyProgressBarValue(Template $template, ?int $value=null)
Definition: Renderer.php:107
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
register(string $name)
Add a dependency.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:34
renderProgressBar(Bar $component, RendererInterface $default_renderer)
Definition: Renderer.php:49
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.