ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 
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) { il.UI.button.activateLoadingAnimation('$id')});";
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 
101  if ($component instanceof Component\Button\Engageable
102  && $component->isEngageable()
103  ) {
104  if ($component->isEngaged()) {
105  $tpl->touchBlock("engaged");
106  $aria_pressed = 'true';
107  } else {
108  $aria_pressed = 'false';
109  }
110  $tpl->setCurrentBlock("with_aria_pressed");
111  $tpl->setVariable("ARIA_PRESSED", $aria_pressed);
112  $tpl->parseCurrentBlock();
113  }
114 
115  $this->maybeRenderId($component, $tpl);
116 
117  if ($component instanceof Component\Button\Tag) {
118  $this->additionalRenderTag($component, $tpl);
119  }
120 
121  if ($component instanceof Component\Button\Bulky) {
122  $this->additionalRenderBulky($component, $default_renderer, $tpl);
123  }
124 
125  return $tpl->get();
126  }
127 
131  public function registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
132  {
133  parent::registerResources($registry);
134  $registry->register('./src/UI/templates/js/Button/button.js');
135  $registry->register("./libs/bower/bower_components/moment/min/moment-with-locales.min.js");
136  $registry->register("./libs/bower/bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js");
137  }
138 
139  protected function renderClose($component)
140  {
141  $tpl = $this->getTemplate("tpl.close.html", true, true);
142  // This is required as the rendering seems to only create any output at all
143  // if any var was set or block was touched.
144  $tpl->setVariable("FORCE_RENDERING", "");
145  $this->maybeRenderId($component, $tpl);
146  return $tpl->get();
147  }
148 
149  protected function renderToggle(Component\Button\Toggle $component)
150  {
151  $tpl = $this->getTemplate("tpl.toggle.html", true, true);
152 
153  $on_action = $component->getActionOn();
154  $off_action = $component->getActionOff();
155 
156  $on_url = (is_string($on_action))
157  ? $on_action
158  : "";
159 
160  $off_url = (is_string($off_action))
161  ? $off_action
162  : "";
163 
164  $signals = [];
165 
166  foreach ($component->getTriggeredSignals() as $s) {
167  $signals[] = [
168  "signal_id" => $s->getSignal()->getId(),
169  "event" => $s->getEvent(),
170  "options" => $s->getSignal()->getOptions()
171  ];
172  }
173 
174  $signals = json_encode($signals);
175 
176  $button_status = 'off';
177  if ($component->isEngaged()) {
178  $button_status = 'on';
179  };
180 
181  if ($component->isActive()) {
182  $component = $component->withAdditionalOnLoadCode(function ($id) use ($on_url, $off_url, $signals) {
183  $code = "$('#$id').on('click', function(event) {
184  il.UI.button.handleToggleClick(event, '$id', '$on_url', '$off_url', $signals);
185  return false; // stop event propagation
186  });";
187  //var_dump($code); exit;
188  return $code;
189  });
190  } else {
191  $tpl->touchBlock("disabled");
192  $button_status = 'unavailable';
193  }
194 
195  $tpl->touchBlock($button_status);
196 
197  $label = $component->getLabel();
198  if (!empty($label)) {
199  $tpl->setCurrentBlock("with_label");
200  $tpl->setVariable("LABEL", $label);
201  $tpl->parseCurrentBlock();
202  }
203  $aria_label = $component->getAriaLabel();
204  if ($aria_label != null) {
205  $tpl->setCurrentBlock("with_aria_label");
206  $tpl->setVariable("ARIA_LABEL", $aria_label);
207  $tpl->parseCurrentBlock();
208  }
209  $this->maybeRenderId($component, $tpl);
210  return $tpl->get();
211  }
212 
213  protected function maybeRenderId(Component\Component $component, $tpl)
214  {
215  $id = $this->bindJavaScript($component);
216  if ($id !== null) {
217  $tpl->setCurrentBlock("with_id");
218  $tpl->setVariable("ID", $id);
219  $tpl->parseCurrentBlock();
220  }
221  }
222 
223  protected function renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
224  {
225  $def = $component->getDefault();
226 
227  for ($i = 1; $i <= 12; $i++) {
228  $this->toJS(array("month_" . str_pad($i, 2, "0", STR_PAD_LEFT) . "_short"));
229  }
230 
231  $tpl = $this->getTemplate("tpl.month.html", true, true);
232 
233  $month = explode("-", $def);
234  $tpl->setVariable("DEFAULT_LABEL", $this->txt("month_" . str_pad($month[0], 2, "0", STR_PAD_LEFT) . "_short") . " " . $month[1]);
235  $tpl->setVariable("DEF_DATE", $month[0] . "/1/" . $month[1]);
236  // see https://github.com/moment/moment/tree/develop/locale
237  $lang_key = in_array($this->getLangKey(), array("ar", "bg", "cs", "da", "de", "el", "en", "es", "et", "fa", "fr", "hu", "it",
238  "ja", "ka", "lt", "nl", "pl", "pt", "ro", "ru", "sk", "sq", "sr", "tr", "uk", "vi", "zh"))
239  ? $this->getLangKey()
240  : "en";
241  if ($lang_key == "zh") {
242  $lang_key = "zh-cn";
243  }
244  $tpl->setVariable("LANG", $lang_key);
245 
246  $id = $this->bindJavaScript($component);
247 
248  if ($id !== null) {
249  $tpl->setCurrentBlock("with_id");
250  $tpl->setVariable("ID", $id);
251  $tpl->parseCurrentBlock();
252  $tpl->setVariable("JSID", $id);
253  }
254 
255  return $tpl->get();
256  }
257 
258  protected function additionalRenderTag(Component\Button\Tag $component, $tpl)
259  {
260  $tpl->touchBlock('rel_' . $component->getRelevance());
261 
262  $classes = trim(join(' ', $component->getClasses()));
263  if ($classes !== '') {
264  $tpl->setVariable("CLASSES", $classes);
265  }
266 
267  $bgcol = $component->getBackgroundColor();
268  if ($bgcol) {
269  $tpl->setVariable("BGCOL", $bgcol->asHex());
270  }
271  $forecol = $component->getForegroundColor();
272  if ($forecol) {
273  $tpl->setVariable("FORECOL", $forecol->asHex());
274  }
275  }
276 
277  protected function additionalRenderBulky(Component\Button\Button $component, RendererInterface $default_renderer, $tpl)
278  {
279  $renderer = $default_renderer->withAdditionalContext($component);
280  $tpl->setVariable("ICON_OR_GLYPH", $renderer->render($component->getIconOrGlyph()));
281  $label = $component->getLabel();
282  if ($label !== null) {
283  $tpl->setVariable("LABEL", $label);
284  }
285 
286  $aria_role = $component->getAriaRole();
287  if ($aria_role != null) {
288  $tpl->setCurrentBlock("with_aria_role");
289  $tpl->setVariable("ARIA_ROLE", $aria_role);
290  $tpl->parseCurrentBlock();
291  }
292  if ($aria_role == Bulky::MENUITEM) {
293  $tpl->touchBlock("with_aria_haspopup");
294  }
295  }
296 
300  protected function getComponentInterfaceName()
301  {
302  return array(Component\Button\Primary::class
303  , Component\Button\Standard::class
304  , Component\Button\Close::class
305  , Component\Button\Shy::class
306  , Component\Button\Month::class
307  , Component\Button\Tag::class
308  , Component\Button\Bulky::class
309  , Component\Button\Toggle::class
310  );
311  }
312 }
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:17
registerResources(\ILIAS\UI\Implementation\Render\ResourceRegistry $registry)
Definition: Renderer.php:131
Class Factory.
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
Class ChatMainBarProvider .
additionalRenderBulky(Component\Button\Button $component, RendererInterface $default_renderer, $tpl)
Definition: Renderer.php:277
toJS($key)
Add language var to client side (il.Language)
renderToggle(Component\Button\Toggle $component)
Definition: Renderer.php:149
maybeRenderId(Component\Component $component, $tpl)
Definition: Renderer.php:213
renderButton(Component\Button\Button $component, RendererInterface $default_renderer)
Definition: Renderer.php:39
renderMonth(Component\Button\Month $component, RendererInterface $default_renderer)
Definition: Renderer.php:223
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
additionalRenderTag(Component\Button\Tag $component, $tpl)
Definition: Renderer.php:258
$i
Definition: metadata.php:24
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.