Debian Codegraph @

vendor/oojs/oojs-ui/php/widgets/DropdownInputWidget.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Dropdown input widget, wrapping a `<select>` element. Intended to be used within a
7 * OO.ui.FormLayout.
8 */
9 class DropdownInputWidget extends InputWidget {
10 use RequiredElement;
11
12 /**
13 * HTML `<option>` tags for this widget.
14 * @var Tag[]
15 */
16 protected $options = [];
17
18 /**
19 * @param array $config Configuration options
20 * - array[] $config['options'] Array of menu options in the format
21 * described in DropdownInputWidget::setOptions().
22 */
23 public function __construct( array $config = [] ) {
24 // Parent constructor
25 parent::__construct( $config );
26
27 // Traits
28 $this->initializeRequiredElement(
29 array_merge( [ 'indicatorElement' => null ], $config )
30 );
31
32 // Initialization
33 $this->setOptions( $config['options'] ?? [] );
34 $this->addClasses( [ 'oo-ui-dropdownInputWidget', 'oo-ui-dropdownInputWidget-php' ] );
35 $this->input->addClasses( [ 'oo-ui-indicator-down' ] );
36 }
37
38 /** @inheritDoc */
39 protected function getInputElement( $config ) {
40 return new Tag( 'select' );
41 }
42
43 /** @inheritDoc */
44 public function setValue( $value ) {
45 $this->value = $this->cleanUpValue( $value );
46 foreach ( $this->options as $opt ) {
47 if ( $opt->getAttribute( 'value' ) === $this->value ) {
48 $opt->setAttributes( [ 'selected' => 'selected' ] );
49 } else {
50 $opt->removeAttributes( [ 'selected' ] );
51 }
52 }
53 return $this;
54 }
55
56 /**
57 * Set the options available for this input.
58 *
59 * Each element of the `$options` array should be an array with one of the
60 * following structures:
61 *
62 * -# For normal menu items (`label` and `disabled` are optional; if no
63 * label is provided, the 'data' value will be also used as the label):
64 * ~~~~~
65 * [ 'data' => 'optionvalue', 'label' => 'Option Label', 'disabled' => true ]
66 * ~~~~~
67 * -# For option groups ('disabled' is optional):
68 * ~~~~~
69 * [ 'optgroup' => 'Group Label', 'disabled' => true ]
70 * ~~~~~
71 *
72 * An `optgroup` will contain all subsequent options up until the next
73 * `optgroup` or the end of the array.
74 *
75 * @param array[] $options Array of options in format described above.
76 * @return $this
77 */
78 public function setOptions( $options ) {
79 $value = $this->getValue();
80 $availableValue = null;
81 $this->options = [];
82 $container = $this->input;
83
84 // Rebuild the dropdown menu
85 $this->input->clearContent();
86 foreach ( $options as $opt ) {
87 if ( !isset( $opt['optgroup'] ) ) {
88 $optValue = $this->cleanUpValue( $opt['data'] );
89 $option = ( new Tag( 'option' ) )
90 ->setAttributes( [ 'value' => $optValue ] )
91 ->appendContent( $opt['label'] ?? $optValue );
92
93 // Prefer the previous value, if available, otherwise select the first one
94 if ( $value === $optValue || $availableValue === null ) {
95 $availableValue = $optValue;
96 }
97 $container->appendContent( $option );
98 } else {
99 $option = ( new Tag( 'optgroup' ) )
100 ->setAttributes( [ 'label' => $opt['optgroup'] ] );
101 $this->input->appendContent( $option );
102 $container = $option;
103 }
104 // Add disabled attribute if required (both the <option> and
105 // <optgroup> elements can be disabled).
106 if ( $opt['disabled'] ?? false ) {
107 $option->setAttributes( [ 'disabled' => 'disabled' ] );
108 }
109
110 $this->options[] = $option;
111 }
112
113 // Restore the previous value, or reset to something sensible
114 if ( $availableValue !== null ) {
115 $this->setValue( $availableValue );
116 }
117
118 return $this;
119 }
120
121 /** @inheritDoc */
122 public function getConfig( &$config ) {
123 $optionsConfig = [];
124 foreach ( $this->options as $option ) {
125 if ( $option->getTag() !== 'optgroup' ) {
126 $label = $option->content[0];
127 $data = $option->getAttribute( 'value' );
128 $optionConfig = [ 'data' => $data, 'label' => $label ];
129 } else {
130 $optgroup = $option->getAttribute( 'label' );
131 $optionConfig = [ 'optgroup' => $optgroup ];
132 }
133 if ( $option->getAttribute( 'disabled' ) ) {
134 $optionConfig[ 'disabled' ] = true;
135 }
136 $optionsConfig[] = $optionConfig;
137 }
138 $config['options'] = $optionsConfig;
139 $config['$overlay'] = true;
140 return parent::getConfig( $config );
141 }
142 }