1f526e254cc76ee1b45c9b880c0246f02cf19f4c
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.SearchInputWidget.js
1 /*!
2 * MediaWiki Widgets - SearchInputWidget 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 a mw.widgets.SearchInputWidget object.
11 *
12 * @class
13 * @extends mw.widgets.TitleInputWidget
14 *
15 * @constructor
16 * @cfg {boolean} [pushPending=true] Visually mark the input field as "pending", while
17 * requesting suggestions.
18 */
19 mw.widgets.SearchInputWidget = function MwWidgetsSearchInputWidget( config ) {
20 config = $.extend( {
21 type: 'search',
22 icon: 'search',
23 maxLength: undefined
24 }, config );
25
26 // Parent constructor
27 mw.widgets.SearchInputWidget.parent.call( this, config );
28
29 // Initialization
30 this.$element.addClass( 'mw-widget-searchInputWidget' );
31 this.lookupMenu.$element.addClass( 'mw-widget-searchWidget-menu' );
32 if ( !config.pushPending ) {
33 this.pushPending = false;
34 }
35 this.setLookupsDisabled( !this.suggestions );
36 };
37
38 /* Setup */
39
40 OO.inheritClass( mw.widgets.SearchInputWidget, mw.widgets.TitleInputWidget );
41
42 /* Methods */
43
44 /**
45 * @inheritdoc mw.widgets.TitleWidget
46 */
47 mw.widgets.SearchInputWidget.prototype.getSuggestionsPromise = function () {
48 var api = new mw.Api();
49
50 // reuse the searchSuggest function from mw.searchSuggest
51 return mw.searchSuggest.request( api, this.getQueryValue(), $.noop, this.limit );
52 };
53
54 /**
55 * @inheritdoc mw.widgets.TitleInputWidget
56 */
57 mw.widgets.SearchInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
58 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
59 // so return the whole response (titles only, and links)
60 return response || {};
61 };
62
63 /**
64 * @inheritdoc mw.widgets.TitleWidget
65 */
66 mw.widgets.SearchInputWidget.prototype.getOptionsFromData = function ( data ) {
67 var items = [],
68 self = this;
69
70 // mw.widgets.TitleWidget does a lot more work here, because the TitleOptionWidgets can
71 // differ a lot, depending on the returned data from the request. With the request used here
72 // we get only the search results.
73 $.each( data[ 1 ], function ( i, result ) {
74 items.push( new mw.widgets.TitleOptionWidget(
75 // data[ 3 ][ i ] is the link for this result
76 self.getOptionWidgetData( result, null, data[ 3 ][ i ] )
77 ) );
78 } );
79
80 mw.track( 'mw.widgets.SearchInputWidget', {
81 action: 'impression-results',
82 numberOfResults: items.length,
83 resultSetType: mw.searchSuggest.type
84 } );
85
86 return items;
87 };
88
89 /**
90 * @inheritdoc mw.widgets.TitleWidget
91 *
92 * @param {string} title
93 * @param {Object} data
94 * @param {string} url The Url to the result
95 */
96 mw.widgets.SearchInputWidget.prototype.getOptionWidgetData = function ( title, data, url ) {
97 // the values used in mw.widgets-TitleWidget doesn't exist here, that's why
98 // the values are hard-coded here
99 return {
100 data: title,
101 url: url,
102 imageUrl: null,
103 description: null,
104 missing: false,
105 redirect: false,
106 disambiguation: false,
107 query: this.getQueryValue()
108 };
109 };
110
111 }( jQuery, mediaWiki ) );