3 namespace MediaWiki\Widget
;
6 * Check matrix widget. Displays a matrix of checkboxes for given options
8 * @copyright 2018 MediaWiki Widgets Team and others; see AUTHORS.txt
11 class CheckMatrixWidget
extends \OOUI\Widget
{
14 protected $columns = [];
16 protected $tooltips = [];
17 protected $values = [];
18 protected $forcedOn = [];
19 protected $forcedOff = [];
22 * CheckMatrixWidget constructor
24 * Operates similarly to MultiSelectWidget, but instead of using an array of
25 * options, uses an array of rows and an array of columns to dynamically
26 * construct a matrix of options. The tags used to identify a particular cell
27 * are of the form "columnName-rowName"
29 * @param array $config Configuration array with the following options:
31 * - Required associative array mapping column labels (as HTML) to their tags.
33 * - Required associative array mapping row labels (as HTML) to their tags.
35 * - Array of column-row tags to be displayed as enabled but unavailable to change.
37 * - Array of column-row tags to be displayed as disabled but unavailable to change.
39 * - Optional associative array mapping row labels to tooltips (as text, will be escaped).
41 public function __construct( array $config = [] ) {
42 // Configuration initialization
44 parent
::__construct( $config );
46 $this->name
= $config['name'] ??
null;
47 $this->id
= $config['id'] ??
null;
50 $this->rows
= $config['rows'] ??
[];
51 $this->columns
= $config['columns'] ??
[];
52 $this->tooltips
= $config['tooltips'] ??
[];
54 $this->values
= $config['values'] ??
[];
56 $this->forcedOn
= $config['forcedOn'] ??
[];
57 $this->forcedOff
= $config['forcedOff'] ??
[];
60 $table = new \OOUI\
Tag( 'table' );
61 $table->addClasses( [ 'mw-htmlform-matrix mw-widget-checkMatrixWidget-matrix' ] );
62 $thead = new \OOUI\
Tag( 'thead' );
63 $table->appendContent( $thead );
64 $tr = new \OOUI\
Tag( 'tr' );
67 $tr->appendContent( $this->getCellTag( "\u{00A0}" ) );
68 foreach ( $this->columns
as $columnLabel => $columnTag ) {
70 $this->getCellTag( new \OOUI\
HtmlSnippet( $columnLabel ), 'th' )
73 $thead->appendContent( $tr );
75 // Build the options matrix
76 $tbody = new \OOUI\
Tag( 'tbody' );
77 $table->appendContent( $tbody );
78 foreach ( $this->rows
as $rowLabel => $rowTag ) {
79 $tbody->appendContent(
80 $this->getTableRow( $rowLabel, $rowTag )
85 $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
86 $this->appendContent( $table );
90 * Get a formatted table row for the option, with
93 * @param string $label Row label (as HTML)
94 * @param string $tag Row tag name
95 * @return \OOUI\Tag The resulting table row
97 private function getTableRow( $label, $tag ) {
98 $row = new \OOUI\
Tag( 'tr' );
99 $tooltip = $this->getTooltip( $label );
100 $labelFieldConfig = $tooltip ?
[ 'help' => $tooltip ] : [];
102 $labelField = new \OOUI\
FieldLayout(
103 new \OOUI\
Widget(), // Empty widget, since we don't have the checkboxes here
105 'label' => new \OOUI\
HtmlSnippet( $label ),
107 ] +
$labelFieldConfig
109 $row->appendContent( $this->getCellTag( $labelField ) );
111 // Build checkbox column cells
112 foreach ( $this->columns
as $columnTag ) {
113 $thisTag = "$columnTag-$tag";
115 // Construct a checkbox
116 $checkbox = new \OOUI\
CheckboxInputWidget( [
118 'name' => $this->name ?
"{$this->name}[]" : null,
119 'id' => $this->id ?
"{$this->id}-$thisTag" : null,
120 'selected' => $this->isTagChecked( $thisTag ),
121 'disabled' => $this->isTagDisabled( $thisTag ),
124 $row->appendContent( $this->getCellTag( $checkbox ) );
130 * Get an individual cell tag with requested content
132 * @param mixed $content Content for the <td> cell
133 * @return \OOUI\Tag Resulting cell
135 private function getCellTag( $content, $tagElement = 'td' ) {
136 $cell = new \OOUI\
Tag( $tagElement );
137 $cell->appendContent( $content );
142 * Check whether the given tag's checkbox should
145 * @param string $tagName Tag name
146 * @return boolean Tag should be checked
148 private function isTagChecked( $tagName ) {
149 // If the tag is in the value list
150 return in_array( $tagName, (array)$this->values
, true ) ||
151 // Or if the tag is forced on
152 in_array( $tagName, (array)$this->forcedOn
, true );
156 * Check whether the given tag's checkbox should
159 * @param string $tagName Tag name
160 * @return boolean Tag should be disabled
162 private function isTagDisabled( $tagName ) {
164 // If the entire widget is disabled
165 $this->isDisabled() ||
166 // If the tag is 'forced on' or 'forced off'
167 in_array( $tagName, (array)$this->forcedOn
, true ) ||
168 in_array( $tagName, (array)$this->forcedOff
, true )
173 * Get the tooltip help associated with this row
175 * @param string $label Label name
176 * @return string Tooltip. Null if none is available.
178 private function getTooltip( $label ) {
179 return $this->tooltips
[ $label ] ??
null;
182 protected function getJavaScriptClassName() {
183 return 'mw.widgets.CheckMatrixWidget';
186 public function getConfig( &$config ) {
188 'name' => $this->name
,
190 'rows' => $this->rows
,
191 'columns' => $this->columns
,
192 'tooltips' => $this->tooltips
,
193 'forcedOff' => $this->forcedOff
,
194 'forcedOn' => $this->forcedOn
,
195 'values' => $this->values
,
197 return parent
::getConfig( $config );