MediaWiki:Gadget-lazyload-core.js: Difference between revisions

From [N8]
Jump to navigation Jump to search
 
No edit summary
 
Line 2: Line 2:
/**
/**
  * Allows loading of images (or other files) on click instead of on page load
  * Allows loading of images (or other files) on click instead of on page load
*
* @version 1.0
* @author Elessar2
  *  
  *  
  */
  */

Latest revision as of 14:30, 14 August 2020

//<nowiki>
/**
 * Allows loading of images (or other files) on click instead of on page load
 * 
 */

/*global jQuery, mediaWiki, mw */
'use strict';

;(function($){

	// load image
	function loadImg($span) {
		mw.log('load image');
		mw.log($span);
		if ($span.find('img').length) {
			mw.log('image already loaded');
			return;
		}
		$span.off('click');

		var $link = $span.find('a'),
			alt = $span.attr('data-alt') || $link.attr('title').replace('File:', ''),
			$img = $('<img>').attr({ alt:alt, src:$span.attr('data-file') }).addClass( $span.attr('data-class') );

		$span.addClass('image');
		$link.attr({href:$span.attr('data-href'), title:alt}).empty().append($img);
	}

	// initialise
	function init() {
		$('span.gsw-lazyload-all a').each(function (i) {
			$(this).parent().attr('id', 'llall'+i);
			$(this).attr('href', '#llall'+i);
			$(this).click(function () {
				mw.log('load all');
				$('span.gsw-lazyload').each(function () {
					loadImg($(this));
				});
				$('span.gsw-lazyload-all a').each(function () {
					$(this).off('click');
				});
				return false;
			});
		});

		$('span.gsw-lazyload').each(function (i) {
			$(this).attr({'data-href':$(this).find('a').attr('href'), id:'llitem'+i });
			$(this).find('a').attr('href', '#llitem'+i);
			$(this).click(function () {
				loadImg($(this));
				return false;
			});
		});
	}

	$(init);
}(jQuery));
//</nowiki>