TitleInputWidget: Add 'relative' option

Currently the JavaScript widget will return a value that is relative to
the namespace, so for "Category:Foo", it will return "Foo". This is
problematic for server-side forms that want a full title returned, so
make this configurable.

Change-Id: I605df2ca41831cae1c8f0a3331600d4487e7798f
This commit is contained in:
Kunal Mehta 2015-07-04 00:39:41 -07:00
parent 0407248483
commit ddca1c657e
2 changed files with 16 additions and 4 deletions

View file

@ -15,21 +15,26 @@ use OOUI\TextInputWidget;
class TitleInputWidget extends TextInputWidget {
protected $namespace = null;
protected $relative = null;
/**
* @param array $config Configuration options
* @param number|null $config['namespace'] Namespace to prepend to queries
* @param int|null $config['namespace'] Namespace to prepend to queries
* @param bool|null $config['relative'] If a namespace is set, return a title relative to it (default; true)
*/
public function __construct( array $config = array() ) {
// Parent constructor
parent::__construct( array_merge( $config, array( 'infusable' => true ) ) );
// Properties
// Properties, which are ignored in PHP and just shipped back to JS
if ( isset( $config['namespace'] ) ) {
// Actually ignored in PHP, we just ship it back to JS
$this->namespace = $config['namespace'];
}
if ( isset( $config['relative'] ) ) {
$this->relative = $config['relative'];
}
// Initialization
$this->addClasses( array( 'mw-widget-TitleInputWidget' ) );
}
@ -38,6 +43,9 @@ class TitleInputWidget extends TextInputWidget {
if ( $this->namespace !== null ) {
$config['namespace'] = $this->namespace;
}
if ( $this->relative !== null ) {
$config['relative'] = $this->relative;
}
return parent::getConfig( $config );
}
}

View file

@ -16,6 +16,7 @@
* @param {Object} [config] Configuration options
* @cfg {number} [limit=10] Number of results to show
* @cfg {number} [namespace] Namespace to prepend to queries
* @cfg {boolean} [relative=true] If a namespace is set, return a title relative to it
* @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
* @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
* @cfg {boolean} [showImages] Show page images
@ -37,6 +38,7 @@
// Properties
this.limit = config.limit || 10;
this.namespace = config.namespace || null;
this.relative = config.relative !== undefined ? config.relative : true;
this.showRedirectTargets = config.showRedirectTargets !== false;
this.showRedlink = !!config.showRedlink;
this.showImages = !!config.showImages;
@ -258,7 +260,9 @@
mw.widgets.TitleInputWidget.prototype.getOptionWidgetData = function ( title, data ) {
var mwTitle = new mw.Title( title );
return {
data: this.namespace !== null ? mwTitle.getRelativeText( this.namespace ) : title,
data: this.namespace !== null && this.relative
? mwTitle.getRelativeText( this.namespace )
: title,
imageUrl: this.showImages ? data.imageUrl : null,
description: this.showDescriptions ? data.description : null,
missing: data.missing,