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

	/**
	 * MediaWiki media resource queue.
	 *
	 * @class
	 * @extends mw.widgets.MediaResourceQueue
	 *
	 * @constructor
	 * @param {Object} [config] Configuration options
	 * @cfg {number} maxHeight The maximum height of the media, used in the
	 *  search call to the API.
	 */
	mw.widgets.MediaSearchQueue = function MwWidgetsMediaSearchQueue( config ) {
		config = config || {};

		// Parent constructor
		mw.widgets.MediaSearchQueue.super.call( this, config );

		this.searchQuery = '';
	};

	/* Inheritance */
	OO.inheritClass( mw.widgets.MediaSearchQueue, mw.widgets.MediaResourceQueue );

	/**
	 * Override parent method to set up the providers according to
	 * the file repos
	 *
	 * @return {jQuery.Promise} Promise that resolves when the resources are set up
	 */
	mw.widgets.MediaSearchQueue.prototype.setup = function () {
		var i, len,
			queue = this;

		return this.getFileRepos().then( function ( sources ) {
			if ( queue.providers.length === 0 ) {
				// Set up the providers
				for ( i = 0, len = sources.length; i < len; i++ ) {
					queue.providers.push( new mw.widgets.MediaSearchProvider(
						sources[ i ].apiurl,
						{
							name: sources[ i ].name,
							local: sources[ i ].local,
							scriptDirUrl: sources[ i ].scriptDirUrl,
							userParams: {
								gsrsearch: queue.getSearchQuery()
							},
							staticParams: {
								iiurlheight: queue.getMaxHeight()
							}
						} )
					);
				}
			}
		} );
	};

	/**
	 * Set the search query
	 *
	 * @param {string} searchQuery API search query
	 */
	mw.widgets.MediaSearchQueue.prototype.setSearchQuery = function ( searchQuery ) {
		this.setParams( { gsrsearch: searchQuery } );
	};

	/**
	 * Get the search query
	 *
	 * @return {string} API search query
	 */
	mw.widgets.MediaSearchQueue.prototype.getSearchQuery = function () {
		return this.getParams().gsrsearch;
	};
}( mediaWiki ) );