UNPKG

@stencil/core

Version:

A Compiler for Web Components and Progressive Web Apps

1 lines • 11.1 kB
import{r as t,h as e,g as s,c as o}from"./p-c001178f.js";import{m as i,a as n,s as r,b as a,c,g as l,d as h,e as u,f as p,h as d,i as f,j as m,k as g,l as y,n as b,o as w,p as T}from"./p-8f0955b7.js";import{A as P}from"./p-5ada7d7c.js";class v{constructor(e){t(this,e)}render(){return e("stencil-router",{class:"full-screen"},e("stencil-route-switch",null,e("stencil-route",{url:"/:buildIdA/:buildIdB",exact:!0,component:"screenshot-compare",class:"full-screen"}),e("stencil-route",{url:"/:buildId",component:"screenshot-preview",class:"full-screen"}),e("stencil-route",{url:"/",component:"screenshot-preview",exact:!0,class:"full-screen"})))}}class O{constructor(e){t(this,e),this.group=null,this.match=null,this.componentProps={},this.exact=!1,this.scrollOnNextRender=!1,this.previousMatch=null}computeMatch(t){const e=null!=this.group||null!=this.el.parentElement&&"stencil-route-switch"===this.el.parentElement.tagName.toLowerCase();if(t&&!e)return this.previousMatch=this.match,this.match=i(t.pathname,{path:this.url,exact:this.exact,strict:!0})}async loadCompleted(){let t={};this.history&&this.history.location.hash?t={scrollToId:this.history.location.hash.substr(1)}:this.scrollTopOffset&&(t={scrollTopOffset:this.scrollTopOffset}),"function"==typeof this.componentUpdated?this.componentUpdated(t):this.match&&!n(this.match,this.previousMatch)&&this.routeViewsUpdated&&this.routeViewsUpdated(t)}async componentDidUpdate(){await this.loadCompleted()}async componentDidLoad(){await this.loadCompleted()}render(){if(!this.match||!this.history)return null;const t=Object.assign({},this.componentProps,{history:this.history,match:this.match});return this.routeRender?this.routeRender(Object.assign({},t,{component:this.component})):this.component?e(this.component,Object.assign({},t)):void 0}get el(){return s(this)}static get watchers(){return{location:["computeMatch"]}}static get style(){return"stencil-route.inactive{display:none}"}}P.injectProps(O,["location","history","historyType","routeViewsUpdated"]);const L=()=>((1e17*Math.random()).toString().match(/.{4}/g)||[]).join("-"),x=(t,e,s)=>i(t,{path:e,exact:s,strict:!0}),E=t=>"STENCIL-ROUTE"===t.tagName;class k{constructor(e){t(this,e),this.group=L(),this.subscribers=[],this.queue=o(this,"queue")}componentWillLoad(){null!=this.location&&this.regenerateSubscribers(this.location)}async regenerateSubscribers(t){if(null==t)return;let e=-1;if(this.subscribers=Array.prototype.slice.call(this.el.children).filter(E).map((s,o)=>{const i=x(t.pathname,s.url,s.exact);return i&&-1===e&&(e=o),{el:s,match:i}}),-1===e)return;if(this.activeIndex===e)return void(this.subscribers[e].el.match=this.subscribers[e].match);this.activeIndex=e;const s=this.subscribers[this.activeIndex];this.scrollTopOffset&&(s.el.scrollTopOffset=this.scrollTopOffset),s.el.group=this.group,s.el.match=s.match,s.el.componentUpdated=t=>{this.queue.write(()=>{this.subscribers.forEach((t,e)=>{if(t.el.componentUpdated=void 0,e===this.activeIndex)return t.el.style.display="";this.scrollTopOffset&&(t.el.scrollTopOffset=this.scrollTopOffset),t.el.group=this.group,t.el.match=null,t.el.style.display="none"})}),this.routeViewsUpdated&&this.routeViewsUpdated(Object.assign({scrollTopOffset:this.scrollTopOffset},t))}}render(){return e("slot",null)}get el(){return s(this)}static get watchers(){return{location:["regenerateSubscribers"]}}}P.injectProps(k,["location","routeViewsUpdated"]);const U=(t,...e)=>{t||console.warn(...e)},S=()=>{let t,e=[];return{setPrompt:e=>(U(null==t,"A history supports only one prompt at a time"),t=e,()=>{t===e&&(t=null)}),confirmTransitionTo:(e,s,o,i)=>{if(null!=t){const n="function"==typeof t?t(e,s):t;"string"==typeof n?"function"==typeof o?o(n,i):(U(!1,"A history needs a getUserConfirmation function in order to use a prompt message"),i(!0)):i(!1!==n)}else i(!0)},appendListener:t=>{let s=!0;const o=(...e)=>{s&&t(...e)};return e.push(o),()=>{s=!1,e=e.filter(t=>t!==o)}},notifyListeners:(...t)=>{e.forEach(e=>e(...t))}}},j=(t,e="scrollPositions")=>{let s=new Map;const o=(e,o)=>{if(s.set(e,o),r(t,"sessionStorage")){const e=[];s.forEach((t,s)=>{e.push([s,t])}),t.sessionStorage.setItem("scrollPositions",JSON.stringify(e))}};if(r(t,"sessionStorage")){const o=t.sessionStorage.getItem(e);s=o?new Map(JSON.parse(o)):s}return"scrollRestoration"in t.history&&(history.scrollRestoration="manual"),{set:o,get:t=>s.get(t),has:t=>s.has(t),capture:e=>{o(e,[t.scrollX,t.scrollY])}}},I={hashbang:{encodePath:t=>"!"===t.charAt(0)?t:"!/"+w(t),decodePath:t=>"!"===t.charAt(0)?t.substr(1):t},noslash:{encodePath:w,decodePath:u},slash:{encodePath:u,decodePath:u}},A=(t,e)=>{const s=0==t.pathname.indexOf(e)?"/"+t.pathname.slice(e.length):t.pathname;return Object.assign({},t,{pathname:s})},R={browser:(t,e={})=>{let s=!1;const o=t.history,i=t.location,n=t.navigator,r=a(t),b=!c(n),w=j(t),T=null!=e.forceRefresh&&e.forceRefresh,P=null!=e.getUserConfirmation?e.getUserConfirmation:l,v=null!=e.keyLength?e.keyLength:6,O=e.basename?h(u(e.basename)):"",L=()=>{try{return t.history.state||{}}catch(t){return{}}},x=t=>{t=t||{};const{key:e,state:s}=t,{pathname:o,search:n,hash:r}=i;let a=o+n+r;return U(!O||f(a,O),'You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "'+a+'" to begin with "'+O+'".'),O&&(a=m(a,O)),p(a,s,e||d(v))},E=S(),k=t=>{w.capture(Y.location.key),Object.assign(Y,t),Y.location.scrollPosition=w.get(Y.location.key),Y.length=o.length,E.notifyListeners(Y.location,Y.action)},I=t=>{y(n,t)||R(x(t.state))},A=()=>{R(x(L()))},R=t=>{if(s)s=!1,k();else{const e="POP";E.confirmTransitionTo(t,e,P,s=>{s?k({action:e,location:t}):C(t)})}},C=t=>{let e=V.indexOf(Y.location.key),o=V.indexOf(t.key);-1===e&&(e=0),-1===o&&(o=0);const i=e-o;i&&(s=!0,B(i))},H=x(L());let V=[H.key],M=0,q=!1;const _=t=>O+g(t),B=t=>{o.go(t)},N=e=>{1===(M+=e)?(t.addEventListener("popstate",I),b&&t.addEventListener("hashchange",A)):0===M&&(t.removeEventListener("popstate",I),b&&t.removeEventListener("hashchange",A))},Y={length:o.length,action:"POP",location:H,createHref:_,push:(t,e)=>{U(!("object"==typeof t&&void 0!==t.state&&void 0!==e),"You should avoid providing a 2nd state argument to push when the 1st argument is a location-like object that already has state; it is ignored");const s=p(t,e,d(v),Y.location);E.confirmTransitionTo(s,"PUSH",P,t=>{if(!t)return;const e=_(s),{key:n,state:a}=s;if(r)if(o.pushState({key:n,state:a},"",e),T)i.href=e;else{const t=V.indexOf(Y.location.key),e=V.slice(0,-1===t?0:t+1);e.push(s.key),V=e,k({action:"PUSH",location:s})}else U(void 0===a,"Browser history cannot push state in browsers that do not support HTML5 history"),i.href=e})},replace:(t,e)=>{U(!("object"==typeof t&&void 0!==t.state&&void 0!==e),"You should avoid providing a 2nd state argument to replace when the 1st argument is a location-like object that already has state; it is ignored");const s=p(t,e,d(v),Y.location);E.confirmTransitionTo(s,"REPLACE",P,t=>{if(!t)return;const e=_(s),{key:n,state:a}=s;if(r)if(o.replaceState({key:n,state:a},"",e),T)i.replace(e);else{const t=V.indexOf(Y.location.key);-1!==t&&(V[t]=s.key),k({action:"REPLACE",location:s})}else U(void 0===a,"Browser history cannot replace state in browsers that do not support HTML5 history"),i.replace(e)})},go:B,goBack:()=>B(-1),goForward:()=>B(1),block:(t="")=>{const e=E.setPrompt(t);return q||(N(1),q=!0),()=>(q&&(q=!1,N(-1)),e())},listen:t=>{const e=E.appendListener(t);return N(1),()=>{N(-1),e()}},win:t};return Y},hash:(t,e={})=>{let s=!1,o=null,i=0,n=!1;const r=t.location,a=t.history,c=b(t.navigator),y=null!=e.keyLength?e.keyLength:6,{getUserConfirmation:w=l,hashType:P="slash"}=e,v=e.basename?h(u(e.basename)):"",{encodePath:O,decodePath:L}=I[P],x=()=>{const t=r.href,e=t.indexOf("#");return-1===e?"":t.substring(e+1)},E=t=>{const e=r.href.indexOf("#");r.replace(r.href.slice(0,e>=0?e:0)+"#"+t)},k=()=>{let t=L(x());return U(!v||f(t,v),'You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "'+t+'" to begin with "'+v+'".'),v&&(t=m(t,v)),p(t,void 0,d(y))},j=S(),A=t=>{Object.assign(Y,t),Y.length=a.length,j.notifyListeners(Y.location,Y.action)},R=()=>{const t=x(),e=O(t);if(t!==e)E(e);else{const t=k(),e=Y.location;if(!s&&T(e,t))return;if(o===g(t))return;o=null,C(t)}},C=t=>{if(s)s=!1,A();else{const e="POP";j.confirmTransitionTo(t,e,w,s=>{s?A({action:e,location:t}):H(t)})}},H=t=>{let e=_.lastIndexOf(g(Y.location)),o=_.lastIndexOf(g(t));-1===e&&(e=0),-1===o&&(o=0);const i=e-o;i&&(s=!0,B(i))},V=x(),M=O(V);V!==M&&E(M);const q=k();let _=[g(q)];const B=t=>{U(c,"Hash history go(n) causes a full page reload in this browser"),a.go(t)},N=(t,e)=>{1===(i+=e)?t.addEventListener("hashchange",R):0===i&&t.removeEventListener("hashchange",R)},Y={length:a.length,action:"POP",location:q,createHref:t=>"#"+O(v+g(t)),push:(t,e)=>{U(void 0===e,"Hash history cannot push state; it is ignored");const s=p(t,void 0,d(y),Y.location);j.confirmTransitionTo(s,"PUSH",w,t=>{if(!t)return;const e=g(s),i=O(v+e);if(x()!==i){o=e,(t=>r.hash=t)(i);const t=_.lastIndexOf(g(Y.location)),n=_.slice(0,-1===t?0:t+1);n.push(e),_=n,A({action:"PUSH",location:s})}else U(!1,"Hash history cannot PUSH the same path; a new entry will not be added to the history stack"),A()})},replace:(t,e)=>{U(void 0===e,"Hash history cannot replace state; it is ignored");const s=p(t,void 0,d(y),Y.location);j.confirmTransitionTo(s,"REPLACE",w,t=>{if(!t)return;const e=g(s),i=O(v+e);x()!==i&&(o=e,E(i));const n=_.indexOf(g(Y.location));-1!==n&&(_[n]=e),A({action:"REPLACE",location:s})})},go:B,goBack:()=>B(-1),goForward:()=>B(1),block:(e="")=>{const s=j.setPrompt(e);return n||(N(t,1),n=!0),()=>(n&&(n=!1,N(t,-1)),s())},listen:e=>{const s=j.appendListener(e);return N(t,1),()=>{N(t,-1),s()}},win:t};return Y}};class C{constructor(e){t(this,e),this.root="/",this.historyType="browser",this.titleSuffix="",this.routeViewsUpdated=(t={})=>{if(this.history&&t.scrollToId&&"browser"===this.historyType){const e=this.history.win.document.getElementById(t.scrollToId);if(e)return e.scrollIntoView()}this.scrollTo(t.scrollTopOffset||this.scrollTopOffset)},this.isServer=o(this,"isServer"),this.queue=o(this,"queue")}componentWillLoad(){this.history=R[this.historyType](this.el.ownerDocument.defaultView),this.history.listen(t=>{t=A(t,this.root),this.location=t}),this.location=A(this.history.location,this.root)}scrollTo(t){const e=this.history;if(null!=t&&!this.isServer&&e)return"POP"===e.action&&Array.isArray(e.location.scrollPosition)?this.queue.write(()=>{e&&e.location&&Array.isArray(e.location.scrollPosition)&&e.win.scrollTo(e.location.scrollPosition[0],e.location.scrollPosition[1])}):this.queue.write(()=>{e.win.scrollTo(0,t)})}render(){if(this.location&&this.history)return e(P.Provider,{state:{historyType:this.historyType,location:this.location,titleSuffix:this.titleSuffix,root:this.root,history:this.history,routeViewsUpdated:this.routeViewsUpdated}},e("slot",null))}get el(){return s(this)}}export{v as app_root,O as stencil_route,k as stencil_route_switch,C as stencil_router};