-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.brickfolio.min.js
9 lines (9 loc) · 5.37 KB
/
jquery.brickfolio.min.js
1
2
3
4
5
6
7
8
9
/**!
* Brickfolio - A jQuery plugin for equally spaced grid layouts
* @version 0.0.5
* @link https://github.com/fooplugins/brickfolio
* @copyright Steven Usher & Brad Vincent 2014
* @license Released under the GPL license.
* You are free to use Brickfolio in personal projects as long as this copyright header is left intact.
*/
!function(a){function b(c,d){if(!(this instanceof b))return new b(c,d);var e=this,f={};return e.defaults={animation:"",filter:"",itemSelector:".bf-item",imageSelector:"img:first",gutter:40,responseTime:100,hideErrors:!1,loadTime:0,classes:{container:"brickfolio",loaded:"bf-loaded",animated:"bf-animated",item:"bf-item",error:"bf-error",filtered:"bf-filtered"}},e.$el=a(c),e.options=a.extend(!0,e.defaults,d),e.reinit=function(b){a(window).off("resize.brickfolio",f.onWindowResize),e.$el.removeClass([e.options.animation,e.options.classes.animated,e.options.classes.loaded].join(" ")).find(e.options.itemSelector).removeClass([e.options.classes.loaded,e.options.classes.error,e.options.classes.filtered].join(" ")).css("visibility","hidden"),e.options=a.extend(!0,e.options,b),f.init()},e.filter=function(a){e.options.filter=a,e.layout()},e.layout=function(){var a=e.$el.find(e.options.itemSelector);a=f.filter(a),f.layout(a),a.css("visibility","")},f.loader=null,f.layout_timer=null,f.resize_timer=null,f.isIE=null,f.init=function(){"static"==e.$el.css("position")&&e.$el.css("position","relative"),e.$el.addClass(e.options.classes.container).css("overflow","hidden");var b=e.options.animation.replace(/\s*mixed-delay\s*/g," ").replace(/^\s+|\s+$/g,"");f.supportsAnimation()&&"string"==typeof b&&b.length>0&&e.$el.addClass(e.options.classes.animated).addClass(e.options.animation);var c=e.$el.find(e.options.itemSelector).addClass(e.options.classes.item).css({position:"absolute",display:"inline-block",margin:0,visibility:"hidden"});return c.length>0&&f.wait(c).always(function(){c=f.filter(c),f.layout(c),c.css("visibility",""),a(window).on("resize.brickfolio",f.onWindowResize),e.$el.addClass(e.options.classes.loaded)}),e},f.onWindowResize=function(){null!=f.resize_timer&&clearTimeout(f.resize_timer),f.resize_timer=setTimeout(function(){f.resize_timer=null,e.layout()},e.options.responseTime)},f.damnYouIE=function(a){null==f.isIE&&(f.isIE=window.navigator.userAgent.indexOf("MSIE ")>0||!!navigator.userAgent.match(/Trident.*rv\:11\./)),1==f.isIE&&a.attr("src",a.attr("src"))},f.supportsAnimation=function(){var a,b=document.body||document.documentElement,c=b.style,d="animation";if("string"==typeof c[d])return!0;a=["Moz","Webkit","Khtml","O","ms"],d=d.charAt(0).toUpperCase()+d.substr(1);for(var e=0;e<a.length;e++)if("string"==typeof c[a[e]+d])return!0;return!1},f.filter=function(a){return a.removeClass(e.options.classes.filtered).css({visibility:"",opacity:"",animation:"","-webkit-animation":""}),e.options.hideErrors&&a.filter("."+e.options.classes.error).addClass(e.options.classes.filtered).css({visibility:"hidden",opacity:0,animation:"none","-webkit-animation":"none"}),"string"==typeof e.options.filter&&e.options.filter.length>0&&a.not(e.options.filter).addClass(e.options.classes.filtered).css({visibility:"hidden",opacity:0,animation:"none","-webkit-animation":"none"}),a.not("."+e.options.classes.filtered)},f.wait=function(b){var c=[];return b.each(function(){var b=a(this),d=b.find(e.options.imageSelector),g=new a.Deferred(function(a){setTimeout(function(){if(0==d.length)a.resolve();else if(1==d.get(0).complete){var c=d.get(0);"naturalHeight"in c&&"naturalWidth"in c&&0==c.naturalHeight&&0==c.naturalWidth?b.addClass(e.options.classes.error):b.addClass(e.options.classes.loaded),a.resolve()}else d.on({"load.brickfolio":function(){d.off(".brickfolio"),b.addClass(e.options.classes.loaded),a.resolve()},"error.brickfolio":function(){d.off(".brickfolio"),b.addClass(e.options.classes.error),a.resolve()}}),f.damnYouIE(d)},e.options.loadTime)});c.push(g)}),a.when.apply(a,c)},f.layout=function(b){var c=b.first(":not(."+e.options.classes.error+")").outerWidth(),d=e.$el.width(),g=parseInt(e.$el.css("paddingLeft")),h=parseInt(e.$el.css("paddingTop")),i={height:0,outer:0},j=[],k=e.options.gutter,l=h,m=Math.floor(d/c);m=Math.floor((d-(m-1)*e.options.gutter)/c),m=0>=m?1:m,e.options.hideErrors&&(b=b.not("."+e.options.classes.error)),b.each(function(b){var h=a(this);h.css("height","");var n={height:h.height(),outer:h.outerHeight()};b%m==0?(j.length>0&&(k=f.update(j,i.height,c,d,m,k,l,g),l+=i.outer+e.options.gutter),j.length=0,i=n,j.push(h)):(i=n.height>i.height?n:i,j.push(h))}),f.update(j,i.height,c,d,m,k,l,g),l+=i.outer-h,e.$el.height(l),f.layout_timer&&clearTimeout(f.layout_timer),f.layout_timer=setTimeout(function(){f.layout_timer=null,d!=e.$el.width()&&f.layout(b)},600)},f.update=function(a,b,c,d,e,f,g,h){if(0==a.length)return f;var i=a.length<e||a.length<=2,j=d-a.length*c;if(1==a.length)a[0].height(b),h+=j/2,a[0].css({top:g,left:h});else{f=i?f:Math.floor(j/(a.length-1)),h+=i?Math.floor((j-(a.length-1)*f)/2):0;for(var k=0;k<a.length;k++)a[k].height(b),a[k].css({top:g,left:h+c*k+f*k})}return f},f.init()}a.fn.brickfolio=function(c,d,e){if("string"==typeof c){var f=Array.prototype.slice.call(arguments),g=f.shift(),h=function(b){return a.isFunction(b[g])?b[g]:a.noop};return this.each(function(){this.__brickfolio__ instanceof b&&h(this.__brickfolio__).apply(this.__brickfolio__,f)})}return this.each(function(){this.__brickfolio__ instanceof b?this.__brickfolio__.reinit(c):this.__brickfolio__=new b(this,c)})}}(jQuery);