-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.prefetch.min.js
2 lines (2 loc) · 3.22 KB
/
jquery.prefetch.min.js
1
2
/*! jQuery Prefetch 0.1.3 - MIT & GPL-2.0 license - Copyright 2012-2015 Daniel 'Eisbehr' Kern */
!function(t){"use strict";function i(i,n){return n&&t.extend(this.configuration,n),this.addImage(i),this._initialize()}t.Prefetch=t.prefetch=function(t,n){return new i(t,n)},t.fn.Prefetch=t.fn.prefetch=function(i){var n=t.prefetch(this,i);return n.configuration.chainable?this:n},t.extend(i.prototype,{_images:[],_running:!1,_runningAmount:0,configuration:{chainable:!0,startAutomatically:!0,delay:0,simultaneous:3,objectProperty:"image",imagesBasePath:null,onStartLoading:null,getImageSource:function(t,i){return i.configuration.imagesBasePath?i.configuration.imagesBasePath+t:t},onImageLoaded:null,onImageError:null,onAbortLoading:null,onAllLoaded:null},_initialize:function(){var t=this;return this._images.length>0&&this.configuration.startAutomatically&&setTimeout(function(){t.start()},this.configuration.delay),this},_checkForNextImage:function(){if(this._running&&!(this.configuration.simultaneous>0&&this._runningAmount>=this.configuration.simultaneous)){for(var t=!1,i=0;i<this._images.length;++i)if(!this._images[i].running&&!this._images[i].loaded){t=!0,this._preloadImage(this._images[i]);break}t||this.getLoadedAmount()!=this.getImagesAmount()||(this._running=!1,this._triggerCallback(this.configuration.onAllLoaded))}},_preloadImage:function(i){i.running=!0,++this._runningAmount;var n=this,e=t(new Image);e.error(function(){n._finishImage(i,!1)}),e.one("load",function(){e.unbind("load").remove(),n._finishImage(i,!0)}),e.attr("src",this._triggerCallback(this.configuration.getImageSource,i.file)),e.complete&&e.load()},_finishImage:function(t,i){t.loaded=!0,t.running=!1,--this._runningAmount,i?this._triggerCallback(this.configuration.onImageLoaded,t.file):this._triggerCallback(this.configuration.onImageError,t.file),this._checkForNextImage()},_triggerCallback:function(t,i){return t?i?t(i,this):t(this):null},start:function(){if(!this._running&&this.getLoadedAmount()<this.getImagesAmount()){this._running=!0,this._triggerCallback(this.configuration.onStartLoading);for(var t=this._runningAmount;(this.configuration.simultaneous<=0||t<this.configuration.simultaneous)&&t<this._images.length;t++)this._checkForNextImage()}return this},stop:function(){return this._running&&(this._running=!1,this._triggerCallback(this.configuration.onAbortLoading)),this},getImagesAmount:function(){return this._images.length},getLoadedAmount:function(){for(var t=0,i=0;i<this._images.length;++i)i in this._images&&this._images[i].loaded&&++t;return t},addImage:function(i){var n={file:"",running:!1,loaded:!1},e=Object.prototype.toString.call(i);if("[object Array]"==e)for(var r=0;r<i.length;++r)r in i&&this._images.push(jQuery.extend({},n,{file:i[r][this.configuration.objectProperty]||i[r]}));else if("[object Object]"==e){var a=this;t.each(i,function(){var i=t(this);(this.hasOwnProperty(a.configuration.objectProperty)||i.attr("src")||i.css("background-image"))&&a._images.push(jQuery.extend({},n,{file:this[a.configuration.objectProperty]||i.attr("src")||i.css("background-image").replace(/^url\(["']?/,"").replace(/["']?\)$/,"")}))})}else"[object String]"==e&&this._images.push(jQuery.extend({},n,{file:i}));return this},destroy:function(){return this.stop(),this._images=[],this}})}(jQuery);