forked from sapegin/jquery.mosaicflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mosaicflow.min.js
2 lines (2 loc) · 5.12 KB
/
jquery.mosaicflow.min.js
1
2
/*! jQuery Mosaic Flow v0.2.5 by Artem Sapegin - http://sapegin.github.io/jquery.mosaicflow/ - Licensed MIT */
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";function b(a,b){this.container=a,this.options=b,this.container.trigger("start"),this.init(),this.container.trigger("ready")}function c(a){function b(a,b){return b.toUpper()}var c={},d=a.data();for(var e in d)c[e.replace(/-(\w)/g,b)]=d[e];return c}function d(a){var b={};if(b.height=parseInt(a.attr("height"),10),b.width=parseInt(a.attr("width"),10),0===b.height||0===b.width){var c=new Image;c.src=a.attr("src"),b.width=c.width,b.height=c.height}return b}var e=0;a.fn.mosaicflow=function(d){var e=Array.prototype.slice.call(arguments,0);return this.each(function(){var f=a(this),g=f.data("mosaicflow");g?"string"==typeof d&&g[d](e[1]):(d=a.extend({},a.fn.mosaicflow.defaults,d,c(f)),g=new b(f,d),f.data("mosaicflow",g))})},a.fn.mosaicflow.defaults={itemSelector:"> *",columnClass:"mosaicflow__column",minItemWidth:240,itemHeightCalculation:"auto"},b.prototype={init:function(){this.__uid=e++,this.__uidItemCounter=0,this.items=this.container.find(this.options.itemSelector),this.columns=a([]),this.columnsHeights=[],this.itemsHeights={},this.tempContainer=a("<div>").css({visibility:"hidden",width:"100%"}),this.workOnTemp=!1,this.autoCalculation="auto"===this.options.itemHeightCalculation,this.container.append(this.tempContainer);var b=this;this.items.each(function(){var c=a(this),d=c.attr("id");d||(d=b.generateUniqueId(),c.attr("id",d))}),this.container.css("visibility","hidden"),this.autoCalculation?a(window).load(a.proxy(this.refill,this)):this.refill(),a(window).resize(a.proxy(this.refill,this))},refill:function(){this.container.trigger("fill"),this.numberOfColumns=Math.floor(this.container.width()/this.options.minItemWidth),this.numberOfColumns<1&&(this.numberOfColumns=1);var a=this.ensureColumns();a&&(this.fillColumns(),this.columns.filter(":hidden").remove()),this.container.css("visibility","visible"),this.container.trigger("filled")},ensureColumns:function(){var b=this.columns.length,c=this.numberOfColumns;if(this.workingContainer=0===b?this.tempContainer:this.container,c>b)for(var d=c-b,e=0;d>e;e++){var f=a("<div>",{"class":this.options.columnClass});this.workingContainer.append(f)}else if(b>c){for(var g=b;g>=c;)this.columns.eq(g).hide(),g--;var h=b-c;this.columnsHeights.splice(this.columnsHeights.length-h,h)}return c!==b?(this.columns=this.workingContainer.find("."+this.options.columnClass),this.columns.css("width",100/c+"%"),!0):!1},fillColumns:function(){for(var a=this.numberOfColumns,b=this.items.length,c=0;a>c;c++){var d=this.columns.eq(c);this.columnsHeights[c]=0;for(var e=c;b>e;e+=a){var f=this.items.eq(e),g=0;d.append(f),g=this.autoCalculation?f.outerHeight():parseInt(f.find("img").attr("height"),10),this.itemsHeights[f.attr("id")]=g,this.columnsHeights[c]+=g}}this.levelBottomEdge(this.itemsHeights,this.columnsHeights),this.workingContainer===this.tempContainer&&this.container.append(this.tempContainer.children()),this.container.trigger("mosaicflow-layout")},levelBottomEdge:function(b,c){for(;;){var d=a.inArray(Math.min.apply(null,c),c),e=a.inArray(Math.max.apply(null,c),c);if(d===e)return;var f=this.columns.eq(e).children().last(),g=b[f.attr("id")],h=c[d],i=c[e],j=h+g;if(j>=i)return;this.columns.eq(d).append(f),c[e]-=g,c[d]+=g}},add:function(b){this.container.trigger("add");var c=a.inArray(Math.min.apply(null,this.columnsHeights),this.columnsHeights),e=0;if(this.autoCalculation){b.css({position:"static",visibility:"hidden",display:"block"}).appendTo(this.columns.eq(c)),e=b.outerHeight();var f=b.find("img");0!==f.length&&f.each(function(){var b=a(this),c=d(b),f=b.width()*c.height/c.width;e+=f}),b.detach().css({position:"static",visibility:"visible"})}else e=parseInt(b.find("img").attr("height"),10);b.attr("id")||b.attr("id",this.generateUniqueId());var g=this.items.toArray();g.push(b),this.items=a(g),this.itemsHeights[b.attr("id")]=e,this.columnsHeights[c]+=e,this.columns.eq(c).append(b),this.levelBottomEdge(this.itemsHeights,this.columnsHeights),this.container.trigger("mosaicflow-layout"),this.container.trigger("added")},remove:function(a){this.container.trigger("remove");var b=a.parents("."+this.options.columnClass);this.columnsHeights[b.index()-1]-=this.itemsHeights[a.attr("id")],a.detach(),this.items=this.items.not(a),this.levelBottomEdge(this.itemsHeights,this.columnsHeights),this.container.trigger("mosaicflow-layout"),this.container.trigger("removed")},empty:function(){var b=this.numberOfColumns;this.items=a([]),this.itemsHeights={};for(var c=0;b>c;c++){var d=this.columns.eq(c);this.columnsHeights[c]=0,d.empty()}this.container.trigger("mosaicflow-layout")},recomputeHeights:function(){function b(b,d){d=a(d);var f=0;f=c.autoCalculation?d.outerHeight():parseInt(d.find("img").attr("height"),10),c.itemsHeights[d.attr("id")]=f,c.columnsHeights[e]+=f}for(var c=this,d=this.numberOfColumns,e=0;d>e;e++){var f=this.columns.eq(e);this.columnsHeights[e]=0,f.children().each(b)}},generateUniqueId:function(){return this.__uidItemCounter++,"mosaic-"+this.__uid+"-itemid-"+this.__uidItemCounter}},a(function(){a(".mosaicflow").mosaicflow()})});