UNPKG

3.75 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const __chunk_1 = require('./stencilrouter-a3d77a87.js');
6const __chunk_2 = require('./chunk-94c92d88.js');
7const __chunk_3 = require('./chunk-b786ea51.js');
8require('./chunk-e6311a56.js');
9
10const getUniqueId = () => {
11 return ((Math.random() * 10e16).toString().match(/.{4}/g) || []).join('-');
12};
13const getMatch = (pathname, url, exact) => {
14 return __chunk_3.matchPath(pathname, {
15 path: url,
16 exact: exact,
17 strict: true
18 });
19};
20const isHTMLStencilRouteElement = (elm) => {
21 return elm.tagName === 'STENCIL-ROUTE';
22};
23class RouteSwitch {
24 constructor(hostRef) {
25 __chunk_1.registerInstance(this, hostRef);
26 this.group = getUniqueId();
27 this.subscribers = [];
28 this.queue = __chunk_1.getContext(this, "queue");
29 }
30 componentWillLoad() {
31 if (this.location != null) {
32 this.regenerateSubscribers(this.location);
33 }
34 }
35 async regenerateSubscribers(newLocation) {
36 if (newLocation == null) {
37 return;
38 }
39 let newActiveIndex = -1;
40 this.subscribers = Array.prototype.slice.call(this.el.children)
41 .filter(isHTMLStencilRouteElement)
42 .map((childElement, index) => {
43 const match = getMatch(newLocation.pathname, childElement.url, childElement.exact);
44 if (match && newActiveIndex === -1) {
45 newActiveIndex = index;
46 }
47 return {
48 el: childElement,
49 match: match
50 };
51 });
52 if (newActiveIndex === -1) {
53 return;
54 }
55 // Check if this actually changes which child is active
56 // then just pass the new match down if the active route isn't changing.
57 if (this.activeIndex === newActiveIndex) {
58 this.subscribers[newActiveIndex].el.match = this.subscribers[newActiveIndex].match;
59 return;
60 }
61 this.activeIndex = newActiveIndex;
62 // Set all props on the new active route then wait until it says that it
63 // is completed
64 const activeChild = this.subscribers[this.activeIndex];
65 if (this.scrollTopOffset) {
66 activeChild.el.scrollTopOffset = this.scrollTopOffset;
67 }
68 activeChild.el.group = this.group;
69 activeChild.el.match = activeChild.match;
70 activeChild.el.componentUpdated = (routeViewUpdatedOptions) => {
71 // After the new active route has completed then update visibility of routes
72 this.queue.write(() => {
73 this.subscribers.forEach((child, index) => {
74 child.el.componentUpdated = undefined;
75 if (index === this.activeIndex) {
76 return child.el.style.display = '';
77 }
78 if (this.scrollTopOffset) {
79 child.el.scrollTopOffset = this.scrollTopOffset;
80 }
81 child.el.group = this.group;
82 child.el.match = null;
83 child.el.style.display = 'none';
84 });
85 });
86 if (this.routeViewsUpdated) {
87 this.routeViewsUpdated(Object.assign({ scrollTopOffset: this.scrollTopOffset }, routeViewUpdatedOptions));
88 }
89 };
90 }
91 render() {
92 return (__chunk_1.h("slot", null));
93 }
94 get el() { return __chunk_1.getElement(this); }
95 static get watchers() { return {
96 "location": ["regenerateSubscribers"]
97 }; }
98}
99__chunk_2.ActiveRouter.injectProps(RouteSwitch, [
100 'location',
101 'routeViewsUpdated'
102]);
103
104exports.stencil_route_switch = RouteSwitch;