Merge "Add support for blacklisting common passwords"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.TitleSearchWidget.js
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 ) {
8
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 || {};
21
22 // Parent constructor
23 mw.widgets.TitleSearchWidget.parent.call( this, config );
24
25 // Mixin constructors
26 mw.widgets.TitleWidget.call( this, config );
27 OO.ui.mixin.RequestManager.call( this, config );
28
29 this.query.setValidation( this.isQueryValid.bind( this ) );
30
31 // Events
32 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
33
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 };
47
48 /* Setup */
49
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 );
53
54 /* Methods */
55
56 /**
57 * @inheritdoc mw.widgets.TitleWidget
58 */
59 mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
60 return this.getQuery().getValue();
61 };
62
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 };
71
72 /**
73 * @inheritdoc
74 */
75 mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
76 var widget = this;
77
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 };
84
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 };
103
104 }( jQuery, mediaWiki ) );