26b2f5d7923e8a313a85ab862b6fca858f6aaf92
2 * MediaWiki Widgets – TitleInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
9 * Creates an mw.widgets.TitleInputWidget object.
12 * @extends OO.ui.TextInputWidget
13 * @mixins OO.ui.mixin.LookupElement
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [namespace] Namespace to prepend to queries
19 mw
.widgets
.TitleInputWidget
= function MWWTitleInputWidget( config
) {
20 // Config initialization
21 config
= config
|| {};
24 OO
.ui
.TextInputWidget
.call( this, config
);
27 OO
.ui
.mixin
.LookupElement
.call( this, config
);
30 this.namespace = config
.namespace || null;
33 this.$element
.addClass( 'mw-widget-TitleInputWidget' );
34 this.lookupMenu
.$element
.addClass( 'mw-widget-TitleInputWidget-menu' );
39 OO
.inheritClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.TextInputWidget
);
41 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.mixin
.LookupElement
);
48 mw
.widgets
.TitleInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
49 this.closeLookupMenu();
50 this.setLookupsDisabled( true );
51 this.setValue( item
.getData() );
52 this.setLookupsDisabled( false );
58 mw
.widgets
.TitleInputWidget
.prototype.getLookupRequest = function () {
59 var value
= this.value
;
61 // Prefix with default namespace name
62 if ( this.namespace !== null && mw
.Title
.newFromText( value
, this.namespace ) ) {
63 value
= mw
.Title
.newFromText( value
, this.namespace ).getPrefixedText();
66 // Dont send leading ':' to open search
67 if ( value
[0] === ':' ) {
68 value
= value
.slice( 1 );
71 return new mw
.Api().get( {
81 mw
.widgets
.TitleInputWidget
.prototype.getLookupCacheDataFromResponse = function ( data
) {
88 mw
.widgets
.TitleInputWidget
.prototype.getLookupMenuOptionsFromData = function ( data
) {
89 var i
, len
, title
, value
,
94 if ( matchingPages
&& matchingPages
.length
) {
95 for ( i
= 0, len
= matchingPages
.length
; i
< len
; i
++ ) {
96 title
= new mw
.Title( matchingPages
[i
] );
97 if ( this.namespace !== null ) {
98 value
= title
.getRelativeText( this.namespace );
100 value
= title
.getPrefixedText();
102 items
.push( new OO
.ui
.MenuOptionWidget( {
113 * Get title object corresponding to #getValue
115 * @returns {mw.Title|null} Title object, or null if value is invalid
117 mw
.widgets
.TitleInputWidget
.prototype.getTitle = function () {
118 var title
= this.getValue(),
119 // mw.Title doesn't handle null well
120 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
128 mw
.widgets
.TitleInputWidget
.prototype.isValid = function () {
129 return $.Deferred().resolve( !!this.getTitle() ).promise();
132 }( jQuery
, mediaWiki
) );