forked from tin-cat/jquery-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mosaic.min.js
executable file
·1 lines (1 loc) · 3.91 KB
/
jquery.mosaic.min.js
1
!function($){$.Mosaic=function(a,b){var d,c=this;c.el=a,c.$el=$(a),c.$el.data("Mosaic",c);var e,f=!1;c.init=function(){c.options=d=$.extend({},$.Mosaic.defaults,b),$(c.el).addClass("mosaic"),d.outerMargin&&$(c.el).css("padding",d.outerMargin),d.innerGap&&$(c.el).css("margin-bottom",d.innerGap*-1),c.getItems().each(function(a,b){$(b).attr("width")&&$(b).css("width",$(b).attr("width")),$(b).attr("height")&&$(b).css("height",$(b).attr("height"))}),c.fit(),d.refitOnResize&&$(window).on("resize",null,null,function(){d.refitOnResizeDelay?(f&&clearTimeout(f),f=setTimeout(function(){c.fit()},d.refitOnResizeDelay)):c.fit()})},c.getItems=function(){return $("> div:not([data-no-mosaic=true]), > a:not([data-no-mosaic=true]), > img:not([data-no-mosaic=true])",c.el)},c.getItemAtIndex=function(a){return!!c.getItems()[a]&&$(c.getItems()[a])},c.getItemsSubset=function(a,b){var d=c.getItems();return!(a>d.length)&&(a+b>d.length&&(b=d.length-a),d.slice(a,a+b))},c.isLastItemsSubset=function(a,b){var d=c.getItems();return a>d.length||a+b>d.length},c.getItemWidth=function(a){return $(a).outerWidth()?$(a).outerWidth():$(a).attr("width")?$(a).attr("width"):void 0},c.getItemHeight=function(a){return $(a).outerHeight()?$(a).outerHeight():$(a).attr("height")?$(a).attr("height"):void 0},c.getItemAspectRatio=function(a){if($(a).data("aspect-ratio"))return $(a).data("aspect-ratio");if(c.getItemWidth(a)&&c.getItemHeight(a)){var b=c.getItemWidth(a)/c.getItemHeight(a);return $(a).data("aspect-ratio",b),b}return d.defaultAspectRatio},c.getItemWidthForGivenHeight=function(a,b){return b*c.getItemAspectRatio(a)},c.getItemHeightForGivenWidth=function(a,b){return b/c.getItemAspectRatio(a)},c.setItemSizeByGivenHeight=function(a,b){var e=Math.floor(c.getItemWidthForGivenHeight(a,b));if($(a).css("height",Math.floor(b)+"px").css("width",e+"px"),d.highResImagesWidthThreshold)if(e>d.highResImagesWidthThreshold){var f=$(a).data("high-res-background-image-url");f&&!$(a).data("low-res-background-image-url")&&($(a).data("low-res-background-image-url",$(a).css("background-image")),$(a).css("background-image",'url("'+f+'")'),$(a).addClass("highRes"));var g=$(a).data("high-res-image-src");g&&!$(a).data("low-res-image-src")&&($(a).data("low-res-image-src",$(a).attr("src")),$(a).attr("src",g),$(a).addClass("highRes"))}else{var h=$(a).data("low-res-background-image-url");h&&($(a).css("background-image",h),$(a).data("low-res-background-image-url",!1),$(a).removeClass("highRes"));var i=$(a).data("low-res-image-src");i&&($(a).attr("src",i),$(a).data("low-res-image-src",!1),$(a).removeClass("highRes"))}return e},c.calculateHeightToFit=function(a){var b=0;return a.each(function(){b+=parseFloat(c.getItemAspectRatio(this))}),(e-d.innerGap*(a.length-1))/b},c.retrieveBaseWidth=function(){e=Math.floor($(c.el).width())},c.fit=function(){c.retrieveBaseWidth();for(var a,b,e=1,f=0,g=!1;;){if(a=c.getItemsSubset(f,e),c.isLastItemsSubset(f,e)){a.length&&c.fitItems(a);break}b=c.calculateHeightToFit(a),b>d.maxRowHeight?e++:(c.fitItems(a),f+=e,e=1,g=!0)}g||c.fitItems(c.getItems())},c.fitItems=function(a){var b=c.calculateHeightToFit(a);if(b>d.maxRowHeight)switch(d.maxRowHeightPolicy){case"skip":return void a.each(function(){$(this).hide()});case"crop":b=d.maxRowHeight;break;case"oversize":}a.each(function(){$(this).show()});var f=0;if(a.each(function(e){f+=c.setItemSizeByGivenHeight(this,b),d.innerGap&&($(this).css("margin-right",e<a.length-1?d.innerGap:0),$(this).css("margin-bottom",d.innerGap))}),f!=e-(a.length-1)*d.innerGap){difference=e-(a.length-1)*d.innerGap-f;var g=a.last().width();a.last().width(g+difference)}},c.init()},$.Mosaic.defaults={maxRowHeight:400,refitOnResize:!0,refitOnResizeDelay:!1,defaultAspectRatio:1,maxRowHeightPolicy:"skip",highResImagesWidthThreshold:350,outerMargin:0,innerGap:0},$.fn.Mosaic=function(options,params){return this.each(function(){var me=$(this).data("Mosaic");(typeof options).match("object|undefined")?new $.Mosaic(this,options):eval("me."+options)(params)})}}(jQuery);