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 
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 Bar) {
44  return $this->renderProgressBar($component, $default_renderer);
45  }
46 
47  $this->cannotHandleComponent($component);
48  }
49 
50  protected function renderProgressBar(Bar $component, RendererInterface $default_renderer): string
51  {
52  $template = $this->getTemplate('tpl.progress_bar.html', true, true);
53 
54  $template->setVariable('LABEL', $component->getLabel());
55 
56  $template->setVariable('SUCCESS_GLYPH', $default_renderer->render(
57  $this->getUIFactory()->symbol()->glyph()->apply(),
58  ));
59 
60  $template->setVariable('FAILURE_GLYPH', $default_renderer->render(
61  $this->getUIFactory()->symbol()->glyph()->close(),
62  ));
63 
64  $async_url = $component->getAsyncUrl();
65  if (null !== $async_url) {
66  $template->setVariable('ASYNC_URL', (string) $async_url);
67  }
68 
69  if (null !== $component->getAsyncUrl()) {
70  $enriched_component = $component->withAdditionalOnLoadCode(
71  static fn(string $id): string => "
72  const progressbar = il.UI.Progress.Bar.createAsync(
73  document.getElementById('$id'),
74  '{$component->getUpdateSignal()->getId()}',
75  {$component->getAsyncRefreshInterval()->getRefreshIntervalInMs()},
76  );
77 
78  $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
79  ",
80  );
81  } else {
82  $enriched_component = $component->withAdditionalOnLoadCode(
83  static fn(string $id): string => "
84  const progressBar = il.UI.Progress.Bar.create(
85  document.getElementById('$id'),
86  '{$component->getUpdateSignal()->getId()}',
87  );
88 
89  $(document).on('{$component->getResetSignal()}', () => progressBar.reset());
90  ",
91  );
92  }
93 
94  $this->maybeApplyProgressBarValue($template, 0);
95  $this->applyProgressBarMaxValue($template);
96 
97  $id = $this->bindJavaScript($enriched_component) ?? $this->createId();
98  $template->setVariable('ID', $id);
99 
100  return $template->get();
101  }
102 
103  protected function applyProgressBarMaxValue(Template $template, int $max = Bar::MAX_VALUE): void
104  {
105  $template->setVariable('MAX_VALUE', (string) $max);
106  }
107 
108  protected function maybeApplyProgressBarValue(Template $template, ?int $value = null): void
109  {
110  if (null !== $value) {
111  $template->setVariable('VALUE', (string) $value);
112  }
113  }
114 }
Registry for resources required by rendered output like Javascript or CSS.
render(Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:41
applyProgressBarMaxValue(Template $template, int $max=Bar::MAX_VALUE)
Definition: Renderer.php:103
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.
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.
maybeApplyProgressBarValue(Template $template, ?int $value=null)
Definition: Renderer.php:108
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:23
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:35
renderProgressBar(Bar $component, RendererInterface $default_renderer)
Definition: Renderer.php:50
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.