forked from nitinhayaran/Justified.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.justified.min.js
1 lines (1 loc) · 3.54 KB
/
jquery.justified.min.js
1
!function(t,i,h,e){"use strict";function o(i,h){this.element=i,this.$el=t(i),this._name=s,this.init(h)}var s="justifiedImages";o.prototype={defaults:{template:function(t){return'<div class="photo-container" style="height:'+t.displayHeight+"px;margin-right:"+t.marginRight+'px;"><img class="image-thumb" src="'+t.src+'" style="width:'+t.displayWidth+"px;height:"+t.displayHeight+'px;" ></div>'},appendBlocks:function(){return[]},rowHeight:150,maxRowHeight:350,handleResize:!1,margin:1,imageSelector:"image-thumb",imageContainer:"photo-container"},init:function(i){this.options=t.extend({},this.defaults,i),this.displayImages(),this.options.handleResize&&this.handleResize()},getBlockInRow:function(t){for(var i=this.options.appendBlocks(),h=0;h<i.length;h++){var e=i[h];if(e.rowNum===t)return e}},displayImages:function(){var i=this,h=[],e=0,o=0,s=this.options.images.length,n=this.options.images,a=[],r=0,p=this.options.appendBlocks(),l=this.$el.width(),d=parseInt(this.options.margin,10),g=this.$el,c=parseInt(this.options.rowHeight,10);t.each(this.options.images,function(t,e){var o=i.options.getSize(e),s=parseInt(o.width,10),n=parseInt(o.height,10);n!==c&&(s=Math.floor(s*(c/n))),r+=s,h.push(s)}),t.each(p,function(t,i){r+=i.width});var m=r/Math.ceil(r/l);console.log("rows",Math.ceil(r/l));for(var f=0;s>o;){var w={width:0,photos:[]},u=0,v=this.getBlockInRow(a.length+1);for(v&&(w.width+=v.width,f+=v.width);f+h[o+u]/2<=m*(a.length+1)&&s>o+u;)f+=h[o+u],w.width+=h[o+u],w.photos.push({width:h[o+u],photo:n[o+u]}),u++;o+=u,a.push(w)}console.log(a.length,a);for(var y=0;y<a.length;y++){var w=a[y],R=!1;if(e=y+1,this.options.maxRows&&e>this.options.maxRows)break;y===a.length-1&&(R=!0),f=-1*d;var I=this.getBlockInRow(R?-1:e),x=l;I&&(x-=I.width,f=0);var b=x/w.width,u=w.photos.length,k=Math.min(Math.floor(c*b),parseInt(this.options.maxRowHeight,10));b=k/this.options.rowHeight;var H=t("<div/>",{"class":"picrow"});H.height(k+d),g.append(H);for(var M="",B=0;B<w.photos.length;B++){var j=w.photos[B].photo,z=Math.floor(w.photos[B].width*b);f+=z+d,M+=this.renderPhoto(j,{src:this.options.thumbnailPath(j,z,k),width:z,height:k},I?!1:B===w.photos.length-1)}if(""!==M){if(H.html(M),Math.abs(f-x)<.05*x){for(var C=0;x>f;){var S=H.find("."+this.options.imageContainer+":nth-child("+(C+1)+")"),$=S.find("."+this.options.imageSelector);$.width($.width()+1),C=(C+1)%u,f++}for(C=0;f>x;){var P=H.find("."+this.options.imageContainer+":nth-child("+(C+1)+")"),_=P.find("."+this.options.imageSelector);_.width(_.width()-1),C=(C+1)%u,f--}}else if(x-f>.05*x)for(var O=x-f,W=0,q=H.find("."+this.options.imageContainer),A=0,N=0;N<q.length;N++){var Q=O/q.length,T=q.eq(N),D=T.find("."+this.options.imageSelector),E=D.width(),F=D.height();y===q.length-1&&(Q=O-W),D.width(E+Q),D.height(F/E*(E+Q)),A=(F-D.height())/2,D.css("margin-top",A),W+=Q}I&&t("<div />",{"class":this.options.imageContainer+" added-block",css:{width:I.width,height:k},html:I.html}).appendTo(H)}else H.remove()}},renderPhoto:function(i,h,e){var o={},s;return s=t.extend({},i,{src:h.src,displayWidth:h.width,displayHeight:h.height,marginRight:e?0:this.options.margin}),this.options.dataObject?o[this.options.dataObject]=s:o=s,this.options.template(o)},handleResize:function(){},refresh:function(i){this.options=t.extend({},this.defaults,i),this.$el.empty(),this.displayImages()}},t.fn[s]=function(i){var h=arguments,e;return this.each(function(){var n=t(this),a=t.data(this,"plugin_"+s),r="object"==typeof i&&i;a?"string"==typeof i?e=a[i].apply(a,Array.prototype.slice.call(h,1)):a.refresh.call(a,r):n.data("plugin_"+s,a=new o(this,r))}),e||this}}(jQuery,window,document);