| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Element with a button. |
| 7 |
* |
| 8 |
* Buttons are used for controls which can be clicked. They can be configured to use tab indexing |
| 9 |
* and access keys for accessibility purposes. |
| 10 |
* |
| 11 |
* @abstract |
| 12 |
*/ |
| 13 |
trait ButtonElement { |
| 14 |
|
| 15 |
/** |
| 16 |
* Button is framed. |
| 17 |
* |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $framed = false; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var Tag |
| 24 |
*/ |
| 25 |
protected $button; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param array $config Configuration options |
| 29 |
* - bool $config['framed'] Render button with a frame (default: true) |
| 30 |
*/ |
| 31 |
public function initializeButtonElement( array $config = [] ) { |
| 32 |
// Properties |
| 33 |
if ( !$this instanceof Element ) { |
| 34 |
throw new Exception( "ButtonElement trait can only be used on Element instances" ); |
| 35 |
} |
| 36 |
$target = $config['button'] ?? new Tag( 'a' ); |
| 37 |
$this->button = $target; |
| 38 |
|
| 39 |
// Initialization |
| 40 |
$this->addClasses( [ 'oo-ui-buttonElement' ] ); |
| 41 |
$this->button->addClasses( [ 'oo-ui-buttonElement-button' ] ); |
| 42 |
$this->toggleFramed( $config['framed'] ?? true ); |
| 43 |
|
| 44 |
// Add `role="button"` on `<a>` elements, where it's needed |
| 45 |
if ( strtolower( $this->button->getTag() ) === 'a' ) { |
| 46 |
$this->button->setAttributes( [ |
| 47 |
'role' => 'button', |
| 48 |
] ); |
| 49 |
} |
| 50 |
|
| 51 |
$this->registerConfigCallback( function ( &$config ) { |
| 52 |
if ( $this->framed !== true ) { |
| 53 |
$config['framed'] = $this->framed; |
| 54 |
} |
| 55 |
} ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Toggle frame. |
| 60 |
* |
| 61 |
* @param bool|null $framed Make button framed, omit to toggle |
| 62 |
* @return $this |
| 63 |
*/ |
| 64 |
public function toggleFramed( $framed = null ) { |
| 65 |
$this->framed = $framed !== null ? (bool)$framed : !$this->framed; |
| 66 |
$this->toggleClasses( [ 'oo-ui-buttonElement-framed' ], $this->framed ); |
| 67 |
$this->toggleClasses( [ 'oo-ui-buttonElement-frameless' ], !$this->framed ); |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if button has a frame. |
| 73 |
* |
| 74 |
* @return bool Button is framed |
| 75 |
*/ |
| 76 |
public function isFramed() { |
| 77 |
return $this->framed; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Toggle CSS classes. |
| 82 |
* |
| 83 |
* @param array $classes List of classes to add |
| 84 |
* @param bool|null $toggle Add classes |
| 85 |
* @return $this |
| 86 |
*/ |
| 87 |
abstract public function toggleClasses( array $classes, $toggle = null ); |
| 88 |
|
| 89 |
/** |
| 90 |
* @param callable $func |
| 91 |
*/ |
| 92 |
abstract public function registerConfigCallback( callable $func ); |
| 93 |
} |