Merge "selenium: invoke jobs to enforce eventual consistency"
[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 () {
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 * @param {Object} [config] Configuration options
19 */
20 mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
21 config = config || {};
22
23 // Parent constructor
24 mw.widgets.TitleSearchWidget.parent.call( this, config );
25
26 // Mixin constructors
27 mw.widgets.TitleWidget.call( this, config );
28 OO.ui.mixin.RequestManager.call( this, config );
29
30 this.query.setValidation( this.isQueryValid.bind( this ) );
31
32 // Events
33 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
34
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 };
48
49 /* Setup */
50
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 );
54
55 /* Methods */
56
57 /**
58 * @inheritdoc mw.widgets.TitleWidget
59 */
60 mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
61 return this.getQuery().getValue();
62 };
63
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 };
72
73 /**
74 * @inheritdoc
75 */
76 mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
77 var widget = this;
78
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 };
85
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 };
104
105 }() );