/
ViewStacks.ts
88 lines (75 loc) · 2.14 KB
/
ViewStacks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Location as HistoryLocation } from 'history';
import { matchPath } from 'react-router-dom';
import { IonRouteData } from './IonRouteData';
import { ViewItem } from './ViewItem';
export interface ViewStack {
id: string;
routerOutlet: HTMLIonRouterOutletElement;
views: ViewItem[];
}
/**
* The holistic view of all the Routes configured for an application inside of an IonRouterOutlet.
*/
export class ViewStacks {
private viewStacks: { [key: string]: ViewStack | undefined } = {};
get(key: string) {
return this.viewStacks[key];
}
set(key: string, viewStack: ViewStack) {
this.viewStacks[key] = viewStack;
}
getKeys() {
return Object.keys(this.viewStacks);
}
delete(key: string) {
delete this.viewStacks[key];
}
findViewInfoByLocation(location: HistoryLocation, viewKey?: string) {
let view: ViewItem<IonRouteData> | undefined;
let match: IonRouteData['match'] | null | undefined;
let viewStack: ViewStack | undefined;
if (viewKey) {
viewStack = this.viewStacks[viewKey];
if (viewStack) {
viewStack.views.some(matchView);
}
} else {
const keys = this.getKeys();
keys.some(key => {
viewStack = this.viewStacks[key];
return viewStack!.views.some(matchView);
});
}
const result = { view, viewStack, match };
return result;
function matchView(v: ViewItem) {
const matchProps = {
exact: v.routeData.childProps.exact,
path: v.routeData.childProps.path || v.routeData.childProps.from,
component: v.routeData.childProps.component
};
match = matchPath(location.pathname, matchProps);
if (match) {
view = v;
return true;
}
return false;
}
}
findViewInfoById(id = '') {
let view: ViewItem<IonRouteData> | undefined;
let viewStack: ViewStack | undefined;
const keys = this.getKeys();
keys.some(key => {
const vs = this.viewStacks[key];
view = vs!.views.find(x => x.id === id);
if (view) {
viewStack = vs;
return true;
} else {
return false;
}
});
return { view, viewStack };
}
}