-
Notifications
You must be signed in to change notification settings - Fork 33
/
jquery.mosaic.min.js
executable file
·1 lines (1 loc) · 4.88 KB
/
jquery.mosaic.min.js
1
!function($){$.Mosaic=function(e,t){var n,o,r=this;r.el=e,r.$el=$(e),r.$el.data("Mosaic",r);var i=!1;r.init=function(){r.options=n=$.extend({},$.Mosaic.defaults,r.$el.data(),t),$(r.el).addClass("jQueryMosaic"),n.outerMargin&&$(r.el).css("padding",n.outerMargin),n.innerGap&&$(r.el).css("margin-bottom",-1*n.innerGap),r.getItems().each(function(e,t){$(t).attr("width")&&$(t).width($(t).attr("width")),$(t).attr("height")&&$(t).height($(t).attr("height"))}),r.fit(),n.refitOnResize&&$(window).on("resize",null,null,function(){$(r.el).is(":hidden")||(n.refitOnResizeDelay?(i&&clearTimeout(i),i=setTimeout(function(){r.fit()},n.refitOnResizeDelay)):r.fit())})},r.getItems=function(){return $("> div:not([data-no-mosaic=true]), > a:not([data-no-mosaic=true]), > img:not([data-no-mosaic=true]), > video:not([data-no-mosaic=true])",r.el)},r.getItemAtIndex=function(e){return!!r.getItems()[e]&&$(r.getItems()[e])},r.getItemsSubset=function(e,t){var i=r.getItems();return t||(t=i.length-e),!(e>i.length)&&(e+t>i.length&&(t=i.length-e),i.slice(e,e+t))},r.isLastItemsSubset=function(e,t){var i=r.getItems();return e>i.length||e+t>i.length},r.getItemWidth=function(e){return $(e).outerWidth()?$(e).outerWidth():$(e).attr("width")?$(e).attr("width"):void 0},r.getItemHeight=function(e){return $(e).outerHeight()?$(e).outerHeight():$(e).attr("height")?$(e).attr("height"):void 0},r.getItemAspectRatio=function(e){if($(e).data("aspect-ratio"))return $(e).data("aspect-ratio");if(r.getItemWidth(e)&&r.getItemHeight(e)){var t=r.getItemWidth(e)/r.getItemHeight(e);return $(e).data("aspect-ratio",t),t}return n.defaultAspectRatio},r.getItemWidthForGivenHeight=function(e,t){return t*r.getItemAspectRatio(e)},r.getItemHeightForGivenWidth=function(e,t){return t/r.getItemAspectRatio(e)},r.setItemSizeByGivenHeight=function(e,t,i){var s=Math.floor(r.getItemWidthForGivenHeight(e,t));return $(e).width(s+"px"),i?$(e).height("auto"):$(e).height(Math.floor(t)),r.handleHighResForItem(e),s},r.handleHighResForItem=function(e){if(n.highResImagesWidthThreshold&&$(e).width()>n.highResImagesWidthThreshold){var t=$("[data-high-res-background-image-url]",e);$(e).data("high-res-background-image-url")&&(t=$(t).add(e)),$(t).each(function(){$(this).css("background-image",'url("'+$(this).data("high-res-background-image-url")+'"'),$(this).addClass("highRes")});t=$("[data-high-res-image-src]",e);$(e).data("high-res-image-src")&&(t=$(t).add(e)),$(t).each(function(){$(this).attr("src",$(this).data("high-res-image-src")),$(this).addClass("highRes")})}},r.calculateHeightToFit=function(e){var t=0;return e.each(function(){t+=parseFloat(r.getItemAspectRatio(this))}),(o-n.innerGap*(e.length-1))/t},r.retrieveBaseWidth=function(){var e=parseFloat($(r.el).width());o=$.isNumeric(e)?e:0},r.isBelowResponsiveWidthThreshold=function(){return n.responsiveWidthThreshold&&o<n.responsiveWidthThreshold},r.fit=function(){if(r.retrieveBaseWidth(),r.isBelowResponsiveWidthThreshold()){var e=r.getItems();return n.maxItemsToShowWhenResponsiveThresholdSurpassed&&($(e).slice(n.maxItemsToShowWhenResponsiveThresholdSurpassed).remove(),e=$(e).slice(0,n.maxItemsToShowWhenResponsiveThresholdSurpassed)),void e.each(function(){var e=r.getItemHeightForGivenWidth(this,o);$(this).width(o),$(this).data("only-force-height-when-necessary")?$(this).height("auto"):$(this).height(e),n.innerGap&&$(this).css("margin-bottom",n.innerGap)})}for(var t=1,i=0,s=!1,a=1;;){if(e=r.getItemsSubset(i,t),r.isLastItemsSubset(i,t)){e.length&&r.fitItems(e);break}if(r.calculateHeightToFit(e)>n.maxRowHeight)t++;else{if(r.fitItems(e),i+=t,t=1,s=!0,!r.isBelowResponsiveWidthThreshold()&&n.maxRows&&a==n.maxRows)return void r.getItemsSubset(i).each(function(){$(this).hide()});a++}}s||(n.showTailWhenNotEnoughItemsForEvenOneRow&&(n.maxRowHeightPolicy="tail"),r.fitItems(r.getItems()))},r.fitItems=function(t){var i=!1,s=r.calculateHeightToFit(t);if(s>n.maxRowHeight)switch(n.maxRowHeightPolicy){case"skip":return void t.each(function(){$(this).hide()});case"crop":s=n.maxRowHeight;break;case"oversize":i=!0;break;case"tail":s=n.maxRowHeight;var e=!0}t.each(function(){$(this).show()});var a=0;if(t.each(function(e){a+=r.setItemSizeByGivenHeight(this,s,i&&$(this).data("only-force-height-when-necessary")),n.innerGap&&($(this).css("margin-right",e<t.length-1?n.innerGap:0),$(this).css("margin-bottom",n.innerGap))}),!e&&a!=o-(t.length-1)*n.innerGap){difference=o-(t.length-1)*n.innerGap-a;var h=t.last().width();t.last().width(h+difference)}},r.init()},$.Mosaic.defaults={maxRowHeight:400,refitOnResize:!0,refitOnResizeDelay:!1,defaultAspectRatio:1,maxRowHeightPolicy:"skip",maxRows:!1,highResImagesWidthThreshold:350,outerMargin:0,innerGap:0,responsiveWidthThreshold:!1,maxItemsToShowWhenResponsiveThresholdSurpassed:!1,showTailWhenNotEnoughItemsForEvenOneRow:!1},$.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);