File: SelectWidget.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 (128 lines) | stat: -rw-r--r-- 2,921 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
<?php

namespace OOUI;

/**
 * A SelectWidget is of a generic selection of options.
 *
 * Should be used in conjunction with OptionWidget
 *
 * @method OptionWidget[] getItems()
 */
class SelectWidget extends Widget {

	use GroupWidget;

	/**
	 * @var bool
	 */
	protected $multiselect;

	/**
	 * @param array $config Configuration options
	 *      - OptionWidget[] $config['items'] OptionWidget objects to add to the select
	 *      - bool $config['multiselect'] Allow for multiple selections
	 */
	public function __construct( array $config = [] ) {
		$config = array_merge(
			[ 'items' => [] ],
			$config
		);

		$this->initializeGroupElement( array_merge( $config, [ 'group' => $this ] ) );

		$this->multiselect = $config['multiselect'] ?? false;

		$this->addClasses( [
			'oo-ui-selectWidget',
			'oo-ui-selectWidget-unpressed',
		] );
		$this->setAttributes( [
			'role' => 'listbox',
			'aria-multiselectable' => $this->multiselect ? 'true' : 'false',
		] );

		$this->addItems( $config['items'] );
		parent::__construct( $config );
	}

	/**
	 * @return OptionWidget[]|OptionWidget|null
	 */
	public function findSelectedItems() {
		/** @var OptionWidget[] $selected */
		$selected = array_filter( $this->getItems(), static function ( $item ) {
			return $item->isSelected();
		} );

		return $this->multiselect ?
			$selected :
			( count( $selected ) ?
				$selected[ 0 ] :
				null );
	}

	/**
	 * @return OptionWidget[]|OptionWidget|null
	 */
	public function findSelectedItem() {
		return $this->findSelectedItems();
	}

	/**
	 * Programmatically select an option by its data. If the `data` parameter is omitted,
	 * or if the item does not exist, all options will be deselected.
	 *
	 * @param mixed $data Value of the item to select, omit to deselect all
	 * @return $this
	 */
	public function selectItemByData( $data = null ) {
		$itemFromData = $this->findItemFromData( $data );
		'@phan-var OptionWidget|null $itemFromData';
		if ( $data === null || $itemFromData === null ) {
			return $this->selectItem();
		}
		return $this->selectItem( $itemFromData );
	}

	/**
	 * Unselect an option by its reference.
	 *
	 * @param OptionWidget $item Item to unselect, omit to deselect all
	 * @return $this
	 */
	public function unselectItem( $item ) {
		if ( $item ) {
			$item->setSelected( false );
		} else {
			foreach ( $this->getItems() as $i ) {
				if ( $i->isSelected() ) {
					$i->setSelected( false );
				}
			}
		}

		return $this;
	}

	/**
	 * Select an option by its reference.
	 *
	 * @param OptionWidget|null $item Item to select, omit to deselect all
	 * @return $this
	 */
	public function selectItem( $item = null ) {
		if ( $this->multiselect && $item ) {
			$item->setSelected( true );
		} else {
			foreach ( $this->getItems() as $i ) {
				$selected = $item === $i;
				if ( $i->isSelected() !== $selected ) {
					$i->setSelected( $selected );
				}
			}
		}

		return $this;
	}
}