diff --git a/.gitignore b/.gitignore index c2658d7..7b132aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +tests/ diff --git a/bower.json b/bower.json index ab61e92..3bb13bf 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "voila", - "version": "1.0.1", + "version": "1.0.2", "description": "A jQuery plugin that provides callbacks for images, letting you know when they've loaded.", "keywords": [ "image", diff --git a/package.json b/package.json index 4bc4122..f7c7568 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "voila", "title": "Voilà", - "version": "1.0.1", + "version": "1.0.2", "description": "A jQuery plugin that provides callbacks for images, letting you know when they've loaded.", "keywords": [ "image", diff --git a/src/voila.js b/src/voila.js index 8dafa40..9952339 100644 --- a/src/voila.js +++ b/src/voila.js @@ -1,16 +1,21 @@ -function Voila(elements, options, callback) { +function Voila(elements, opts, cb) { if (!(this instanceof Voila)) { - return new Voila(elements, options, callback); + return new Voila(elements, opts, cb); } + var argTypeOne = $.type(arguments[1]), + options = (argTypeOne === 'object' ? arguments[1] : {}); + callback = argTypeOne === 'function' ? arguments[1] : + $.type(arguments[2]) === 'function' ? arguments[2] : false; + this.options = $.extend({ render: true - }, $.type(options) == 'object' ? options : callback || {}); + }, options); this.deferred = new jQuery.Deferred(); // if there's a callback, push it onto the stack - if ($.type(callback) == 'function') { + if (callback) { this.always(callback); } diff --git a/voila.pkgd.js b/voila.pkgd.js index cd9e345..e930f2c 100644 --- a/voila.pkgd.js +++ b/voila.pkgd.js @@ -1,5 +1,5 @@ /*! - * Voilà - v1.0.1 + * Voilà - v1.0.2 * (c) 2014 Nick Stakenburg * * MIT License @@ -22,19 +22,24 @@ var Supports = { })() }; -function Voila(elements, options, callback) { +function Voila(elements, opts, cb) { if (!(this instanceof Voila)) { - return new Voila(elements, options, callback); + return new Voila(elements, opts, cb); } + var argTypeOne = $.type(arguments[1]), + options = (argTypeOne === 'object' ? arguments[1] : {}); + callback = argTypeOne === 'function' ? arguments[1] : + $.type(arguments[2]) === 'function' ? arguments[2] : false; + this.options = $.extend({ render: true - }, $.type(options) == 'object' ? options : callback || {}); + }, options); this.deferred = new jQuery.Deferred(); // if there's a callback, push it onto the stack - if ($.type(callback) == 'function') { + if (callback) { this.always(callback); } diff --git a/voila.pkgd.min.js b/voila.pkgd.min.js index 76baad6..de784e6 100644 --- a/voila.pkgd.min.js +++ b/voila.pkgd.min.js @@ -1,7 +1,7 @@ /*! - * Voilà - v1.0.1 + * Voilà - v1.0.2 * (c) 2014 Nick Stakenburg * * MIT License */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):jQuery&&!window.Voila&&(window.Voila=a(jQuery))}(function(a){function b(c,d,e){return this instanceof b?(this.options=a.extend({render:!0},"object"==a.type(d)?d:e||{}),this.deferred=new jQuery.Deferred,"function"==a.type(e)&&this.always(e),this._processed=0,this.images=[],this._add(c),this):new b(c,d,e)}var c=Array.prototype.slice,d={naturalWidth:function(){return"naturalWidth"in new Image}()};a.extend(b.prototype,{_add:function(b){var c="string"==a.type(b)?a(b):b instanceof jQuery||b.length>0?b:[b];a.each(c,a.proxy(function(b,c){var d=a(),f=a(c);d=d.add(f.is("img")?f:f.find("img")),d.each(a.proxy(function(b,c){this.images.push(new e(c,a.proxy(function(a){this._progress(a)},this),a.proxy(function(a){this._progress(a)},this),this.options))},this))},this)),this.images.length<1&&setTimeout(a.proxy(function(){this._resolve()},this))},abort:function(){this._progress=this._notify=this._reject=this._resolve=function(){},a.each(this.images,function(a,b){b.abort()}),this.images=[]},_progress:function(a){this._processed++,a.isLoaded||(this._broken=!0),this._notify(a),this._processed==this.images.length&&this[this._broken?"_reject":"_resolve"]()},_notify:function(a){this.deferred.notify(this,a)},_reject:function(){this.deferred.reject(this)},_resolve:function(){this.deferred.resolve(this)},always:function(a){return this.deferred.always(a),this},done:function(a){return this.deferred.done(a),this},fail:function(a){return this.deferred.fail(a),this},progress:function(a){return this.deferred.progress(a),this}}),a.fn.voila=function(){return b.apply(b,[this].concat(c.call(arguments)))};var e=function(){return this.initialize.apply(this,c.call(arguments))};return a.extend(e.prototype,{initialize:function(b,c,e){return this.img=a(b)[0],this.successCallback=c,this.errorCallback=e,this.isLoaded=!1,this.options=a.extend({render:!0,tickFallbackAfter:1e3},arguments[3]||{}),this.img.complete&&"undefined"!=a.type(this.img.naturalWidth)?void setTimeout(a.proxy(function(){this.img.naturalWidth>0?(this.isLoaded=!0,this.successCallback(this)):this.errorCallback(this)},this)):!d.naturalWidth||this.options.render?void setTimeout(a.proxy(this.fallback,this)):(a(this.img).bind("error",a.proxy(function(){setTimeout(a.proxy(function(){this.error()},this))},this)),this.intervals=[[1e3,10],[2e3,50],[4e3,100],[2e4,500]],this._ipos=0,this._time=0,this._delay=this.intervals[this._ipos][1],void this.poll())},poll:function(){this._ticking=setTimeout(a.proxy(function(){if(this.img.naturalWidth>0)return void this.success();if(this._time+=this._delay,this.options.pollFallbackAfter&&this._time>=this.options.pollFallbackAfter&&!this._usedPollFallback&&(this._usedPollFallback=!0,this.fallback()),this._time>this.intervals[this._ipos][0]){if(!this.intervals[this._ipos+1])return void this.error();this._ipos++,this._delay=this.intervals[this._ipos][1]}this.poll()},this),this._delay)},fallback:function(){var b=new Image;this._fallbackImg=b,b.onload=a.proxy(function(){b.onload=function(){},d.naturalWidth||(this.img.naturalWidth=b.width,this.img.naturalHeight=b.height),this.success()},this),b.onerror=a.proxy(this.error,this),b.src=a(this.img).attr("src")},abort:function(){this._fallbackImg&&(this._fallbackImg.onload=function(){}),this._ticking&&(clearTimeout(this._ticking),this._ticking=0)},success:function(){this._calledSuccess||(this._calledSuccess=!0,this.isLoaded=!0,this.successCallback(this))},error:function(){this._calledError||(this._calledError=!0,this.abort(),this.errorCallback&&this.errorCallback(this))}}),b}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):jQuery&&!window.Voila&&(window.Voila=a(jQuery))}(function(a){function b(c,d,e){if(!(this instanceof b))return new b(c,d,e);var f=a.type(arguments[1]),g="object"===f?arguments[1]:{};return callback="function"===f?arguments[1]:"function"===a.type(arguments[2])?arguments[2]:!1,this.options=a.extend({render:!0},g),this.deferred=new jQuery.Deferred,callback&&this.always(callback),this._processed=0,this.images=[],this._add(c),this}var c=Array.prototype.slice,d={naturalWidth:function(){return"naturalWidth"in new Image}()};a.extend(b.prototype,{_add:function(b){var c="string"==a.type(b)?a(b):b instanceof jQuery||b.length>0?b:[b];a.each(c,a.proxy(function(b,c){var d=a(),f=a(c);d=d.add(f.is("img")?f:f.find("img")),d.each(a.proxy(function(b,c){this.images.push(new e(c,a.proxy(function(a){this._progress(a)},this),a.proxy(function(a){this._progress(a)},this),this.options))},this))},this)),this.images.length<1&&setTimeout(a.proxy(function(){this._resolve()},this))},abort:function(){this._progress=this._notify=this._reject=this._resolve=function(){},a.each(this.images,function(a,b){b.abort()}),this.images=[]},_progress:function(a){this._processed++,a.isLoaded||(this._broken=!0),this._notify(a),this._processed==this.images.length&&this[this._broken?"_reject":"_resolve"]()},_notify:function(a){this.deferred.notify(this,a)},_reject:function(){this.deferred.reject(this)},_resolve:function(){this.deferred.resolve(this)},always:function(a){return this.deferred.always(a),this},done:function(a){return this.deferred.done(a),this},fail:function(a){return this.deferred.fail(a),this},progress:function(a){return this.deferred.progress(a),this}}),a.fn.voila=function(){return b.apply(b,[this].concat(c.call(arguments)))};var e=function(){return this.initialize.apply(this,c.call(arguments))};return a.extend(e.prototype,{initialize:function(b,c,e){return this.img=a(b)[0],this.successCallback=c,this.errorCallback=e,this.isLoaded=!1,this.options=a.extend({render:!0,tickFallbackAfter:1e3},arguments[3]||{}),this.img.complete&&"undefined"!=a.type(this.img.naturalWidth)?void setTimeout(a.proxy(function(){this.img.naturalWidth>0?(this.isLoaded=!0,this.successCallback(this)):this.errorCallback(this)},this)):!d.naturalWidth||this.options.render?void setTimeout(a.proxy(this.fallback,this)):(a(this.img).bind("error",a.proxy(function(){setTimeout(a.proxy(function(){this.error()},this))},this)),this.intervals=[[1e3,10],[2e3,50],[4e3,100],[2e4,500]],this._ipos=0,this._time=0,this._delay=this.intervals[this._ipos][1],void this.poll())},poll:function(){this._ticking=setTimeout(a.proxy(function(){if(this.img.naturalWidth>0)return void this.success();if(this._time+=this._delay,this.options.pollFallbackAfter&&this._time>=this.options.pollFallbackAfter&&!this._usedPollFallback&&(this._usedPollFallback=!0,this.fallback()),this._time>this.intervals[this._ipos][0]){if(!this.intervals[this._ipos+1])return void this.error();this._ipos++,this._delay=this.intervals[this._ipos][1]}this.poll()},this),this._delay)},fallback:function(){var b=new Image;this._fallbackImg=b,b.onload=a.proxy(function(){b.onload=function(){},d.naturalWidth||(this.img.naturalWidth=b.width,this.img.naturalHeight=b.height),this.success()},this),b.onerror=a.proxy(this.error,this),b.src=a(this.img).attr("src")},abort:function(){this._fallbackImg&&(this._fallbackImg.onload=function(){}),this._ticking&&(clearTimeout(this._ticking),this._ticking=0)},success:function(){this._calledSuccess||(this._calledSuccess=!0,this.isLoaded=!0,this.successCallback(this))},error:function(){this._calledError||(this._calledError=!0,this.abort(),this.errorCallback&&this.errorCallback(this))}}),b}); \ No newline at end of file