UNPKG

test-isc

Version:

An Ionic component similar to Ionic Select, that allows to search items, including async search, group, add, edit, delete items, and much more.

1 lines 7.47 kB
import{r as t,d as o,e as n}from"./p-aa742337.js";import{e as r}from"./p-54ff9b40.js";const e=t=>"/"+t.filter(t=>t.length>0).join("/"),s=t=>{if(null==t)return[""];const o=t.split("?")[0].split("/").map(t=>t.trim()).filter(t=>t.length>0);return 0===o.length?[""]:o},i=async(t,o,n,r,e=!1)=>{try{const s=a(t);if(r>=o.length||!s)return e;await s.componentOnReady();const c=o[r],u=await s.setRouteId(c.id,c.params,n);return u.changed&&(n="root",e=!0),e=await i(u.element,o,n,r+1,e),u.markVisible&&await u.markVisible(),e}catch(s){return console.error(s),!1}},c=":not([no-router]) ion-nav, :not([no-router]) ion-tabs, :not([no-router]) ion-router-outlet",a=t=>{if(!t)return;if(t.matches(c))return t;return t.querySelector(c)||void 0},u=(t,o)=>o.find(o=>((t,o)=>{const{from:n,to:r}=o;if(void 0===r)return!1;if(n.length>t.length)return!1;for(let e=0;e<n.length;e++){const o=n[e];if("*"===o)return!0;if(o!==t[e])return!1}return n.length===t.length})(t,o)),l=(t,o)=>{const n=Math.min(t.length,o.length);let r=0;for(;r<n&&t[r].toLowerCase()===o[r].id;r++);return r},h=(t,o)=>{const n=new w(t);let r,e=!1;for(let s=0;s<o.length;s++){const t=o[s].path;if(""===t[0])e=!0;else{for(const o of t){const t=n.next();if(":"===o[0]){if(""===t)return null;r=r||[],(r[s]||(r[s]={}))[o.slice(1)]=t}else if(t!==o)return null}e=!1}}return e&&e!==(""===n.next())?null:r?o.map((t,o)=>({id:t.id,path:t.path,params:f(t.params,r[o])})):o},f=(t,o)=>!t&&o?o:t&&!o?t:t&&o?Object.assign(Object.assign({},t),o):void 0,d=t=>{let o=1,n=1;for(const r of t)for(const t of r.path)":"===t[0]?o+=Math.pow(1,n):""!==t&&(o+=Math.pow(2,n)),n++;return o};class w{constructor(t){this.path=t.slice()}next(){return this.path.length>0?this.path.shift():""}}const m=t=>Array.from(t.children).filter(t=>"ION-ROUTE-REDIRECT"===t.tagName).map(t=>{const o=g(t,"to");return{from:s(g(t,"from")),to:null==o?void 0:s(o)}}),p=t=>v(R(t)),R=(t,o=t)=>Array.from(o.children).filter(t=>"ION-ROUTE"===t.tagName&&t.component).map(o=>{const n=g(o,"component");if(null==n)throw new Error("component missing in ion-route");return{path:s(g(o,"url")),id:n.toLowerCase(),params:o.componentProps,children:R(t,o)}}),g=(t,o)=>o in t?t[o]:t.hasAttribute(o)?t.getAttribute(o):null,v=t=>{const o=[];for(const n of t)y([],o,n);return o},y=(t,o,n)=>{const r=t.slice();if(r.push({id:n.id,path:n.path,params:n.params}),0!==n.children.length)for(const e of n.children)y(r,o,e);else o.push(r)},b=class{constructor(n){t(this,n),this.previousPath=null,this.busy=!1,this.state=0,this.lastState=0,this.root="/",this.useHash=!0,this.ionRouteWillChange=o(this,"ionRouteWillChange",7),this.ionRouteDidChange=o(this,"ionRouteDidChange",7)}async componentWillLoad(){console.debug("[ion-router] router will load"),await(a(document.body)?Promise.resolve():new Promise(t=>{window.addEventListener("ionNavWillLoad",t,{once:!0})})),console.debug("[ion-router] found nav"),await this.onRoutesChanged()}componentDidLoad(){window.addEventListener("ionRouteRedirectChanged",r(this.onRedirectChanged.bind(this),10)),window.addEventListener("ionRouteDataChanged",r(this.onRoutesChanged.bind(this),100))}onPopState(){const t=this.historyDirection(),o=this.getPath();return console.debug("[ion-router] URL changed -> update nav",o,t),this.writeNavStateRoot(o,t)}onBackButton(t){t.detail.register(0,t=>{this.back(),t()})}push(t,o="forward"){t.startsWith(".")&&(t=new URL(t,window.location.href).pathname),console.debug("[ion-router] URL pushed -> updating nav",t,o);const n=s(t),r=t.split("?")[1];return this.setPath(n,o,r),this.writeNavStateRoot(n,o)}back(){return window.history.back(),Promise.resolve(this.waitPromise)}async printDebug(){console.debug("CURRENT PATH",this.getPath()),console.debug("PREVIOUS PATH",this.previousPath),(t=>{console.group(`[ion-core] ROUTES[${t.length}]`);for(const o of t){const t=[];o.forEach(o=>t.push(...o.path));const n=o.map(t=>t.id);console.debug("%c "+e(t),"font-weight: bold; padding-left: 20px","=>\t",`(${n.join(", ")})`)}console.groupEnd()})(p(this.el)),(t=>{console.group(`[ion-core] REDIRECTS[${t.length}]`);for(const o of t)o.to&&console.debug("FROM: ","$c "+e(o.from),"font-weight: bold"," TO: ","$c "+e(o.to),"font-weight: bold");console.groupEnd()})(m(this.el))}async navChanged(t){if(this.busy)return console.warn("[ion-router] router is busy, navChanged was cancelled"),!1;const{ids:o,outlet:n}=await(async()=>{const t=[];let o,n=window.document.body;for(;o=a(n),o;){const r=await o.getRouteId();if(!r)break;n=r.element,r.element=void 0,t.push(r)}return{ids:t,outlet:o}})(),r=((t,o)=>{let n=null,r=0;const e=t.map(t=>t.id);for(const s of o){const t=l(e,s);t>r&&(n=s,r=t)}return n?n.map((o,n)=>({id:o.id,path:o.path,params:f(o.params,t[n]&&t[n].params)})):null})(o,p(this.el));if(!r)return console.warn("[ion-router] no matching URL for ",o.map(t=>t.id)),!1;const e=(t=>{const o=[];for(const n of t)for(const t of n.path)if(":"===t[0]){const r=n.params&&n.params[t.slice(1)];if(!r)return null;o.push(r)}else""!==t&&o.push(t);return o})(r);return e?(console.debug("[ion-router] nav changed -> update URL",o,e),this.setPath(e,t),await this.safeWriteNavState(n,r,"root",e,null,o.length),!0):(console.warn("[ion-router] router could not match path because some required param is missing"),!1)}onRedirectChanged(){const t=this.getPath();t&&u(t,m(this.el))&&this.writeNavStateRoot(t,"root")}onRoutesChanged(){return this.writeNavStateRoot(this.getPath(),"root")}historyDirection(){const t=window;null===t.history.state&&(this.state++,t.history.replaceState(this.state,t.document.title,t.document.location&&t.document.location.href));const o=t.history.state,n=this.lastState;return this.lastState=o,o>n?"forward":o<n?"back":"root"}async writeNavStateRoot(t,o){if(!t)return console.error("[ion-router] URL is not part of the routing set"),!1;const n=m(this.el),r=u(t,n);let e=null;r&&(this.setPath(r.to,o),e=r.from,t=r.to);const s=((t,o)=>{let n=null,r=0;for(const e of o){const o=h(t,e);if(null!==o){const t=d(o);t>r&&(r=t,n=o)}}return n})(t,p(this.el));return s?this.safeWriteNavState(document.body,s,o,t,e):(console.error("[ion-router] the path does not match any route"),!1)}async safeWriteNavState(t,o,n,r,e,s=0){const i=await this.lock();let c=!1;try{c=await this.writeNavState(t,o,n,r,e,s)}catch(a){console.error(a)}return i(),c}async lock(){const t=this.waitPromise;let o;return this.waitPromise=new Promise(t=>o=t),void 0!==t&&await t,o}async writeNavState(t,o,n,r,e,s=0){if(this.busy)return console.warn("[ion-router] router is busy, transition was cancelled"),!1;this.busy=!0;const c=this.routeChangeEvent(r,e);c&&this.ionRouteWillChange.emit(c);const a=await i(t,o,n,s);return this.busy=!1,a&&console.debug("[ion-router] route changed",r),c&&this.ionRouteDidChange.emit(c),a}setPath(t,o,n){this.state++,((t,o,n,r,i,c,a)=>{let u=e([...s(this.root),...r]);n&&(u="#"+u),void 0!==a&&(u=u+"?"+a),"forward"===i?t.pushState(c,"",u):t.replaceState(c,"",u)})(window.history,0,this.useHash,t,o,this.state,n)}getPath(){return((t,o)=>{let n=t.pathname;if(this.useHash){const o=t.hash;n="#"===o[0]?o.slice(1):""}return((t,o)=>{if(t.length>o.length)return null;if(t.length<=1&&""===t[0])return o;for(let n=0;n<t.length;n++)if(t[n].length>0&&t[n]!==o[n])return null;return o.length===t.length?[""]:o.slice(t.length)})(s(o),s(n))})(window.location,this.root)}routeChangeEvent(t,o){const n=this.previousPath,r=e(t);return this.previousPath=r,r===n?null:{from:n,redirectedFrom:o?e(o):null,to:r}}get el(){return n(this)}};export{b as ion_router}