/*! * savvior v0.6.1 - A Javascript multicolumn layout tool alternative to Masonry or Salvattore. * http://savvior.org * */ !function(a,b){"function"==typeof define&&define.amd?define("savvior",["enquire.js"],function(c){return a["new GridDispatch()"]=b(c)}):"object"==typeof exports?module.exports=b(require("enquire.js")):a.savvior=b(a.enquire)}(this,function(a){function b(a,b,c,d){d||!a.dataset?a.setAttribute("data-"+b,c):a.dataset[b]=c}function c(a,b,c){var d=0;for(d;d *:nth-child("+d.columns+"n-"+k+")",j.push(a.querySelectorAll(e));c(j,function(a){f=document.createElement("div"),g=document.createDocumentFragment(),f.className=h,c(a,function(a){g.appendChild(a)}),f.appendChild(g),i.appendChild(f)}),this.element.appendChild(i),b(this.element,"columns",d.columns),this.columns=d.columns,this.columnClasses=d.columnClasses},g.prototype.filterItems=function(a,d){if(!d)return a;var e,f,g;return g=Array.prototype.slice.call(a.children),f=a.querySelectorAll("[data-columns] > "+d),c(f,function(a){e=g.indexOf(a),this.filtered.appendChild(a),b(a,"position",e)},this),a},g.prototype.removeColumns=function(){var a,d=document.createRange(),e=document.createElement("div"),f=[];return d.selectNodeContents(this.element),a=Array.prototype.filter.call(d.extractContents().childNodes,function(a){return a instanceof window.HTMLElement}),f.length=a[0].childNodes.length*a.length,c(a,function(b,d){c(b.children,function(b,c){f[c*a.length+d]=b})}),b(e,"columns",0),f.filter(function(a){return!!a}).forEach(function(a){e.appendChild(a)}),e},g.prototype.redraw=function(a,b){var c,e=new CustomEvent("savvior:redraw",{detail:{element:this.element,from:this.columns,to:a.columns,filter:a.filter||null}});window.requestAnimationFrame(function(){this.columns!==a.columns&&(c=this.restoreFiltered(this.removeColumns()),this.addColumns(c,a)),window.dispatchEvent(e),d(b)&&b(this)}.bind(this))},g.prototype.restoreFiltered=function(a){if(0===this.filtered.childNodes.length)return a;var b,d=a;return c(this.filtered.querySelectorAll("[data-position]"),function(a){b=Number(a.getAttribute("data-position")),a.removeAttribute("data-position"),d.insertBefore(a,d.children[b]||null)}),a},g.prototype.restore=function(a,b){if(!this.status)return d(a)&&a(!1),!1;var e,f=document.createDocumentFragment(),g=[],h=new CustomEvent("savvior:restore",{detail:{element:this.element,from:this.columns}});window.requestAnimationFrame(function(){e=this.restoreFiltered(this.removeColumns()),c(e.childNodes,function(a){g.push(a)}),g.forEach(function(a){f.appendChild(a)}),this.element.appendChild(f),this.element.removeAttribute("data-columns"),window.dispatchEvent(h),d(a)&&a.call(b,b||this)}.bind(this))},g.prototype.addItems=function(a,b,e){var f=new CustomEvent("savvior:addItems",{detail:{element:this.element,grid:this}}),g=function(a){return b.clone?a.cloneNode(!0):a},h={append:function(a,b){var c=g(a);return b.appendChild(c),b},prepend:function(a,b){var c=g(a);return b.insertBefore(c,b.firstChild),b}};window.requestAnimationFrame(function(){var g=this.restoreFiltered(this.removeColumns());a instanceof NodeList||a instanceof Array?c(a,function(a){g=h[b.method].call(null,a,g)}):g=h[b.method].call(null,a,g),this.addColumns(g,{columns:this.columns,columnClasses:this.columnClasses,filter:this.filter}),window.dispatchEvent(f),d(e)&&e(this)}.bind(this))};var h=function(a,b){this.selector=a,this.options=b,this.queryHandlers=[],this.grids=[],this.ready=!1};h.prototype.register=function(){c(document.querySelectorAll(this.selector),function(a){this.grids.push(new g(a,this.options))},this);for(var b in this.options)this.queryHandlers.push(this.constructHandler(b,this.options[b]));return c(this.queryHandlers,function(b){a.register(b.mq,b.handler)}),this.ready=!0,this},h.prototype.constructHandler=function(a){return{mq:a,handler:{deferSetup:!0,setup:function(){this.gridSetup(a)}.bind(this),match:function(){this.gridMatch(a)}.bind(this),destroy:function(){}}}},h.prototype.gridSetup=function(a){var b;c(this.grids,function(c){c.setup(this.options[a],function(){b=new CustomEvent("savvior:setup",{detail:{element:c.element,columns:c.columns,filter:this.filter}}),window.dispatchEvent(b)})},this)},h.prototype.gridMatch=function(a){var b;c(this.grids,function(c){b=new CustomEvent("savvior:match",{detail:{element:c.element,from:c.columns,to:this.options[a].columns,query:a}}),c.redraw(this.options[a],function(){window.dispatchEvent(b)})},this)},h.prototype.unregister=function(b,e){c(this.queryHandlers,function(b){a.unregister(b.mq)}),c(this.grids,function(a){a.restore(function(){this.queryHandlers=[],this.ready=!1,d(b)&&b.call(this,e||this)},this)},this),this.grids=[]};var i=function(){if(!a)throw new Error("enquire.js not present, please load it before calling any methods");this.grids={}};return i.prototype.init=function(a,b){if(!a)throw new TypeError("Missing selector");if("string"!=typeof a)throw new TypeError("Selector must be a string");if("object"!=typeof b)throw new TypeError("Options must be an object");return this.grids[a]?this:document.querySelectorAll(a).length<1?this:(this.grids[a]=new h(a,b),this.grids[a].register(b),window.dispatchEvent(new CustomEvent("savvior:init")),this)},i.prototype.destroy=function(a,b){var f=new CustomEvent("savvior:destroy",{detail:{selectors:a}}),g=void 0===a||e(a)?Object.keys(this.grids):a,h=g.length,i=0,j=function(a){delete this.grids[g[i]],++i===h&&(window.dispatchEvent(f),d(b)&&b.call(a,this))}.bind(this);c(g,function(a){this.grids[a]&&this.grids[a].unregister(j)},this)},i.prototype.ready=function(a){if(void 0===a){var b=[];for(var c in this.grids)this.grids[c].ready&&b.push(c);return b.length>0&&b}return!!this.grids[a]&&this.grids[a].ready},i.prototype.addItems=function(a,b,e,g){var h,i,j={clone:!1,method:"append"};if(!this.grids[a])throw new TypeError("Grid does not exist.");if("string"==typeof b&&(b=document.querySelectorAll(b)),b instanceof Array)c(b,function(a){if(!(a instanceof Node))throw new TypeError("Supplied element in array is not instance of Node.")},this);else if(!(b instanceof Node||b instanceof NodeList))throw new TypeError("Supplied argument is not a Node or a NodeList.");return d(e)?(h=e,i=j):(h=g,i=f(e,j)),c(this.grids[a].grids,function(a){a.addItems(b,i,h)}),this},new i});