Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleSearchWidget.js
blob5ba9481a0ccf684081a3b19de78c9a19db026e38
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 OO.ui.mixin.RequestManager
15          * @mixins mw.widgets.TitleWidget
16          *
17          * @constructor
18          * @param {Object} [config] Configuration options
19          */
20         mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
21                 config = config || {};
23                 // Parent constructor
24                 mw.widgets.TitleSearchWidget.parent.call( this, config );
26                 // Mixin constructors
27                 mw.widgets.TitleWidget.call( this, config );
28                 OO.ui.mixin.RequestManager.call( this, config );
30                 this.query.setValidation( this.isQueryValid.bind( this ) );
32                 // Events
33                 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
35                 // Initialization
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' );
40                 }
41                 if ( this.showDescriptions ) {
42                         this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
43                 }
44                 if ( this.maxLength !== undefined ) {
45                         this.getQuery().$input.attr( 'maxlength', this.maxLength );
46                 }
47         };
49         /* Setup */
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 );
55         /* Methods */
57         /**
58          * @inheritdoc mw.widgets.TitleWidget
59          */
60         mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
61                 return this.getQuery().getValue();
62         };
64         /**
65          * Handle choose events from the result widget
66          *
67          * @param {OO.ui.OptionWidget} item Chosen item
68          */
69         mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) {
70                 this.getQuery().setValue( item.getData() );
71         };
73         /**
74          * @inheritdoc
75          */
76         mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
77                 var widget = this;
79                 this.getRequestData().done( function ( data ) {
80                         // Parent method
81                         mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
82                         widget.results.addItems( widget.getOptionsFromData( data ) );
83                 } );
84         };
86         /**
87          * @inheritdoc OO.ui.mixin.RequestManager
88          */
89         mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () {
90                 return this.getQueryValue();
91         };
92         /**
93          * @inheritdoc OO.ui.mixin.RequestManager
94          */
95         mw.widgets.TitleSearchWidget.prototype.getRequest = function () {
96                 return this.getSuggestionsPromise();
97         };
98         /**
99          * @inheritdoc OO.ui.mixin.RequestManager
100          */
101         mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) {
102                 return response.query || {};
103         };
105 }( jQuery, mediaWiki ) );