| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Checkbox input widget. |
| 7 |
*/ |
| 8 |
class CheckboxInputWidget extends InputWidget { |
| 9 |
use RequiredElement; |
| 10 |
|
| 11 |
/* Static Properties */ |
| 12 |
|
| 13 |
/** @var string */ |
| 14 |
public static $tagName = 'span'; |
| 15 |
|
| 16 |
/* Properties */ |
| 17 |
|
| 18 |
/** |
| 19 |
* Whether the checkbox is selected. |
| 20 |
* |
| 21 |
* @var bool |
| 22 |
*/ |
| 23 |
protected $selected; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
protected $indeterminate; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var IconWidget |
| 32 |
*/ |
| 33 |
protected $checkIcon; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array $config Configuration options |
| 37 |
* - bool $config['selected'] Whether the checkbox is initially selected |
| 38 |
* (default: false) |
| 39 |
* - bool $config['indeterminate'] Whether the checkbox is in the indeterminate state. |
| 40 |
* (default: false) |
| 41 |
*/ |
| 42 |
public function __construct( array $config = [] ) { |
| 43 |
// Parent constructor |
| 44 |
parent::__construct( $config ); |
| 45 |
|
| 46 |
// Properties |
| 47 |
$this->checkIcon = new IconWidget( [ |
| 48 |
'icon' => 'check', |
| 49 |
'classes' => [ 'oo-ui-checkboxInputWidget-checkIcon' ], |
| 50 |
] ); |
| 51 |
|
| 52 |
// Traits |
| 53 |
$this->initializeRequiredElement( |
| 54 |
array_merge( [ 'indicatorElement' => null ], $config ) |
| 55 |
); |
| 56 |
|
| 57 |
// Initialization |
| 58 |
$this->addClasses( [ 'oo-ui-checkboxInputWidget' ] ); |
| 59 |
// Required for pretty styling in WikimediaUI theme |
| 60 |
$this->appendContent( $this->checkIcon ); |
| 61 |
$this->setSelected( $config['selected'] ?? false ); |
| 62 |
$this->setIndeterminate( $config['indeterminate'] ?? false ); |
| 63 |
} |
| 64 |
|
| 65 |
/** @inheritDoc */ |
| 66 |
protected function getInputElement( $config ) { |
| 67 |
return ( new Tag( 'input' ) )->setAttributes( [ 'type' => 'checkbox' ] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Set selection state of this checkbox. |
| 72 |
* |
| 73 |
* @param bool $state Whether the checkbox is selected |
| 74 |
* @return $this |
| 75 |
*/ |
| 76 |
public function setSelected( $state ) { |
| 77 |
$this->selected = (bool)$state; |
| 78 |
if ( $this->selected ) { |
| 79 |
$this->input->setAttributes( [ 'checked' => 'checked' ] ); |
| 80 |
} else { |
| 81 |
$this->input->removeAttributes( [ 'checked' ] ); |
| 82 |
} |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Check if this checkbox is selected. |
| 88 |
* |
| 89 |
* @return bool Checkbox is selected |
| 90 |
*/ |
| 91 |
public function isSelected() { |
| 92 |
return $this->selected; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Set indeterminate state of this checkbox. |
| 97 |
* |
| 98 |
* @param bool $state Whether the checkbox is indeterminate |
| 99 |
* @return $this |
| 100 |
*/ |
| 101 |
public function setIndeterminate( $state ) { |
| 102 |
$this->indeterminate = (bool)$state; |
| 103 |
// NB: A checkbox can't be made indeterminate through HTML, |
| 104 |
// there is no indeterminate attribute. We can only store |
| 105 |
// the state for infusion. |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if this checkbox is indeterminate. |
| 111 |
* |
| 112 |
* @return bool Checkbox is indeterminate |
| 113 |
*/ |
| 114 |
public function isIndeterminate() { |
| 115 |
return $this->indeterminate; |
| 116 |
} |
| 117 |
|
| 118 |
/** @inheritDoc */ |
| 119 |
public function getConfig( &$config ) { |
| 120 |
if ( $this->selected ) { |
| 121 |
$config['selected'] = $this->selected; |
| 122 |
} |
| 123 |
if ( $this->indeterminate ) { |
| 124 |
$config['indeterminate'] = $this->indeterminate; |
| 125 |
} |
| 126 |
return parent::getConfig( $config ); |
| 127 |
} |
| 128 |
} |