ILIAS  trunk Revision v12.0_alpha-1338-g8f7e531aa3c
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ILIAS\UI\Component\Dropzone\File\File as FileInterface;
28use ILIAS\UI\Renderer as RenderInterface;
29use LogicException;
30
37{
38 public function render(Component $component, RenderInterface $default_renderer): string
39 {
40 if ($component instanceof \ILIAS\UI\Component\Dropzone\File\Wrapper) {
41 return $this->renderWrapper($component, $default_renderer);
42 }
43 if ($component instanceof \ILIAS\UI\Component\Dropzone\File\Standard) {
44 return $this->renderStandard($component, $default_renderer);
45 }
46 $this->cannotHandleComponent($component);
47 }
48
49 public function registerResources(ResourceRegistry $registry): void
50 {
51 $registry->register("assets/js/dropzone.js");
52
53 parent::registerResources($registry);
54 }
55
56 protected function renderWrapper(Wrapper $dropzone, RenderInterface $default_renderer): string
57 {
58 $modal = $dropzone->getModal();
59
60 $template = $this->getTemplate("tpl.dropzone.html", true, true);
61 $template->setVariable('MODAL', $default_renderer->render($modal));
62 $template->setVariable('CONTENT', $default_renderer->render($dropzone->getContent()));
63 $template->setVariable('WRAPPER_CLASS', 'ui-dropzone-wrapper');
64
65 $dropzone = $this->initClientsideDropzone($dropzone);
66 $dropzone = $dropzone->withAdditionalDrop($modal->getShowSignal());
67 $dropzone = $this->addTriggererOnLoadCode($dropzone);
68
69 $this->bindAndApplyJavaScript($dropzone, $template);
70
71 return $template->get();
72 }
73
74 protected function renderStandard(Standard $dropzone, RenderInterface $default_renderer): string
75 {
76 $modal = $dropzone->getModal();
77
78 $template = $this->getTemplate("tpl.dropzone.html", true, true);
79 $template->setVariable('MODAL', $default_renderer->render($modal));
80 $template->setCurrentBlock('with_message');
81 $template->setVariable('MESSAGE', $dropzone->getMessage());
82 $template->parseCurrentBlock();
83
84 $upload_button = $dropzone->getUploadButton();
85 if (null !== $upload_button) {
86 // override default onClick behaviour with modal signal
87 // to prevent the action from triggering.
88 $upload_button = $upload_button->withOnClick(
89 $modal->getShowSignal()
90 );
91
92 $template->setVariable('BUTTON', $default_renderer->render($upload_button));
93 }
94
95 if ($dropzone->isBulky()) {
96 $template->touchBlock('bulky');
97 }
98
99 $dropzone = $this->initClientsideDropzone($dropzone);
100 $dropzone = $dropzone->withAdditionalDrop($modal->getShowSignal());
101 $dropzone = $this->addTriggererOnLoadCode($dropzone);
102
103 $this->bindAndApplyJavaScript($dropzone, $template);
104
105 return $template->get();
106 }
107
108 protected function initClientsideDropzone(FileInterface $dropzone): FileInterface
109 {
110 return $dropzone->withAdditionalOnLoadCode(static function ($id) use ($dropzone) {
111 return "
112 il.UI.Dropzone.init('$id');
113
114 // @TODO: we need to refactor the signal-management to prevent using jQuery here.
115 $(document).on('{$dropzone->getClearSignal()}', function () {
116 il.UI.Dropzone.removeAllFilesFromQueue('$id');
117 });
118 ";
119 });
120 }
121
122 protected function bindAndApplyJavaScript(FileInterface $dropzone, Template $template): void
123 {
124 $template->setVariable('ID', $this->bindJavaScript($dropzone));
125 }
126}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
withAdditionalDrop(Signal $signal)
Get a component like this, triggering a signal of another component when files have been dropped.
Definition: File.php:259
bindAndApplyJavaScript(FileInterface $dropzone, Template $template)
Definition: Renderer.php:122
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:49
renderStandard(Standard $dropzone, RenderInterface $default_renderer)
Definition: Renderer.php:74
render(Component $component, RenderInterface $default_renderer)
Definition: Renderer.php:38
renderWrapper(Wrapper $dropzone, RenderInterface $default_renderer)
Definition: Renderer.php:56
getMessage()
Get the message of this dropzone.
Definition: Standard.php:62
getUploadButton()
Get the button to upload the files to the server.
Definition: Standard.php:74
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
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.