UNPKG

@tindtechnologies/universalviewer

Version:

The Universal Viewer is a community-developed open source project on a mission to help you share your 📚📜📰📽️📻🗿 with the 🌎

2 lines (1 loc) 11.5 kB
import{k as w}from"./chunk-4DAZ7WDM.js";import{a as N,i as b,l as E}from"./chunk-VNJCZCB4.js";import"./chunk-L67DAW3A.js";var c=function(r,t,e,n){return new(e||(e=Promise))(function(i,o){function u(a){try{l(n.next(a))}catch(f){o(f)}}function s(a){try{l(n.throw(a))}catch(f){o(f)}}function l(a){a.done?i(a.value):new e(function(f){f(a.value)}).then(u,s)}l((n=n.apply(r,t||[])).next())})},h=function(r,t){var e={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},n,i,o,u;return u={next:s(0),throw:s(1),return:s(2)},typeof Symbol=="function"&&(u[Symbol.iterator]=function(){return this}),u;function s(a){return function(f){return l([a,f])}}function l(a){if(n)throw new TypeError("Generator is already executing.");for(;e;)try{if(n=1,i&&(o=a[0]&2?i.return:a[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,a[1])).done)return o;switch(i=0,o&&(a=[a[0]&2,o.value]),a[0]){case 0:case 1:o=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,i=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!o||a[1]>o[0]&&a[1]<o[3])){e.label=a[1];break}if(a[0]===6&&e.label<o[1]){e.label=o[1],o=a;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(a);break}o[2]&&e.ops.pop(),e.trys.pop();continue}a=t.call(r,e)}catch(f){a=[6,f],i=0}finally{n=o=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}},T=void 0,p="root",O="forward",j="back",d=function(r){var t=r.filter(function(e){return e.length>0}).join("/");return"/"+t},H=function(r){for(var t=[],e=0,n=r;e<n.length;e++)for(var i=n[e],o=0,u=i.path;o<u.length;o++){var s=u[o];if(s[0]===":"){var l=i.params&&i.params[s.slice(1)];if(!l)return null;t.push(l)}else s!==""&&t.push(s)}return t},M=function(r,t,e,n,i,o){var u=d(v(t).concat(n));e&&(u="#"+u),i===O?r.pushState(o,"",u):r.replaceState(o,"",u)},F=function(r,t){if(r.length>t.length)return null;if(r.length<=1&&r[0]==="")return t;for(var e=0;e<r.length;e++)if(r[e].length>0&&r[e]!==t[e])return null;return t.length===r.length?[""]:t.slice(r.length)},B=function(r,t,e){var n=r.pathname;if(e){var i=r.hash;n=i[0]==="#"?i.slice(1):""}var o=v(t),u=v(n);return F(o,u)},v=function(r){if(r==null)return[""];var t=r.split("/").map(function(e){return e.trim()}).filter(function(e){return e.length>0});return t.length===0?[""]:t},V=function(r){console.group("[ion-core] ROUTES["+r.length+"]");for(var t=function(o){var u=[];o.forEach(function(l){return u.push.apply(u,l.path)});var s=o.map(function(l){return l.id});console.debug("%c "+d(u),"font-weight: bold; padding-left: 20px","=> ","("+s.join(", ")+")")},e=0,n=r;e<n.length;e++){var i=n[e];t(i)}console.groupEnd()},q=function(r){console.group("[ion-core] REDIRECTS["+r.length+"]");for(var t=0,e=r;t<e.length;t++){var n=e[t];n.to&&console.debug("FROM: ","$c "+d(n.from),"font-weight: bold"," TO: ","$c "+d(n.to),"font-weight: bold")}console.groupEnd()},U=function(r,t,e,n,i){return i===void 0&&(i=!1),c(T,void 0,void 0,function(){var o,u,s,l;return h(this,function(a){switch(a.label){case 0:return a.trys.push([0,6,,7]),o=P(r),n>=t.length||!o?[2,i]:[4,o.componentOnReady()];case 1:return a.sent(),u=t[n],[4,o.setRouteId(u.id,u.params,e)];case 2:return s=a.sent(),s.changed&&(e=p,i=!0),[4,U(s.element,t,e,n+1,i)];case 3:return i=a.sent(),s.markVisible?[4,s.markVisible()]:[3,5];case 4:a.sent(),a.label=5;case 5:return[2,i];case 6:return l=a.sent(),console.error(l),[2,!1];case 7:return[2]}})})},$=function(r){return c(T,void 0,void 0,function(){var t,e,n,i;return h(this,function(o){switch(o.label){case 0:t=[],n=r,o.label=1;case 1:return e=P(n),e?[4,e.getRouteId()]:[3,3];case 2:if(i=o.sent(),i)n=i.element,i.element=void 0,t.push(i);else return[3,5];return[3,4];case 3:return[3,5];case 4:return[3,1];case 5:return[2,{ids:t,outlet:e}]}})})},G=function(){return P(document.body)?Promise.resolve():new Promise(function(r){window.addEventListener("ionNavWillLoad",r,{once:!0})})},S=":not([no-router]) ion-nav, :not([no-router]) ion-tabs, :not([no-router]) ion-router-outlet",P=function(r){if(r){if(r.matches(S))return r;var t=r.querySelector(S);return t||void 0}},K=function(r,t){var e=t.from,n=t.to;if(n===void 0||e.length>r.length)return!1;for(var i=0;i<e.length;i++){var o=e[i];if(o==="*")return!0;if(o!==r[i])return!1}return e.length===r.length},C=function(r,t){return t.find(function(e){return K(r,e)})},Q=function(r,t){for(var e=Math.min(r.length,t.length),n=0;n<e&&r[n].toLowerCase()===t[n].id;n++);return n},Y=function(r,t){for(var e=new Z(r),n=!1,i,o=0;o<t.length;o++){var u=t[o].path;if(u[0]==="")n=!0;else{for(var s=0,l=u;s<l.length;s++){var a=l[s],f=e.next();if(a[0]===":"){if(f==="")return null;i=i||[];var L=i[o]||(i[o]={});L[a.slice(1)]=f}else if(f!==a)return null}n=!1}}var W=n?n===(e.next()===""):!0;return W?i?t.map(function(m,A){return{id:m.id,path:m.path,params:k(m.params,i[A])}}):t:null},k=function(r,t){if(!r&&t)return t;if(r&&!t)return r;if(r&&t)return Object.assign(Object.assign({},r),t)},z=function(r,t){for(var e=null,n=0,i=r.map(function(a){return a.id}),o=0,u=t;o<u.length;o++){var s=u[o],l=Q(i,s);l>n&&(e=s,n=l)}return e?e.map(function(a,f){return{id:a.id,path:a.path,params:k(a.params,r[f]&&r[f].params)}}):null},J=function(r,t){for(var e=null,n=0,i=0,o=t;i<o.length;i++){var u=o[i],s=Y(r,u);if(s!==null){var l=X(s);l>n&&(n=l,e=s)}}return e},X=function(r){for(var t=1,e=1,n=0,i=r;n<i.length;n++)for(var o=i[n],u=0,s=o.path;u<s.length;u++){var l=s[u];l[0]===":"?t+=Math.pow(1,e):l!==""&&(t+=Math.pow(2,e)),e++}return t},Z=function(){function r(t){this.path=t.slice()}return r.prototype.next=function(){return this.path.length>0?this.path.shift():""},r}(),y=function(r){return Array.from(r.children).filter(function(t){return t.tagName==="ION-ROUTE-REDIRECT"}).map(function(t){var e=g(t,"to");return{from:v(g(t,"from")),to:e==null?void 0:v(e)}})},R=function(r){return x(D(r))},D=function(r,t){return t===void 0&&(t=r),Array.from(t.children).filter(function(e){return e.tagName==="ION-ROUTE"&&e.component}).map(function(e){var n=g(e,"component");if(n==null)throw new Error("component missing in ion-route");return{path:v(g(e,"url")),id:n.toLowerCase(),params:e.componentProps,children:D(r,e)}})},g=function(r,t){return t in r?r[t]:r.hasAttribute(t)?r.getAttribute(t):null},x=function(r){for(var t=[],e=0,n=r;e<n.length;e++){var i=n[e];I([],t,i)}return t},I=function(r,t,e){var n=r.slice();if(n.push({id:e.id,path:e.path,params:e.params}),e.children.length===0){t.push(n);return}for(var i=0,o=e.children;i<o.length;i++){var u=o[i];I(n,t,u)}},et=function(){function r(t){N(this,t),this.previousPath=null,this.busy=!1,this.state=0,this.lastState=0,this.root="/",this.useHash=!0,this.ionRouteWillChange=b(this,"ionRouteWillChange",7),this.ionRouteDidChange=b(this,"ionRouteDidChange",7)}return r.prototype.componentWillLoad=function(){return c(this,void 0,void 0,function(){return h(this,function(t){switch(t.label){case 0:return console.debug("[ion-router] router will load"),[4,G()];case 1:return t.sent(),console.debug("[ion-router] found nav"),[4,this.onRoutesChanged()];case 2:return t.sent(),[2]}})})},r.prototype.componentDidLoad=function(){window.addEventListener("ionRouteRedirectChanged",w(this.onRedirectChanged.bind(this),10)),window.addEventListener("ionRouteDataChanged",w(this.onRoutesChanged.bind(this),100))},r.prototype.onPopState=function(){var t=this.historyDirection(),e=this.getPath();return console.debug("[ion-router] URL changed -> update nav",e,t),this.writeNavStateRoot(e,t)},r.prototype.onBackButton=function(t){var e=this;t.detail.register(0,function(){return e.back()})},r.prototype.push=function(t,e){e===void 0&&(e="forward"),t.startsWith(".")&&(t=new URL(t,window.location.href).pathname),console.debug("[ion-router] URL pushed -> updating nav",t,e);var n=v(t);return this.setPath(n,e),this.writeNavStateRoot(n,e)},r.prototype.back=function(){return window.history.back(),Promise.resolve(this.waitPromise)},r.prototype.printDebug=function(){return c(this,void 0,void 0,function(){return h(this,function(t){return console.debug("CURRENT PATH",this.getPath()),console.debug("PREVIOUS PATH",this.previousPath),V(R(this.el)),q(y(this.el)),[2]})})},r.prototype.navChanged=function(t){return c(this,void 0,void 0,function(){var e,n,i,o,u,s;return h(this,function(l){switch(l.label){case 0:return this.busy?(console.warn("[ion-router] router is busy, navChanged was cancelled"),[2,!1]):[4,$(window.document.body)];case 1:return e=l.sent(),n=e.ids,i=e.outlet,o=R(this.el),u=z(n,o),u?(s=H(u),s?(console.debug("[ion-router] nav changed -> update URL",n,s),this.setPath(s,t),[4,this.safeWriteNavState(i,u,p,s,null,n.length)]):(console.warn("[ion-router] router could not match path because some required param is missing"),[2,!1])):(console.warn("[ion-router] no matching URL for ",n.map(function(a){return a.id})),[2,!1]);case 2:return l.sent(),[2,!0]}})})},r.prototype.onRedirectChanged=function(){var t=this.getPath();t&&C(t,y(this.el))&&this.writeNavStateRoot(t,p)},r.prototype.onRoutesChanged=function(){return this.writeNavStateRoot(this.getPath(),p)},r.prototype.historyDirection=function(){var t=window;t.history.state===null&&(this.state++,t.history.replaceState(this.state,t.document.title,t.document.location&&t.document.location.href));var e=t.history.state,n=this.lastState;return this.lastState=e,e>n?O:e<n?j:p},r.prototype.writeNavStateRoot=function(t,e){return c(this,void 0,void 0,function(){var n,i,o,u,s;return h(this,function(l){return t?(n=y(this.el),i=C(t,n),o=null,i&&(this.setPath(i.to,e),o=i.from,t=i.to),u=R(this.el),s=J(t,u),s?[2,this.safeWriteNavState(document.body,s,e,t,o)]:(console.error("[ion-router] the path does not match any route"),[2,!1])):(console.error("[ion-router] URL is not part of the routing set"),[2,!1])})})},r.prototype.safeWriteNavState=function(t,e,n,i,o,u){return u===void 0&&(u=0),c(this,void 0,void 0,function(){var s,l,a;return h(this,function(f){switch(f.label){case 0:return[4,this.lock()];case 1:s=f.sent(),l=!1,f.label=2;case 2:return f.trys.push([2,4,,5]),[4,this.writeNavState(t,e,n,i,o,u)];case 3:return l=f.sent(),[3,5];case 4:return a=f.sent(),console.error(a),[3,5];case 5:return s(),[2,l]}})})},r.prototype.lock=function(){return c(this,void 0,void 0,function(){var t,e;return h(this,function(n){switch(n.label){case 0:return t=this.waitPromise,this.waitPromise=new Promise(function(i){return e=i}),t===void 0?[3,2]:[4,t];case 1:n.sent(),n.label=2;case 2:return[2,e]}})})},r.prototype.writeNavState=function(t,e,n,i,o,u){return u===void 0&&(u=0),c(this,void 0,void 0,function(){var s,l;return h(this,function(a){switch(a.label){case 0:return this.busy?(console.warn("[ion-router] router is busy, transition was cancelled"),[2,!1]):(this.busy=!0,s=this.routeChangeEvent(i,o),s&&this.ionRouteWillChange.emit(s),[4,U(t,e,n,u)]);case 1:return l=a.sent(),this.busy=!1,l&&console.debug("[ion-router] route changed",i),s&&this.ionRouteDidChange.emit(s),[2,l]}})})},r.prototype.setPath=function(t,e){this.state++,M(window.history,this.root,this.useHash,t,e,this.state)},r.prototype.getPath=function(){return B(window.location,this.root,this.useHash)},r.prototype.routeChangeEvent=function(t,e){var n=this.previousPath,i=d(t);if(this.previousPath=i,i===n)return null;var o=e?d(e):null;return{from:n,redirectedFrom:o,to:i}},Object.defineProperty(r.prototype,"el",{get:function(){return E(this)},enumerable:!0,configurable:!0}),r}();export{et as ion_router};