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 list of columns in the matrix.
33 * - Required list of rows in the matrix.
35 * - Accepts array of column-row tags to be displayed as enabled but unavailable to change
37 * - Accepts array of column-row tags to be displayed as disabled but unavailable to change.
39 * - Optional array mapping row label to tooltip content
41 * - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
43 public function __construct( array $config = [] ) {
44 // Configuration initialization
46 parent
::__construct( $config );
48 $this->name
= isset( $config['name'] ) ?
49 $config[ 'name' ] : null;
50 $this->id
= isset( $config['id'] ) ?
54 $this->rows
= isset( $config['rows'] ) ?
56 $this->columns
= isset( $config['columns'] ) ?
57 $config['columns'] : [];
58 $this->tooltips
= isset( $config['tooltips'] ) ?
59 $config['tooltips'] : [];
61 $this->values
= isset( $config['values'] ) ?
62 $config['values'] : [];
64 $this->forcedOn
= isset( $config['forcedOn'] ) ?
65 $config['forcedOn'] : [];
66 $this->forcedOff
= isset( $config['forcedOff'] ) ?
67 $config['forcedOff'] : [];
70 $table = new \OOUI\
Tag( 'table' );
71 $tr = new \OOUI\
Tag( 'tr' );
73 $tr->appendContent( $this->getCellTag( "\u{00A0}" ) );
74 foreach ( $this->columns
as $columnLabel => $columnTag ) {
76 $this->getCellTag( $columnLabel )
79 $table->appendContent( $tr );
81 // Build the options matrix
82 foreach ( $this->rows
as $rowLabel => $rowTag ) {
83 $table->appendContent(
84 $this->getTableRow( $rowLabel, $rowTag )
89 $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
90 $this->appendContent( $table );
94 * Get a formatted table row for the option, with
97 * @param string $label Row label
98 * @param string $tag Row tag name
99 * @return \OOUI\Tag The resulting table row
101 private function getTableRow( $label, $tag ) {
102 $row = new \OOUI\
Tag( 'tr' );
103 $tooltip = $this->getTooltip( $label );
104 $labelFieldConfig = $tooltip ?
[ 'help' => $tooltip ] : [];
106 $labelField = new \OOUI\
FieldLayout(
107 new \OOUI\
Widget(), // Empty widget, since we don't have the checkboxes here
111 ] +
$labelFieldConfig
113 $row->appendContent( $this->getCellTag( $labelField ) );
115 // Build checkbox column cells
116 foreach ( $this->columns
as $columnTag ) {
117 $thisTag = "$columnTag-$tag";
119 // Construct a checkbox
120 $checkbox = new \OOUI\
CheckboxInputWidget( [
122 'name' => $this->name ?
"{$this->name}[]" : null,
123 'id' => $this->id ?
"{$this->id}-$thisTag" : null,
124 'selected' => $this->isTagChecked( $thisTag ),
125 'disabled' => $this->isTagDisabled( $thisTag ),
128 $row->appendContent( $this->getCellTag( $checkbox ) );
134 * Get an individual cell tag with requested content
136 * @param string $content Content for the <td> cell
137 * @return \OOUI\Tag Resulting cell
139 private function getCellTag( $content ) {
140 $cell = new \OOUI\
Tag( 'td' );
141 $cell->appendContent( $content );
146 * Check whether the given tag's checkbox should
149 * @param string $tagName Tag name
150 * @return boolean Tag should be checked
152 private function isTagChecked( $tagName ) {
153 // If the tag is in the value list
154 return in_array( $tagName, (array)$this->values
, true ) ||
155 // Or if the tag is forced on
156 in_array( $tagName, (array)$this->forcedOn
, true );
160 * Check whether the given tag's checkbox should
163 * @param string $tagName Tag name
164 * @return boolean Tag should be disabled
166 private function isTagDisabled( $tagName ) {
168 // If the entire widget is disabled
169 $this->isDisabled() ||
170 // If the tag is 'forced on' or 'forced off'
171 in_array( $tagName, (array)$this->forcedOn
, true ) ||
172 in_array( $tagName, (array)$this->forcedOff
, true )
177 * Get the tooltip help associated with this row
179 * @param string $label Label name
180 * @return string Tooltip. Null if none is available.
182 private function getTooltip( $label ) {
183 return isset( $this->tooltips
[ $label ] ) ?
184 $this->tooltips
[ $label ] : null;
187 protected function getJavaScriptClassName() {
188 return 'mw.widgets.CheckMatrixWidget';
191 public function getConfig( &$config ) {
193 'name' => $this->name
,
195 'rows' => $this->rows
,
196 'columns' => $this->columns
,
197 'tooltips' => $this->tooltips
,
198 'forcedOff' => $this->forcedOff
,
199 'forcedOn' => $this->forcedOn
,
200 'values' => $this->values
,
202 return parent
::getConfig( $config );