File: CheckboxInputWidget.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,896 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;

/**
 * Checkbox input widget.
 */
class CheckboxInputWidget extends InputWidget {
	use RequiredElement;

	/* Static Properties */

	/** @var string */
	public static $tagName = 'span';

	/* Properties */

	/**
	 * Whether the checkbox is selected.
	 *
	 * @var bool
	 */
	protected $selected;

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

	/**
	 * @var IconWidget
	 */
	protected $checkIcon;

	/**
	 * @param array $config Configuration options
	 *      - bool $config['selected'] Whether the checkbox is initially selected
	 *          (default: false)
	 *      - bool $config['indeterminate'] Whether the checkbox is in the indeterminate state.
	 *          (default: false)
	 */
	public function __construct( array $config = [] ) {
		// Parent constructor
		parent::__construct( $config );

		// Properties
		$this->checkIcon = new IconWidget( [
			'icon' => 'check',
			'classes' => [ 'oo-ui-checkboxInputWidget-checkIcon' ],
		] );

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

		// Initialization
		$this->addClasses( [ 'oo-ui-checkboxInputWidget' ] );
		// Required for pretty styling in WikimediaUI theme
		$this->appendContent( $this->checkIcon );
		$this->setSelected( $config['selected'] ?? false );
		$this->setIndeterminate( $config['indeterminate'] ?? false );
	}

	/** @inheritDoc */
	protected function getInputElement( $config ) {
		return ( new Tag( 'input' ) )->setAttributes( [ 'type' => 'checkbox' ] );
	}

	/**
	 * Set selection state of this checkbox.
	 *
	 * @param bool $state Whether the checkbox is selected
	 * @return $this
	 */
	public function setSelected( $state ) {
		$this->selected = (bool)$state;
		if ( $this->selected ) {
			$this->input->setAttributes( [ 'checked' => 'checked' ] );
		} else {
			$this->input->removeAttributes( [ 'checked' ] );
		}
		return $this;
	}

	/**
	 * Check if this checkbox is selected.
	 *
	 * @return bool Checkbox is selected
	 */
	public function isSelected() {
		return $this->selected;
	}

	/**
	 * Set indeterminate state of this checkbox.
	 *
	 * @param bool $state Whether the checkbox is indeterminate
	 * @return $this
	 */
	public function setIndeterminate( $state ) {
		$this->indeterminate = (bool)$state;
		// NB: A checkbox can't be made indeterminate through HTML,
		// there is no indeterminate attribute. We can only store
		// the state for infusion.
		return $this;
	}

	/**
	 * Check if this checkbox is indeterminate.
	 *
	 * @return bool Checkbox is indeterminate
	 */
	public function isIndeterminate() {
		return $this->indeterminate;
	}

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