| 1 |
<?php |
| 2 |
namespace MediaWiki\Skins\Vector\Components; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class VectorComponentButton implements VectorComponent { |
| 8 |
|
| 9 |
private $label; |
| 10 |
|
| 11 |
private $icon; |
| 12 |
|
| 13 |
private $id; |
| 14 |
|
| 15 |
private $class; |
| 16 |
|
| 17 |
private $attributes; |
| 18 |
|
| 19 |
private $weight; |
| 20 |
|
| 21 |
private $action; |
| 22 |
|
| 23 |
private $iconOnly; |
| 24 |
|
| 25 |
private $href; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct( |
| 39 |
string $label, |
| 40 |
$icon = null, |
| 41 |
$id = null, |
| 42 |
$class = null, |
| 43 |
$attributes = [], |
| 44 |
$weight = 'normal', |
| 45 |
$action = 'default', |
| 46 |
$iconOnly = false, |
| 47 |
$href = null |
| 48 |
) { |
| 49 |
$this->label = $label; |
| 50 |
$this->icon = $icon; |
| 51 |
$this->id = $id; |
| 52 |
$this->class = $class; |
| 53 |
$this->attributes = $attributes; |
| 54 |
$this->weight = $weight; |
| 55 |
$this->action = $action; |
| 56 |
$this->iconOnly = $iconOnly; |
| 57 |
$this->href = $href; |
| 58 |
|
| 59 |
|
| 60 |
if ( $this->weight !== 'primary' && $this->weight !== 'quiet' ) { |
| 61 |
$this->weight = 'normal'; |
| 62 |
} |
| 63 |
|
| 64 |
if ( $this->action !== 'progressive' && $this->action !== 'destructive' ) { |
| 65 |
$this->action = 'default'; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private function getClasses(): string { |
| 73 |
$classes = 'cdx-button'; |
| 74 |
if ( $this->href ) { |
| 75 |
$classes .= ' cdx-button--fake-button cdx-button--fake-button--enabled'; |
| 76 |
} |
| 77 |
switch ( $this->weight ) { |
| 78 |
case 'primary': |
| 79 |
$classes .= ' cdx-button--weight-primary'; |
| 80 |
break; |
| 81 |
case 'quiet': |
| 82 |
$classes .= ' cdx-button--weight-quiet'; |
| 83 |
break; |
| 84 |
} |
| 85 |
switch ( $this->action ) { |
| 86 |
case 'progressive': |
| 87 |
$classes .= ' cdx-button--action-progressive'; |
| 88 |
break; |
| 89 |
case 'destructive': |
| 90 |
$classes .= ' cdx-button--action-destructive'; |
| 91 |
break; |
| 92 |
} |
| 93 |
if ( $this->iconOnly ) { |
| 94 |
$classes .= ' cdx-button--icon-only'; |
| 95 |
} |
| 96 |
if ( $this->class ) { |
| 97 |
$classes .= ' ' . $this->class; |
| 98 |
} |
| 99 |
return $classes; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getTemplateData(): array { |
| 106 |
$arrayAttributes = []; |
| 107 |
foreach ( $this->attributes as $key => $value ) { |
| 108 |
if ( $value === null ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
$arrayAttributes[] = [ 'key' => $key, 'value' => $value ]; |
| 112 |
} |
| 113 |
return [ |
| 114 |
'label' => $this->label, |
| 115 |
'icon' => $this->icon, |
| 116 |
'id' => $this->id, |
| 117 |
'class' => $this->getClasses(), |
| 118 |
'href' => $this->href, |
| 119 |
'array-attributes' => $arrayAttributes |
| 120 |
]; |
| 121 |
} |
| 122 |
} |