Merge "Give instructions on removing email address from account"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleSearchWidget.js
blobc37c723b4d794eecf9f757b6d04db12ce6601126
1 /*!
2  * MediaWiki Widgets - TitleSearchWidget class.
3  *
4  * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5  * @license The MIT License (MIT); see LICENSE.txt
6  */
7 ( function ( $, mw ) {
9         /**
10          * Creates an mw.widgets.TitleSearchWidget object.
11          *
12          * @class
13          * @extends OO.ui.SearchWidget
14          * @mixins mw.widgets.TitleWidget
15          *
16          * @constructor
17          */
18         mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
19                 config = config || {};
21                 // Parent constructor
22                 mw.widgets.TitleSearchWidget.parent.call( this, config );
24                 // Mixin constructors
25                 mw.widgets.TitleWidget.call( this, config );
27                 this.query.setValidation( this.isQueryValid.bind( this ) );
29                 // Events
30                 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
32                 // Initialization
33                 this.$element.addClass( 'mw-widget-titleSearchWidget' );
34                 this.results.$element.addClass( 'mw-widget-titleWidget-menu' );
35                 if ( this.showImages ) {
36                         this.results.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
37                 }
38                 if ( this.showDescriptions ) {
39                         this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
40                 }
41                 if ( this.maxLength !== undefined ) {
42                         this.getQuery().$input.attr( 'maxlength', this.maxLength );
43                 }
44         };
46         /* Setup */
48         OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
49         OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );
51         /* Methods */
53         /**
54          * @inheritdoc mw.widgets.TitleWidget
55          */
56         mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
57                 return this.getQuery().getValue();
58         };
60         /**
61          * Handle choose events from the result widget
62          *
63          * @param {OO.ui.OptionWidget} item Chosen item
64          */
65         mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) {
66                 // TOOD: Pressing enter can incorrectly trigger 'choose' with null.
67                 // Remove this check when oojs-ui 0.12.10 lands.
68                 if ( item ) {
69                         this.getQuery().setValue( item.getData() );
70                 }
71         };
73         /**
74          * @inheritdoc
75          */
76         mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
77                 var widget = this;
79                 this.getSuggestionsPromise().done( function ( response ) {
80                         // Parent method
81                         mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
83                         widget.results.addItems( widget.getOptionsFromData( response.query || {} ) );
84                 } );
85         };
87 }( jQuery, mediaWiki ) );