/
breakpoints-min.js
5 lines (5 loc) · 2.47 KB
/
breakpoints-min.js
1
2
3
4
5
(function(){var j=this.Breakpoints={};j.VERSION="0.0.1";var f={debounce:function(b,c){var a;return function(){clearTimeout(a);a=setTimeout(c,b)}},extend:function(b){l.call(arguments,1).forEach(function(c){for(var a in c)b[a]=c[a]});return b},includes:function(b,c){return-1<b.indexOf(c)}},n=j.SETTINGS={debounce:50},l=Array.prototype.slice,k=/\s+/,p=j.Listener=function(b){var c=this;this.breakpoints=b;this.current=this.getBreakpoint(window.innerWidth);this.onWindowResizeDebounced=f.debounce(n.debounce,
function(){c.onWindowResize()});window.addEventListener("resize",this.onWindowResizeDebounced);this.Relay=function(a){return new m(c,a)}};f.extend(p.prototype,{on:function(b,c,a){var d,g,e,i,h;if(!c)return this;b=b.split(k);for(d=this._callbacks||(this._callbacks={});g=b.shift();)e=(h=d[g])?h.tail:{},e.next=i={},e.context=a,e.callback=c,d[g]={tail:i,next:h?h.next:e};return this},off:function(b,c,a){var d,g,e,i,h,f;if(g=this._callbacks){if(!b&&!c&&!a)return delete this._callbacks,this;for(b=b?b.split(k):
Object.keys(g);d=b.shift();)if(e=g[d],delete g[d],e&&(c||a))for(i=e.tail;(e=e.next)!==i;)if(h=e.callback,f=e.context,c&&h!==c||a&&f!==a)this.on(d,h,f);return this}},trigger:function(b){var c,a,d,g,e,f;if(!(d=this._callbacks))return this;e=d.all;b=b.split(k);for(f=l.call(arguments,1);c=b.shift();){if(a=d[c])for(g=a.tail;(a=a.next)!==g;)a.callback.apply(a.context||this,f);if(a=e){g=a.tail;for(c=[c].concat(f);(a=a.next)!==g;)a.callback.apply(a.context||this,c)}}return this}},{getBreakpoint:function(b){for(var c in this.breakpoints)if(b<
this.breakpoints[c])return c},onWindowResize:function(){var b=this.getBreakpoint(window.innerWidth);b!==this.current&&(this.previous=this.current,this.current=b,this.trigger("change",this.current,this.previous))},destroy:function(){window.removeEventListener("resize",this.onWindowResizeDebounced);this.previous=this.current=null}});var m=j.Relay=function(b,c){this.listener=b;this.list=this._createList(c);this.listener.on("change",this._onBreakpointChange,this)};f.extend(m.prototype,{_createList:function(b){var c=
[],a,d;for(d in b)a=b[d],a="function"===typeof a?{enter:a}:a,c.push({breakpoints:d.split(" "),callbacks:a});return c},_onBreakpointChange:function(b,c){this.list.forEach(function(a){var d=a.breakpoints;f.includes(d,b)&&!f.includes(d,c)&&a.callbacks.enter();!f.includes(d,b)&&f.includes(d,c)&&a.callbacks.leave&&a.callbacks.leave()})},destroy:function(){this.listener.off("change",this._onBreakpointChange,this);this.list=this.listener=null}})}).call(this);