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
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global mw*/
|
|
|
|
|
|
|
|
/**
|
2013-06-25 00:41:01 +00:00
|
|
|
* Creates an ve.ui.MWMediaSearchWidget object.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @class
|
2013-06-25 00:41:01 +00:00
|
|
|
* @extends ve.ui.SearchWidget
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Config options
|
|
|
|
* @param {number} [size] Vertical size of thumbnails
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget = function VeUiMWMediaSearchWidget( config ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
// Configuration intialization
|
2013-06-25 00:41:01 +00:00
|
|
|
config = ve.extendObject( {}, config, {
|
|
|
|
'placeholder': ve.msg( 'visualeditor-media-input-placeholder' ),
|
|
|
|
'value': mw.config.get( 'wgTitle' )
|
|
|
|
} );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Parent constructor
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.SearchWidget.call( this, config );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Properties
|
2013-07-28 20:51:32 +00:00
|
|
|
this.sources = ve.copy( ve.init.platform.getMediaSources() );
|
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 = {};
|
|
|
|
this.queryMediaSourcesCallback = ve.bind( this.queryMediaSources, this );
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.$results.on( 'scroll', ve.bind( this.onResultsScroll, this ) );
|
|
|
|
|
|
|
|
// Initialization
|
2013-06-25 00:41:01 +00:00
|
|
|
this.$.addClass( 've-ui-mwMediaSearchWidget' );
|
2013-06-06 23:14:11 +00:00
|
|
|
this.queryMediaSources();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.inheritClass( ve.ui.MWMediaSearchWidget, ve.ui.SearchWidget );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
ve.ui.SearchWidget.prototype.onQueryChange.call( this );
|
|
|
|
|
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-06-25 00:41:01 +00:00
|
|
|
threshold = this.results.$.outerHeight() - this.size;
|
|
|
|
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 () {
|
2013-06-18 21:25:56 +00:00
|
|
|
var i, len, source,
|
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
|
|
|
|
|
|
|
for ( i = 0, len = this.sources.length; i < len; i++ ) {
|
|
|
|
source = this.sources[i];
|
|
|
|
if ( source.request ) {
|
|
|
|
source.request.abort();
|
|
|
|
}
|
|
|
|
if ( !source.gsroffset ) {
|
|
|
|
source.gsroffset = 0;
|
|
|
|
}
|
2013-06-25 00:41:01 +00:00
|
|
|
this.query.pushPending();
|
2013-06-06 23:14:11 +00:00
|
|
|
source.request = $.ajax( {
|
|
|
|
'url': source.url,
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'query',
|
|
|
|
'generator': 'search',
|
2013-06-18 21:25:56 +00:00
|
|
|
'gsrsearch': value,
|
2013-06-06 23:14:11 +00:00
|
|
|
'gsrnamespace': 6,
|
|
|
|
'gsrlimit': 15,
|
|
|
|
'gsroffset': source.gsroffset,
|
|
|
|
'prop': 'imageinfo',
|
|
|
|
'iiprop': 'dimensions|url',
|
|
|
|
'iiurlheight': this.size
|
|
|
|
},
|
2013-06-19 01:02:23 +00:00
|
|
|
// This request won't be cached since the JSON-P callback is unique. However make sure
|
|
|
|
// to allow jQuery to cache otherwise so it won't e.g. add "&_=(random)" which will
|
|
|
|
// trigger a MediaWiki API error for invalid parameter "_".
|
|
|
|
'cache': true,
|
|
|
|
// TODO: Only use JSON-P for cross-domain.
|
|
|
|
// jQuery has this logic built-in (if url is not same-origin ..)
|
|
|
|
// but isn't working for some reason.
|
2013-06-06 23:14:11 +00:00
|
|
|
'dataType': 'jsonp'
|
|
|
|
} )
|
2013-06-19 01:02:23 +00:00
|
|
|
.done( ve.bind( this.onMediaQueryDone, this, source ) )
|
|
|
|
.always( ve.bind( this.onMediaQueryAlways, this, source ) );
|
|
|
|
source.value = value;
|
2013-06-06 23:14:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle media query response events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} source Media query source
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onMediaQueryAlways = function ( source ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
source.request = null;
|
2013-06-25 00:41:01 +00:00
|
|
|
this.query.popPending();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var page, title,
|
|
|
|
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 ) {
|
2013-07-11 17:11:24 +00:00
|
|
|
title = new mw.Title( pages[page].title ).getMainText();
|
2013-06-06 23:14:11 +00:00
|
|
|
if ( !( title in this.titles ) ) {
|
|
|
|
this.titles[title] = true;
|
|
|
|
items.push(
|
2013-06-25 00:41:01 +00:00
|
|
|
new ve.ui.MWMediaResultWidget(
|
2013-06-06 23:14:11 +00:00
|
|
|
pages[page],
|
|
|
|
{ '$$': this.$$, 'size': this.size }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
this.results.addItems( items );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|