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
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 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 = {};
|
2015-02-02 22:15:52 +00:00
|
|
|
this.searchValue = '';
|
2015-01-15 18:50:30 +00:00
|
|
|
this.rowHeight = config.rowHeight || 200;
|
2014-09-18 19:49:22 +00:00
|
|
|
this.$panels = config.$panels;
|
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 = [];
|
2015-01-16 17:11:13 +00:00
|
|
|
this.layoutQueue = [];
|
2014-09-18 19:49:22 +00:00
|
|
|
this.numItems = 0;
|
|
|
|
this.lang = config.lang || 'en';
|
|
|
|
|
|
|
|
this.selected = null;
|
|
|
|
|
|
|
|
this.rows = [];
|
2014-05-13 06:27:18 +00:00
|
|
|
|
|
|
|
this.$noItemsMessage = this.$( '<div>' )
|
2015-01-29 23:09:47 +00:00
|
|
|
.addClass( 've-ui-mwMediaSearchWidget-noresults oo-ui-element-hidden' )
|
2014-05-13 06:27:18 +00:00
|
|
|
.text( ve.msg( 'visualeditor-dialog-media-noresults' ) )
|
|
|
|
.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 ) );
|
2014-09-18 19:49:22 +00:00
|
|
|
this.results.connect( this, {
|
|
|
|
choose: 'onResultsChoose',
|
|
|
|
add: 'onResultsAdd',
|
|
|
|
remove: 'onResultsRemove'
|
|
|
|
} );
|
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;
|
|
|
|
};
|
|
|
|
|
2015-01-15 18:50:30 +00:00
|
|
|
/**
|
|
|
|
* Set the fileRepo sources for the media search
|
|
|
|
* @param {Object} sources The sources object
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.getSources = function () {
|
|
|
|
return this.sources;
|
|
|
|
};
|
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
/**
|
|
|
|
* Handle select widget select events.
|
|
|
|
*
|
|
|
|
* @param {string} value New value
|
|
|
|
*/
|
2015-02-02 22:15:52 +00:00
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onQueryChange = function ( value ) {
|
|
|
|
var i, len,
|
|
|
|
trimmed = $.trim( value );
|
|
|
|
|
|
|
|
if ( trimmed === this.searchValue ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.searchValue = trimmed;
|
2013-06-06 23:14:11 +00:00
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
// Parent method
|
2015-02-02 22:15:52 +00:00
|
|
|
OO.ui.SearchWidget.prototype.onQueryChange.apply( this, arguments );
|
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 );
|
2015-02-02 22:01:49 +00:00
|
|
|
this.queryTimeout = setTimeout( this.queryMediaSourcesCallback, 250 );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
/**
|
|
|
|
* Respond to choosing result event.
|
|
|
|
*
|
|
|
|
* @param {OO.ui.OptionWidget} item Selected item
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onResultsChoose = function ( item ) {
|
|
|
|
this.emit( 'choose', item.getData() );
|
|
|
|
};
|
|
|
|
|
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(),
|
2015-01-15 18:50:30 +00:00
|
|
|
threshold = this.results.$element.outerHeight() - this.rowHeight * 3;
|
|
|
|
|
|
|
|
// Check if we need to ask for more results
|
2013-06-25 00:41:01 +00:00
|
|
|
if ( !this.query.isPending() && position > threshold ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
this.queryMediaSources();
|
|
|
|
}
|
2015-01-15 18:50:30 +00:00
|
|
|
|
|
|
|
this.lazyLoadResults();
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
2015-01-15 18:50:30 +00:00
|
|
|
/**
|
|
|
|
* Lazy-load the images that are visible.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.lazyLoadResults = function () {
|
|
|
|
var i, elementTop,
|
|
|
|
items = this.results.getItems(),
|
|
|
|
resultsScrollTop = this.$results.scrollTop(),
|
|
|
|
position = resultsScrollTop + this.$results.outerHeight();
|
|
|
|
// Lazy-load results
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
elementTop = items[i].$element.position().top;
|
|
|
|
if ( elementTop <= position && !items[i].hasSrc() ) {
|
|
|
|
// Load the image
|
|
|
|
items[i].lazyLoad();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-06-06 23:14:11 +00:00
|
|
|
/**
|
|
|
|
* 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-09-18 19:49:22 +00:00
|
|
|
lang = this.getLang(),
|
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
|
2015-01-29 23:09:47 +00:00
|
|
|
this.$noItemsMessage.addClass( 'oo-ui-element-hidden' );
|
|
|
|
|
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,
|
2015-01-15 18:50:30 +00:00
|
|
|
// Initial number of images
|
|
|
|
// NOTE: If this is too high, it triggers Common's bot prevention code
|
|
|
|
gsrlimit: 30,
|
2014-08-22 20:50:48 +00:00
|
|
|
gsroffset: source.gsroffset,
|
|
|
|
prop: 'imageinfo',
|
2014-09-18 19:49:22 +00:00
|
|
|
// Language of the extmetadata details
|
|
|
|
iiextmetadatalanguage: lang,
|
|
|
|
iiprop: 'dimensions|url|mediatype|extmetadata|timestamp',
|
2015-01-15 18:50:30 +00:00
|
|
|
iiurlheight: this.rowHeight,
|
2014-09-18 19:49:22 +00:00
|
|
|
// Width of the dialog
|
|
|
|
iiurlwidth: 600 - 30 // Take off 30px for the margins
|
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 ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
/**
|
|
|
|
* Reset all the rows; destroy the jQuery elements and reset
|
|
|
|
* the rows array.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.resetRows = function () {
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
for ( i = 0, len = this.rows.length; i < len; i++ ) {
|
|
|
|
this.rows[i].$element.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rows = [];
|
|
|
|
};
|
|
|
|
|
2014-09-05 05:21:12 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
this.rowIndex = 0;
|
2014-09-05 05:21:12 +00:00
|
|
|
// Empty the promise array
|
|
|
|
this.promises = [];
|
2015-01-16 17:11:13 +00:00
|
|
|
// Empty the results queue
|
|
|
|
this.layoutQueue = [];
|
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 ) {
|
2015-01-29 23:09:47 +00:00
|
|
|
this.$noItemsMessage.removeClass( 'oo-ui-element-hidden' );
|
2014-09-04 22:57:40 +00:00
|
|
|
} else {
|
2015-01-29 23:09:47 +00:00
|
|
|
this.$noItemsMessage.addClass( 'oo-ui-element-hidden' );
|
2015-01-15 18:50:30 +00:00
|
|
|
this.lazyLoadResults();
|
2014-05-16 22:35:41 +00:00
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
/**
|
|
|
|
* Find an available row at the end. Either we will need to create a new
|
|
|
|
* row or use the last available row if it isn't full.
|
|
|
|
* @return {number} Row index
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.getAvailableRow = function () {
|
|
|
|
var row,
|
|
|
|
maxLineWidth = this.results.$element.innerWidth() - 10;
|
|
|
|
|
|
|
|
if ( this.rows.length === 0 ) {
|
|
|
|
row = 0;
|
|
|
|
} else {
|
|
|
|
row = this.rows.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.rows[row] ) {
|
|
|
|
// Create new row
|
|
|
|
this.rows[row] = {
|
|
|
|
isFull: false,
|
|
|
|
width: 0,
|
|
|
|
items: [],
|
|
|
|
$element: this.$( '<div>' )
|
|
|
|
.addClass( 've-ui-mwMediaResultWidget-row' )
|
|
|
|
.css( {
|
|
|
|
width: maxLineWidth,
|
|
|
|
overflow: 'hidden'
|
|
|
|
} )
|
|
|
|
.data( 'row', row )
|
|
|
|
.attr( 'data-full', false )
|
|
|
|
};
|
|
|
|
// Append to results
|
|
|
|
this.results.$element.append( this.rows[row].$element );
|
|
|
|
} else if ( this.rows[row].isFull ) {
|
|
|
|
row++;
|
|
|
|
// Create new row
|
|
|
|
this.rows[row] = {
|
|
|
|
isFull: false,
|
|
|
|
width: 0,
|
|
|
|
items: [],
|
|
|
|
$element: this.$( '<div>' )
|
|
|
|
.addClass( 've-ui-mwMediaResultWidget-row' )
|
|
|
|
.css( {
|
|
|
|
width: maxLineWidth,
|
|
|
|
overflow: 'hidden'
|
|
|
|
} )
|
|
|
|
.data( 'row', row )
|
|
|
|
.attr( 'data-full', false )
|
|
|
|
};
|
|
|
|
// Append to results
|
|
|
|
this.results.$element.append( this.rows[row].$element );
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to add results event in the results widget.
|
|
|
|
* Override the way SelectWidget and GroupElement append the items
|
|
|
|
* into the group so we can append them in groups of rows.
|
|
|
|
* @param {ve.ui.MWMediaResultWidget[]} items An array of item elements
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onResultsAdd = function ( items ) {
|
2015-01-16 17:11:13 +00:00
|
|
|
var search = this;
|
|
|
|
|
|
|
|
// Add method to a queue; this queue will only run when the widget
|
|
|
|
// is visible
|
|
|
|
this.layoutQueue.push( function () {
|
|
|
|
var i, j, ilen, jlen, itemWidth, row, effectiveWidth, resizeFactor,
|
|
|
|
maxLineWidth = search.results.$element.innerWidth() - 10;
|
|
|
|
|
|
|
|
// Go over the added items
|
|
|
|
row = search.getAvailableRow();
|
|
|
|
for ( i = 0, ilen = items.length; i < ilen; i++ ) {
|
|
|
|
// TODO: Figure out a better way to calculate the margins
|
|
|
|
// between images (for now, hard-coded as 6)
|
|
|
|
itemWidth = items[i].$element.outerWidth() + 6;
|
|
|
|
// Add items to row until it is full
|
|
|
|
if ( search.rows[row].width + itemWidth >= maxLineWidth ) {
|
|
|
|
// Mark this row as full
|
|
|
|
search.rows[row].isFull = true;
|
|
|
|
search.rows[row].$element.attr( 'data-full', true );
|
|
|
|
// Resize all images in the row to fit the width
|
|
|
|
effectiveWidth = search.rows[row].width;
|
|
|
|
resizeFactor = maxLineWidth / effectiveWidth;
|
|
|
|
for ( j = 0, jlen = search.rows[row].items.length; j < jlen; j++ ) {
|
|
|
|
search.rows[row].items[j].resizeThumb( resizeFactor );
|
|
|
|
}
|
|
|
|
// find another row
|
|
|
|
row = search.getAvailableRow();
|
2014-09-18 19:49:22 +00:00
|
|
|
}
|
2015-01-16 17:11:13 +00:00
|
|
|
// Append to row
|
|
|
|
search.rows[row].width += itemWidth;
|
|
|
|
// Store reference to the item
|
|
|
|
search.rows[row].items.push( items[i] );
|
|
|
|
items[i].setRow( row );
|
|
|
|
// Append the item
|
|
|
|
search.rows[row].$element.append( items[i].$element );
|
2014-09-18 19:49:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:11:13 +00:00
|
|
|
// If we have less than 4 rows, call for more images
|
|
|
|
if ( search.rows.length < 4 ) {
|
|
|
|
search.queryMediaSources();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
this.runLayoutQueue();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run layout methods from the queue only if the element is visible.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.runLayoutQueue = function () {
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
if ( this.$element.is( ':visible' ) ) {
|
|
|
|
for ( i = 0, len = this.layoutQueue.length; i < len; i++ ) {
|
|
|
|
this.layoutQueue.pop()();
|
|
|
|
}
|
2014-09-18 19:49:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to removing results event in the results widget.
|
|
|
|
* Clear the relevant rows.
|
|
|
|
* @param {OO.ui.OptionWidget[]} items Removed items
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.onResultsRemove = function ( items ) {
|
|
|
|
if ( items.length > 0 ) {
|
|
|
|
// In the case of the media search widget, if any items are removed
|
|
|
|
// all are removed (new search)
|
|
|
|
this.resetRows();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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],
|
2015-01-15 18:50:30 +00:00
|
|
|
size: this.rowHeight,
|
2014-09-18 19:49:22 +00:00
|
|
|
maxSize: this.results.$element.width() / 3
|
2014-11-22 01:40:00 +00:00
|
|
|
} )
|
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
|
|
|
};
|
2014-09-18 19:49:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set language for the search results.
|
|
|
|
* @param {string} lang Language
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.setLang = function ( lang ) {
|
|
|
|
this.lang = lang;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get language for the search results.
|
|
|
|
* @returns {string} lang Language
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaSearchWidget.prototype.getLang = function () {
|
|
|
|
return this.lang;
|
|
|
|
};
|