Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basebutton.php
1<?php
2
3namespace Bitrix\UI\Buttons;
4
5
11
12//We know about lazy load. So, the code loads common messages for default buttons,
13//which implemented by subclasses of BaseButton.
14Loc::loadLanguageFile(__FILE__);
15
17{
18 const UNIQ_ID_DATA_ATTR = 'btn-uniqid';
19 const JSON_OPTIONS_DATA_ATTR = 'json-options';
20
22 protected $id;
24 protected $text;
26 protected $tag = Tag::BUTTON;
28 protected $baseClass = "ui-btn";
30 protected $link;
32 protected $counter;
34 protected $events = [];
36 private $attributes;
37
38 final public function __construct(array $params = [])
39 {
40 $this->attributes = new ButtonAttributes();
41 $this->attributes->addDataAttribute(self::UNIQ_ID_DATA_ATTR, $this->generateUniqid());
42 $this->addClass($this->getBaseClass());
43
44 $this->init($params);
45 }
46
50 protected function getDefaultParameters()
51 {
52 return [];
53 }
54
55 protected function init(array $params = [])
56 {
57 $this->buildFromArray(array_merge(
58 $this->getDefaultParameters(),
59 $params
60 ));
61 }
62
63 final public static function create(array $params = [])
64 {
65 return new static($params);
66 }
67
68 protected function buildFromArray($params)
69 {
70 if (isset($params['text']))
71 {
72 $this->setText($params['text']);
73 }
74
75 if (!empty($params['styles']))
76 {
77 $this->setStyles($params['styles']);
78 }
79
80 if (!empty($params['maxWidth']))
81 {
82 $this->setMaxWidth($params['maxWidth']);
83 }
84
85 if (!empty($params['className']) && is_string($params['className']))
86 {
87 $params['classList'] = array_filter(explode(' ', $params['className']));
88 }
89
90 if (empty($params['classList']))
91 {
92 $params['classList'] = [];
93 }
94
95 $params['classList'] = array_merge(
96 [$this->getBaseClass()],
97 $params['classList']
98 );
99
100 $this->getAttributeCollection()->setClassList($params['classList']);
101
102 if (!empty($params['counter']))
103 {
104 $this->setCounter($params['counter']);
105 }
106
107 if (!empty($params['id']))
108 {
109 $this->setId($params['id']);
110 }
111
112 if (!empty($params['tag']))
113 {
114 $this->setTag($params['tag']);
115 }
116
117 if (!empty($params['link']))
118 {
119 $this->setLink($params['link']);
120 }
121
122 if (!empty($params['click']))
123 {
124 $this->bindEvent('click', $params['click']);
125 }
126
127 if (!empty($params['onclick']))
128 {
129 $this->bindEvent('click', $params['onclick']);
130 }
131
132 if (!empty($params['events']))
133 {
134 $this->bindEvents($params['events']);
135 }
136
137 if (isset($params['dataset']) && is_array($params['dataset']))
138 {
139 foreach ($params['dataset'] as $name => $value)
140 {
141 $this->addDataAttribute($name, $value);
142 }
143 }
144 }
145
146 protected function listExtensions()
147 {
148 return [];
149 }
150
151 public static function getJsClass()
152 {
153 return 'BX.UI.' . (new \ReflectionClass(get_called_class()))->getShortName();
154 }
155
156 protected function appendDefaultJsonOption(ButtonAttributes $attributes)
157 {
158 if (count($this->getEvents()) > 0)
159 {
160 $attributes->addJsonOption('events', $this->getEvents());
161 }
162
163 return $attributes;
164 }
165
166 public function render($jsInit = true)
167 {
168 Extension::load($this->listExtensions());
169
170 $output = '';
171 $tagName = $this->getTag();
172 $attributes = clone $this->getAttributeCollection();
173 $this->appendDefaultJsonOption($attributes);
174
175 switch ($tagName)
176 {
177 case Tag::LINK:
178 case Tag::BUTTON:
179 if ($tagName === Tag::LINK && $this->getLink())
180 {
181 $attributes['href'] = $this->getLink();
182 }
183
184 $inner = $this->renderInner();
185 $output = "<{$tagName} {$attributes}>{$inner}</{$tagName}>";
186 break;
187 case Tag::INPUT:
188 case Tag::SUBMIT:
189 $attributes['value'] = htmlspecialcharsbx($this->getText());
190 $attributes['type'] = Tag::BUTTON;
191
192 if ($tagName === Tag::SUBMIT)
193 {
194 $tagName = Tag::INPUT;
195 $attributes['type'] = Tag::SUBMIT;
196 }
197
198 $output = "<{$tagName} {$attributes}/>";
199 break;
200 }
201
202 if ($jsInit)
203 {
204 $js = $this->renderJavascript();
205 if ($js)
206 {
207 $output .= "<script>BX.ready(function(){ {$js} });</script>";
208 }
209 }
210
211 return $output;
212 }
213
214 protected function generateUniqid()
215 {
216 return 'uibtn-' . Random::getString(8);
217 }
218
219 public function isInputTag()
220 {
221 return $this->isInputType();
222 }
223
224 public function isInputType()
225 {
226 return in_array($this->tag, [
229 ], true);
230 }
231
232 protected function renderInner()
233 {
234 $counter = $this->getCounter();
235 return (
236 (!empty($this->getText()) ? '<span class="ui-btn-text">'.htmlspecialcharsbx($this->getText()).'</span>' : '').
237 ($counter !== null ? '<span class="ui-btn-counter">'.htmlspecialcharsbx($counter).'</span>' : '' )
238 );
239 }
240
241 protected function renderJavascript()
242 {
243 $selector = $this->getQuerySelector();
244
245 return "BX.UI.ButtonManager.createFromNode(document.querySelector('{$selector}'));";
246 }
247
248 protected function getQuerySelector()
249 {
250 $tag = $this->getTag();
251 $uniqId = $this->getUniqId();
252 $uniqIdName = "data-" . self::UNIQ_ID_DATA_ATTR;
253
254 return "{$tag}[{$uniqIdName}=\"{$uniqId}\"]";
255 }
256
257 public function getUniqId()
258 {
259 return $this->getAttributeCollection()->getDataAttribute(self::UNIQ_ID_DATA_ATTR);
260 }
261
262 public function getId()
263 {
264 return $this->id;
265 }
266
267 public function setId($id)
268 {
269 $this->id = $id;
270
271 return $this;
272 }
273
274 public function getMaxWidth()
275 {
276 return isset($this->getAttributeCollection()['style']['max-width'])?
277 $this->getAttributeCollection()['style']['max-width'] : null;
278 }
279
280 public function setMaxWidth($width)
281 {
282 if (!isset($this->getAttributeCollection()['style']))
283 {
284 $this->getAttributeCollection()['style'] = [];
285 }
286
287 $this->getAttributeCollection()['style']['max-width'] = $width;
288
289 return $this;
290 }
291
292 public function getLink()
293 {
294 return $this->link;
295 }
296
297 public function setLink($link)
298 {
299 if (is_string($link) && !empty($link))
300 {
301 $this->link = $link;
302 $this->setTag(Tag::LINK);
303 }
304
305 return $this;
306 }
307
308 public function getCounter()
309 {
310 return $this->counter;
311 }
312
313 public function setCounter($counter)
314 {
315 if (in_array($counter, [0, '0', '', null, false], true))
316 {
317 $this->counter = null;
318 }
319 else if ((is_int($counter) && $counter > 0) || (is_string($counter) && mb_strlen($counter)))
320 {
321 $this->counter = $counter;
322 }
323
324 return $this;
325 }
326
327 public function addClass($className)
328 {
329 $this->getAttributeCollection()->addClass($className);
330
331 return $this;
332 }
333
334 public function unsetClass($className)
335 {
336 $this->getAttributeCollection()->removeClass($className);
337
338 return $this;
339 }
340
341 public function removeClass($className)
342 {
343 return $this->unsetClass($className);
344 }
345
346 public function hasClass($className)
347 {
348 return $this->getAttributeCollection()->hasClass($className);
349 }
350
351 public function getClassList()
352 {
353 return $this->getAttributeCollection()['class']?: [];
354 }
355
356 public function addAttribute($name, $value = null)
357 {
358 if (mb_strtolower($name) === 'class')
359 {
360 throw new ArgumentException('Could not add "class" attribute. You should use ::addClass()', 'class');
361 }
362
363 $this->getAttributeCollection()[$name] = $value;
364
365 return $this;
366 }
367
368 public function unsetAttribute($name)
369 {
370 unset($this->getAttributeCollection()[$name]);
371
372 return $this;
373 }
374
375 public function removeAttribute($name)
376 {
377 return $this->unsetAttribute($name);
378 }
379
380 public function getAttribute($name, $defaultValue = null)
381 {
382 return $this->getAttributeCollection()->getAttribute($name, $defaultValue);
383 }
384
385 public function addDataAttribute($name, $value = null)
386 {
387 $this->getAttributeCollection()->addDataAttribute($name, $value);
388
389 return $this;
390 }
391
392 public function getDataAttribute($name, $defaultValue = null)
393 {
394 return $this->getAttributeCollection()->getDataAttribute($name, $defaultValue);
395 }
396
397 public function setDataRole($dataRole)
398 {
399 $this->addDataAttribute('role', $dataRole);
400
401 return $this;
402 }
403
404 public function getDataRole()
405 {
406 return $this->getDataAttribute('role');
407 }
408
409 public function setStyles(array $styles)
410 {
411 $this->getAttributeCollection()['style'] = $styles;
412 }
413
414 public function getStyles()
415 {
416 return $this->getAttributeCollection()['style'];
417 }
418
422 public function getAttributeCollection()
423 {
424 return $this->attributes;
425 }
426
430 public function getText()
431 {
432 return $this->text;
433 }
434
440 public function setText($text)
441 {
442 $this->text = $text;
443
444 return $this;
445 }
446
450 public function getTag()
451 {
452 return $this->tag;
453 }
454
460 public function setTag($tag)
461 {
462 $this->tag = $tag;
463
464 return $this;
465 }
466
470 public function getBaseClass()
471 {
472 return $this->baseClass;
473 }
474
479 public function setDisabled($flag = true)
480 {
481 if ($flag === false)
482 {
483 unset($this->getAttributeCollection()['disabled']);
484 }
485 else
486 {
487 $this->getAttributeCollection()['disabled'] = true;
488 }
489
490 return $this;
491 }
492
496 public function isDisabled()
497 {
498 return $this->getAttributeCollection()['disabled'] === true;
499 }
500
504 public function getEvents()
505 {
506 return $this->events;
507 }
508
516 public function bindEvent($eventName, $fn)
517 {
518 if (is_string($fn))
519 {
520 $fn = new JsHandler($fn);
521 }
522
523 $this->events[$eventName] = $fn;
524
525 return $this;
526 }
527
533 public function bindEvents(array $events)
534 {
535 foreach ($events as $name => $fn)
536 {
537 $this->bindEvent($name, $fn);
538 }
539
540 return $this;
541 }
542
548 public function unbindEvent($eventName)
549 {
550 unset($this->events[$eventName]);
551
552 return $this;
553 }
554
558 public function unbindEvents()
559 {
560 unset($this->events);
561
562 return $this;
563 }
564}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
getDataAttribute($name, $defaultValue=null)
appendDefaultJsonOption(ButtonAttributes $attributes)
getAttribute($name, $defaultValue=null)
__construct(array $params=[])
static create(array $params=[])
addDataAttribute($name, $value=null)
addAttribute($name, $value=null)