64b75927d49406d99b8949f6868de5356393bcb9
3 * A JavaScript version of CheckMatrixWidget.
6 * @extends OO.ui.Widget
9 * @param {Object} [config] Configuration options
10 * @cfg {Object} columns Required object mapping column labels (as HTML) to
12 * @cfg {Object} rows Required object mapping row labels (as HTML) to their
14 * @cfg {string[]} [forcedOn] Array of column-row tags to be displayed as
15 * enabled but unavailable to change.
16 * @cfg {string[]} [forcedOff] Array of column-row tags to be displayed as
17 * disabled but unavailable to change.
18 * @cfg {Object} [tooltips] Optional object mapping row labels to tooltips
19 * (as text, will be escaped).
21 mw
.widgets
.CheckMatrixWidget
= function MWWCheckMatrixWidget( config
) {
22 var $headRow
= $( '<tr>' ),
23 $table
= $( '<table>' ),
25 config
= config
|| {};
28 mw
.widgets
.CheckMatrixWidget
.parent
.call( this, config
);
30 this.name
= config
.name
;
32 this.rows
= config
.rows
|| {};
33 this.columns
= config
.columns
|| {};
34 this.tooltips
= config
.tooltips
|| [];
35 this.values
= config
.values
|| [];
36 this.forcedOn
= config
.forcedOn
|| [];
37 this.forcedOff
= config
.forcedOff
|| [];
40 $headRow
.append( $( '<td>' ).text( '\u00A0' ) );
42 // Iterate over the columns object (ignore the value)
43 // eslint-disable-next-line no-restricted-properties
44 $.each( this.columns
, function ( columnLabel
) {
45 $headRow
.append( $( '<td>' ).html( columnLabel
) );
47 $table
.append( $headRow
);
50 // eslint-disable-next-line no-restricted-properties
51 $.each( this.rows
, function ( rowLabel
, rowTag
) {
52 var $row
= $( '<tr>' ),
53 labelField
= new OO
.ui
.FieldLayout(
54 new OO
.ui
.Widget(), // Empty widget, since we don't have the checkboxes here
56 label
: new OO
.ui
.HtmlSnippet( rowLabel
),
57 help
: widget
.tooltips
[ rowLabel
],
63 $row
.append( $( '<td>' ).append( labelField
.$element
) );
66 // eslint-disable-next-line no-restricted-properties
67 $.each( widget
.columns
, function ( columnLabel
, columnTag
) {
68 var thisTag
= columnTag
+ '-' + rowTag
,
69 checkbox
= new OO
.ui
.CheckboxInputWidget( {
71 name
: widget
.name
? widget
.name
+ '[]' : undefined,
72 id
: widget
.id
? widget
.id
+ '-' + thisTag
: undefined,
73 selected
: widget
.isTagSelected( thisTag
),
74 disabled
: widget
.isTagDisabled( thisTag
)
77 widget
.checkboxes
[ thisTag
] = checkbox
;
78 $row
.append( $( '<td>' ).append( checkbox
.$element
) );
81 $table
.append( $row
);
85 .addClass( 'mw-widget-checkMatrixWidget' )
91 OO
.inheritClass( mw
.widgets
.CheckMatrixWidget
, OO
.ui
.Widget
);
96 * Check whether the given tag is selected
98 * @param {string} tagName Tag name
99 * @return {boolean} Tag is selected
101 mw
.widgets
.CheckMatrixWidget
.prototype.isTagSelected = function ( tagName
) {
103 // If tag is not forced off
104 this.forcedOff
.indexOf( tagName
) === -1 &&
106 // If tag is in values
107 this.values
.indexOf( tagName
) > -1 ||
108 // If tag is forced on
109 this.forcedOn
.indexOf( tagName
) > -1
115 * Check whether the given tag is disabled
117 * @param {string} tagName Tag name
118 * @return {boolean} Tag is disabled
120 mw
.widgets
.CheckMatrixWidget
.prototype.isTagDisabled = function ( tagName
) {
122 // If the entire widget is disabled
124 // If tag is forced off or forced on
125 this.forcedOff
.indexOf( tagName
) > -1 ||
126 this.forcedOn
.indexOf( tagName
) > -1
132 mw
.widgets
.CheckMatrixWidget
.prototype.setDisabled = function ( isDisabled
) {
136 mw
.widgets
.CheckMatrixWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
138 // setDisabled sometimes gets called before the widget is ready
139 if ( this.checkboxes
&& Object
.keys( this.checkboxes
).length
> 0 ) {
140 // Propagate to all checkboxes and update their disabled state
141 // eslint-disable-next-line no-restricted-properties
142 $.each( this.checkboxes
, function ( name
, checkbox
) {
143 checkbox
.setDisabled( widget
.isTagDisabled( name
) );
147 }( jQuery
, mediaWiki
) );