//
// here's more elaborate, and user friendly, version
// works the same as previous,
// I've just added the possibility to subscribe funcions
// for load/error/abort/done events separately,
// and pass images as image array with the hash map of options to the funcion,
// and some cleanup code to remove unnecesary function handlers
// after download is done.
//
// use:
//
// cacheimages(
// {
// imgs : [
// 'http://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Flag_of_the_United_Nations.svg/2000px-Flag_of_the_United_Nations.svg.png',
// 'http://upload.wikimedia.org/wikipedia/commons/e/e5/IBM_Port-A-Punch.jpg',
// 'http://upload.wikimedia.org/wikipedia/commons/7/7e/Tim_Berners-Lee_CP_2.jpg',
// 'http://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/NewTux.svg/2000px-NewTux.svg.png',
// 'http://upload.wikimedia.org/wikipedia/commons/4/4c/Beekeeper_keeping_bees.jpg',
// 'http://upload.wikimedia.org/wikipedia/commons/9/9a/100607-F-1234S-004.jpg'
// ],
//
// load : function () { console.log( arguments, this ); },
// # triggered when single image is sucessfuly cached
// # @param1, ( string ), loaded image source path
// # @param2, ( event object ), native event object generated
// # context ( this ), Image object, target obj related with event
//
// error : function () { console.log( arguments, this ); },
// # triggered when error occured when tring to download image
// # @param1, ( string ), path to the image failed to load
// # @param2, ( event object ), native event object describing the circumstance
// # context ( this ), Image object, target obj related with event
//
// abort : function () { console.log( arguments, this ); },
// # triggered when download is haulted by user action ( browsers 'stop' button )
// # @param1, ( string ), path to the image failed to load
// # @param2, ( event object ), native event object generated
// # context ( this ), Image object, target obj related with event
//
// done : function () { console.log( arguments, this ); }
// # triggered after download proccess completes
// # @params, ( string(s) ), images in question
// # context ( this ), document
// }
// );
//
;(( function ( methodName, dfn ) {
// add "cacheimages" function identifier to target context ( window )
this[methodName] = dfn();
} ).call(
self,
"cacheimages",
function () {
var emptyfn = function () {};
var isfn = function ( o ) {
return typeof o === "function";
};
var isplainobj = function ( o ) {
return Object.prototype.toString.call( o ) === "[object Object]";
};
var isarray = function ( o ) {
return Object.prototype.toString.call( o ) === "[object Array]";
};
var defaults = {
load : emptyfn,
error : emptyfn,
abort : emptyfn,
done : emptyfn
};
var imgreg = /\.(?:jpe?g|jpe|png|gif|bmp|tiff?|tga|iff)$/i;
var events = ["onload", ,"onerror" ,"onabort"];
var _F = function ( settup ) {
if (
isplainobj( settup )
&& (
isarray( settup.imgs )
&& ( settup.imgs.length > 0 )
)
) {
var pics = [];
var n = 0;
var opts = {
load : isfn( settup.load ) ? settup.load : defaults.load,
error : isfn( settup.error ) ? settup.error : defaults.error,
abort : isfn( settup.abort ) ? settup.abort : defaults.abort,
done : isfn( settup.done ) ? settup.done : defaults.done,
imgs : settup.imgs.filter(
function ( imgstr ) {
return imgreg.test( String( imgstr ) );
}
)
};
var loadhandler;
if (
opts.imgs.length == 0
) {
opts.done.call( document );
return [];
}
loadhandler = function ( e ) {
e || ( e = window.event );
n += 1;
if ( n < opts.imgs.length ) {
opts[ e.type ].call( this, this.src, e );
// console && console.log( e.type, ' --> [ ', this.src, ' ].' );
} else {
opts.done.apply( document, opts.imgs );
pics.forEach(
function ( imgobj ) {
events.forEach(
function ( vnt ) {
imgobj[vnt] = null;
}
);
}
);
}
};
opts.imgs.forEach(
function ( imgstr, i ) {
pics[i] = new Image;
events.forEach(
function ( vnt ) {
pics[i][vnt] = loadhandler;
}
);
pics[i].src = imgstr;
}
);
return opts.imgs.concat();
} else {
return false;
}
};
return _F;
}
));
//