Merge "Move the "shit" out"
[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 mw.widgets.TitleWidget
15 *
16 * @constructor
17 */
18 mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
19 config = config || {};
20
21 // Parent constructor
22 mw.widgets.TitleSearchWidget.parent.call( this, config );
23
24 // Mixin constructors
25 mw.widgets.TitleWidget.call( this, config );
26
27 this.query.setValidation( this.isQueryValid.bind( this ) );
28
29 // Events
30 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
31
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 };
45
46 /* Setup */
47
48 OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
49 OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );
50
51 /* Methods */
52
53 /**
54 * @inheritdoc mw.widgets.TitleWidget
55 */
56 mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
57 return this.getQuery().getValue();
58 };
59
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 this.getQuery().setValue( item.getData() );
67 };
68
69 /**
70 * @inheritdoc
71 */
72 mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
73 var widget = this;
74
75 this.getSuggestionsPromise().done( function ( response ) {
76 // Parent method
77 mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
78
79 widget.results.addItems( widget.getOptionsFromData( response.query || {} ) );
80 } );
81 };
82
83 }( jQuery, mediaWiki ) );