-
Notifications
You must be signed in to change notification settings - Fork 12
/
row-grid.min.js
5 lines (5 loc) · 2.41 KB
/
row-grid.min.js
1
2
3
4
5
var rowGrid=function(f,b){function u(b,d){for(var g=[b];b=b.nextElementSibling;)(Element.prototype.matches||Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector).call(b,d)&&g.push(b);return g}function p(b,d,g){var f=0,e=[];g=Array.prototype.slice.call(g||b.querySelectorAll(d.itemSelector));var r=g.length;singleImagePerRow=!!window.matchMedia&&!window.matchMedia("(min-width:"+d.minWidth+"px)").matches;var h=getComputedStyle(b);b=Math.floor(b.getBoundingClientRect().width)-parseFloat(h.getPropertyValue("padding-left"))-
parseFloat(h.getPropertyValue("padding-right"));h=[];for(var c,k,q,a=0;a<r;++a)(c=g[a].getElementsByTagName("img")[0])?((k=parseInt(c.getAttribute("width")))||c.setAttribute("width",k=c.offsetWidth),(q=parseInt(c.getAttribute("height")))||c.setAttribute("height",q=c.offsetHeight),h[a]={width:k,height:q}):(g.splice(a,1),--a,--r);for(c=0;c<r;++c){g[c].classList?(g[c].classList.remove(d.firstItemClass),g[c].classList.remove(d.lastRowClass)):g[c].className=g[c].className.replace(new RegExp("(^|\\b)"+
d.firstItemClass+"|"+d.lastRowClass+"(\\b|$)","gi")," ");f+=h[c].width;e.push(g[c]);if(c===r-1)for(a=0;a<e.length;a++){0===a&&(e[a].className+=" "+d.lastRowClass);var l="width: "+h[c+parseInt(a)-e.length+1].width+"px;height: "+h[c+parseInt(a)-e.length+1].height+"px;";a<e.length-1&&(l+="margin-right:"+d.minMargin+"px");e[a].style.cssText=l}if(f+d.maxMargin*(e.length-1)>b||singleImagePerRow){k=f+d.maxMargin*(e.length-1)-b;a=e.length;(d.maxMargin-d.minMargin)*(a-1)<k?(q=d.minMargin,k-=(d.maxMargin-d.minMargin)*
(a-1)):(q=d.maxMargin-k/(a-1),k=0);var p=null,n=0;for(a=0;a<e.length;a++){var t=e[a];l=h[c+parseInt(a)-e.length+1].width;var m=l-l/f*k;p=p||Math.round(h[c+parseInt(a)-e.length+1].height*(m/l));.5<=n+1-m%1?(n-=m%1,m=Math.floor(m)):(n+=1-m%1,m=Math.ceil(m));l="width: "+m+"px;height: "+p+"px;";a<e.length-1&&(l+="margin-right: "+q+"px");t.style.cssText=l;0===a&&d.firstItemClass&&(t.className+=" "+d.firstItemClass)}e=[];f=0}}}if(null!==f&&void 0!==f)if("appended"===b){b=JSON.parse(f.getAttribute("data-row-grid"));
var n=f.getElementsByClassName(b.lastRowClass)[0];n=u(n,b.itemSelector);p(f,b,n)}else b?(void 0===b.minWidth&&(b.minWidth=0),void 0===b.lastRowClass&&(b.lastRowClass="last-row"),f.setAttribute("data-row-grid",JSON.stringify(b)),window.addEventListener("resize",function(n){p(f,b)})):b=JSON.parse(f.getAttribute("data-row-grid")),p(f,b)};"object"===typeof exports&&(module.exports=rowGrid);