User:Popoffka/SlideShow.js

/*	Wikimedia Slideshow Userscript by Aleksejs Popovs This userscript allows you to see all pictures on a Wiki(m/p)edia page in a nice slideshow viewer. The slideshow can be invoked by either clicking any picture on the page or by pressing the "Slideshow" button in the actions portlet. The slideshow can be controlled by using arrow keys or the on-screen buttons. Source code is provided under the terms of the MIT License.

Copyright (C) 2011 by Aleksejs Popovs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is	furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

( function( $ ) {	var slideShowQueue;	var slideShowReady = 0;	var APILIMIT = 50;	var slideShowSlide = 0;

var generateThumbURL = function ( img, width ) { if ( width == img.width ) { return img.url; }		var res = img.url.replace( "commons", "commons/thumb" ); res += "/" + width.toString + "px-"; res += img.url.split( "/" ).pop; return res; }	var initQueue = function { var queueUpdater = function ( data ) { /*				we got (a part of) info for the slideShowQueue */			if ( slideShowReady == -1 ) { return; }

// we need this because we store titles non-normalized var alias = []; for ( var i = 0; i < data.query.normalized.length; i++ ) { alias[data.query.normalized[i].to] = data.query.normalized[i].from; }

var j;			for ( i = 0; i < data.query.pageids.length; i++ ) { filename = data.query.pages[data.query.pageids[i]].title; if ( filename in alias ) { filename = alias[filename]; }				for ( j = 0; j < slideShowQueue.length; j++ ) { if ( slideShowQueue[j].filename == filename ) { break; }				}

slideShowQueue[j].width = data.query.pages[data.query.pageids[i]].imageinfo[0].width; slideShowQueue[j].height = data.query.pages[data.query.pageids[i]].imageinfo[0].height; slideShowQueue[j].descurl = data.query.pages[data.query.pageids[i]].imageinfo[0].descriptionurl; slideShowQueue[j].url = data.query.pages[data.query.pageids[i]].imageinfo[0].url; }

slideShowReady++; };

var queueUpdateError = function ( jqXHR, textStatus, errorThrown ) { /* 				some error occured, so it's better if we just die and output some debug info to console */			slideShowReady = -1; mw.log( "Slideshow data loading failed. Here's some debug info: ", jqXHR, textStatus, errorThrown ); };

var filenames = $( ".thumb .image, .infobox .image" ).map( function {			return this.href.split( "/" ).pop; 		} ); slideShowQueue = filenames.map( function { 			return {				filename: unescape(this.toString),				width: undefined,				height: undefined,				url: undefined,				descurl: undefined			}; 		} ); var titles = ""; for ( var i = 0; i < Math.ceil( filenames.length / APILIMIT ); i++ ) { $.ajax( mw.config.get( "wgServer" ) + mw.config.get( "wgScriptPath" ) + "/api.php"				+ "?format=json&action=query&prop=imageinfo&indexpageids&iiprop=url|dimensions"				+ "&titles=" + filenames.slice( i * APILIMIT, (i + 1) * APILIMIT ).get.join( "|" ),				{ success: queueUpdater, error: queueUpdateError }			); }	};

var slideShowChange = function ( delta ) { slideShowSlide += delta; slideShowUpdate; }

var slideShowHide = function { $( ".slideShowViewer" ).hide; $( "body" ).removeClass( "slideShowActive" ); $( window ).unbind( "resize", slideShowResize ); $( window ).unbind( "keydown", slideShowKeyHandler ); }

var slideShowShow = function { $( ".slideShowViewer" ).show; $( "body" ).addClass( "slideShowActive" ); $( window ).resize( slideShowResize ); $( window ).keydown( slideShowKeyHandler ); }

var slideShowKeyHandler = function ( e ) { switch (e.which) { case 39: if ( slideShowSlide < (slideShowQueue.length - 1) ) { slideShowChange(1); }			break; case 37: if ( slideShowSlide > 0 ) { slideShowChange(-1); }			break; case 27: slideShowHide; break; }	}

var slideShowResize = function { var imageFullWidth = slideShowQueue[slideShowSlide].width; var imageFullHeight = slideShowQueue[slideShowSlide].height; var availWidth = window.innerWidth - $( "#slideShowPrev" ).outerWidth * 2; var availHeight = window.innerHeight - $( ".slideShowDescription" ).outerHeight; var imageWidth = Math.min(			Math.min( availWidth, imageFullWidth ),			Math.floor( Math.min( availHeight, imageFullHeight ) * ( imageFullWidth / imageFullHeight ) )		); $(".slideShowImage").width( imageWidth ); $(".slideShowImage").css(			"left", Math.round( $("#slideShowPrev").outerWidth + ( ( availWidth - imageWidth ) / 2) ).toString + "px"		); $(".slideShowImage").css(			"top", Math.round( ( availHeight - (imageWidth * imageFullHeight / imageFullWidth) ) / 2 ).toString + "px"		);

$("#slideShowPreviewRes").text( imageWidth.toString + "×" + Math.round(imageWidth * imageFullHeight / imageFullWidth).toString ); }

var slideShowUpdate = function { $( "#slideShowFilename" ).text( slideShowQueue[slideShowSlide].filename ); $( "#slideShowFilename" ).attr( "href", slideShowQueue[slideShowSlide].descurl ); $( "#slideShowFullRes" ).text( slideShowQueue[slideShowSlide].width + "×" + slideShowQueue[slideShowSlide].height ); $( "#slideShowCounter" ).text( (slideShowSlide + 1).toString + "/" + slideShowQueue.length.toString );

if ( slideShowSlide == 0 ) { $( "#slideShowPrev" ).addClass( "disabled" ); } else { $( "#slideShowPrev" ).removeClass( "disabled" ); }		if ( slideShowSlide == (slideShowQueue.length - 1) ) { $( "#slideShowNext" ).addClass( "disabled" ); } else { $( "#slideShowNext" ).removeClass( "disabled" ); }

// the maximum width of the image is either it's real width // or the max width at which users' screen can display it, whichever is lower var imageMaxWidth = Math.min( 			window.screen.width, 			Math.min( slideShowQueue[slideShowSlide].width, Math.round(window.screen.height * (slideShowQueue[slideShowSlide].width / slideShowQueue[slideShowSlide].height)) )		);		$( ".slideShowImage" ).load ( function {			$( ".slideShowLoading" ).hide;			$( ".slideShowImage" ).show( "fast" );		}); $( ".slideShowLoading" ).show; $( ".slideShowImage" ).hide; $( ".slideShowImage" ).attr(			"src", generateThumbURL( slideShowQueue[slideShowSlide], imageMaxWidth )		); slideShowResize; }

var slideShowInit = function { var slideShowDiv = $( document.createElement( "div" ) ); slideShowDiv.hide; slideShowDiv.addClass( "slideShowViewer" ); slideShowDiv.html(			''			+ ' Loading… '			+ ' ←  '			+ '  →  '			+ ' × '			+ ' '				+ 'imagename, previewed at pvR, full resolution: fR cnt '			+ ' '		); $( document.body ).append( slideShowDiv ); $( "#slideShowPrev" ).click( function {			if ($( this ).hasClass( "disabled" )) {				return;			}			slideShowChange(-1);		} ); $( "#slideShowNext" ).click( function {			if ($( this ).hasClass( "disabled" )) {				return;			}			slideShowChange(1);		} ) $( "#slideShowClose" ).click ( function {			slideShowHide;		} ); };

jQuery( document ).ready( function ( $ ) {		importStylesheetURI( '//www.mediawiki.org/w/index.php?action=raw&ctype=text/css&title=User:Popoffka/SlideShow.css' );

initQueue; slideShowInit; if ( slideShowQueue.length == 0 ) { return; }

$( ".thumb .image, .infobox .image" ).click( function ( e ) {			if ( slideShowReady < Math.ceil( slideShowQueue.length / APILIMIT ) ) {				return;			}			e.preventDefault;			// we search for this element in our queue			for ( var i = 0; i < slideShowQueue.length; i++ ) {				if (slideShowQueue[i].filename == this.href.split( "/" ).pop) {					break;				}			}			slideShowSlide = i;			slideShowShow;			slideShowUpdate;		} );

mw.util.addPortletLink ( "p-cactions", "#", "Slideshow", "slideShowFromBeginning", "See all pictures on this page in a slideshow" ); $( "#slideShowFromBeginning" ).click( function ( e ) {			e.preventDefault;			slideShowSlide = 0;			slideShowShow;			slideShowUpdate;		} ); } ); } )( jQuery );