2023-01-30 14:55:10 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VectorSearchBox component
|
|
|
|
*/
|
|
|
|
class VectorComponentButton implements VectorComponent {
|
|
|
|
/** @var string */
|
|
|
|
private $label;
|
|
|
|
/** @var string|null */
|
2023-04-24 21:36:45 +00:00
|
|
|
private $icon;
|
|
|
|
/** @var string|null */
|
2023-01-30 14:55:10 +00:00
|
|
|
private $id;
|
|
|
|
/** @var string|null */
|
2023-04-24 21:36:45 +00:00
|
|
|
private $class;
|
|
|
|
/** @var array|null */
|
|
|
|
private $attributes;
|
2023-01-30 14:55:10 +00:00
|
|
|
/** @var string|null */
|
2023-04-24 21:36:45 +00:00
|
|
|
private $weight;
|
|
|
|
/** @var string|null */
|
|
|
|
private $action;
|
|
|
|
/** @var bool|null */
|
|
|
|
private $iconOnly;
|
2023-03-07 22:03:23 +00:00
|
|
|
/** @var string|null */
|
2023-04-24 21:36:45 +00:00
|
|
|
private $href;
|
2023-01-30 14:55:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $label
|
2023-04-24 21:36:45 +00:00
|
|
|
* @param string|null $icon
|
2023-01-30 14:55:10 +00:00
|
|
|
* @param string|null $id
|
2023-04-24 21:36:45 +00:00
|
|
|
* @param string|null $class
|
|
|
|
* @param array|null $attributes
|
|
|
|
* @param string|null $weight
|
|
|
|
* @param string|null $action
|
|
|
|
* @param bool|null $iconOnly
|
2023-01-30 14:55:10 +00:00
|
|
|
* @param string|null $href
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $label,
|
2023-03-07 22:03:23 +00:00
|
|
|
$icon = null,
|
2023-04-24 21:36:45 +00:00
|
|
|
$id = null,
|
|
|
|
$class = null,
|
|
|
|
$attributes = [],
|
|
|
|
$weight = 'normal',
|
|
|
|
$action = 'default',
|
|
|
|
$iconOnly = false,
|
|
|
|
$href = null
|
2023-01-30 14:55:10 +00:00
|
|
|
) {
|
|
|
|
$this->label = $label;
|
|
|
|
$this->icon = $icon;
|
2023-04-24 21:36:45 +00:00
|
|
|
$this->id = $id;
|
|
|
|
$this->class = $class;
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
$this->weight = $weight;
|
|
|
|
$this->action = $action;
|
|
|
|
$this->iconOnly = $iconOnly;
|
|
|
|
$this->href = $href;
|
|
|
|
|
|
|
|
// Weight can only be normal, primary, or quiet
|
|
|
|
if ( $this->weight != 'primary' && $this->weight != 'quiet' ) {
|
|
|
|
$this->weight = 'normal';
|
|
|
|
}
|
|
|
|
// Action can only be default, progressive or destructive
|
|
|
|
if ( $this->action != 'progressive' && $this->action != 'destructive' ) {
|
|
|
|
$this->action = 'default';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs button classes based on the props
|
|
|
|
*/
|
|
|
|
private function getClasses(): string {
|
|
|
|
$classes = 'mw-ui-button';
|
|
|
|
switch ( $this->weight ) {
|
|
|
|
case 'primary':
|
|
|
|
$classes .= ' mw-ui-primary';
|
|
|
|
break;
|
|
|
|
case 'quiet':
|
|
|
|
$classes .= ' mw-ui-quiet';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch ( $this->action ) {
|
|
|
|
case 'progressive':
|
|
|
|
$classes .= ' mw-ui-progressive';
|
|
|
|
break;
|
|
|
|
case 'destructive':
|
|
|
|
$classes .= ' mw-ui-destructive';
|
|
|
|
break;
|
|
|
|
}
|
2023-04-25 16:53:19 +00:00
|
|
|
if ( $this->iconOnly ) {
|
2023-04-24 21:36:45 +00:00
|
|
|
$classes .= ' mw-ui-icon-element';
|
|
|
|
}
|
|
|
|
if ( $this->class ) {
|
|
|
|
$classes .= ' ' . $this->class;
|
|
|
|
}
|
|
|
|
return $classes;
|
2023-01-30 14:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
2023-04-24 21:36:45 +00:00
|
|
|
$arrayAttributes = [];
|
|
|
|
foreach ( $this->attributes as $key => $value ) {
|
|
|
|
if ( $value === null ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$arrayAttributes[] = [ 'key' => $key, 'value' => $value ];
|
|
|
|
}
|
2023-01-30 14:55:10 +00:00
|
|
|
return [
|
2023-04-24 21:36:45 +00:00
|
|
|
'label' => $this->label,
|
|
|
|
'icon' => $this->icon,
|
2023-01-30 14:55:10 +00:00
|
|
|
'id' => $this->id,
|
2023-04-24 21:36:45 +00:00
|
|
|
'class' => $this->getClasses(),
|
2023-01-30 14:55:10 +00:00
|
|
|
'href' => $this->href,
|
2023-04-24 21:36:45 +00:00
|
|
|
'array-attributes' => $arrayAttributes
|
2023-01-30 14:55:10 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|