TileFlooring.js/tile.min.js

2 lines
3.1 KiB
JavaScript

/*! TileFlooring.js 0.0.3 | (c) 2015 Kegan Myers | https://terribleplan.com/TileFlooring.js/LICENSE.md */
!function(a,b,c,d){"function"==typeof define&&define.amd?define(function(){return d(a,b,c)}):"object"==typeof exports?module.exports=d:a.TileFloor=d(a,b,c)}(window,document,void 0,function(a,b,c){function d(a){null!==a.parentElement&&a.parentElement.removeChild(a)}function e(a){this.element=a,this.originalParent=a.parent}function f(a){this.element=a}function g(c,d){if(!c.tagName||"function"!=typeof c.querySelectorAll)throw new Error("The first argument must be a DOM Element");if(!d.hasOwnProperty("elementSelector"))throw new Error("You must define an element selector");if(!d.hasOwnProperty("columns"))throw new Error("The only display mode is `columns`");if(!d.columns.hasOwnProperty("maxWidth"))throw new Error("You must define a max width for your columns");d.columns.hasOwnProperty("maxCount")||(d.columns.maxCount=1/0),this.masterElement=c,this.destroyed=!1,this.options=d,this.elements=Array.prototype.map.call(c.querySelectorAll(d.elementSelector),function(a){return new e(a)}),this.containers=[],this.containerElement=null;var f="-generated-tf"+Math.floor(1e4*Math.random());this.clearfixClassName="clearfix"+f,this.columnClassName="responsive-column"+f,this.clearfixStylesheet=b.createElement("style"),b.head.appendChild(this.clearfixStylesheet),this.clearfixStylesheet.sheet.insertRule("."+this.clearfixClassName+':after{content:"";display:table;clear:both}',0),this.clearfixStylesheet.sheet.insertRule("."+this.columnClassName+"{float:left;max-width:"+d.columns.maxWidth+"px}",0),this.flow();var g=this;this.onResize=function(){var a=g.determineColumns();return g.containers.length!==a?g.flow():void 0},a.addEventListener("resize",this.onResize,!1)}function h(a){a.removeSelf()}function i(){return b.createElement("div")}function j(a){var b=0,c=1/0;return a.forEach(function(a,d){var e=a.height();c>e&&(b=d,c=e)}),a[b]}if("function"!=typeof b.querySelectorAll)throw new Error("TileFloor is only compatible with HTML5 browsers");return e.prototype.removeSelf=function(){d(this.element)},f.prototype.removeSelf=e.prototype.removeSelf,f.prototype.height=function(){return this.element.offsetHeight},f.prototype.addChild=function(a){return this.element.appendChild(a)},g.prototype.flow=function(){var a=this;null!==this.containerElement&&d(this.containerElement),this.elements.forEach(h),this.containers.forEach(h);var b=this.determineColumns();if(0===b)throw new Error("Cowardly refusing to flow elements into 0 columns");this.containerElement=i(),this.containerElement.className=this.clearfixClassName,this.containers=new Array(b);for(var c=0;b>c;c++){var e=i();e.className=a.columnClassName,e.style.width=100/b+"%",a.containerElement.appendChild(e),this.containers[c]=new f(e)}this.masterElement.appendChild(this.containerElement),this.elements.forEach(function(b){a.pushElement(b)})},g.prototype.pushElement=function(a){var b=a instanceof e?a:new e(a);b.removeSelf(),j(this.containers).addChild(b.element)},g.prototype.determineColumns=function(){return Math.min(Math.ceil(this.masterElement.offsetWidth/this.options.columns.maxWidth),this.options.columns.maxCount)},g});