2013-06-06 23:14:11 +00:00
|
|
|
/*!
|
2013-06-25 00:41:01 +00:00
|
|
|
* VisualEditor UserInterface MWMediaSearchWidget class.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-06 23:14:11 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-06-25 00:41:01 +00:00
|
|
|
* Creates an ve.ui.MWMediaSearchWidget object.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @class
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.SearchWidget
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-06-06 23:14:11 +00:00
|
|
|
* @param {number} [size] Vertical size of thumbnails
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget = function VeUiMWMediaSearchWidget( config ) {
|
2014-11-21 13:00:50 +00:00
|
|
|
// Configuration initialization
|
2013-09-20 07:09:06 +00:00
|
|
|
config = ve.extendObject( {
|
2014-08-22 20:50:48 +00:00
|
|
|
placeholder: ve.msg( 'visualeditor-media-input-placeholder' )
|
2013-09-20 07:09:06 +00:00
|
|
|
}, config );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.SearchWidget.call( this, config );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Properties
|
2013-12-16 12:09:54 +00:00
|
|
|
this.sources = {};
|
2013-06-06 23:14:11 +00:00
|
|
|
this.size = config.size || 150;
|
2013-06-25 00:41:01 +00:00
|
|
|
this.queryTimeout = null;
|
2013-06-06 23:14:11 +00:00
|
|
|
this.titles = {};
|
2014-07-08 22:33:32 +00:00
|
|
|
this.queryMediaSourcesCallback = this.queryMediaSources.bind( this );
|
2014-09-05 05:21:12 +00:00
|
|
|
this.promises = [];
|
2014-05-13 06:27:18 +00:00
|
|
|
|
|
|
|
this.$noItemsMessage = this.$( '<div>' )
|
|
|
|
.addClass( 've-ui-mwMediaSearchWidget-noresults' )
|
|
|
|
.text( ve.msg( 'visualeditor-dialog-media-noresults' ) )
|
2014-07-28 19:30:34 +00:00
|
|
|
.hide()
|
2014-05-13 06:27:18 +00:00
|
|
|
.appendTo( this.$query );
|
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
// Events
|
2014-07-08 22:33:32 +00:00
|
|
|
this.$results.on( 'scroll', this.onResultsScroll.bind( this ) );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwMediaSearchWidget' );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWMediaSearchWidget, OO.ui.SearchWidget );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2013-12-16 12:09:54 +00:00
|
|
|
/**
|
|
|
|
* Set the fileRepo sources for the media search
|
|
|
|
* @param {Object} sources The sources object
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.setSources = function ( sources ) {
|
|
|
|
this.sources = sources;
|
|
|
|
};
|
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
/**
|
|
|
|
* Handle select widget select events.
|
|
|
|
*
|
|
|
|
* @param {string} value New value
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onQueryChange = function () {
|
2013-06-06 23:14:11 +00:00
|
|
|
var i, len;
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
// Parent method
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.SearchWidget.prototype.onQueryChange.call( this );
|
2013-06-25 00:41:01 +00:00
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
// Reset
|
2013-06-19 01:02:23 +00:00
|
|
|
this.titles = {};
|
2013-06-06 23:14:11 +00:00
|
|
|
for ( i = 0, len = this.sources.length; i < len; i++ ) {
|
|
|
|
delete this.sources[i].gsroffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue
|
2013-06-25 00:41:01 +00:00
|
|
|
clearTimeout( this.queryTimeout );
|
|
|
|
this.queryTimeout = setTimeout( this.queryMediaSourcesCallback, 100 );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle results scroll events.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} e Scroll event
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onResultsScroll = function () {
|
2013-06-06 23:14:11 +00:00
|
|
|
var position = this.$results.scrollTop() + this.$results.outerHeight(),
|
2013-11-01 19:45:59 +00:00
|
|
|
threshold = this.results.$element.outerHeight() - this.size;
|
2013-06-25 00:41:01 +00:00
|
|
|
if ( !this.query.isPending() && position > threshold ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
this.queryMediaSources();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query all sources for media.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.queryMediaSources = function () {
|
2014-07-28 19:30:34 +00:00
|
|
|
var i, len, source, request,
|
2014-07-28 22:01:46 +00:00
|
|
|
ajaxOptions = {},
|
2013-06-25 00:41:01 +00:00
|
|
|
value = this.query.getValue();
|
2013-06-18 21:25:56 +00:00
|
|
|
|
|
|
|
if ( value === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
|
2014-09-05 05:21:12 +00:00
|
|
|
// Reset message
|
2014-05-13 06:27:18 +00:00
|
|
|
this.$noItemsMessage.hide();
|
|
|
|
|
2014-09-05 05:21:12 +00:00
|
|
|
// Abort previous promises if they are pending
|
|
|
|
this.resetPromises();
|
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
for ( i = 0, len = this.sources.length; i < len; i++ ) {
|
|
|
|
source = this.sources[i];
|
2013-12-16 12:09:54 +00:00
|
|
|
// If we don't have either 'apiurl' or 'scriptDirUrl'
|
|
|
|
// the source is invalid, and we will skip it
|
2014-02-10 18:42:05 +00:00
|
|
|
if ( source.apiurl || source.scriptDirUrl !== undefined ) {
|
2013-12-16 12:09:54 +00:00
|
|
|
if ( !source.gsroffset ) {
|
|
|
|
source.gsroffset = 0;
|
|
|
|
}
|
|
|
|
if ( source.local ) {
|
2014-07-28 22:01:46 +00:00
|
|
|
ajaxOptions = {
|
2014-08-22 20:50:48 +00:00
|
|
|
url: mw.util.wikiScript( 'api' ),
|
2014-07-28 22:01:46 +00:00
|
|
|
// If the url is local use json
|
2014-08-22 20:50:48 +00:00
|
|
|
dataType: 'json'
|
2014-07-28 22:01:46 +00:00
|
|
|
};
|
2013-12-16 12:09:54 +00:00
|
|
|
} else {
|
2014-07-28 22:01:46 +00:00
|
|
|
ajaxOptions = {
|
|
|
|
// If 'apiurl' is set, use that. Otherwise, build the url
|
|
|
|
// from scriptDirUrl and /api.php suffix
|
2014-08-22 20:50:48 +00:00
|
|
|
url: source.apiurl || ( source.scriptDirUrl + '/api.php' ),
|
2014-07-28 22:01:46 +00:00
|
|
|
// If the url is not the same origin use jsonp
|
2014-08-22 20:50:48 +00:00
|
|
|
dataType: 'jsonp',
|
2014-07-28 22:01:46 +00:00
|
|
|
// JSON-P requests are not cached by default and get a &_=random trail.
|
|
|
|
// While setting cache=true will still bypass cache in most case due to the
|
|
|
|
// callback parameter, at least drop the &_=random trail which triggers
|
|
|
|
// an API warning (invalid parameter).
|
2014-08-22 20:50:48 +00:00
|
|
|
cache: true
|
2014-07-28 22:01:46 +00:00
|
|
|
};
|
2013-12-16 12:09:54 +00:00
|
|
|
}
|
|
|
|
this.query.pushPending();
|
2014-07-28 19:30:34 +00:00
|
|
|
request = ve.init.target.constructor.static.apiRequest( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'query',
|
|
|
|
generator: 'search',
|
|
|
|
gsrsearch: value,
|
|
|
|
gsrnamespace: 6,
|
|
|
|
gsrlimit: 20,
|
|
|
|
gsroffset: source.gsroffset,
|
|
|
|
prop: 'imageinfo',
|
|
|
|
iiprop: 'dimensions|url|mediatype',
|
|
|
|
iiurlheight: this.size
|
2014-07-28 22:01:46 +00:00
|
|
|
}, ajaxOptions )
|
2014-07-28 19:30:34 +00:00
|
|
|
.done( this.onMediaQueryDone.bind( this, source ) );
|
2013-12-16 12:09:54 +00:00
|
|
|
source.value = value;
|
2014-09-05 05:21:12 +00:00
|
|
|
this.promises.push( request );
|
2013-06-06 23:14:11 +00:00
|
|
|
}
|
2014-07-28 19:30:34 +00:00
|
|
|
|
|
|
|
// When all sources are done, check to see if there are results
|
2014-09-05 05:21:12 +00:00
|
|
|
$.when.apply( $, this.promises ).done( this.onAllMediaQueriesDone.bind( this ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort all api search query promises
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.resetPromises = function () {
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for ( i = 0; i < this.promises.length; i++ ) {
|
|
|
|
this.promises[i].abort();
|
|
|
|
this.query.popPending();
|
2013-06-06 23:14:11 +00:00
|
|
|
}
|
2014-09-05 05:21:12 +00:00
|
|
|
|
|
|
|
// Empty the promise array
|
|
|
|
this.promises = [];
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle media query response events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} source Media query source
|
|
|
|
*/
|
2014-09-05 05:21:12 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onAllMediaQueriesDone = function () {
|
2013-06-25 00:41:01 +00:00
|
|
|
this.query.popPending();
|
2014-05-13 06:27:18 +00:00
|
|
|
|
2014-07-28 19:30:34 +00:00
|
|
|
if ( this.results.getItems().length === 0 ) {
|
|
|
|
this.$noItemsMessage.show();
|
2014-09-04 22:57:40 +00:00
|
|
|
} else {
|
|
|
|
this.$noItemsMessage.hide();
|
2014-05-16 22:35:41 +00:00
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle media query load events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} source Media query source
|
|
|
|
* @param {Object} data Media query response
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onMediaQueryDone = function ( source, data ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
if ( !data.query || !data.query.pages ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-04 20:06:19 +00:00
|
|
|
var page, title,
|
2013-06-06 23:14:11 +00:00
|
|
|
items = [],
|
2013-06-18 21:25:56 +00:00
|
|
|
pages = data.query.pages,
|
2013-06-25 00:41:01 +00:00
|
|
|
value = this.query.getValue();
|
2013-06-18 21:25:56 +00:00
|
|
|
|
2013-06-19 01:02:23 +00:00
|
|
|
if ( value === '' || value !== source.value ) {
|
2013-06-18 21:25:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
if ( data['query-continue'] && data['query-continue'].search ) {
|
|
|
|
source.gsroffset = data['query-continue'].search.gsroffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( page in pages ) {
|
2014-02-14 22:58:30 +00:00
|
|
|
// Verify that imageinfo exists
|
|
|
|
// In case it does not, skip the image to avoid errors in
|
|
|
|
// ve.ui.MWMediaResultWidget
|
|
|
|
if ( pages[page].imageinfo && pages[page].imageinfo.length > 0 ) {
|
|
|
|
title = new mw.Title( pages[page].title ).getMainText();
|
2014-11-05 20:18:24 +00:00
|
|
|
if ( !Object.prototype.hasOwnProperty.call( this.titles, title ) ) {
|
2014-02-14 22:58:30 +00:00
|
|
|
this.titles[title] = true;
|
|
|
|
items.push(
|
2014-11-22 01:40:00 +00:00
|
|
|
new ve.ui.MWMediaResultWidget( {
|
|
|
|
$: this.$,
|
|
|
|
data: pages[page],
|
|
|
|
size: this.size
|
|
|
|
} )
|
2014-02-14 22:58:30 +00:00
|
|
|
);
|
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
this.results.addItems( items );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|