Jump to content

Manual:NamespaceInputWidget

From mediawiki.org
NamespaceInputWidget Example

NamespaceInputWidget is a Namespace input widget which is used to select a wiki namespace. It is based on OOUI's DropdownInputWidget. but provide wiki namespace as options.

Javascript Examples

[edit]
In MediaWiki Extension

Using the widget in MediaWiki Extension will be done through the 3 steps. First create the resource javascript file, Second register the js file in extension.json file. So that the module can load by ResourceLoader. And finally load it by Output Context.

  • Create the resource.js file in extension directory with following code.
var config = {};
var namespaceInput = new mw.widgets.NamespaceInputWidget( config );

$( '#bodyContent' ).append( namespaceInput.$element );
  • Register the js file as module in extension.json
"ResourceModules": {
    "ext.myExtResource": {
        "scripts": [
            "resource.js"
        ],
        "dependencies": [
            "mediawiki.widgets"
        ]
    }
}
  • Finally load it by Output Context in your SpecialPage/ParserHook.
......
// Get Output Context
$out = $this->getOutput();
$out->addModules( 'ext.myExtResource' );
......
In UserScript

To use the widget in UserScript is very easy, You just need to load the dependencie by mw.loader.using(). and put the code into the block. Try the below code in your Special:MyPage/common.js

mw.loader.using('mediawiki.widgets').then( function (){

	var config = {};
	var namespaceInput = new mw.widgets.NamespaceInputWidget( config );

	$( '#bodyContent' ).append( namespaceInput.$element );

});
In Gadget

To use in gadgets, you have to add a mediawiki.widgets entry in the dependencies field of gadget description. See Gadgets' documentation for instructions and examples.

* mygadget[ResourceLoader|dependencies=mediawiki.widgets]|mygadget.js

  • Create MediaWiki:Gadget-mygadget.js page with code. You does not need to use mw.loader.using() as we did in UserScript.
var config = {};
var namespaceInput = new mw.widgets.NamespaceInputWidget( config );

$( '#bodyContent' ).append( namespaceInput.$element );

PHP

[edit]

To use NamespaceInputWidget in MediaWiki Extension's Special Page. You need to decleare the use of MediaWiki\Widget\NamespaceInputWidget. And have to enable OOUI in SpecialPage.

<?php

use MediaWiki\Widget\NamespaceInputWidget;

class SpecialExample extends SpecialPage {
    
    function __construct(){
        parent::__construct("Example");
    }

    function execute($par){
        // Get Output Context
        $out = $this->getOutput();

        // Enable the OOUI
        $out->enableOOUI();

        // Create the NamespaceInput Widget 
        $widget = new NamespaceInputWidget( [
			'value' => '',
			'name' => 'namespaceInputWidget',
			'id' => 'mw-namespace-widget',
        ] );
        
        // Add the Widget in Special Page Display
        $out->addHTML( $widget );

    }
}