Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
basebutton.php
1
<?php
2
3
namespace
Bitrix\UI\Buttons
;
4
5
6
use
Bitrix\Main\ArgumentException
;
7
use
Bitrix\Main\Localization\Loc
;
8
use
Bitrix\Main\Security\Random
;
9
use
Bitrix\Main\UI\Extension
;
10
use
Bitrix\UI\Contract
;
11
12
//We know about lazy load. So, the code loads common messages for default buttons,
13
//which implemented by subclasses of BaseButton.
14
Loc::loadLanguageFile
(__FILE__);
15
16
class
BaseButton
implements
Contract\Renderable
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, [
227
Tag::INPUT
,
228
Tag::SUBMIT
,
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
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadLanguageFile
static loadLanguageFile($file, $language=null, $normalize=true)
Definition
loc.php:224
Bitrix\Main\Security\Random
Definition
random.php:6
Bitrix\Main\UI\Extension
Definition
extension.php:11
Bitrix\UI\Buttons\BaseButton
Definition
basebutton.php:17
Bitrix\UI\Buttons\BaseButton\getDataAttribute
getDataAttribute($name, $defaultValue=null)
Definition
basebutton.php:392
Bitrix\UI\Buttons\BaseButton\renderJavascript
renderJavascript()
Definition
basebutton.php:241
Bitrix\UI\Buttons\BaseButton\UNIQ_ID_DATA_ATTR
const UNIQ_ID_DATA_ATTR
Definition
basebutton.php:18
Bitrix\UI\Buttons\BaseButton\getId
getId()
Definition
basebutton.php:262
Bitrix\UI\Buttons\BaseButton\buildFromArray
buildFromArray($params)
Definition
basebutton.php:68
Bitrix\UI\Buttons\BaseButton\$baseClass
$baseClass
Definition
basebutton.php:28
Bitrix\UI\Buttons\BaseButton\$events
$events
Definition
basebutton.php:34
Bitrix\UI\Buttons\BaseButton\getStyles
getStyles()
Definition
basebutton.php:414
Bitrix\UI\Buttons\BaseButton\appendDefaultJsonOption
appendDefaultJsonOption(ButtonAttributes $attributes)
Definition
basebutton.php:156
Bitrix\UI\Buttons\BaseButton\setStyles
setStyles(array $styles)
Definition
basebutton.php:409
Bitrix\UI\Buttons\BaseButton\setTag
setTag($tag)
Definition
basebutton.php:460
Bitrix\UI\Buttons\BaseButton\bindEvents
bindEvents(array $events)
Definition
basebutton.php:533
Bitrix\UI\Buttons\BaseButton\setCounter
setCounter($counter)
Definition
basebutton.php:313
Bitrix\UI\Buttons\BaseButton\setLink
setLink($link)
Definition
basebutton.php:297
Bitrix\UI\Buttons\BaseButton\unbindEvent
unbindEvent($eventName)
Definition
basebutton.php:548
Bitrix\UI\Buttons\BaseButton\listExtensions
listExtensions()
Definition
basebutton.php:146
Bitrix\UI\Buttons\BaseButton\unbindEvents
unbindEvents()
Definition
basebutton.php:558
Bitrix\UI\Buttons\BaseButton\getAttribute
getAttribute($name, $defaultValue=null)
Definition
basebutton.php:380
Bitrix\UI\Buttons\BaseButton\renderInner
renderInner()
Definition
basebutton.php:232
Bitrix\UI\Buttons\BaseButton\unsetClass
unsetClass($className)
Definition
basebutton.php:334
Bitrix\UI\Buttons\BaseButton\getEvents
getEvents()
Definition
basebutton.php:504
Bitrix\UI\Buttons\BaseButton\isInputTag
isInputTag()
Definition
basebutton.php:219
Bitrix\UI\Buttons\BaseButton\$link
$link
Definition
basebutton.php:30
Bitrix\UI\Buttons\BaseButton\getUniqId
getUniqId()
Definition
basebutton.php:257
Bitrix\UI\Buttons\BaseButton\getDataRole
getDataRole()
Definition
basebutton.php:404
Bitrix\UI\Buttons\BaseButton\getCounter
getCounter()
Definition
basebutton.php:308
Bitrix\UI\Buttons\BaseButton\getJsClass
static getJsClass()
Definition
basebutton.php:151
Bitrix\UI\Buttons\BaseButton\JSON_OPTIONS_DATA_ATTR
const JSON_OPTIONS_DATA_ATTR
Definition
basebutton.php:19
Bitrix\UI\Buttons\BaseButton\getClassList
getClassList()
Definition
basebutton.php:351
Bitrix\UI\Buttons\BaseButton\$tag
$tag
Definition
basebutton.php:26
Bitrix\UI\Buttons\BaseButton\setId
setId($id)
Definition
basebutton.php:267
Bitrix\UI\Buttons\BaseButton\hasClass
hasClass($className)
Definition
basebutton.php:346
Bitrix\UI\Buttons\BaseButton\getText
getText()
Definition
basebutton.php:430
Bitrix\UI\Buttons\BaseButton\removeClass
removeClass($className)
Definition
basebutton.php:341
Bitrix\UI\Buttons\BaseButton\getMaxWidth
getMaxWidth()
Definition
basebutton.php:274
Bitrix\UI\Buttons\BaseButton\render
render($jsInit=true)
Definition
basebutton.php:166
Bitrix\UI\Buttons\BaseButton\__construct
__construct(array $params=[])
Definition
basebutton.php:38
Bitrix\UI\Buttons\BaseButton\getBaseClass
getBaseClass()
Definition
basebutton.php:470
Bitrix\UI\Buttons\BaseButton\getAttributeCollection
getAttributeCollection()
Definition
basebutton.php:422
Bitrix\UI\Buttons\BaseButton\bindEvent
bindEvent($eventName, $fn)
Definition
basebutton.php:516
Bitrix\UI\Buttons\BaseButton\setDisabled
setDisabled($flag=true)
Definition
basebutton.php:479
Bitrix\UI\Buttons\BaseButton\setDataRole
setDataRole($dataRole)
Definition
basebutton.php:397
Bitrix\UI\Buttons\BaseButton\getQuerySelector
getQuerySelector()
Definition
basebutton.php:248
Bitrix\UI\Buttons\BaseButton\getTag
getTag()
Definition
basebutton.php:450
Bitrix\UI\Buttons\BaseButton\removeAttribute
removeAttribute($name)
Definition
basebutton.php:375
Bitrix\UI\Buttons\BaseButton\getLink
getLink()
Definition
basebutton.php:292
Bitrix\UI\Buttons\BaseButton\addClass
addClass($className)
Definition
basebutton.php:327
Bitrix\UI\Buttons\BaseButton\isDisabled
isDisabled()
Definition
basebutton.php:496
Bitrix\UI\Buttons\BaseButton\setMaxWidth
setMaxWidth($width)
Definition
basebutton.php:280
Bitrix\UI\Buttons\BaseButton\create
static create(array $params=[])
Definition
basebutton.php:63
Bitrix\UI\Buttons\BaseButton\unsetAttribute
unsetAttribute($name)
Definition
basebutton.php:368
Bitrix\UI\Buttons\BaseButton\$counter
$counter
Definition
basebutton.php:32
Bitrix\UI\Buttons\BaseButton\addDataAttribute
addDataAttribute($name, $value=null)
Definition
basebutton.php:385
Bitrix\UI\Buttons\BaseButton\$text
$text
Definition
basebutton.php:24
Bitrix\UI\Buttons\BaseButton\isInputType
isInputType()
Definition
basebutton.php:224
Bitrix\UI\Buttons\BaseButton\$id
$id
Definition
basebutton.php:22
Bitrix\UI\Buttons\BaseButton\setText
setText($text)
Definition
basebutton.php:440
Bitrix\UI\Buttons\BaseButton\generateUniqid
generateUniqid()
Definition
basebutton.php:214
Bitrix\UI\Buttons\BaseButton\init
init(array $params=[])
Definition
basebutton.php:55
Bitrix\UI\Buttons\BaseButton\addAttribute
addAttribute($name, $value=null)
Definition
basebutton.php:356
Bitrix\UI\Buttons\BaseButton\getDefaultParameters
getDefaultParameters()
Definition
basebutton.php:50
Bitrix\UI\Buttons\ButtonAttributes
Definition
buttonattributes.php:9
Bitrix\UI\Buttons\ButtonAttributes\addJsonOption
addJsonOption($key, $value)
Definition
buttonattributes.php:72
Bitrix\UI\Buttons\JsHandler
Definition
jshandler.php:6
Bitrix\UI\Buttons\Tag\INPUT
const INPUT
Definition
tag.php:9
Bitrix\UI\Buttons\Tag\BUTTON
const BUTTON
Definition
tag.php:6
Bitrix\UI\Buttons\Tag\LINK
const LINK
Definition
tag.php:7
Bitrix\UI\Buttons\Tag\SUBMIT
const SUBMIT
Definition
tag.php:8
Bitrix\UI\Contract\Renderable
Definition
renderable.php:6
Bitrix\UI\Buttons
Definition
addbutton.php:3
Bitrix\UI\Contract
Definition
renderable.php:3
modules
ui
lib
buttons
basebutton.php
Создано системой
1.10.0