@peter.naydenov/route-emitter
Version:
Changes in URL are converted in events according routes definition list
1 lines • 5.46 kB
JavaScript
;(function(e,t){typeof exports==`object`&&typeof module<`u`?module.exports=t(require(`@peter.naydenov/notice`),require(`url-pattern`),require(`ask-for-promise`)):typeof define==`function`&&define.amd?define([`@peter.naydenov/notice`,`url-pattern`,`ask-for-promise`],t):(e=typeof globalThis<`u`?globalThis:e||self,e.routeEmitter=t(e.notice,e.UrlPattern,e.askForPromise))})(this,function(e,t,n){var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e},u=(e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule?i(n,`default`,{value:e,enumerable:!0}):n,e));e=u(e),t=u(t),n=u(n);function d(){let e=null,t=null;function r({state:e,title:t,url:n},r){r?window.history.pushState(e,``,n):window.history.replaceState(e,``,n),document.title=typeof t==`function`?t(e.data):t}function i(){return window.location.pathname}function a(r){t=function(t){let{PGID:i,url:a,data:o}=t.state;e||=(0,n.default)(),r(e,{addressName:i,data:o,url:a}),e=null},window.addEventListener(`popstate`,t)}function o(t=1){return e=(0,n.default)().timeout(1500,`expire`),window.history.back(t),e.onComplete(t=>{t===`expire`&&(e=null)}),e.promise}function s(t=1){return e=(0,n.default)().timeout(1500,`expire`),window.history.go(t),e.onComplete(t=>{t===`expire`&&(e=null)}),e.promise}function c(){t&&window.removeEventListener(`popstate`,t)}return{write:r,read:i,back:o,go:s,listen:a,destroy:c}}function f(e,t){return function(n,{addressName:r,data:i,url:a}){let{eBus:o,API:s}=e,c=t.lastLocation;s.navigate(r,i,!0),c===a?o.emit(`_RELOAD`,r,i,a):o.emit(`_CHANGE`,r,i,a),n.done(r,i)}}function p(e,t){return function(){let{eBus:n,history:r,API:i}=e,a=!1,o=!0,s=!1,c=sessionStorage.getItem(t.SSName),l=r.read();if(c&&c===l&&(a=!0),o=t.rt.every(({name:e,pattern:o,title:c,redirect:u,data:d={}})=>{let f=o.match(l);return f?u?(i.navigate(u,d,!0),s=!0,!1):(sessionStorage.setItem(t.SSName,l),t.lastLocation=l,t.lastAddress=e,r.write({state:{PGID:e,url:l,data:f},url:l,title:c},!0),a?n.emit(`_RELOAD`,e,f,l):n.emit(`_CHANGE`,e,f,l),!1):!0}),o){if(s)return;n.emit(`_ERROR`,{code:404,message:`There is no defined address for path "${l}".`});return}t.lastRoute=l}}function m(e,t){return function({name:n,path:r,title:i,inHistory:a,redirect:o,data:s}){if(n==null||r==null)return null;i??=t.appName,a??=!1;let{UrlPattern:c}=e,l=new c(r);return{name:n,path:r,title:i,inHistory:a,pattern:l,redirect:o,data:s}}}function h(e,t){return function(e,n={}){let{routes:r}=t;if(!r[e])return console.error(`Address "${e}" is not registered`),null;let{pattern:i}=r[e];try{return i.stringify(n)}catch{return console.error(`Data provided for address "${e}" is not correct.`),null}}}function g(e,t){return function(){let{lastAddress:e,lastLocation:n,routes:r}=t,{pattern:i}=e?r[e]:{pattern:`null`},a=i.match(n);return[t.lastAddress,a]}}function _(e,t){return function(){let{eBus:n,history:r,dead:i}=e;t.isActive=!1,n.off(),r.destroy(),sessionStorage.removeItem(t.SSName),e.API={on:i,navigate:i,destroy:i}}}function v(e,t){return function(){return t.rt.map(e=>e.name)}}function y(e,t){return function(){let{rt:e}=t;return e.map(e=>`${e.name} ---> ${e.path}`)}}function b(e,t){let{history:n,eBus:r}=e;return function e(i,a={},o=!1){if(!t.isActive){console.error(`Router is not active. Use router.run() to activate it.`);return}let{lastAddress:s,lastLocation:c,routes:l}=t;if(!l[i]){console.error(`Address "${i}" is not registered`),r.emit(`_ERROR`,{code:404,message:`Address "${i}" is not registered`});return}let u=!1,{pattern:d,title:f,redirect:p,data:m}=l[i];if(p){e(p,m);return}s&&(u=l[s].inHistory);try{let e=d.stringify(a);if(e===c)return;t.lastLocation=e,sessionStorage.setItem(t.SSName,e),t.lastAddress=i,o&&(u=!1),n.write({state:{PGID:i,url:e,data:a},url:e,title:f},u)}catch(e){r.emit(`_ERROR`,{code:400,message:`Data provided for address "${i}" is not correct. ${e}`});return}}}function x(e,t){return function(n,r=[]){let{_setAddressRecord:i}=e.inAPI;return n.forEach(e=>{let n=i(e);if(!n||r.includes(n.name))return;let a=n.name;t.rt.push(n),t.routes[a]=n}),e.API}}function S(e,t){return function(n){let{rt:r}=t;return t.rt=r.reduce((e,r)=>{let{name:i}=r;return n.includes(i)?(delete t.routes[i],e):(e.push(r),e)},[]),e.API}}function C(e,t){return function(){let{inAPI:n,history:r}=e;t.isActive=!0,r.listen(n._historyActions),n._locationChange()}}var w={_historyActions:f,_locationChange:p,_setAddressRecord:m,createURL:h,getCurrentAddress:g,destroy:_,listAciveAddresses:v,listActiveRoutes:y,navigate:b,removeAddresses:S,run:C,setAddresses:x};function T(n){let r=(0,e.default)(),i=d(),{appName:a,sessionStorageKey:o}=n||{},s={lastLocation:``,lastAddress:null,SSName:`_routeEmmiterLastLocation`,appName:`App Name`,rt:[],routes:{},isActive:!1},c={UrlPattern:t.default,eBus:r,history:i,dead:()=>console.error(`Router was destroyed`)},l={},u={};return a&&typeof a==`string`&&(s.appName=a),o&&typeof o==`string`&&(s.SSName=o),Object.entries(w).forEach(([e,t])=>{e.startsWith(`_`)?u[e]=t(c,s):l[e]=t(c,s)}),c.inAPI=u,c.API={onChange:e=>(r.on(`_CHANGE`,e),c.API),onError:e=>(r.on(`_ERROR`,e),c.API),onReload:e=>(r.on(`_RELOAD`,e),c.API),back:e=>i.back(e),forward:e=>i.go(e),...l},c.API}return T});