| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
mw.widgets.CheckMatrixWidget = function MWWCheckMatrixWidget( config ) { |
| 25 |
config = config || {}; |
| 26 |
|
| 27 |
|
| 28 |
mw.widgets.CheckMatrixWidget.super.call( this, config ); |
| 29 |
this.checkboxes = {}; |
| 30 |
this.name = config.name; |
| 31 |
this.id = config.id; |
| 32 |
this.rows = config.rows || {}; |
| 33 |
this.columns = config.columns || {}; |
| 34 |
this.tooltips = config.tooltips || []; |
| 35 |
this.tooltipsHtml = config.tooltipsHtml || []; |
| 36 |
this.values = config.values || []; |
| 37 |
this.forcedOn = config.forcedOn || []; |
| 38 |
this.forcedOff = config.forcedOff || []; |
| 39 |
|
| 40 |
|
| 41 |
const $headRow = $( '<tr>' ); |
| 42 |
$headRow.append( $( '<td>' ).text( '\u00A0' ) ); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
$.each( this.columns, ( columnLabel ) => { |
| 47 |
$headRow.append( $( '<th>' ).html( columnLabel ) ); |
| 48 |
} ); |
| 49 |
const $thead = $( '<thead>' ); |
| 50 |
$thead.append( $headRow ); |
| 51 |
|
| 52 |
const $tbody = $( '<tbody>' ); |
| 53 |
|
| 54 |
|
| 55 |
$.each( this.rows, ( rowLabel, rowTag ) => { |
| 56 |
const $row = $( '<tr>' ), |
| 57 |
labelField = new OO.ui.FieldLayout( |
| 58 |
new OO.ui.Widget(), |
| 59 |
{ |
| 60 |
label: new OO.ui.HtmlSnippet( rowLabel ), |
| 61 |
help: this.tooltips[ rowLabel ] || |
| 62 |
this.tooltipsHtml[ rowLabel ] && new OO.ui.HtmlSnippet( this.tooltipsHtml[ rowLabel ] ), |
| 63 |
align: 'inline' |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
|
| 68 |
$row.append( $( '<td>' ).append( labelField.$element ) ); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
$.each( this.columns, ( columnLabel, columnTag ) => { |
| 73 |
const thisTag = columnTag + '-' + rowTag, |
| 74 |
checkbox = new OO.ui.CheckboxInputWidget( { |
| 75 |
value: thisTag, |
| 76 |
name: this.name ? this.name + '[]' : undefined, |
| 77 |
id: this.id ? this.id + '-' + thisTag : undefined, |
| 78 |
selected: this.isTagSelected( thisTag ), |
| 79 |
disabled: this.isTagDisabled( thisTag ) |
| 80 |
} ); |
| 81 |
|
| 82 |
this.checkboxes[ thisTag ] = checkbox; |
| 83 |
$row.append( $( '<td>' ).append( checkbox.$element ) ); |
| 84 |
} ); |
| 85 |
|
| 86 |
$tbody.append( $row ); |
| 87 |
} ); |
| 88 |
const $table = $( '<table>' ); |
| 89 |
$table |
| 90 |
.addClass( 'mw-htmlform-matrix mw-widget-checkMatrixWidget-matrix' ) |
| 91 |
.append( $thead, $tbody ); |
| 92 |
|
| 93 |
this.$element |
| 94 |
.addClass( 'mw-widget-checkMatrixWidget' ) |
| 95 |
.append( $table ); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
OO.inheritClass( mw.widgets.CheckMatrixWidget, OO.ui.Widget ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
mw.widgets.CheckMatrixWidget.prototype.isTagSelected = function ( tagName ) { |
| 111 |
return ( |
| 112 |
|
| 113 |
this.forcedOff.indexOf( tagName ) === -1 && |
| 114 |
( |
| 115 |
|
| 116 |
this.values.indexOf( tagName ) > -1 || |
| 117 |
|
| 118 |
this.forcedOn.indexOf( tagName ) > -1 |
| 119 |
) |
| 120 |
); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
mw.widgets.CheckMatrixWidget.prototype.isTagDisabled = function ( tagName ) { |
| 130 |
return ( |
| 131 |
|
| 132 |
this.isDisabled() || |
| 133 |
|
| 134 |
this.forcedOff.indexOf( tagName ) > -1 || |
| 135 |
this.forcedOn.indexOf( tagName ) > -1 |
| 136 |
); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
mw.widgets.CheckMatrixWidget.prototype.setDisabled = function ( isDisabled ) { |
| 142 |
|
| 143 |
mw.widgets.CheckMatrixWidget.super.prototype.setDisabled.call( this, isDisabled ); |
| 144 |
|
| 145 |
|
| 146 |
if ( this.checkboxes ) { |
| 147 |
|
| 148 |
|
| 149 |
$.each( this.checkboxes, ( name, checkbox ) => { |
| 150 |
checkbox.setDisabled( this.isTagDisabled( name ) ); |
| 151 |
} ); |
| 152 |
} |
| 153 |
}; |
| 154 |
}() ); |