2 * MediaWiki Widgets - TitleSearchWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Creates an mw.widgets.TitleSearchWidget object.
13 * @extends OO.ui.SearchWidget
14 * @mixins OO.ui.mixin.RequestManager
15 * @mixins mw.widgets.TitleWidget
18 * @param {Object} [config] Configuration options
20 mw
.widgets
.TitleSearchWidget
= function MwWidgetsTitleSearchWidget( config
) {
21 config
= config
|| {};
24 mw
.widgets
.TitleSearchWidget
.parent
.call( this, config
);
27 mw
.widgets
.TitleWidget
.call( this, config
);
28 OO
.ui
.mixin
.RequestManager
.call( this, config
);
30 this.query
.setValidation( this.isQueryValid
.bind( this ) );
33 this.results
.connect( this, { choose
: 'onTitleSearchResultsChoose' } );
36 this.$element
.addClass( 'mw-widget-titleSearchWidget' );
37 this.results
.$element
.addClass( 'mw-widget-titleWidget-menu' );
38 if ( this.showImages
) {
39 this.results
.$element
.addClass( 'mw-widget-titleWidget-menu-withImages' );
41 if ( this.showDescriptions
) {
42 this.results
.$element
.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
44 if ( this.maxLength
!== undefined ) {
45 this.getQuery().$input
.attr( 'maxlength', this.maxLength
);
51 OO
.inheritClass( mw
.widgets
.TitleSearchWidget
, OO
.ui
.SearchWidget
);
52 OO
.mixinClass( mw
.widgets
.TitleSearchWidget
, OO
.ui
.mixin
.RequestManager
);
53 OO
.mixinClass( mw
.widgets
.TitleSearchWidget
, mw
.widgets
.TitleWidget
);
58 * @inheritdoc mw.widgets.TitleWidget
60 mw
.widgets
.TitleSearchWidget
.prototype.getQueryValue = function () {
61 return this.getQuery().getValue();
65 * Handle choose events from the result widget
67 * @param {OO.ui.OptionWidget} item Chosen item
69 mw
.widgets
.TitleSearchWidget
.prototype.onTitleSearchResultsChoose = function ( item
) {
70 this.getQuery().setValue( item
.getData() );
76 mw
.widgets
.TitleSearchWidget
.prototype.onQueryChange = function () {
79 this.getRequestData().done( function ( data
) {
80 if ( widget
.query
.isReadOnly() ) {
81 // The request object is always abortable, so just
82 // prevent the results from displaying
86 mw
.widgets
.TitleSearchWidget
.parent
.prototype.onQueryChange
.call( widget
);
87 widget
.results
.addItems( widget
.getOptionsFromData( data
) );
92 * @inheritdoc OO.ui.mixin.RequestManager
94 mw
.widgets
.TitleSearchWidget
.prototype.getRequestQuery = function () {
95 return this.getQueryValue();
98 * @inheritdoc OO.ui.mixin.RequestManager
100 mw
.widgets
.TitleSearchWidget
.prototype.getRequest = function () {
101 return this.getSuggestionsPromise();
104 * @inheritdoc OO.ui.mixin.RequestManager
106 mw
.widgets
.TitleSearchWidget
.prototype.getRequestCacheDataFromResponse = function ( response
) {
107 return response
.query
|| {};
111 * Check if the widget is read-only.
115 mw
.widgets
.TitleSearchWidget
.prototype.isReadOnly = function () {
116 return this.query
.isReadOnly();
120 * Set the read-only state of the widget.
122 * @param {boolean} readOnly Make input read-only
124 * @return {mw.widgets.TitleSearchWidget} The widget, for chaining
126 mw
.widgets
.TitleSearchWidget
.prototype.setReadOnly = function ( readOnly
) {
127 this.query
.setReadOnly( readOnly
);
130 this.results
.clearItems();