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