jquery.mosaicflow.min.js 4.9 KB
/*! jQuery Mosaic Flow v0.2.2 by Artem Sapegin - http://sapegin.github.com/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}var d=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=d++,this.__uid_item_counter=0,this.items=this.container.find(this.options.itemSelector),this.columns=a([]),this.columnsHeights=[],this.itemsHeights={},this.tempContainer=a("<div>").css("visibility","hidden"),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.tempContainer.css("width",this.container.width()),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),d=0;this.autoCalculation?(b.css({position:"static",visibility:"hidden",display:"block"}).appendTo(this.columns.eq(c)),d=b.outerHeight(),b.detach().css({position:"static",visibility:"visible"})):d=parseInt(b.find("img").attr("height"),10),b.attr("id")||b.attr("id",this.generateUniqueId());var e=this.items.toArray();e.push(b),this.items=a(e),this.itemsHeights[b.attr("id")]=d,this.columnsHeights[c]+=d,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.__uid_item_counter++,"mosaic-"+this.__uid+"-itemid-"+this.__uid_item_counter}},a(function(){a(".mosaicflow").mosaicflow()})});