ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
8use ILIAS\UI\Renderer as RendererInterface;
10
12{
16 public function render(Component\Component $component, RendererInterface $default_renderer)
17 {
18 $this->checkComponent($component);
19
20 if ($component instanceof Component\Button\Close) {
21 return $this->renderClose($component);
22 } elseif ($component instanceof Component\Button\Month) {
23 return $this->renderMonth($component, $default_renderer);
24 } else {
25 return $this->renderButton($component, $default_renderer);
26 }
27 }
28
29 protected function renderButton(Component\Button\Button $component, RendererInterface $default_renderer)
30 {
31 // TODO: It would be nice if we could use <button> for rendering a button
32 // instead of <a>. This was not done atm, as there is no attribute on a
33 // button to make it open an URL. This would require JS.
34
35 if ($component instanceof Component\Button\Primary) {
36 $tpl_name = "tpl.primary.html";
37 }
38 if ($component instanceof Component\Button\Standard) {
39 $tpl_name = "tpl.standard.html";
40 }
41 if ($component instanceof Component\Button\Shy) {
42 $tpl_name = "tpl.shy.html";
43 }
44 if ($component instanceof Component\Button\Tag) {
45 $tpl_name = "tpl.tag.html";
46 }
47
48 $tpl = $this->getTemplate($tpl_name, true, true);
49 $action = $component->getAction();
50 // The action is always put in the data-action attribute to have it available
51 // on the client side, even if it is not available on rendering.
52 $tpl->setVariable("ACTION", $action);
53 $label = $component->getLabel();
54 if ($label !== null) {
55 $tpl->setVariable("LABEL", $component->getLabel());
56 }
57 if ($component->isActive()) {
58 $tpl->setCurrentBlock("with_href");
59 $tpl->setVariable("HREF", $action);
60 $tpl->parseCurrentBlock();
61 } else {
62 $tpl->touchBlock("disabled");
63 }
64 $aria_label = $component->getAriaLabel();
65 if ($aria_label != null) {
66 $tpl->setCurrentBlock("with_aria_label");
67 $tpl->setVariable("ARIA_LABEL", $aria_label);
68 $tpl->parseCurrentBlock();
69 }
70 if ($component->isAriaChecked()) {
71 $tpl->setCurrentBlock("with_aria_checked");
72 $tpl->setVariable("ARIA_CHECKED", "true");
73 $tpl->parseCurrentBlock();
74 }
75 $this->maybeRenderId($component, $tpl);
76
77 if ($component instanceof Component\Button\Tag) {
78 $this->additionalRenderTag($component, $tpl);
79 }
80
81 return $tpl->get();
82 }
83
87 public function registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
88 {
89 parent::registerResources($registry);
90 $registry->register('./src/UI/templates/js/Button/button.js');
91 $registry->register("./libs/bower/bower_components/moment/min/moment-with-locales.min.js");
92 $registry->register("./libs/bower/bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js");
93 }
94
95 protected function renderClose($component)
96 {
97 $tpl = $this->getTemplate("tpl.close.html", true, true);
98 // This is required as the rendering seems to only create any output at all
99 // if any var was set or block was touched.
100 $tpl->setVariable("FORCE_RENDERING", "");
101 $this->maybeRenderId($component, $tpl);
102 return $tpl->get();
103 }
104
105 protected function maybeRenderId(Component\Component $component, $tpl)
106 {
107 $id = $this->bindJavaScript($component);
108 if ($id !== null) {
109 $tpl->setCurrentBlock("with_id");
110 $tpl->setVariable("ID", $id);
111 $tpl->parseCurrentBlock();
112 }
113 }
114
115 protected function renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
116 {
117 $def = $component->getDefault();
118
119 for ($i = 1; $i<=12; $i++) {
120 $this->toJS(array("month_" . str_pad($i, 2, "0", STR_PAD_LEFT) . "_short"));
121 }
122
123 $tpl = $this->getTemplate("tpl.month.html", true, true);
124
125 $month = explode("-", $def);
126 $tpl->setVariable("DEFAULT_LABEL", $this->txt("month_" . str_pad($month[0], 2, "0", STR_PAD_LEFT) . "_short") . " " . $month[1]);
127 $tpl->setVariable("DEF_DATE", $month[0] . "/1/" . $month[1]);
128 // see https://github.com/moment/moment/tree/develop/locale
129 $lang_key = in_array($this->getLangKey(), array("ar", "bg", "cs", "da", "de", "el", "en", "es", "et", "fa", "fr", "hu", "it",
130 "ja", "ka", "lt", "nl", "pl", "pt", "ro", "ru", "sk", "sq", "sr", "tr", "uk", "vi", "zh"))
131 ? $this->getLangKey()
132 : "en";
133 if ($lang_key == "zh") {
134 $lang_key = "zh-cn";
135 }
136 $tpl->setVariable("LANG", $lang_key);
137
138 $id = $this->bindJavaScript($component);
139
140 if ($id !== null) {
141 $tpl->setCurrentBlock("with_id");
142 $tpl->setVariable("ID", $id);
143 $tpl->parseCurrentBlock();
144 $tpl->setVariable("JSID", $id);
145 }
146
147 return $tpl->get();
148 }
149
150 protected function additionalRenderTag(Component\Button\Tag $component, $tpl)
151 {
152 $tpl->touchBlock('rel_' . $component->getRelevance());
153
154 $classes = trim(join(' ', $component->getClasses()));
155 if ($classes !== '') {
156 $tpl->setVariable("CLASSES", $classes);
157 }
158
159 $bgcol = $component->getBackgroundColor();
160 if ($bgcol) {
161 $tpl->setVariable("BGCOL", $bgcol->asHex());
162 }
163 $forecol = $component->getForegroundColor();
164 if ($forecol) {
165 $tpl->setVariable("FORECOL", $forecol->asHex());
166 }
167 }
168
172 protected function getComponentInterfaceName()
173 {
174 return array(Component\Button\Primary::class
175 , Component\Button\Standard::class
176 , Component\Button\Close::class
177 , Component\Button\Shy::class
178 , Component\Button\Month::class
179 , Component\Button\Tag::class
180 );
181 }
182}
$tpl
Definition: ilias.php:10
An exception for terminatinating execution or to throw for unit testing.
This implements commonalities between standard and primary buttons.
Definition: Button.php:17
registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
Definition: Renderer.php:87
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:16
additionalRenderTag(Component\Button\Tag $component, $tpl)
Definition: Renderer.php:150
renderButton(Component\Button\Button $component, RendererInterface $default_renderer)
Definition: Renderer.php:29
getComponentInterfaceName()
Get the name of the component-interface this renderer is supposed to render.ATTENTION: Fully qualifie...
Definition: Renderer.php:172
renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
Definition: Renderer.php:115
maybeRenderId(Component\Component $component, $tpl)
Definition: Renderer.php:105
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
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
Class BaseForm.
Class Factory.