trait HTMLFormElement {
protected $hideIf = null;
+ protected $modules = null;
public function initializeHTMLFormElement( array $config = [] ) {
// Properties
$this->hideIf = isset( $config['hideIf'] ) ? $config['hideIf'] : null;
+ $this->modules = isset( $config['modules'] ) ? $config['modules'] : [];
// Initialization
if ( $this->hideIf ) {
$this->addClasses( [ 'mw-htmlform-hide-if' ] );
}
+ if ( $this->modules ) {
+ // JS code must be able to read this before infusing (before OOjs UI is even loaded),
+ // so we put this in a separate attribute (not with the rest of the config).
+ // And it's not needed anymore after infusing, so we don't put it in JS config at all.
+ $this->setAttributes( [ 'data-mw-modules' => implode( ',', $this->modules ) ] );
+ }
$this->registerConfigCallback( function( &$config ) {
if ( $this->hideIf !== null ) {
$config['hideIf'] = $this->hideIf;
'infusable' => $infusable,
];
+ $preloadModules = false;
+
if ( $infusable && $this->shouldInfuseOOUI() ) {
- $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
+ $preloadModules = true;
$config['classes'][] = 'mw-htmlform-field-autoinfuse';
}
}
if ( $this->mHideIf ) {
- $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
+ $preloadModules = true;
$config['hideIf'] = $this->mHideIf;
}
+ $config['modules'] = $this->getOOUIModules();
+
+ if ( $preloadModules ) {
+ $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
+ $this->mParent->getOutput()->addModules( $this->getOOUIModules() );
+ }
+
return $this->getFieldLayoutOOUI( $inputField, $config );
}
return $this->getHelpText() !== null;
}
+ /**
+ * Get the list of extra ResourceLoader modules which must be loaded client-side before it's
+ * possible to infuse this field's OOjs UI widget.
+ *
+ * @return string[]
+ */
+ protected function getOOUIModules() {
+ return [];
+ }
+
/**
* Get the complete raw fields for the input, including help text,
* labels, and whatever.
] );
}
+ protected function getOOUIModules() {
+ // FIXME: NamespaceInputWidget should be in its own module (probably?)
+ return [ 'mediawiki.widgets' ];
+ }
+
protected function shouldInfuseOOUI() {
return true;
}
}
protected function getInputWidget( $params ) {
- $this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
if ( $this->mParams['namespace'] !== false ) {
$params['namespace'] = $this->mParams['namespace'];
}
return true;
}
+ protected function getOOUIModules() {
+ // FIXME: TitleInputWidget should be in its own module
+ return [ 'mediawiki.widgets' ];
+ }
+
public function getInputHtml( $value ) {
// add mw-searchInput class to enable search suggestions for non-OOUI, too
$this->mClass .= 'mw-searchInput';
}
protected function getInputWidget( $params ) {
- $this->mParent->getOutput()->addModules( 'mediawiki.widgets.UserInputWidget' );
-
return new UserInputWidget( $params );
}
return true;
}
+ protected function getOOUIModules() {
+ return [ 'mediawiki.widgets.UserInputWidget' ];
+ }
+
public function getInputHtml( $value ) {
// add the required module and css class for user suggestions in non-OOUI mode
$this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
* HTMLForm enhancements:
* Infuse some OOjs UI HTMLForm fields (those which benefit from always being infused).
*/
-( function ( mw ) {
+( function ( mw, $ ) {
mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
- var $oouiNodes, modules;
+ var $oouiNodes, modules, extraModules;
$oouiNodes = $root.find( '.mw-htmlform-field-autoinfuse' );
if ( $oouiNodes.length ) {
// The modules are preloaded (added server-side in HTMLFormField, and the individual fields
// which need extra ones), but this module doesn't depend on them. Wait until they're loaded.
modules = [ 'mediawiki.htmlform.ooui' ];
- if ( $oouiNodes.filter( '.mw-htmlform-field-HTMLTitleTextField' ).length ) {
- // FIXME: TitleInputWidget should be in its own module
- modules.push( 'mediawiki.widgets' );
- }
- if ( $oouiNodes.filter( '.mw-htmlform-field-HTMLUserTextField' ).length ) {
- modules.push( 'mediawiki.widgets.UserInputWidget' );
- }
- if (
- $oouiNodes.filter( '.mw-htmlform-field-HTMLSelectNamespace' ).length ||
- $oouiNodes.filter( '.mw-htmlform-field-HTMLSelectNamespaceWithButton' ).length
- ) {
- // FIXME: NamespaceInputWidget should be in its own module (probably?)
- modules.push( 'mediawiki.widgets' );
- }
+ $oouiNodes.each( function () {
+ var data = $( this ).data( 'mw-modules' );
+ if ( data ) {
+ // We can trust this value, 'data-mw-*' attributes are banned from user content in Sanitizer
+ extraModules = data.split( ',' );
+ modules.push.apply( modules, extraModules );
+ }
+ } );
mw.loader.using( modules ).done( function () {
$oouiNodes.each( function () {
OO.ui.infuse( this );
} );
-}( mediaWiki ) );
+}( mediaWiki, jQuery ) );
mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
$root.find( '.mw-htmlform-hide-if' ).each( function () {
- var v, i, fields, test, func, spec, self, modules,
+ var v, i, fields, test, func, spec, self, modules, data,extraModules,
$el = $( this );
modules = [];
if ( $el.is( '[data-ooui]' ) ) {
modules.push( 'mediawiki.htmlform.ooui' );
- if ( $el.filter( '.mw-htmlform-field-HTMLTitleTextField' ).length ) {
- // FIXME: TitleInputWidget should be in its own module
- modules.push( 'mediawiki.widgets' );
- }
- if ( $el.filter( '.mw-htmlform-field-HTMLUserTextField' ).length ) {
- modules.push( 'mediawiki.widgets.UserInputWidget' );
- }
- if (
- $el.filter( '.mw-htmlform-field-HTMLSelectNamespace' ).length ||
- $el.filter( '.mw-htmlform-field-HTMLSelectNamespaceWithButton' ).length
- ) {
- // FIXME: NamespaceInputWidget should be in its own module (probably?)
- modules.push( 'mediawiki.widgets' );
+ data = $el.data( 'mw-modules' );
+ if ( data ) {
+ // We can trust this value, 'data-mw-*' attributes are banned from user content in Sanitizer
+ extraModules = data.split( ',' );
+ modules.push.apply( modules, extraModules );
}
}