2 * MediaWiki Widgets - APIResultsProvider class.
4 * @copyright 2011-2016 VisualEditor Team and others; see http://ve.mit-license.org
9 * @classdesc API results provider.
12 * @mixes OO.EventEmitter
15 * @description Create an instance of `mw.widgets.APIResultsProvider`.
16 * @param {string} apiurl The URL to the api
17 * @param {Object} [config] Configuration options
18 * @param {number} config.fetchLimit The default number of results to fetch
19 * @param {string} config.lang The language of the API
20 * @param {number} config.offset Initial offset, if relevant, to call results from
21 * @param {Object} config.ajaxSettings The settings for the ajax call
22 * @param {Object} config.staticParams The data parameters that are static and should
23 * always be sent to the API request, as opposed to user parameters.
24 * @param {Object} config.userParams Initial user parameters to be sent as data to
25 * the API request. These can change per request, like the search query term
26 * or sizing parameters for images, etc.
28 mw.widgets.APIResultsProvider = function MwWidgetsAPIResultsProvider( apiurl, config ) {
29 config = config || {};
31 this.setAPIurl( apiurl );
32 this.setDefaultFetchLimit( config.fetchLimit || 30 );
33 this.setLang( config.lang );
34 this.setOffset( config.offset || 0 );
35 this.setAjaxSettings( config.ajaxSettings || {} );
37 this.staticParams = config.staticParams || {};
38 this.userParams = config.userParams || {};
40 this.toggleDepleted( false );
43 OO.EventEmitter.call( this );
47 OO.mixinClass( mw.widgets.APIResultsProvider, OO.EventEmitter );
52 * Get results from the source.
54 * @param {number} howMany Number of results to ask for
55 * @return {jQuery.Promise} Promise that is resolved into an array
56 * of available results, or is rejected if no results are available.
58 mw.widgets.APIResultsProvider.prototype.getResults = function () {
59 const deferred = $.Deferred(),
60 allParams = Object.assign( {}, this.getStaticParams(), this.getUserParams() );
62 const xhr = $.getJSON( this.getAPIurl(), allParams )
64 if ( Array.isArray( data ) && data.length ) {
65 deferred.resolve( data );
70 return deferred.promise( { abort: xhr.abort } );
76 * @param {string} apiurl API URL
78 mw.widgets.APIResultsProvider.prototype.setAPIurl = function ( apiurl ) {
85 * @return {string} API URL
87 mw.widgets.APIResultsProvider.prototype.getAPIurl = function () {
92 * Get the static, non-changing data parameters sent to the API.
94 * @return {Object} Data parameters
96 mw.widgets.APIResultsProvider.prototype.getStaticParams = function () {
97 return this.staticParams;
101 * Get the user-inputted dynamic data parameters sent to the API.
103 * @return {Object} Data parameters
105 mw.widgets.APIResultsProvider.prototype.getUserParams = function () {
106 return this.userParams;
110 * Set the data parameters sent to the API.
112 * @param {Object} params User defined data parameters
114 mw.widgets.APIResultsProvider.prototype.setUserParams = function ( params ) {
115 // Asymmetrically compare (params is subset of this.userParams)
116 if ( !OO.compare( params, this.userParams, true ) ) {
117 this.userParams = Object.assign( {}, this.userParams, params );
123 * Reset the provider.
125 mw.widgets.APIResultsProvider.prototype.reset = function () {
128 // Reset depleted status
129 this.toggleDepleted( false );
133 * Get fetch limit or 'page' size. This is the number
134 * of results per request.
136 * @return {number} limit
138 mw.widgets.APIResultsProvider.prototype.getDefaultFetchLimit = function () {
145 * @param {number} limit Default number of results to fetch from the API
147 mw.widgets.APIResultsProvider.prototype.setDefaultFetchLimit = function ( limit ) {
152 * Get provider API language.
154 * @return {string} Provider API language
156 mw.widgets.APIResultsProvider.prototype.getLang = function () {
161 * Set provider API language.
163 * @param {string} lang Provider API language
165 mw.widgets.APIResultsProvider.prototype.setLang = function ( lang ) {
172 * @return {number} Offset Results offset for the upcoming request
174 mw.widgets.APIResultsProvider.prototype.getOffset = function () {
181 * @param {number} offset Results offset for the upcoming request
183 mw.widgets.APIResultsProvider.prototype.setOffset = function ( offset ) {
184 this.offset = offset;
188 * Check whether the provider is depleted and has no more results
191 * @return {boolean} The provider is depleted
193 mw.widgets.APIResultsProvider.prototype.isDepleted = function () {
194 return this.depleted;
198 * Toggle depleted state.
200 * @param {boolean} isDepleted The provider is depleted
202 mw.widgets.APIResultsProvider.prototype.toggleDepleted = function ( isDepleted ) {
203 this.depleted = isDepleted !== undefined ? isDepleted : !this.depleted;
207 * Get the default ajax settings.
209 * @return {Object} Ajax settings
211 mw.widgets.APIResultsProvider.prototype.getAjaxSettings = function () {
212 return this.ajaxSettings;
216 * Set the default ajax settings.
218 * @param {Object} settings Ajax settings
220 mw.widgets.APIResultsProvider.prototype.setAjaxSettings = function ( settings ) {
221 this.ajaxSettings = settings;