ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
68 $this->bindAndApplyJavaScript($dropzone, $template);
69
70 return $template->get();
71 }
72
73 protected function renderStandard(Standard $dropzone, RenderInterface $default_renderer): string
74 {
75 $modal = $dropzone->getModal();
76
77 $template = $this->getTemplate("tpl.dropzone.html", true, true);
78 $template->setVariable('MODAL', $default_renderer->render($modal));
79 $template->setCurrentBlock('with_message');
80 $template->setVariable('MESSAGE', $dropzone->getMessage());
81 $template->parseCurrentBlock();
82
83 $upload_button = $dropzone->getUploadButton();
84 if (null !== $upload_button) {
85 // override default onClick behaviour with modal signal
86 // to prevent the action from triggering.
87 $upload_button = $upload_button->withOnClick(
88 $modal->getShowSignal()
89 );
90
91 $template->setVariable('BUTTON', $default_renderer->render($upload_button));
92 }
93
94 if ($dropzone->isBulky()) {
95 $template->touchBlock('bulky');
96 }
97
98 $dropzone = $this->initClientsideDropzone($dropzone);
99 $dropzone = $dropzone->withAdditionalDrop($modal->getShowSignal());
100
101 $this->bindAndApplyJavaScript($dropzone, $template);
102
103 return $template->get();
104 }
105
106 protected function initClientsideDropzone(FileInterface $dropzone): FileInterface
107 {
108 return $dropzone->withAdditionalOnLoadCode(static function ($id) use ($dropzone) {
109 return "
110 il.UI.Dropzone.init('$id');
111
112 // @TODO: we need to refactor the signal-management to prevent using jQuery here.
113 $(document).on('{$dropzone->getClearSignal()}', function () {
114 il.UI.Dropzone.removeAllFilesFromQueue('$id');
115 });
116 ";
117 });
118 }
119
120 protected function bindAndApplyJavaScript(FileInterface $dropzone, Template $template): void
121 {
122 $template->setVariable('ID', $this->bindJavaScript($dropzone));
123 }
124}
$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:252
bindAndApplyJavaScript(FileInterface $dropzone, Template $template)
Definition: Renderer.php:120
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:49
renderStandard(Standard $dropzone, RenderInterface $default_renderer)
Definition: Renderer.php:73
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.
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.