File: DropdownInputWidget.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (142 lines) | stat: -rw-r--r-- 4,111 bytes parent folder | download | duplicates (3)
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
<?php

namespace OOUI;

/**
 * Dropdown input widget, wrapping a `<select>` element. Intended to be used within a
 * OO.ui.FormLayout.
 */
class DropdownInputWidget extends InputWidget {
	use RequiredElement;

	/**
	 * HTML `<option>` tags for this widget.
	 * @var Tag[]
	 */
	protected $options = [];

	/**
	 * @param array $config Configuration options
	 *      - array[] $config['options'] Array of menu options in the format
	 * described in DropdownInputWidget::setOptions().
	 */
	public function __construct( array $config = [] ) {
		// Parent constructor
		parent::__construct( $config );

		// Traits
		$this->initializeRequiredElement(
			array_merge( [ 'indicatorElement' => null ], $config )
		);

		// Initialization
		$this->setOptions( $config['options'] ?? [] );
		$this->addClasses( [ 'oo-ui-dropdownInputWidget', 'oo-ui-dropdownInputWidget-php' ] );
		$this->input->addClasses( [ 'oo-ui-indicator-down' ] );
	}

	/** @inheritDoc */
	protected function getInputElement( $config ) {
		return new Tag( 'select' );
	}

	/** @inheritDoc */
	public function setValue( $value ) {
		$this->value = $this->cleanUpValue( $value );
		foreach ( $this->options as $opt ) {
			if ( $opt->getAttribute( 'value' ) === $this->value ) {
				$opt->setAttributes( [ 'selected' => 'selected' ] );
			} else {
				$opt->removeAttributes( [ 'selected' ] );
			}
		}
		return $this;
	}

	/**
	 * Set the options available for this input.
	 *
	 * Each element of the `$options` array should be an array with one of the
	 * following structures:
	 *
	 *   -# For normal menu items (`label` and `disabled` are optional; if no
	 *      label is provided, the 'data' value will be also used as the label):
	 * ~~~~~
	 * [ 'data' => 'optionvalue', 'label' => 'Option Label', 'disabled' => true ]
	 * ~~~~~
	 *   -# For option groups ('disabled' is optional):
	 * ~~~~~
	 * [ 'optgroup' => 'Group Label', 'disabled' => true ]
	 * ~~~~~
	 *
	 * An `optgroup` will contain all subsequent options up until the next
	 * `optgroup` or the end of the array.
	 *
	 * @param array[] $options Array of options in format described above.
	 * @return $this
	 */
	public function setOptions( $options ) {
		$value = $this->getValue();
		$availableValue = null;
		$this->options = [];
		$container = $this->input;

		// Rebuild the dropdown menu
		$this->input->clearContent();
		foreach ( $options as $opt ) {
			if ( !isset( $opt['optgroup'] ) ) {
				$optValue = $this->cleanUpValue( $opt['data'] );
				$option = ( new Tag( 'option' ) )
					->setAttributes( [ 'value' => $optValue ] )
					->appendContent( $opt['label'] ?? $optValue );

				// Prefer the previous value, if available, otherwise select the first one
				if ( $value === $optValue || $availableValue === null ) {
					$availableValue = $optValue;
				}
				$container->appendContent( $option );
			} else {
				$option = ( new Tag( 'optgroup' ) )
					->setAttributes( [ 'label' => $opt['optgroup'] ] );
				$this->input->appendContent( $option );
				$container = $option;
			}
			// Add disabled attribute if required (both the <option> and
			// <optgroup> elements can be disabled).
			if ( $opt['disabled'] ?? false ) {
				$option->setAttributes( [ 'disabled' => 'disabled' ] );
			}

			$this->options[] = $option;
		}

		// Restore the previous value, or reset to something sensible
		if ( $availableValue !== null ) {
			$this->setValue( $availableValue );
		}

		return $this;
	}

	/** @inheritDoc */
	public function getConfig( &$config ) {
		$optionsConfig = [];
		foreach ( $this->options as $option ) {
			if ( $option->getTag() !== 'optgroup' ) {
				$label = $option->content[0];
				$data = $option->getAttribute( 'value' );
				$optionConfig = [ 'data' => $data, 'label' => $label ];
			} else {
				$optgroup = $option->getAttribute( 'label' );
				$optionConfig = [ 'optgroup' => $optgroup ];
			}
			if ( $option->getAttribute( 'disabled' ) ) {
				$optionConfig[ 'disabled' ] = true;
			}
			$optionsConfig[] = $optionConfig;
		}
		$config['options'] = $optionsConfig;
		$config['$overlay'] = true;
		return parent::getConfig( $config );
	}
}