1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<?php
namespace OOUI;
/**
* Multiple checkbox input widget. Intended to be used within a OO.ui.FormLayout.
*
* @phan-suppress-next-line PhanParamSignatureMismatch Parent returns a string
* @method string[] getValue()
*/
class CheckboxMultiselectInputWidget extends InputWidget {
/* Properties */
/**
* @var string|null
*/
protected $name = null;
/**
* Input value.
*
* @var string[]
*/
protected $value = [];
/**
* Layouts for this input, as FieldLayouts.
*
* @var FieldLayout[]
*/
protected $fields = [];
/**
* @param array $config Configuration options
* - array[] $config['options'] Array of menu options in the format
* `[ 'data' => …, 'label' => …, 'disabled' => … ]`
*/
public function __construct( array $config = [] ) {
// Parent constructor
parent::__construct( $config );
if ( isset( $config['name'] ) ) {
$this->name = $config['name'];
}
// Initialization
$this->setOptions( $config['options'] ?? [] );
// Have to repeat this from parent, as we need options to be set up for this to make sense
$this->setValue( $config['value'] ?? null );
$this->addClasses( [ 'oo-ui-checkboxMultiselectInputWidget' ] );
}
/** @inheritDoc */
protected function getInputElement( $config ) {
// Actually unused
return new Tag( 'unused' );
}
/**
* Set the value of the input.
*
* @param mixed $value New value should be an array of strings
* @return $this
*/
public function setValue( $value ) {
$this->value = $this->cleanUpValue( $value );
// Deselect all options
foreach ( $this->fields as $field ) {
$widget = $field->getField();
'@phan-var CheckboxInputWidget $widget';
$widget->setSelected( false );
}
// Select the requested ones
foreach ( $this->value as $key ) {
$widget = $this->fields[ $key ]->getField();
'@phan-var CheckboxInputWidget $widget';
$widget->setSelected( true );
}
return $this;
}
/**
* Clean up incoming value.
*
* @param mixed $value Original value
* @return string[] Cleaned up value
* @suppress PhanParamSignatureMismatch Parent has 'string' instead of 'string[]'
*/
protected function cleanUpValue( $value ) {
$cleanValue = [];
if ( !is_array( $value ) ) {
return $cleanValue;
}
foreach ( $value as $singleValue ) {
$singleValue = parent::cleanUpValue( $singleValue );
// Remove options that we don't have here
if ( !isset( $this->fields[ $singleValue ] ) ) {
continue;
}
$cleanValue[] = $singleValue;
}
return $cleanValue;
}
/**
* Set the options available for this input.
*
* @param array[] $options Array of menu options in the format
* `[ 'data' => …, 'label' => …, 'disabled' => … ]`
* @return $this
*/
public function setOptions( $options ) {
$this->fields = [];
// Rebuild the checkboxes
$this->clearContent();
$name = $this->name;
foreach ( $options as $opt ) {
$optValue = parent::cleanUpValue( $opt['data'] );
$optDisabled = $opt['disabled'] ?? false;
$field = new FieldLayout(
new CheckboxInputWidget( [
'name' => $name,
'value' => $optValue,
'disabled' => $this->isDisabled() || $optDisabled,
] ),
[
'label' => $opt['label'] ?? $optValue,
'align' => 'inline',
]
);
$this->fields[ $optValue ] = $field;
$this->appendContent( $field );
}
// Re-set the value, checking the checkboxes as needed.
// This will also get rid of any stale options that we just removed.
$this->setValue( $this->getValue() );
return $this;
}
/** @inheritDoc */
public function setDisabled( $disabled ) {
parent::setDisabled( $disabled );
foreach ( $this->fields as $field ) {
$field->getField()->setDisabled( $this->isDisabled() );
}
return $this;
}
/** @inheritDoc */
public function getConfig( &$config ) {
$options = [];
foreach ( $this->fields as $field ) {
$widget = $field->getField();
'@phan-var CheckboxInputWidget $widget';
$options[] = [
'data' => $widget->getValue(),
'label' => $field->getLabel(),
'disabled' => $widget->isDisabled(),
];
}
$config['options'] = $options;
return parent::getConfig( $config );
}
}
|