;(function (factory){
if(typeof define==='function'&&define.amd){
define(['jquery'], factory);
}else if(typeof exports==='object'){
module.exports=factory(require('jquery'));
}else{
factory(jQuery);
}}(function ($){
var eventNamespace='waitForImages';
var hasSrcset=(function(img){
return img.srcset&&img.sizes;
})(new Image());
$.waitForImages={
hasImageProperties: [
'backgroundImage',
'listStyleImage',
'borderImage',
'borderCornerImage',
'cursor'
],
hasImageAttributes: ['srcset']
};
$.expr.pseudos['has-src']=function (obj){
return $(obj).is('img[src][src!=""]');
};
$.expr.pseudos.uncached=function (obj){
if(!$(obj).is(':has-src')){
return false;
}
return !obj.complete;
};
$.fn.waitForImages=function (){
var allImgsLength=0;
var allImgsLoaded=0;
var deferred=$.Deferred();
var originalCollection=this;
var allImgs=[];
var hasImgProperties=$.waitForImages.hasImageProperties||[];
var hasImageAttributes=$.waitForImages.hasImageAttributes||[];
var matchUrl=/url\(\s*(['"]?)(.*?)\1\s*\)/g;
var finishedCallback;
var eachCallback;
var waitForAll;
if($.isPlainObject(arguments[0])){
waitForAll=arguments[0].waitForAll;
eachCallback=arguments[0].each;
finishedCallback=arguments[0].finished;
}else{
if(arguments.length===1&&typeof(arguments[0])==='boolean'){
waitForAll=arguments[0];
}else{
finishedCallback=arguments[0];
eachCallback=arguments[1];
waitForAll=arguments[2];
}}
finishedCallback=finishedCallback||$.noop;
eachCallback=eachCallback||$.noop;
waitForAll = !! waitForAll;
if(typeof(finishedCallback)!=="function"||typeof(eachCallback)!=="function"){
throw new TypeError('An invalid callback was supplied.');
}
this.each(function (){
var obj=$(this);
if(waitForAll){
obj.find('*').addBack().each(function (){
var element=$(this);
if(element.is('img:has-src') &&
!element.is('[srcset]')){
allImgs.push({
src: element.attr('src'),
element: element[0]
});
}
$.each(hasImgProperties, function (i, property){
var propertyValue=element.css(property);
var match;
if(!propertyValue){
return true;
}
while (match=matchUrl.exec(propertyValue)){
allImgs.push({
src: match[2],
element: element[0]
});
}});
$.each(hasImageAttributes, function (i, attribute){
var attributeValue=element.attr(attribute);
var attributeValues;
if(!attributeValue){
return true;
}
allImgs.push({
src: element.attr('src'),
srcset: element.attr('srcset'),
element: element[0]
});
});
});
}else{
obj.find('img:has-src')
.each(function (){
allImgs.push({
src: this.src,
element: this
});
});
}});
allImgsLength=allImgs.length;
allImgsLoaded=0;
if(allImgsLength===0){
finishedCallback.call(originalCollection);
deferred.resolveWith(originalCollection);
}
$.each(allImgs, function (i, img){
var image=new Image();
var events =
'load.' + eventNamespace + ' error.' + eventNamespace;
$(image).one(events, function me (event){
var eachArguments=[
allImgsLoaded,
allImgsLength,
event.type=='load'
];
allImgsLoaded++;
eachCallback.apply(img.element, eachArguments);
deferred.notifyWith(img.element, eachArguments);
$(this).off(events, me);
if(allImgsLoaded==allImgsLength){
finishedCallback.call(originalCollection[0]);
deferred.resolveWith(originalCollection[0]);
return false;
}});
if(hasSrcset&&img.srcset){
image.srcset=img.srcset;
image.sizes=img.sizes;
}
image.src=img.src;
});
return deferred.promise();
};}));