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 * Operates similarly to MultiSelectWidget, but instead of using an array of
23 * options, uses an array of rows and an array of columns to dynamically
24 * construct a matrix of options. The tags used to identify a particular cell
25 * are of the form "columnName-rowName"
27 * @param array $config Configuration array with the following options:
29 * - Required associative array mapping column labels (as HTML) to their tags.
31 * - Required associative array mapping row labels (as HTML) to their tags.
33 * - Array of column-row tags to be displayed as enabled but unavailable to change.
35 * - Array of column-row tags to be displayed as disabled but unavailable to change.
37 * - Optional associative array mapping row labels to tooltips (as text, will be escaped).
39 public function __construct( array $config = [] ) {
40 // Configuration initialization
42 parent
::__construct( $config );
44 $this->name
= $config['name'] ??
null;
45 $this->id
= $config['id'] ??
null;
48 $this->rows
= $config['rows'] ??
[];
49 $this->columns
= $config['columns'] ??
[];
50 $this->tooltips
= $config['tooltips'] ??
[];
52 $this->values
= $config['values'] ??
[];
54 $this->forcedOn
= $config['forcedOn'] ??
[];
55 $this->forcedOff
= $config['forcedOff'] ??
[];
58 $table = new \OOUI\
Tag( 'table' );
59 $table->addClasses( [ 'mw-htmlform-matrix mw-widget-checkMatrixWidget-matrix' ] );
60 $thead = new \OOUI\
Tag( 'thead' );
61 $table->appendContent( $thead );
62 $tr = new \OOUI\
Tag( 'tr' );
65 $tr->appendContent( $this->getCellTag( "\u{00A0}" ) );
66 foreach ( $this->columns
as $columnLabel => $columnTag ) {
68 $this->getCellTag( new \OOUI\
HtmlSnippet( $columnLabel ), 'th' )
71 $thead->appendContent( $tr );
73 // Build the options matrix
74 $tbody = new \OOUI\
Tag( 'tbody' );
75 $table->appendContent( $tbody );
76 foreach ( $this->rows
as $rowLabel => $rowTag ) {
77 $tbody->appendContent(
78 $this->getTableRow( $rowLabel, $rowTag )
83 $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
84 $this->appendContent( $table );
88 * Get a formatted table row for the option, with
91 * @param string $label Row label (as HTML)
92 * @param string $tag Row tag name
93 * @return \OOUI\Tag The resulting table row
95 private function getTableRow( $label, $tag ) {
96 $row = new \OOUI\
Tag( 'tr' );
97 $tooltip = $this->getTooltip( $label );
98 $labelFieldConfig = $tooltip ?
[ 'help' => $tooltip ] : [];
100 $labelField = new \OOUI\
FieldLayout(
101 new \OOUI\
Widget(), // Empty widget, since we don't have the checkboxes here
103 'label' => new \OOUI\
HtmlSnippet( $label ),
105 ] +
$labelFieldConfig
107 $row->appendContent( $this->getCellTag( $labelField ) );
109 // Build checkbox column cells
110 foreach ( $this->columns
as $columnTag ) {
111 $thisTag = "$columnTag-$tag";
113 // Construct a checkbox
114 $checkbox = new \OOUI\
CheckboxInputWidget( [
116 'name' => $this->name ?
"{$this->name}[]" : null,
117 'id' => $this->id ?
"{$this->id}-$thisTag" : null,
118 'selected' => $this->isTagChecked( $thisTag ),
119 'disabled' => $this->isTagDisabled( $thisTag ),
122 $row->appendContent( $this->getCellTag( $checkbox ) );
128 * Get an individual cell tag with requested content
130 * @param mixed $content Content for the <td> cell
131 * @return \OOUI\Tag Resulting cell
133 private function getCellTag( $content, $tagElement = 'td' ) {
134 $cell = new \OOUI\
Tag( $tagElement );
135 $cell->appendContent( $content );
140 * Check whether the given tag's checkbox should
143 * @param string $tagName Tag name
144 * @return boolean Tag should be checked
146 private function isTagChecked( $tagName ) {
147 // If the tag is in the value list
148 return in_array( $tagName, (array)$this->values
, true ) ||
149 // Or if the tag is forced on
150 in_array( $tagName, (array)$this->forcedOn
, true );
154 * Check whether the given tag's checkbox should
157 * @param string $tagName Tag name
158 * @return boolean Tag should be disabled
160 private function isTagDisabled( $tagName ) {
162 // If the entire widget is disabled
163 $this->isDisabled() ||
164 // If the tag is 'forced on' or 'forced off'
165 in_array( $tagName, (array)$this->forcedOn
, true ) ||
166 in_array( $tagName, (array)$this->forcedOff
, true )
171 * Get the tooltip help associated with this row
173 * @param string $label Label name
174 * @return string Tooltip. Null if none is available.
176 private function getTooltip( $label ) {
177 return $this->tooltips
[ $label ] ??
null;
180 protected function getJavaScriptClassName() {
181 return 'mw.widgets.CheckMatrixWidget';
184 public function getConfig( &$config ) {
186 'name' => $this->name
,
188 'rows' => $this->rows
,
189 'columns' => $this->columns
,
190 'tooltips' => $this->tooltips
,
191 'forcedOff' => $this->forcedOff
,
192 'forcedOn' => $this->forcedOn
,
193 'values' => $this->values
,
195 return parent
::getConfig( $config );