Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleSearchWidget.js
blob96f95491ebca227484507c9a93950dc1b52fe492
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          */
19         mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
20                 config = config || {};
22                 // Parent constructor
23                 mw.widgets.TitleSearchWidget.parent.call( this, config );
25                 // Mixin constructors
26                 mw.widgets.TitleWidget.call( this, config );
27                 OO.ui.mixin.RequestManager.call( this, config );
29                 this.query.setValidation( this.isQueryValid.bind( this ) );
31                 // Events
32                 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
34                 // Initialization
35                 this.$element.addClass( 'mw-widget-titleSearchWidget' );
36                 this.results.$element.addClass( 'mw-widget-titleWidget-menu' );
37                 if ( this.showImages ) {
38                         this.results.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
39                 }
40                 if ( this.showDescriptions ) {
41                         this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
42                 }
43                 if ( this.maxLength !== undefined ) {
44                         this.getQuery().$input.attr( 'maxlength', this.maxLength );
45                 }
46         };
48         /* Setup */
50         OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
51         OO.mixinClass( mw.widgets.TitleSearchWidget, OO.ui.mixin.RequestManager );
52         OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );
54         /* Methods */
56         /**
57          * @inheritdoc mw.widgets.TitleWidget
58          */
59         mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
60                 return this.getQuery().getValue();
61         };
63         /**
64          * Handle choose events from the result widget
65          *
66          * @param {OO.ui.OptionWidget} item Chosen item
67          */
68         mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) {
69                 this.getQuery().setValue( item.getData() );
70         };
72         /**
73          * @inheritdoc
74          */
75         mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
76                 var widget = this;
78                 this.getRequestData().done( function ( data ) {
79                         // Parent method
80                         mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
81                         widget.results.addItems( widget.getOptionsFromData( data ) );
82                 } );
83         };
85         /**
86          * @inheritdoc OO.ui.mixin.RequestManager
87          */
88         mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () {
89                 return this.getQueryValue();
90         };
91         /**
92          * @inheritdoc OO.ui.mixin.RequestManager
93          */
94         mw.widgets.TitleSearchWidget.prototype.getRequest = function () {
95                 return this.getSuggestionsPromise();
96         };
97         /**
98          * @inheritdoc OO.ui.mixin.RequestManager
99          */
100         mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) {
101                 return response.query || {};
102         };
104 }( jQuery, mediaWiki ) );