2 * MediaWiki Widgets - MenuTagMultiselectWidget class.
4 * @copyright 2024 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
8 mw.widgets.MenuTagMultiselectWidget = function MwWidgetsMenuTagMultiselectWidget( config ) {
9 const menuTagOptions = [], flatOptions = [], selected = config.selected;
11 // MenuTagMultiselectWidget does not support the optgroup config like DropdownInputWidget
12 Object.keys( config.options ).forEach( ( optionGroup ) => {
14 menuTagOptions.push( new OO.ui.MenuSectionOptionWidget( {
18 config.options[ optionGroup ].forEach( ( option ) => {
19 flatOptions.push( option );
20 menuTagOptions.push( new OO.ui.MenuOptionWidget( option ) );
24 // Parent constructor use these config in a way that is not compatible
29 mw.widgets.MenuTagMultiselectWidget.super.call( this, Object.assign( {}, config, {
36 OO.ui.mixin.PendingElement.call( this, Object.assign( {}, config, { $pending: this.$handle } ) );
38 this.setValue( selected );
40 if ( 'name' in config ) {
41 this.$hiddenInputs = {};
42 const $container = $( '<div>' ).addClass( 'oo-ui-element-hidden' );
43 flatOptions.forEach( ( option ) => {
44 // Use this instead of <input type="hidden">, because hidden inputs do not have separate
45 // 'value' and 'defaultChecked' properties.
46 this.$hiddenInputs[ option.data ] = $( '<input>' )
47 .attr( 'type', 'checkbox' )
48 .attr( 'value', option.data )
49 .attr( 'name', config.name + '[]' )
50 .prop( 'defaultChecked', selected.indexOf( option.data ) >= 0 )
51 .appendTo( $container );
53 $container.appendTo( this.$element );
62 OO.inheritClass( mw.widgets.MenuTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
63 OO.mixinClass( mw.widgets.MenuTagMultiselectWidget, OO.ui.mixin.PendingElement );
65 mw.widgets.MenuTagMultiselectWidget.prototype.onChangeTags = function ( items ) {
66 if ( '$hiddenInputs' in this ) {
67 const values = this.getValue();
68 Object.keys( this.$hiddenInputs ).forEach( ( name ) => {
69 const $input = this.$hiddenInputs[ name ];
70 $input.prop( 'checked', values.indexOf( $input.attr( 'value' ) ) >= 0 );
74 mw.widgets.MenuTagMultiselectWidget.super.prototype.onChangeTags.call( this, items );
77 mw.widgets.MenuTagMultiselectWidget.prototype.onDisable = function ( disabled ) {
78 if ( '$hiddenInputs' in this ) {
79 Object.keys( this.$hiddenInputs ).forEach( ( name ) => {
80 this.$hiddenInputs[ name ].prop( 'disabled', disabled );