ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Renderer.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
9use ILIAS\UI\Renderer as RendererInterface;
11
13{
17 public function render(Component\Component $component, RendererInterface $default_renderer)
18 {
19 $this->checkComponent($component);
20
21 if ($component instanceof Component\Button\Close) {
22 return $this->renderClose($component);
23 } elseif ($component instanceof Component\Button\Toggle) {
24 return $this->renderToggle($component);
25 } elseif ($component instanceof Component\Button\Month) {
26 return $this->renderMonth($component, $default_renderer);
27 } else {
28 return $this->renderButton($component, $default_renderer);
29 }
30 }
31
32
39 protected function renderButton(Component\Button\Button $component, RendererInterface $default_renderer)
40 {
41 if ($component instanceof Component\Button\Primary) {
42 $tpl_name = "tpl.primary.html";
43 }
44 if ($component instanceof Component\Button\Standard) {
45 $tpl_name = "tpl.standard.html";
46 }
47 if ($component instanceof Component\Button\Shy) {
48 $tpl_name = "tpl.shy.html";
49 }
50 if ($component instanceof Component\Button\Tag) {
51 $tpl_name = "tpl.tag.html";
52 }
53 if ($component instanceof Component\Button\Bulky) {
54 $tpl_name = "tpl.bulky.html";
55 }
56
57 $tpl = $this->getTemplate($tpl_name, true, true);
58
59 $action = $component->getAction();
60 // The action is always put in the data-action attribute to have it available
61 // on the client side, even if it is not available on rendering.
62 if (is_string($action)) {
63 $tpl->setCurrentBlock("with_data_action");
64 $tpl->setVariable("ACTION", $action);
65 $tpl->parseCurrentBlock();
66 }
67
68 $label = $component->getLabel();
69 if ($label !== null) {
70 $tpl->setVariable("LABEL", $component->getLabel());
71 }
72 if ($component->isActive()) {
73 // The actions might also be a list of signals, these will be appended by
74 // bindJavascript in maybeRenderId.
75 if (is_string($action) && $action != "") {
76 $component = $component->withAdditionalOnLoadCode(function ($id) use ($action) {
77 $action = str_replace("&amp;", "&", $action);
78
79 return "$('#$id').on('click', function(event) {
80 window.location = '{$action}';
81 return false;
82 });";
83 });
84 }
85
86 if ($component instanceof Component\Button\LoadingAnimationOnClick && $component->hasLoadingAnimationOnClick()) {
87 $component = $component->withAdditionalOnLoadCode(function ($id) {
88 return "$('#$id').click(function(e) { $('#$id').addClass('il-btn-with-loading-animation'); $('#$id').addClass('disabled');});";
89 });
90 }
91 } else {
92 $tpl->touchBlock("disabled");
93 }
94 $aria_label = $component->getAriaLabel();
95 if ($aria_label != null) {
96 $tpl->setCurrentBlock("with_aria_label");
97 $tpl->setVariable("ARIA_LABEL", $aria_label);
98 $tpl->parseCurrentBlock();
99 }
100 if ($component->isAriaChecked()) {
101 $tpl->setCurrentBlock("with_aria_checked");
102 $tpl->setVariable("ARIA_CHECKED", "true");
103 $tpl->parseCurrentBlock();
104 }
105 $this->maybeRenderId($component, $tpl);
106
107 if ($component instanceof Component\Button\Tag) {
108 $this->additionalRenderTag($component, $tpl);
109 }
110
111 if ($component instanceof Component\Button\Bulky) {
112 $this->additionalRenderBulky($component, $default_renderer, $tpl);
113 }
114
115
116 return $tpl->get();
117 }
118
122 public function registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
123 {
124 parent::registerResources($registry);
125 $registry->register('./src/UI/templates/js/Button/button.js');
126 $registry->register("./libs/bower/bower_components/moment/min/moment-with-locales.min.js");
127 $registry->register("./libs/bower/bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js");
128 }
129
130 protected function renderClose($component)
131 {
132 $tpl = $this->getTemplate("tpl.close.html", true, true);
133 // This is required as the rendering seems to only create any output at all
134 // if any var was set or block was touched.
135 $tpl->setVariable("FORCE_RENDERING", "");
136 $this->maybeRenderId($component, $tpl);
137 return $tpl->get();
138 }
139
140 protected function renderToggle(Component\Button\Toggle $component)
141 {
142 $tpl = $this->getTemplate("tpl.toggle.html", true, true);
143
144 $on_action = $component->getActionOn();
145 $off_action = $component->getActionOff();
146
147 $on_url = (is_string($on_action))
148 ? $on_action
149 : "";
150
151 $off_url = (is_string($off_action))
152 ? $off_action
153 : "";
154
155 $signals = [];
156
157 foreach ($component->getTriggeredSignals() as $s) {
158 $signals[] = [
159 "signal_id" => $s->getSignal()->getId(),
160 "event" => $s->getEvent(),
161 "options" => $s->getSignal()->getOptions()
162 ];
163 }
164
165 $signals = json_encode($signals);
166
167 if ($component->isActive()) {
168 $component = $component->withAdditionalOnLoadCode(function ($id) use ($on_url, $off_url, $signals) {
169 $code = "$('#$id').on('click', function(event) {
170 il.UI.button.handleToggleClick(event, '$id', '$on_url', '$off_url', $signals);
171 return false; // stop event propagation
172 });";
173 //var_dump($code); exit;
174 return $code;
175 });
176 } else {
177 $tpl->touchBlock("disabled");
178 }
179
180 $is_on = $component->isOn();
181 if ($is_on) {
182 $tpl->touchBlock("on");
183 }
184 $label = $component->getLabel();
185 if (!empty($label)) {
186 $tpl->setCurrentBlock("with_label");
187 $tpl->setVariable("LABEL", $label);
188 $tpl->parseCurrentBlock();
189 }
190 $aria_label = $component->getAriaLabel();
191 if ($aria_label != null) {
192 $tpl->setCurrentBlock("with_aria_label");
193 $tpl->setVariable("ARIA_LABEL", $aria_label);
194 $tpl->parseCurrentBlock();
195 }
196 $this->maybeRenderId($component, $tpl);
197 return $tpl->get();
198 }
199
200 protected function maybeRenderId(Component\Component $component, $tpl)
201 {
202 $id = $this->bindJavaScript($component);
203 if ($id !== null) {
204 $tpl->setCurrentBlock("with_id");
205 $tpl->setVariable("ID", $id);
206 $tpl->parseCurrentBlock();
207 }
208 }
209
210 protected function renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
211 {
212 $def = $component->getDefault();
213
214 for ($i = 1; $i <= 12; $i++) {
215 $this->toJS(array("month_" . str_pad($i, 2, "0", STR_PAD_LEFT) . "_short"));
216 }
217
218 $tpl = $this->getTemplate("tpl.month.html", true, true);
219
220 $month = explode("-", $def);
221 $tpl->setVariable("DEFAULT_LABEL", $this->txt("month_" . str_pad($month[0], 2, "0", STR_PAD_LEFT) . "_short") . " " . $month[1]);
222 $tpl->setVariable("DEF_DATE", $month[0] . "/1/" . $month[1]);
223 // see https://github.com/moment/moment/tree/develop/locale
224 $lang_key = in_array($this->getLangKey(), array("ar", "bg", "cs", "da", "de", "el", "en", "es", "et", "fa", "fr", "hu", "it",
225 "ja", "ka", "lt", "nl", "pl", "pt", "ro", "ru", "sk", "sq", "sr", "tr", "uk", "vi", "zh"))
226 ? $this->getLangKey()
227 : "en";
228 if ($lang_key == "zh") {
229 $lang_key = "zh-cn";
230 }
231 $tpl->setVariable("LANG", $lang_key);
232
233 $id = $this->bindJavaScript($component);
234
235 if ($id !== null) {
236 $tpl->setCurrentBlock("with_id");
237 $tpl->setVariable("ID", $id);
238 $tpl->parseCurrentBlock();
239 $tpl->setVariable("JSID", $id);
240 }
241
242 return $tpl->get();
243 }
244
245 protected function additionalRenderTag(Component\Button\Tag $component, $tpl)
246 {
247 $tpl->touchBlock('rel_' . $component->getRelevance());
248
249 $classes = trim(join(' ', $component->getClasses()));
250 if ($classes !== '') {
251 $tpl->setVariable("CLASSES", $classes);
252 }
253
254 $bgcol = $component->getBackgroundColor();
255 if ($bgcol) {
256 $tpl->setVariable("BGCOL", $bgcol->asHex());
257 }
258 $forecol = $component->getForegroundColor();
259 if ($forecol) {
260 $tpl->setVariable("FORECOL", $forecol->asHex());
261 }
262 }
263
264 protected function additionalRenderBulky(Component\Button\Button $component, RendererInterface $default_renderer, $tpl)
265 {
266 $renderer = $default_renderer->withAdditionalContext($component);
267 $tpl->setVariable("ICON_OR_GLYPH", $renderer->render($component->getIconOrGlyph()));
268 $label = $component->getLabel();
269 if ($label !== null) {
270 $tpl->setVariable("LABEL", $label);
271 }
272 if ($component->isEngaged()) {
273 $tpl->touchBlock("engaged");
274 $tpl->setVariable("ARIA_PRESSED", 'true');
275 } else {
276 if (is_string($component->getAction())) {
277 $tpl->setVariable("ARIA_PRESSED", 'undefined');
278 } else {
279 $tpl->setVariable("ARIA_PRESSED", 'false');
280 }
281 }
282 }
283
287 protected function getComponentInterfaceName()
288 {
289 return array(Component\Button\Primary::class
290 , Component\Button\Standard::class
291 , Component\Button\Close::class
292 , Component\Button\Shy::class
293 , Component\Button\Month::class
294 , Component\Button\Tag::class
295 , Component\Button\Bulky::class
296 , Component\Button\Toggle::class
297 );
298 }
299}
$tpl
Definition: ilias.php:10
An exception for terminatinating execution or to throw for unit testing.
additionalRenderBulky(Component\Button\Button $component, RendererInterface $default_renderer, $tpl)
Definition: Renderer.php:264
registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
Definition: Renderer.php:122
renderToggle(Component\Button\Toggle $component)
Definition: Renderer.php:140
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:17
additionalRenderTag(Component\Button\Tag $component, $tpl)
Definition: Renderer.php:245
renderButton(Component\Button\Button $component, RendererInterface $default_renderer)
Definition: Renderer.php:39
getComponentInterfaceName()
Get the name of the component-interface this renderer is supposed to render.ATTENTION: Fully qualifie...
Definition: Renderer.php:287
renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
Definition: Renderer.php:210
maybeRenderId(Component\Component $component, $tpl)
Definition: Renderer.php:200
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.
checkComponent(Component $component)
Check if a given component fits this renderer and throw \LogicError if that is not the case.
toJS($key)
Add language var to client side (il.Language)
$action
$def
Definition: croninfo.php:21
$i
Definition: disco.tpl.php:19
$code
Definition: example_050.php:99
if(!array_key_exists('StateId', $_REQUEST)) $id
A component is the most general form of an entity in the UI.
Definition: Component.php:14
An entity that renders components to a string output.
Definition: Renderer.php:15
trait LoadingAnimationOnClick
Implements LoadingAnimationOnClick interface.
Class BaseForm.
Class Factory.
$s
Definition: pwgen.php:45