diff --git a/extension.json b/extension.json index a092be2..2337e8d 100644 --- a/extension.json +++ b/extension.json @@ -132,6 +132,7 @@ }, "ve.ext.isekai.misc": { "scripts": [ + "ve.ui.MWParameterDropdownInputWidget.js", "ve.ext.isekai.misc.js" ], "targets": [ diff --git a/modules/ve.ui.MWParameterDropdownInputWidget.js b/modules/ve.ui.MWParameterDropdownInputWidget.js new file mode 100644 index 0000000..ccffa6d --- /dev/null +++ b/modules/ve.ui.MWParameterDropdownInputWidget.js @@ -0,0 +1,48 @@ +/*! + * VisualEditor UserInterface MWParameterDropdownInputWidget class. + * + * @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt + * @license The MIT License (MIT); see LICENSE.txt + */ + +/** + * Creates an ve.ui.MWParameterDropdownInputWidget object. + * + * @class + * @extends OO.ui.DropdownWidget + * + * @constructor + */ +ve.ui.MWParameterDropdownInputWidget = function VeUiMWParameterDropdownInputWidget( config, defaultValue ) { + console.log('MWParameterDropdownInputWidget defaultValue', defaultValue); + if ( typeof config !== "undefined" ) { + config.options = parseOptions( defaultValue ); + } + console.log('MWParameterDropdownInputWidget params', config.options); + // Parent constructor + ve.ui.MWParameterDropdownInputWidget.parent.apply( this, [ config ] ); +}; + +/* Inheritance */ + +OO.inheritClass( ve.ui.MWParameterDropdownInputWidget, OO.ui.DropdownInputWidget ); + +/* Methods */ +function parseOptions ( optionsText ) { + var optionLine = optionsText.split('\n'); + var options = []; + optionLine.forEach( ( optionText ) => { + if ( optionText.trim().length > 0 ) { + var data = optionText.split( ':' ); + if ( data.length >= 2 ) { + var optionValue = data[ 0 ].trim(); + var optionLabel = data.slice( 1 ).join( ':' ).trim(); + options.push( { + data: optionValue, + label: optionLabel + } ); + } + } + } ); + return options; +}