forked from repp/BalancedGallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.balanced-gallery.min.js
1 lines (1 loc) · 6.4 KB
/
jquery.balanced-gallery.min.js
1
!function(t,e,n){"use strict";function i(e,n){I=this,this.element=e,this.elementChildren=t(e).children("*"),this.options=t.extend({},T,n),this.options.autoResize&&(this.unadulteratedHtml=t(this.element).html(),this.unadulteratedCSS=r(),this.unadulteratedOptions=t.extend({},this.options),o()),this.init(),this.createGallery()}function r(){var e=t(I.element);return{width:e[0].style.width,height:e[0].style.height,background:e.css("background"),paddingLeft:e.css("padding-left"),paddingTop:e.css("padding-top"),overflow:e.css("overflow"),fontSize:e.css("font-size")}}function o(){t(e).resize(function(){clearTimeout(G),G=setTimeout(function(){I.recreate()},500)})}function a(){var t,e,n;t=h(),0===t?I.fallbackToStandardSize():(e=f(),n=v(e,t),S(n))}function l(){var t,e,n;t=s(),0===t?I.fallbackToStandardSize():(e=g(),n=v(e,t),z(n),k(n))}function h(){return Math.round(d()/(I.options.viewportWidth-I.options.padding))}function s(){return Math.round(u()/(I.options.viewportHeight-I.options.padding))}function d(){var e=0;return I.elementChildren.each(function(){e+=p(t(this))}),e}function u(){var e=0;return I.elementChildren.each(function(){e+=c(t(this))}),e}function p(t){return W(t)*(I.options.idealHeight+I.options.padding)}function c(t){return 1/W(t)*(I.options.idealWidth+I.options.padding)}function f(){return I.elementChildren.map(function(){var e=parseInt(100*W(t(this)),R);return{element:this,weight:e}})}function g(){return I.elementChildren.map(function(){var e=parseInt(100*(1/W(t(this))),R);return{element:this,weight:e}})}function v(t,e){if(I.options.maintainOrder)return m(t,e);var n=y(t,e);return I.options.shuffleUnorderedPartitions&&(n=H(n)),C(n),n}function m(t,e){var n=t.length;if(0>=e)return[];if(e>=n)return t.map(function(t,e){return[[e]]});var i=w(t,e);n-=1,e-=2;for(var r=[];e>=0;){for(var o=[],a=i[n-1][e]+1;n+1>a;a++)o.push(t[a]);r=[o].concat(r),n=i[n-1][e],e-=1}for(var l=[],h=0;n+1>h;h++)l.push(t[h]);return[l].concat(r)}function w(t,e){for(var n=t.length,i=[],r=0;n>r;r++){for(var o=[],a=0;e>a;a++)o.push(0);i.push(o)}for(var l=[],h=0;n-1>h;h++){for(var s=[],d=0;e-1>d;d++)s.push(0);l.push(s)}for(var u=0;n>u;u++)i[u][0]=t[u].weight+(0!==u?i[u-1][0]:0);for(var p=0;e>p;p++)i[0][p]=t[0].weight;for(var c=function(t,e){return t[0]-e[0]},f=1;n>f;f++)for(var g=1;e>g;g++){for(var v=[],m=0;f>m;m++)v.push([Math.max(i[m][g-1],i[f][0]-i[m][0]),m]);var w=v.sort(c)[0];i[f][g]=w[0],l[f-1][g-1]=w[1]}return l}function y(t,e){for(var n=t.sort(function(t,e){return e.weight-t.weight}),i=new Array(e),r=0;e>r;r++)i[r]=[];for(var o=0;o<n.length;o++){for(var a=i[0],l=b(a),h=0;h<i.length;h++){var s=b(i[h]);l>s&&(a=i[h],l=s)}a.push(n[o])}return i}function b(e){var n=0;return t.each(e,function(t,e){n+=e.weight}),n}function H(t){for(var e=0;e<t.length;e++)t[e]=x(t[e]);return x(t)}function C(e){t(I.element).html("");for(var n=0;n<e.length;n++)for(var i=e[n],r=0;r<i.length;r++)t(I.element).append(i[r].element)}function S(e){for(var i=I.options.padding,r=0;r<e.length;r++){for(var o=0,a=0;a<e[r].length;a++)o+=W(t(e[r][a].element));for(var l=0;l<e[r].length;l++){var h=t(e[r][l].element),s=(I.options.viewportWidth-i)/o,d=parseInt(s,R),u=parseInt(d*W(h),R)-i;h.width(u),h.height(d),h.css({margin:0,marginRight:i+"px",marginBottom:i+"px"})}}I.element!==n.body&&t(I.element).css({overflow:"scroll"})}function k(e){for(var i=I.options.padding,r=0;r<e.length;r++){for(var o=0,a=0;a<e[r].length;a++)o+=1/W(t(e[r][a].element));for(var l=0;l<e[r].length;l++){var h=t(e[r][l].element),s=(I.options.viewportHeight-i)/o,d=parseInt(s,R),u=parseInt(d*(1/W(h)),R)-i;h.width(d),h.height(u),h.css({margin:0,marginRight:i+"px",marginBottom:i+"px"})}}t(".balanced-gallery-column").css({display:"inline-block",padding:0,margin:0}),t(I.container).width(function(){var e=0;return t(".balanced-gallery-column").each(function(){e+=t(this).width()}),e}()),I.element!==n.body&&t(I.element).css({overflowY:"hidden"}),I.options.viewportHeight!==I.element.clientHeight&&(I.options.viewportHeight=I.element.clientHeight-I.options.padding,t(I.element).height(I.options.viewportHeight-I.options.padding),I.element===n.body&&k(e))}function z(e){var i,r=t(I.element);r.html(""),r.css({overflow:"scroll"}),I.element!==n.body?(i=t('<div id="balanced-gallery-col-container"></div>'),r.append(i[0])):i=t(I.element),I.container=i[0];for(var o=0;o<e.length;o++){var a="balanced-gallery-col"+o,l='<div class="balanced-gallery-column" id="'+a+'"></div>';i.append(l);for(var h=0;h<e[o].length;h++){var s=e[o][h].element,d=t(i.find("div#"+a));d.append(s).append("<br />")}}}function W(t){var e=I.options.padding;return(t.width()+e)/(t.height()+e)}function x(t){for(var e,n,i=t.length;i--;)n=0|Math.random()*i,e=t[i],t[i]=t[n],t[n]=e;return t}var I,L="BalancedGallery",T={autoResize:!0,background:null,idealHeight:null,idealWidth:null,maintainOrder:!0,orientation:"horizontal",padding:5,shuffleUnorderedPartitions:!0,viewportHeight:null,viewportWidth:null},O="ALL_CHILDREN_LOADED",G=null,R=10;t.fn[L]=function(e){return this.each(function(){t.data(this,"plugin_"+L)||t.data(this,"plugin_"+L,new i(this,e))})},i.prototype.recreate=function(){t(this.element).on(O,function(){I.init(),I.createGallery()}),this.reset()},i.prototype.reset=function(){var e=this.elementChildren.length;t(this.element).html(this.unadulteratedHtml),t(this.element).css(this.unadulteratedCSS),this.options=t.extend({},this.unadulteratedOptions),this.elementChildren=t(this.element).children("*");var n=0;this.elementChildren.each(function(){t(this).load(function(){++n===e&&t(I.element).trigger(O)})})},i.prototype.init=function(){null===this.options.viewportWidth&&(this.options.viewportWidth=t(this.element).width()),null===this.options.viewportHeight&&(this.options.viewportHeight=t(this.element).height()),null===this.options.idealWidth&&(this.options.idealWidth=t(this.element).width()/4),null===this.options.idealHeight&&(this.options.idealHeight=t(this.element).height()/2),null!==this.options.background&&t(this.element).css({background:this.options.background}),this.elementChildren.css({display:"inline-block",padding:0,margin:0});var e=this.options.padding+"px";t(this.element).css({fontSize:0,paddingTop:e,paddingLeft:e})},i.prototype.createGallery=function(){var t=this.options.orientation.toLowerCase();if("horizontal"===t)a();else{if("vertical"!==t)throw"BalancedGallery: Invalid Orientation.";l()}},i.prototype.fallbackToStandardSize=function(){var e=this.options.idealHeight;this.elementChildren.each(function(){t(this).height(e),t(this).width(I.idealWidth(t(this)))})}}(jQuery,window,document);