MediaWiki:Gadget-lazyload.js: Difference between revisions

 
Replaced content with "// <nowiki> if ($('span.gsw-lazyload').length) { mw.loader.load( 'ext.gadget.lazyload-core' ); } // </nowiki>"
Tag: Replaced
 
Line 1: Line 1:
//<nowiki>
// <nowiki>
/**
if ($('span.gsw-lazyload').length) {
* Allows loading of images (or other files) on click instead of on page load
mw.loader.load( 'ext.gadget.lazyload-core' );
*
}
*
// </nowiki>
*/
 
/*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>