All files / mobile.mediaViewer ImageGateway.js

95.23% Statements 20/21
78.57% Branches 11/14
100% Functions 4/4
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 681x 1x 1x                   5x 5x 9x   5x                       1x 1x               1x 1x 1x 1x     1x 1x                   1x       1x       1x     1x 1x  
var sizeBuckets = [ 320, 640, 800, 1024, 1280, 1920, 2560, 2880 ],
	actionParams = require( './../mobile.startup/actionParams' ),
	util = require( './../mobile.startup/util' );
 
/**
 * Gets the first size larger than or equal to the provided size
 *
 * @memberof ImageGateway
 * @param {number} size
 * @return {number}
 */
function findSizeBucket( size ) {
	var i = 0;
	while ( size > sizeBuckets[i] && i < sizeBuckets.length - 1 ) {
		++i;
	}
	return sizeBuckets[i];
}
 
/**
 * API for retrieving image thumbnails for a given page
 *
 * @class ImageGateway
 *
 * @param {Object} options Configuration options
 * @cfg {mw.Api} options.api
 */
function ImageGateway( options ) {
	this._cache = {};
	this.api = options.api;
}
/**
 * Get thumbnail via the API and cache it. Return the result from the cache if exists.
 *
 * @param {string} title Url of image
 * @return {jQuery.Deferred} with the image info
 */
ImageGateway.prototype.getThumb = function ( title ) {
	var cachedThumb = this._cache[title],
		$window = util.getWindow(),
		imageSizeMultiplier = ( window.devicePixelRatio && window.devicePixelRatio > 1 ) ?
			window.devicePixelRatio : 1;
 
	Eif ( !cachedThumb ) {
		this._cache[title] = this.api.get( actionParams( {
			prop: 'imageinfo',
			titles: title,
			iiprop: [ 'url', 'extmetadata' ],
			// request an image devicePixelRatio times bigger than the reported screen size
			// for retina displays and zooming
			iiurlwidth: findSizeBucket( $window.width() * imageSizeMultiplier ),
			iiurlheight: findSizeBucket( $window.height() * imageSizeMultiplier )
		} ) ).then( function ( resp ) {
			// imageinfo is undefined for missing pages.
			Iif ( resp.query && resp.query.pages &&
				resp.query.pages[0] && resp.query.pages[0].imageinfo ) {
				return resp.query.pages[0].imageinfo[0];
			}
			throw new Error( 'The API failed to return any pages matching the titles.' );
		} );
	}
 
	return this._cache[title];
};
 
ImageGateway._findSizeBucket = findSizeBucket;
module.exports = ImageGateway;