MediaWiki:Gadget-lazyload-core.js

Revision as of 14:30, 14 August 2020 by Banri (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<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>