| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* User interface control. |
| 7 |
* |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
class Widget extends Element { |
| 11 |
|
| 12 |
/* Properties */ |
| 13 |
|
| 14 |
/** |
| 15 |
* Disabled. |
| 16 |
* |
| 17 |
* @var bool Widget is disabled |
| 18 |
*/ |
| 19 |
protected $disabled = false; |
| 20 |
|
| 21 |
/* Methods */ |
| 22 |
|
| 23 |
/** |
| 24 |
* @param array $config Configuration options |
| 25 |
* - bool $config['disabled'] Disable (default: false) |
| 26 |
*/ |
| 27 |
public function __construct( array $config = [] ) { |
| 28 |
// Parent constructor |
| 29 |
parent::__construct( $config ); |
| 30 |
|
| 31 |
// Initialization |
| 32 |
$this->addClasses( [ 'oo-ui-widget' ] ); |
| 33 |
$this->setDisabled( $config['disabled'] ?? false ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Check if the widget is disabled. |
| 38 |
* |
| 39 |
* @return bool Button is disabled |
| 40 |
*/ |
| 41 |
public function isDisabled() { |
| 42 |
return $this->disabled; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the disabled state of the widget. |
| 47 |
* |
| 48 |
* This should probably change the widgets' appearance and prevent it from being used. |
| 49 |
* |
| 50 |
* @param bool $disabled Disable widget |
| 51 |
* @return $this |
| 52 |
*/ |
| 53 |
public function setDisabled( $disabled ) { |
| 54 |
$this->disabled = (bool)$disabled; |
| 55 |
$this->toggleClasses( [ 'oo-ui-widget-disabled' ], $this->disabled ); |
| 56 |
$this->toggleClasses( [ 'oo-ui-widget-enabled' ], !$this->disabled ); |
| 57 |
if ( $this->disabled ) { |
| 58 |
$this->setAttributes( [ 'aria-disabled' => 'true' ] ); |
| 59 |
} else { |
| 60 |
$this->removeAttributes( [ 'aria-disabled' ] ); |
| 61 |
} |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get an ID of a labelable node which is part of this widget, if any, to be used for |
| 68 |
* `<label for>` value. |
| 69 |
* |
| 70 |
* @return string|null The ID of the labelable node |
| 71 |
*/ |
| 72 |
public function getInputId() { |
| 73 |
return null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set the element with the given ID as a label for this widget. |
| 78 |
* |
| 79 |
* @param string|null $id |
| 80 |
*/ |
| 81 |
public function setLabelledBy( $id ) { |
| 82 |
if ( $id ) { |
| 83 |
$this->setAttributes( [ 'aria-labelledby' => $id ] ); |
| 84 |
} else { |
| 85 |
$this->removeAttributes( [ 'aria-labelledby' ] ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** @inheritDoc */ |
| 90 |
public function getConfig( &$config ) { |
| 91 |
if ( $this->disabled ) { |
| 92 |
$config['disabled'] = $this->disabled; |
| 93 |
} |
| 94 |
return parent::getConfig( $config ); |
| 95 |
} |
| 96 |
} |