File: OptionWidget.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 (75 lines) | stat: -rw-r--r-- 1,885 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
<?php

namespace OOUI;

/**
 * OptionWidgets are special elements that can be selected and configured with data. The
 * data is often unique for each option, but it does not have to be.
 *
 * OptionWidgets are used SelectWidget to create a selection of mutually exclusive options.
 */
class OptionWidget extends Widget {

	use LabelElement;
	use FlaggedElement;
	use AccessKeyedElement;
	use TitledElement;

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

	/**
	 * @param array $config Configuration options
	 *      - bool $config['selected'] Whether to mark the option as selected
	 */
	public function __construct( array $config = [] ) {
		parent::__construct( $config );

		$this->initializeFlaggedElement( $config );
		$this->initializeTitledElement( array_merge( [ 'titled' => $this ], $config ) );
		$this->initializeAccessKeyedElement( array_merge( [ 'accessKeyed' => $this ], $config ) );
		$this->initializeLabelElement( $config );

		$this->appendContent( $this->label );

		$this->setSelected( $config['selected'] ?? false );

		$this->addClasses( [ 'oo-ui-optionWidget' ] );
		$this->setAttributes( [
			'role' => 'option'
		] );
	}

	/**
	 * Set the selected state of the option
	 *
	 * @param bool $selected The options is selected
	 * @return $this
	 */
	public function setSelected( bool $selected ) {
		$this->selected = $selected;
		$this->toggleClasses( [ 'oo-ui-optionWidget-selected' ], $selected );
		$this->setAttributes( [
			// 'selected' is not a config option, so set aria-selected false by default (same as js)
			'aria-selected' => $selected ? 'true' : 'false',
		] );
		return $this;
	}

	/**
	 * @return bool
	 */
	public function isSelected() {
		return $this->selected;
	}

	/** @inheritDoc */
	public function getConfig( &$config ) {
		if ( $this->selected ) {
			$config['selected'] = $this->selected;
		}
		return parent::getConfig( $config );
	}
}