-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 3.85 KB
/
index.js
1
var i=function(t){if(Array.isArray(t))return t.forEach(i);if(!("id"in t)){for(let s of["onenter","onexit","oninit","onresize"])if(s in t)throw new Error('qvp: Missing an "id" reference');return Object.entries(t).forEach(([s,o])=>{let c=typeof t[s];if(c==="string")return i({id:s,query:o});throw new TypeError(`qvp: Invalid query type. Expected "string" received "${c}"`)})}let n=g(t),{id:r}=n.screen;i.viewports.size||addEventListener("resize",u(()=>{i.viewports.forEach(s=>{s.screen.active&&s.onresize(window.innerWidth)})},25),!0),i.viewports.has(r)?console.warn(`qvp: The id "${r}" is already defined, use qvp.add() instead.`):i.viewports.set(r,n)};i.viewports=new Map;Object.defineProperty(i,"isTouch",{get(){return"ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0}});i.get=e=>i.viewports.has(e)?i.viewports.get(e):!1;i.add=(e,t)=>{let n=i.get(e);if(n===!1)return console.error(`qvp: There is no viewport using an id of "${e}"`);f(t,n.screen),n.screen.test.matches&&n.onenter()};i.off=(e,t)=>{let n=i.get(e.split(":")[0]);if(n===!1||!(e in n.events))return;let r=n.events[e].length;if(typeof t=="number")t<=r-1&&n.events[e].splice(t,1);else if(typeof t=="function"){let s=[];for(let o=0;o<r;o++)n.events[e][o]!==t&&s.push(n.events[e][o]);s.length>0&&(n.events[e]=s)}else delete n.events[e]};i.on=(e,t,n)=>{let[r,s]=e.split(":"),o=i.get(r);if(o===!1)return;e in o.events||(o.events[e]=[]);let c=n?t.bind(n):t;return c["qvp:event"]=o.events[e].length,o.events[e].push(c),o.screen.test.matches?s==="oninit"?c.call():(s==="onenter"&&c.call(),f({[s]:c},o.screen)):f({[s]:c},o.screen),c["qvp:event"]};i.list=e=>{let t=Array.from(i.viewports.values()).map(({screen:n})=>n);return e?t.filter(({id:n})=>e.includes(n)):t};i.active=e=>{let t=i.list();if(e){let r=i.get(e);return r?r.screen.active:!1}let n=t.filter(({active:r})=>r===!0);return n.length>1?n:n[0]};i.test=(e,t=",")=>typeof e=="string"?e.indexOf(t)>-1?e.split(t).some(i.active):!!i.active(e):e.some(i.active);i.remove=e=>{i.viewports.has(e)&&(i.viewports.get(e).destroy(),i.viewports.delete(e))};i.destroy=()=>{removeEventListener("resize",u()),i.viewports.forEach(e=>e.destroy()),i.viewports.clear()};i.screens=()=>{throw Error("qvp: The qvp.screens() is deprecated, use the default import, e.g: qvp(...)")};function u(e,t){let n=t;return function(){let r=()=>{n=null,e.apply(this,arguments)};n&&cancelAnimationFrame(n),n=requestAnimationFrame(r)}}function f(e,t){typeof e.onenter=="function"&&("qvp:event"in e.onenter||(e.onenter["qvp:event"]=NaN),t.onenter.add(e.onenter)),typeof e.onexit=="function"&&("qvp:event"in e.onexit||(e.onexit["qvp:event"]=NaN),t.onexit.add(e.onexit)),typeof e.onresize=="function"&&("qvp:event"in e.onresize||(e.onresize["qvp:event"]=NaN),t.onresize.add(e.onresize)),typeof e.oninit=="function"&&("qvp:event"in e.oninit||(e.oninit["qvp:event"]=NaN),t.oninit.add(e.oninit))}function a(e,...t){let n=i.get(e.split(":")[0]);if(n!==!1&&e in n.events)for(let r=0;r<n.events[e].length&&(n.events[e][r].apply(null,t),e.endsWith(":oninit")&&i.off(e,r),e in n.events);r++);}function g(e){let t=e.query||"all",n={id:e.id,query:t,active:!1,test:matchMedia(t),onenter:new Set,onexit:new Set,onresize:new Set,oninit:new Set,events:Object.create(null)};f(e,n);let r=()=>{n.oninit.size>0&&(n.oninit.forEach(v=>!isNaN(v["qvp:event"])||v()),a(`${e.id}:oninit`),n.oninit.clear()),n.onenter.forEach(v=>!isNaN(v["qvp:event"])||v()),a(`${e.id}:onenter`),n.active=!0},s=()=>{n.onexit.forEach(v=>!isNaN(v["qvp:event"])||v()),a(`${e.id}:onexit`),n.active=!1},o=v=>{n.onresize.forEach(d=>!isNaN(d["qvp:event"])||d()),a(`${e.id}:onresize`,v)},c=({matches:v})=>v?r():s(),p=()=>{for(let v in n.events)delete n.events[v];n.test.removeEventListener("change",c)};return n.test.addEventListener("change",c),n.test.matches&&r(),{onenter:r,onexit:s,onresize:o,destroy:p,get screen(){return n},get events(){return n.events}}}var l=i;export{l as default};