1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
basebutton.php
См. документацию.
1<?php
2
3namespace Bitrix\UI\Buttons;
4
5
6use Bitrix\Main\ArgumentException;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Security\Random;
9use Bitrix\Main\UI\Extension;
10use Bitrix\UI\Contract;
11use Bitrix\UI\counter\Counter;
12use Bitrix\UI\Counter\CounterColor;
13use Bitrix\UI\Counter\CounterStyle;
14
15//We know about lazy load. So, the code loads common messages for default buttons,
16//which implemented by subclasses of BaseButton.
17Loc::loadLanguageFile(__FILE__);
18
20{
21 const UNIQ_ID_DATA_ATTR = 'btn-uniqid';
22 const JSON_OPTIONS_DATA_ATTR = 'json-options';
23
25 protected $id;
27 protected $text;
29 protected bool $useAirDesign = false;
31 protected $tag = Tag::BUTTON;
33 protected $baseClass = "ui-btn";
35 protected $link;
37 protected $counter;
39 protected $events = [];
41 private $attributes;
42
43 final public function __construct(array $params = [])
44 {
45 $this->attributes = new ButtonAttributes();
46 $this->attributes->addDataAttribute(self::UNIQ_ID_DATA_ATTR, $this->generateUniqid());
47 $this->addClass($this->getBaseClass());
48
49 $this->init($params);
50 }
51
55 protected function getDefaultParameters()
56 {
57 return [];
58 }
59
60 protected function init(array $params = [])
61 {
62 $this->buildFromArray(array_merge(
63 $this->getDefaultParameters(),
65 ));
66 }
67
68 final public static function create(array $params = [])
69 {
70 return new static($params);
71 }
72
73 protected function buildFromArray($params)
74 {
75 if (isset($params['text']))
76 {
77 $this->setText($params['text']);
78 }
79
80 if (!empty($params['styles']))
81 {
82 $this->setStyles($params['styles']);
83 }
84
85 if (!empty($params['maxWidth']))
86 {
87 $this->setMaxWidth($params['maxWidth']);
88 }
89
90 if (!empty($params['className']) && is_string($params['className']))
91 {
92 $params['classList'] = array_filter(explode(' ', $params['className']));
93 }
94
95 if (empty($params['classList']))
96 {
97 $params['classList'] = [];
98 }
99
100 $params['classList'] = array_merge(
101 [$this->getBaseClass()],
102 $params['classList']
103 );
104
105 $this->getAttributeCollection()->setClassList($params['classList']);
106
107 if (isset($params['air']) && $params['air'] === true)
108 {
109 $this->setAirDesign();
110 }
111
112 if (!empty($params['counter']))
113 {
114 $this->setCounter($params['counter']);
115 }
116
117 if (!empty($params['id']))
118 {
119 $this->setId($params['id']);
120 }
121
122 if (!empty($params['tag']))
123 {
124 $this->setTag($params['tag']);
125 }
126
127 if (!empty($params['link']))
128 {
129 $this->setLink($params['link']);
130 }
131
132 if (!empty($params['click']))
133 {
134 $this->bindEvent('click', $params['click']);
135 }
136
137 if (!empty($params['onclick']))
138 {
139 $this->bindEvent('click', $params['onclick']);
140 }
141
142 if (!empty($params['events']))
143 {
144 $this->bindEvents($params['events']);
145 }
146
147 if (isset($params['dataset']) && is_array($params['dataset']))
148 {
149 foreach ($params['dataset'] as $name => $value)
150 {
151 $this->addDataAttribute($name, $value);
152 }
153 }
154 }
155
156 protected function listExtensions()
157 {
158 return [];
159 }
160
161 public static function getJsClass()
162 {
163 return 'BX.UI.' . (new \ReflectionClass(get_called_class()))->getShortName();
164 }
165
166 protected function appendDefaultJsonOption(ButtonAttributes $attributes)
167 {
168 if (count($this->getEvents()) > 0)
169 {
170 $attributes->addJsonOption('events', $this->getEvents());
171 }
172
173 return $attributes;
174 }
175
176 public function render($jsInit = true)
177 {
178 Extension::load($this->listExtensions());
179
180 $output = '';
181 $tagName = $this->getTag();
182 $attributes = clone $this->getAttributeCollection();
183 $this->appendDefaultJsonOption($attributes);
184
185 switch ($tagName)
186 {
187 case Tag::LINK:
188 case Tag::BUTTON:
189 if ($tagName === Tag::LINK && $this->getLink())
190 {
191 $attributes['href'] = $this->getLink();
192 }
193
194 $inner = $this->renderInner();
195 $output = "<{$tagName} {$attributes}>{$inner}</{$tagName}>";
196 break;
197 case Tag::INPUT:
198 case Tag::SUBMIT:
199 $attributes['value'] = htmlspecialcharsbx($this->getText());
200 $attributes['type'] = Tag::BUTTON;
201
202 if ($tagName === Tag::SUBMIT)
203 {
204 $tagName = Tag::INPUT;
205 $attributes['type'] = Tag::SUBMIT;
206 }
207
208 $output = "<{$tagName} {$attributes}/>";
209 break;
210 }
211
212 if ($jsInit)
213 {
214 $js = $this->renderJavascript();
215 if ($js)
216 {
217 $output .= "<script>BX.ready(function(){ {$js} });</script>";
218 }
219 }
220
221 return $output;
222 }
223
224 protected function generateUniqid()
225 {
226 return 'uibtn-' . Random::getString(8);
227 }
228
229 public function isInputTag()
230 {
231 return $this->isInputType();
232 }
233
234 public function isInputType()
235 {
236 return in_array($this->tag, [
239 ], true);
240 }
241
242 protected function renderInner()
243 {
244 $counter = $this->getCounter();
245
246 if ($this->hasAirDesign())
247 {
248 $result = '<span class="ui-btn-text">';
249
250 $result .= '<span class="ui-btn-text-inner">';
251
252 if (!empty($this->getText()))
253 {
255 }
256
257 $result .= '</span>';
258
259 if ($counter !== null)
260 {
261 $counter = new Counter(
262 useAirDesign: true,
263 style: CounterStyle::FILLED_ALERT,
264 value: (int)$counter,
265 );
266
267 $result .= '<span class="ui-btn-right-counter">' . $counter->render() . '</span>';
268 }
269
270 $result .= '</span>';
271
272 return $result;
273 }
274
275 return (
276 (!empty($this->getText()) ? '<span class="ui-btn-text">'.htmlspecialcharsbx($this->getText()).'</span>' : '').
277 ($counter !== null ? '<span class="ui-btn-counter">'.htmlspecialcharsbx($counter).'</span>' : '' )
278 );
279 }
280
281 protected function renderJavascript()
282 {
283 $selector = $this->getQuerySelector();
284
285 return "BX.UI.ButtonManager.createFromNode(document.querySelector('{$selector}'));";
286 }
287
288 protected function getQuerySelector()
289 {
290 $tag = $this->getTag();
291 $uniqId = $this->getUniqId();
292 $uniqIdName = "data-" . self::UNIQ_ID_DATA_ATTR;
293
294 return "{$tag}[{$uniqIdName}=\"{$uniqId}\"]";
295 }
296
297 public function getUniqId()
298 {
299 return $this->getAttributeCollection()->getDataAttribute(self::UNIQ_ID_DATA_ATTR);
300 }
301
302 public function setUniqId(string $uniqId): self
303 {
304 if (!empty($uniqId))
305 {
306 $this->getAttributeCollection()->addDataAttribute(self::UNIQ_ID_DATA_ATTR, $uniqId);
307 }
308
309 return $this;
310 }
311
312 public function getId()
313 {
314 return $this->id;
315 }
316
317 public function setId($id)
318 {
319 $this->id = $id;
320
321 return $this;
322 }
323
324 public function getMaxWidth()
325 {
326 return isset($this->getAttributeCollection()['style']['max-width'])?
327 $this->getAttributeCollection()['style']['max-width'] : null;
328 }
329
330 public function setMaxWidth($width)
331 {
332 if (!isset($this->getAttributeCollection()['style']))
333 {
334 $this->getAttributeCollection()['style'] = [];
335 }
336
337 $this->getAttributeCollection()['style']['max-width'] = $width;
338
339 return $this;
340 }
341
342 public function getLink()
343 {
344 return $this->link;
345 }
346
347 public function setLink($link)
348 {
349 if (is_string($link) && !empty($link))
350 {
351 $this->link = $link;
352 $this->setTag(Tag::LINK);
353 }
354
355 return $this;
356 }
357
358 public function getCounter()
359 {
360 return $this->counter;
361 }
362
363 public function setCounter($counter)
364 {
365 if (in_array($counter, [0, '0', '', null, false], true))
366 {
367 $this->counter = null;
368 }
369 else if ((is_int($counter) && $counter > 0) || (is_string($counter) && mb_strlen($counter)))
370 {
371 $this->counter = $counter;
372 }
373
374 return $this;
375 }
376
377 public function addClass($className)
378 {
379 $this->getAttributeCollection()->addClass($className);
380
381 return $this;
382 }
383
384 public function unsetClass($className)
385 {
386 $this->getAttributeCollection()->removeClass($className);
387
388 return $this;
389 }
390
391 public function removeClass($className)
392 {
393 return $this->unsetClass($className);
394 }
395
396 public function hasClass($className)
397 {
398 return $this->getAttributeCollection()->hasClass($className);
399 }
400
401 public function getClassList()
402 {
403 return $this->getAttributeCollection()['class']?: [];
404 }
405
406 public function addAttribute($name, $value = null)
407 {
408 if (mb_strtolower($name) === 'class')
409 {
410 throw new ArgumentException('Could not add "class" attribute. You should use ::addClass()', 'class');
411 }
412
413 $this->getAttributeCollection()[$name] = $value;
414
415 return $this;
416 }
417
418 public function unsetAttribute($name)
419 {
420 unset($this->getAttributeCollection()[$name]);
421
422 return $this;
423 }
424
425 public function removeAttribute($name)
426 {
427 return $this->unsetAttribute($name);
428 }
429
430 public function getAttribute($name, $defaultValue = null)
431 {
432 return $this->getAttributeCollection()->getAttribute($name, $defaultValue);
433 }
434
435 public function addDataAttribute($name, $value = null)
436 {
437 $this->getAttributeCollection()->addDataAttribute($name, $value);
438
439 return $this;
440 }
441
442 public function getDataAttribute($name, $defaultValue = null)
443 {
444 return $this->getAttributeCollection()->getDataAttribute($name, $defaultValue);
445 }
446
447 public function setDataRole($dataRole)
448 {
449 $this->addDataAttribute('role', $dataRole);
450
451 return $this;
452 }
453
454 public function getDataRole()
455 {
456 return $this->getDataAttribute('role');
457 }
458
459 public function setStyles(array $styles)
460 {
461 $this->getAttributeCollection()['style'] = $styles;
462 }
463
464 public function getStyles()
465 {
466 return $this->getAttributeCollection()['style'];
467 }
468
472 public function getAttributeCollection()
473 {
474 return $this->attributes;
475 }
476
480 public function getText()
481 {
482 return $this->text;
483 }
484
490 public function setText($text)
491 {
492 $this->text = $text;
493
494 return $this;
495 }
496
501 public function setAirDesign(bool $flag = true): void
502 {
503 if (defined('AIR_SITE_TEMPLATE') === false)
504 {
505 return;
506 }
507
508 $this->useAirDesign = $flag;
509
510 if ($flag)
511 {
512 $this->addClass('--air');
513 }
514 else
515 {
516 $this->removeClass('--air');
517 }
518 }
519
523 public function hasAirDesign(): bool
524 {
525 return $this->useAirDesign;
526 }
527
531 public function getTag()
532 {
533 return $this->tag;
534 }
535
541 public function setTag($tag)
542 {
543 $this->tag = $tag;
544
545 return $this;
546 }
547
551 public function getBaseClass()
552 {
553 return $this->baseClass;
554 }
555
560 public function setDisabled($flag = true)
561 {
562 if ($flag === false)
563 {
564 unset($this->getAttributeCollection()['disabled']);
565 }
566 else
567 {
568 $this->getAttributeCollection()['disabled'] = true;
569 }
570
571 return $this;
572 }
573
577 public function isDisabled()
578 {
579 return $this->getAttributeCollection()['disabled'] === true;
580 }
581
585 public function getEvents()
586 {
587 return $this->events;
588 }
589
597 public function bindEvent($eventName, $fn)
598 {
599 if (is_string($fn))
600 {
601 $fn = new JsHandler($fn);
602 }
603
604 $this->events[$eventName] = $fn;
605
606 return $this;
607 }
608
614 public function bindEvents(array $events)
615 {
616 foreach ($events as $name => $fn)
617 {
618 $this->bindEvent($name, $fn);
619 }
620
621 return $this;
622 }
623
629 public function unbindEvent($eventName)
630 {
631 unset($this->events[$eventName]);
632
633 return $this;
634 }
635
639 public function unbindEvents()
640 {
641 unset($this->events);
642
643 return $this;
644 }
645}
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
getDataAttribute($name, $defaultValue=null)
Определения basebutton.php:442
const UNIQ_ID_DATA_ATTR
Определения basebutton.php:21
buildFromArray($params)
Определения basebutton.php:73
appendDefaultJsonOption(ButtonAttributes $attributes)
Определения basebutton.php:166
setStyles(array $styles)
Определения basebutton.php:459
bindEvents(array $events)
Определения basebutton.php:614
setCounter($counter)
Определения basebutton.php:363
setLink($link)
Определения basebutton.php:347
unbindEvent($eventName)
Определения basebutton.php:629
getAttribute($name, $defaultValue=null)
Определения basebutton.php:430
unsetClass($className)
Определения basebutton.php:384
bool $useAirDesign
Определения basebutton.php:29
static getJsClass()
Определения basebutton.php:161
const JSON_OPTIONS_DATA_ATTR
Определения basebutton.php:22
hasClass($className)
Определения basebutton.php:396
removeClass($className)
Определения basebutton.php:391
render($jsInit=true)
Определения basebutton.php:176
__construct(array $params=[])
Определения basebutton.php:43
getAttributeCollection()
Определения basebutton.php:472
bindEvent($eventName, $fn)
Определения basebutton.php:597
setDisabled($flag=true)
Определения basebutton.php:560
setDataRole($dataRole)
Определения basebutton.php:447
removeAttribute($name)
Определения basebutton.php:425
addClass($className)
Определения basebutton.php:377
setMaxWidth($width)
Определения basebutton.php:330
static create(array $params=[])
Определения basebutton.php:68
unsetAttribute($name)
Определения basebutton.php:418
addDataAttribute($name, $value=null)
Определения basebutton.php:435
setText($text)
Определения basebutton.php:490
init(array $params=[])
Определения basebutton.php:60
addAttribute($name, $value=null)
Определения basebutton.php:406
getDefaultParameters()
Определения basebutton.php:55
setUniqId(string $uniqId)
Определения basebutton.php:302
setAirDesign(bool $flag=true)
Определения basebutton.php:501
addJsonOption($key, $value)
Определения buttonattributes.php:72
const INPUT
Определения tag.php:9
const BUTTON
Определения tag.php:6
const LINK
Определения tag.php:7
const SUBMIT
Определения tag.php:8
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$output
Определения options.php:436
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
$name
Определения menu_edit.php:35
Определения counter.php:3
font style
Определения invoice.php:442
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$width
Определения html.php:68
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799