MediaWiki:Gadget-GallerySlideshow.js

From Wikipedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Installer Script for Gallery-Slideshow
 * Loads big code on demand
 * This code is jshint-valid!
 */
 
/*global $:false, mw:false, importScriptURI:false */
/*jshint curly:false, */
$(document).ready(function () {
	'use strict';
	
	if ($('.gallery').find('li').length < 2 || mw.config.get('wgNamespaceNumber') === -1 ) return; // no need for a gallery with a few images
	
	// Preparation for i18n and ResourceLoader 2.0 - using MediaWiki messages to translate
	var msgs = {
		"gs-start-label": "Slideshow!",
		"gs-screenread-label": "Gallery Slideshow",
		"gs-continue-label": "Continue Slideshow",
		"gs-start-title-label": "Create a slideshow of all images on this page (by alphabetical order) or in this category (alphabetical by sortkey) or re-show a closed slideshow.",
		"gs-screenread-title-label": "Create a slideshow that is reading the images from gallery-boxes from top of the page to the bottom.",
		"gs-continue-title-label": "Continue Slideshow where you had left it last time you visited this page."
	};
	mw.messages.set( msgs );
	
	var getMessage = function (msg) {
		msg = mw.message( 'gs-' + msg + '-label' );
		return (msg.exists() ? msg.toString() : msg);
	};
	
	mw.loader.using(['mediawiki.cookie', 'mediawiki.util'], function () {
 
		var $insertNode = (mw.config.get('wgNamespaceNumber') === 14) ? $('#mw-category-media h2') : $('#firstHeading');

		var startSlideshow = function (o, cont, screenread) {
			if (cont) o.cont = cont;
			if (screenread) {
				o.readFromScreen = true;
				o.remoteUse = true;
			}
			o.start();
		};
		
		var loadSlideshowAndStart = function (cont, screenread) {
			if ('object' === typeof window.GallerySlide) {
				startSlideshow(window.GallerySlide, cont, screenread);
			} else {
				$(document).on('slideshow', function (e, st, o) {
					// If the code requires debugging, you can uncomment the following line
					// console.log('evt: ' + st);
					if ('codeLoaded' === st && o) {
						startSlideshow(o, cont, screenread);
					}
				});
				window.importScriptURI(mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode('MediaWiki:GallerySlideshow.js') + '&action=raw&ctype=text/javascript&dummy=2');
			}
		};
		
		$insertNode.after($('<span>', { id: 'GallerySlideInit', tabindex: 12, title: getMessage('start-title'), text: getMessage('start') }).click(function () {
			loadSlideshowAndStart();
		}));
		
		$insertNode.after($('<span>', { id: 'GallerySlideScreenreadInit', tabindex: 11, title: getMessage('screenread-title'), text: getMessage('screenread') }).click(function () {
			loadSlideshowAndStart(0, true);
		}));
 
		var lastQuery = $.cookie( 'gs' + mw.config.get('wgPageName').replace('Category:', '1:').replace('Commons:', '2:') );
		if (lastQuery) {
			$insertNode.after($('<span>', { id: 'GallerySlideContinue', tabindex: 10, title: getMessage('continue-title'), text: getMessage('continue') }).click( function () {
					loadSlideshowAndStart(lastQuery);
				}
			));
		}
 
		$(document).triggerHandler('slideshow', ['loadedInstaller']); // For external scripts
 
		var autoStart = mw.util.getParamValue('gsAutoStart');
		if (autoStart) {
			if ('1' === autoStart || 'true' === autoStart || 'yes' === autoStart || '-1' === autoStart) {
				loadSlideshowAndStart();
			}
		}
	});
});