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