1 | !function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&module.exports?module.exports=e():t.viewport=e()}(this,function(){var h=["state","remove","matches","current","previous"],n={name:void 0,matches:!1,current:!1};function r(t){return{name:t.name,matches:t.matches,current:t.current}}function s(t,e){return e&&e[t]?r(e[t]):n}function c(t){this.api=null,this.viewports=t,this.store={vps:{},tokenUid:-1,channels:{},channelAll:[],current:void 0,previous:void 0},this.viewports.forEach(function(t){var e,n;t.listener=this.setState.bind(this),t.mql=(e=t.query,n=t.listener,(e=window.matchMedia(e)).addEventListener("change",n),e),this.store.channels[t.name]=[],this.store.vps[t.name]={name:t.name,matches:!1,current:!1}},this),this.setState()}return c.prototype={getMatches:function(){return(this.viewports||[]).filter(function(t){return t.mql.matches}).map(r)},getCurrent:function(){var t=this.getMatches().pop();return t?t.name:void 0},getChanges:function(t,e){var n=t.name,r=this.store.vps[n],s={matches:t.mql.matches,current:e.name===n};return Object.keys(s).reduce(function(t,e){return r[e]!==s[e]&&t.push({key:e,value:s[e]}),t},[])},setState:function(){var n=[],r=s(this.getCurrent(),this.store.vps);this.viewports.forEach(function(t){var e=this.store.vps[t.name],t=this.getChanges(t,r);t.forEach(function(t){e[t.key]=t.value}),t.length&&n.push(e.name)},this),r.name!==this.store.current&&(this.store.previous=this.store.current,this.store.current=r.name),n.forEach(this.publish,this)},addSubscriber:function(t){var e,n,r=this.store.tokenUid=this.store.tokenUid+1;return t.channel.push({token:r,handler:t.handler}),t.handler(t.vp,this.api),e=r,n=t.channel,function(){return n=n.filter(function(t){t.token}),e}},subscribe:function(t,e){return this.addSubscriber({handler:e,channel:this.store.channels[t],vp:this.store.vps[t]})},subscribeAll:function(t){return this.addSubscriber({handler:t,channel:this.store.channelAll,vp:this.current()})},publish:function(e){this.store.channels[e].forEach(function(t){t.handler(this.store.vps[e],this.api)},this),this.store.channelAll.forEach(function(t){t.handler(this.store.vps[e],this.api)},this)},state:function(t){return t?s(t,this.store.vps):Object.keys(this.store.vps||{}).map(function(t){return s(t,this.store.vps)},this)},matches:function(t){return t?s(t,this.store.vps).matches:this.getMatches()},current:function(t){var e=s(this.store.current,this.store.vps);return t?e.name===t:e},previous:function(t){return t?this.store.previous===t:s(this.store.previous,this.store.vps)},remove:function(){return this.viewports.forEach(function(t){t.mql.removeEventListener("change",t.listener)}),this.viewports=this.store.vps=this.store.current=this.store.previous=null}},"undefined"==typeof window||void 0===window.matchMedia?function(t){function e(){}return"string"==typeof t?{remove:e,matches:e}:h.reduce(function(t,e){return/remove/.test(e)?t[e]=function(){}:t[e]=function(t){return"string"!=typeof t&&(/state|matches/.test(e)?[]:n)},t},function(){})}:function(t,e){var n,r,s,i,o;return"string"==typeof t?(n=t,r=e,s=window.matchMedia(n),i={remove:function(){},matches:function(){return s.matches}},r&&(s.addEventListener("change",n=function(t){r({matches:t.matches},i)}),s.matches&&n(s.matches),i.remove=s.removeEventListener.bind(s,"change",n)),i):((o=new c(t)).api=function(t,e){return"string"==typeof t?o.subscribe.call(o,t,e):o.subscribeAll.call(o,t)},h.reduce(function(t,e){return t[e]=o[e].bind(o),t},o.api))}}); |