| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Toggle widget. |
| 7 |
*/ |
| 8 |
class ToggleWidget extends Widget { |
| 9 |
use TitledElement; |
| 10 |
|
| 11 |
/** |
| 12 |
* Defines the value of the widget |
| 13 |
* |
| 14 |
* @var bool |
| 15 |
*/ |
| 16 |
protected $value; |
| 17 |
|
| 18 |
public function __construct( array $config = [] ) { |
| 19 |
parent::__construct( $config ); |
| 20 |
|
| 21 |
// Traits |
| 22 |
$this->initializeTitledElement( $config ); |
| 23 |
|
| 24 |
$this->addClasses( [ 'oo-ui-toggleWidget' ] ); |
| 25 |
$this->setValue( $config['value'] ?? false ); |
| 26 |
} |
| 27 |
|
| 28 |
/** @inheritDoc */ |
| 29 |
public function getConfig( &$config ) { |
| 30 |
$config['value'] = $this->value; |
| 31 |
return parent::getConfig( $config ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get value representing toggle state. |
| 36 |
* |
| 37 |
* @return bool value The on/off state |
| 38 |
*/ |
| 39 |
public function getValue() { |
| 40 |
return $this->value; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set the value of the widget |
| 45 |
* |
| 46 |
* @param bool|null $value Widget |
| 47 |
* @return $this |
| 48 |
*/ |
| 49 |
public function setValue( $value = null ) { |
| 50 |
$this->value = (bool)$value; |
| 51 |
|
| 52 |
$this->toggleClasses( [ 'oo-ui-toggleWidget-on' ], $this->value ); |
| 53 |
$this->toggleClasses( [ 'oo-ui-toggleWidget-off' ], !$this->value ); |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
} |