| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* A SelectWidget is of a generic selection of options. |
| 7 |
* |
| 8 |
* Should be used in conjunction with OptionWidget |
| 9 |
* |
| 10 |
* @method OptionWidget[] getItems() |
| 11 |
*/ |
| 12 |
class SelectWidget extends Widget { |
| 13 |
|
| 14 |
use GroupWidget; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var bool |
| 18 |
*/ |
| 19 |
protected $multiselect; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param array $config Configuration options |
| 23 |
* - OptionWidget[] $config['items'] OptionWidget objects to add to the select |
| 24 |
* - bool $config['multiselect'] Allow for multiple selections |
| 25 |
*/ |
| 26 |
public function __construct( array $config = [] ) { |
| 27 |
$config = array_merge( |
| 28 |
[ 'items' => [] ], |
| 29 |
$config |
| 30 |
); |
| 31 |
|
| 32 |
$this->initializeGroupElement( array_merge( $config, [ 'group' => $this ] ) ); |
| 33 |
|
| 34 |
$this->multiselect = $config['multiselect'] ?? false; |
| 35 |
|
| 36 |
$this->addClasses( [ |
| 37 |
'oo-ui-selectWidget', |
| 38 |
'oo-ui-selectWidget-unpressed', |
| 39 |
] ); |
| 40 |
$this->setAttributes( [ |
| 41 |
'role' => 'listbox', |
| 42 |
'aria-multiselectable' => $this->multiselect ? 'true' : 'false', |
| 43 |
] ); |
| 44 |
|
| 45 |
$this->addItems( $config['items'] ); |
| 46 |
parent::__construct( $config ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @return OptionWidget[]|OptionWidget|null |
| 51 |
*/ |
| 52 |
public function findSelectedItems() { |
| 53 |
/** @var OptionWidget[] $selected */ |
| 54 |
$selected = array_filter( $this->getItems(), static function ( $item ) { |
| 55 |
return $item->isSelected(); |
| 56 |
} ); |
| 57 |
|
| 58 |
return $this->multiselect ? |
| 59 |
$selected : |
| 60 |
( count( $selected ) ? |
| 61 |
$selected[ 0 ] : |
| 62 |
null ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @return OptionWidget[]|OptionWidget|null |
| 67 |
*/ |
| 68 |
public function findSelectedItem() { |
| 69 |
return $this->findSelectedItems(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Programmatically select an option by its data. If the `data` parameter is omitted, |
| 74 |
* or if the item does not exist, all options will be deselected. |
| 75 |
* |
| 76 |
* @param mixed $data Value of the item to select, omit to deselect all |
| 77 |
* @return $this |
| 78 |
*/ |
| 79 |
public function selectItemByData( $data = null ) { |
| 80 |
$itemFromData = $this->findItemFromData( $data ); |
| 81 |
'@phan-var OptionWidget|null $itemFromData'; |
| 82 |
if ( $data === null || $itemFromData === null ) { |
| 83 |
return $this->selectItem(); |
| 84 |
} |
| 85 |
return $this->selectItem( $itemFromData ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Unselect an option by its reference. |
| 90 |
* |
| 91 |
* @param OptionWidget $item Item to unselect, omit to deselect all |
| 92 |
* @return $this |
| 93 |
*/ |
| 94 |
public function unselectItem( $item ) { |
| 95 |
if ( $item ) { |
| 96 |
$item->setSelected( false ); |
| 97 |
} else { |
| 98 |
foreach ( $this->getItems() as $i ) { |
| 99 |
if ( $i->isSelected() ) { |
| 100 |
$i->setSelected( false ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Select an option by its reference. |
| 110 |
* |
| 111 |
* @param OptionWidget|null $item Item to select, omit to deselect all |
| 112 |
* @return $this |
| 113 |
*/ |
| 114 |
public function selectItem( $item = null ) { |
| 115 |
if ( $this->multiselect && $item ) { |
| 116 |
$item->setSelected( true ); |
| 117 |
} else { |
| 118 |
foreach ( $this->getItems() as $i ) { |
| 119 |
$selected = $item === $i; |
| 120 |
if ( $i->isSelected() !== $selected ) { |
| 121 |
$i->setSelected( $selected ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $this; |
| 127 |
} |
| 128 |
} |