summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js
blob: cd937f79fab79628114dd231aeeeb1f2d82cafa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*!
 * MediaWiki Widgets - TitleSearchWidget class.
 *
 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */
( function ( mw ) {

	/**
	 * Creates an mw.widgets.TitleSearchWidget object.
	 *
	 * @class
	 * @extends OO.ui.SearchWidget
	 * @mixins OO.ui.mixin.RequestManager
	 * @mixins mw.widgets.TitleWidget
	 *
	 * @constructor
	 * @param {Object} [config] Configuration options
	 */
	mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
		config = config || {};

		// Parent constructor
		mw.widgets.TitleSearchWidget.parent.call( this, config );

		// Mixin constructors
		mw.widgets.TitleWidget.call( this, config );
		OO.ui.mixin.RequestManager.call( this, config );

		this.query.setValidation( this.isQueryValid.bind( this ) );

		// Events
		this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );

		// Initialization
		this.$element.addClass( 'mw-widget-titleSearchWidget' );
		this.results.$element.addClass( 'mw-widget-titleWidget-menu' );
		if ( this.showImages ) {
			this.results.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
		}
		if ( this.showDescriptions ) {
			this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
		}
		if ( this.maxLength !== undefined ) {
			this.getQuery().$input.attr( 'maxlength', this.maxLength );
		}
	};

	/* Setup */

	OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
	OO.mixinClass( mw.widgets.TitleSearchWidget, OO.ui.mixin.RequestManager );
	OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );

	/* Methods */

	/**
	 * @inheritdoc mw.widgets.TitleWidget
	 */
	mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
		return this.getQuery().getValue();
	};

	/**
	 * Handle choose events from the result widget
	 *
	 * @param {OO.ui.OptionWidget} item Chosen item
	 */
	mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) {
		this.getQuery().setValue( item.getData() );
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
		var widget = this;

		this.getRequestData().done( function ( data ) {
			// Parent method
			mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
			widget.results.addItems( widget.getOptionsFromData( data ) );
		} );
	};

	/**
	 * @inheritdoc OO.ui.mixin.RequestManager
	 */
	mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () {
		return this.getQueryValue();
	};
	/**
	 * @inheritdoc OO.ui.mixin.RequestManager
	 */
	mw.widgets.TitleSearchWidget.prototype.getRequest = function () {
		return this.getSuggestionsPromise();
	};
	/**
	 * @inheritdoc OO.ui.mixin.RequestManager
	 */
	mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) {
		return response.query || {};
	};

}( mediaWiki ) );