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 var trimByteLength
= require( 'mediawiki.String' ).trimByteLength
;
12 * Creates an mw.widgets.TitleInputWidget object.
15 * @extends OO.ui.TextInputWidget
16 * @mixins mw.widgets.TitleWidget
17 * @mixins OO.ui.mixin.LookupElement
20 * @param {Object} [config] Configuration options
21 * @cfg {boolean} [suggestions=true] Display search suggestions
22 * @cfg {RegExp|Function|string} [validate] Perform title validation
24 mw
.widgets
.TitleInputWidget
= function MwWidgetsTitleInputWidget( config
) {
25 config
= config
|| {};
28 mw
.widgets
.TitleInputWidget
.parent
.call( this, $.extend( {}, config
, {
29 validate
: config
.validate
!== undefined ? config
.validate
: this.isQueryValid
.bind( this ),
34 mw
.widgets
.TitleWidget
.call( this, config
);
35 OO
.ui
.mixin
.LookupElement
.call( this, config
);
38 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
41 this.$element
.addClass( 'mw-widget-titleInputWidget' );
42 this.lookupMenu
.$element
.addClass( 'mw-widget-titleWidget-menu' );
43 if ( this.showImages
) {
44 this.lookupMenu
.$element
.addClass( 'mw-widget-titleWidget-menu-withImages' );
46 if ( this.showDescriptions
) {
47 this.lookupMenu
.$element
.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
49 this.setLookupsDisabled( !this.suggestions
);
54 OO
.inheritClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.TextInputWidget
);
55 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, mw
.widgets
.TitleWidget
);
56 OO
.mixinClass( mw
.widgets
.TitleInputWidget
, OO
.ui
.mixin
.LookupElement
);
61 * @inheritdoc mw.widgets.TitleWidget
63 mw
.widgets
.TitleInputWidget
.prototype.getQueryValue = function () {
64 return this.getValue();
68 * @inheritdoc mw.widgets.TitleWidget
70 mw
.widgets
.TitleInputWidget
.prototype.setNamespace = function ( namespace ) {
72 mw
.widgets
.TitleWidget
.prototype.setNamespace
.call( this, namespace );
74 this.lookupCache
= {};
75 this.closeLookupMenu();
81 mw
.widgets
.TitleInputWidget
.prototype.getLookupRequest = function () {
82 return this.getSuggestionsPromise();
86 * @inheritdoc OO.ui.mixin.LookupElement
88 mw
.widgets
.TitleInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
89 return response
.query
|| {};
93 * @inheritdoc OO.ui.mixin.LookupElement
95 mw
.widgets
.TitleInputWidget
.prototype.getLookupMenuOptionsFromData = function ( response
) {
96 return this.getOptionsFromData( response
);
102 mw
.widgets
.TitleInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
103 this.closeLookupMenu();
104 this.setLookupsDisabled( true );
105 this.setValue( item
.getData() );
106 this.setLookupsDisabled( !this.suggestions
);
112 mw
.widgets
.TitleInputWidget
.prototype.focus = function () {
115 // Prevent programmatic focus from opening the menu
116 this.setLookupsDisabled( true );
119 retval
= mw
.widgets
.TitleInputWidget
.parent
.prototype.focus
.apply( this, arguments
);
121 this.setLookupsDisabled( !this.suggestions
);
129 mw
.widgets
.TitleInputWidget
.prototype.cleanUpValue = function ( value
) {
133 value
= mw
.widgets
.TitleInputWidget
.parent
.prototype.cleanUpValue
.call( this, value
);
135 return trimByteLength( this.value
, value
, this.maxLength
, function ( value
) {
136 var title
= widget
.getMWTitle( value
);
137 return title
? title
.getMain() : value
;