summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.widgets/MediaSearch/mw.widgets.MediaSearchProvider.js
blob: a46d911004daa66102912c2f3e848e85186a2bb9 (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
/*!
 * MediaWiki Widgets - MediaSearchProvider class.
 *
 * @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */
( function ( $, mw ) {

	/**
	 * MediaWiki media search provider.
	 *
	 * @class
	 * @extends mw.widgets.MediaResourceProvider
	 *
	 * @constructor
	 * @param {string} apiurl The API url
	 * @param {Object} [config] Configuration options
	 */
	mw.widgets.MediaSearchProvider = function MwWidgetsMediaSearchProvider( apiurl, config ) {
		config = config || {};

		config.staticParams = $.extend( {
			generator: 'search',
			gsrnamespace: mw.config.get( 'wgNamespaceIds' ).file
		}, config.staticParams );

		// Parent constructor
		mw.widgets.MediaSearchProvider.super.call( this, apiurl, config );
	};

	/* Inheritance */
	OO.inheritClass( mw.widgets.MediaSearchProvider, mw.widgets.MediaResourceProvider );

	/* Methods */

	/**
	 * @inheritdoc
	 */
	mw.widgets.MediaSearchProvider.prototype.getContinueData = function ( howMany ) {
		return {
			gsroffset: this.getOffset(),
			gsrlimit: howMany || this.getDefaultFetchLimit()
		};
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.MediaSearchProvider.prototype.setContinue = function ( continueData ) {
		// Update the offset for next time
		this.setOffset( continueData.gsroffset );
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.MediaSearchProvider.prototype.sort = function ( results ) {
		return results.sort( function ( a, b ) {
			return a.index - b.index;
		} );
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.MediaSearchProvider.prototype.isValid = function () {
		return this.getUserParams().gsrsearch && mw.widgets.MediaSearchProvider.super.prototype.isValid.call( this );
	};
}( jQuery, mediaWiki ) );