3 * A JavaScript version of CheckMatrixWidget.
6 * @extends OO.ui.Widget
9 * @param {Object} [config] Configuration options
10 * @cfg {Object} columns Required object representing the column labels and associated
12 * @cfg {Object} rows Required object representing the row labels and associated
14 * @cfg {string[]} [forcedOn] An array of column-row tags to be displayed as
15 * enabled but unavailable to change
16 * @cfg {string[]} [forcedOff] An array of column-row tags to be displayed as
17 * disnabled but unavailable to change
18 * @cfg {Object} Object mapping row label to tooltip content
20 mw
.widgets
.CheckMatrixWidget
= function MWWCheckMatrixWidget( config
) {
21 var $headRow
= $( '<tr>' ),
22 $table
= $( '<table>' ),
24 config
= config
|| {};
27 mw
.widgets
.CheckMatrixWidget
.parent
.call( this, config
);
29 this.name
= config
.name
;
31 this.rows
= config
.rows
|| {};
32 this.columns
= config
.columns
|| {};
33 this.tooltips
= config
.tooltips
|| [];
34 this.values
= config
.values
|| [];
35 this.forcedOn
= config
.forcedOn
|| [];
36 this.forcedOff
= config
.forcedOff
|| [];
39 $headRow
.append( $( '<td>' ).html( ' ' ) );
41 // Iterate over the columns object (ignore the value)
42 $.each( this.columns
, function ( columnLabel
) {
43 $headRow
.append( $( '<td>' ).text( columnLabel
) );
45 $table
.append( $headRow
);
48 $.each( this.rows
, function ( rowLabel
, rowTag
) {
49 var $row
= $( '<tr>' ),
50 labelField
= new OO
.ui
.FieldLayout(
51 new OO
.ui
.Widget(), // Empty widget, since we don't have the checkboxes here
54 help
: widget
.tooltips
[ rowLabel
],
60 $row
.append( $( '<td>' ).append( labelField
.$element
) );
63 $.each( widget
.columns
, function ( columnLabel
, columnTag
) {
64 var thisTag
= columnTag
+ '-' + rowTag
,
65 checkbox
= new OO
.ui
.CheckboxInputWidget( {
67 name
: widget
.name
? widget
.name
+ '[]' : undefined,
68 id
: widget
.id
? widget
.id
+ '-' + thisTag
: undefined,
69 selected
: widget
.isTagSelected( thisTag
),
70 disabled
: widget
.isTagDisabled( thisTag
)
73 widget
.checkboxes
[ thisTag
] = checkbox
;
74 $row
.append( $( '<td>' ).append( checkbox
.$element
) );
77 $table
.append( $row
);
81 .addClass( 'mw-widget-checkMatrixWidget' )
87 OO
.inheritClass( mw
.widgets
.CheckMatrixWidget
, OO
.ui
.Widget
);
92 * Check whether the given tag is selected
94 * @param {string} tagName Tag name
95 * @return {boolean} Tag is selected
97 mw
.widgets
.CheckMatrixWidget
.prototype.isTagSelected = function ( tagName
) {
99 // If tag is not forced off
100 this.forcedOff
.indexOf( tagName
) === -1 &&
102 // If tag is in values
103 this.values
.indexOf( tagName
) > -1 ||
104 // If tag is forced on
105 this.forcedOn
.indexOf( tagName
) > -1
111 * Check whether the given tag is disabled
113 * @param {string} tagName Tag name
114 * @return {boolean} Tag is disabled
116 mw
.widgets
.CheckMatrixWidget
.prototype.isTagDisabled = function ( tagName
) {
118 // If the entire widget is disabled
120 // If tag is forced off or forced on
121 this.forcedOff
.indexOf( tagName
) > -1 ||
122 this.forcedOn
.indexOf( tagName
) > -1
128 mw
.widgets
.CheckMatrixWidget
.prototype.setDisabled = function ( isDisabled
) {
132 mw
.widgets
.CheckMatrixWidget
.parent
.prototype.setDisabled
.call( this, isDisabled
);
134 // setDisabled sometimes gets called before the widget is ready
135 if ( this.checkboxes
&& Object
.keys( this.checkboxes
).length
> 0 ) {
136 // Propagate to all checkboxes and update their disabled state
137 $.each( this.checkboxes
, function ( name
, checkbox
) {
138 checkbox
.setDisabled( widget
.isTagDisabled( name
) );
142 }( jQuery
, mediaWiki
) );