@ionic/angular
Version:
Angular specific wrappers for @ionic/core
107 lines • 4 kB
JavaScript
export const insertView = (views, view, direction) => {
if (direction === 'root') {
return setRoot(views, view);
}
else if (direction === 'forward') {
return setForward(views, view);
}
else {
return setBack(views, view);
}
};
const setRoot = (views, view) => {
views = views.filter((v) => v.stackId !== view.stackId);
views.push(view);
return views;
};
const setForward = (views, view) => {
const index = views.indexOf(view);
if (index >= 0) {
views = views.filter((v) => v.stackId !== view.stackId || v.id <= view.id);
}
else {
views.push(view);
}
return views;
};
const setBack = (views, view) => {
const index = views.indexOf(view);
if (index >= 0) {
return views.filter((v) => v.stackId !== view.stackId || v.id <= view.id);
}
else {
return setRoot(views, view);
}
};
/**
* Whether going from the leaving route to the entering route runs a
* guard or resolver, any of which could cancel the navigation. Angular
* only deactivates and activates the routes below the last one the two
* share, but some checks run on every matched route.
*/
export const hasNavigationGuards = (leaving, entering) => {
const subtree = (route) => [route, ...route.children.flatMap(subtree)];
const isSameRoute = (a, b) => a.routeConfig === b.routeConfig && JSON.stringify(a.snapshot.params) === JSON.stringify(b.snapshot.params);
const leavingPath = leaving.pathFromRoot;
const enteringPath = entering.pathFromRoot;
let shared = 0;
while (shared < leavingPath.length - 1 &&
shared < enteringPath.length - 1 &&
isSameRoute(leavingPath[shared], enteringPath[shared])) {
shared++;
}
const deactivated = [...leavingPath.slice(shared, -1), ...subtree(leaving)];
const activated = [...enteringPath.slice(shared, -1), ...subtree(entering)];
const matched = [...enteringPath.slice(0, -1), ...subtree(entering)];
return (deactivated.some(runsDeactivateGuard) ||
activated.some(runsActivateGuardOrResolver) ||
matched.some(runsOnEveryMatch));
};
// A title that isn't a string is a resolver.
const hasResolvers = (config) => Object.keys(config.resolve ?? {}).length > 0 || (config.title !== undefined && typeof config.title !== 'string');
const runsDeactivateGuard = ({ routeConfig }) => !!routeConfig?.canDeactivate?.length;
const runsActivateGuardOrResolver = ({ routeConfig }) => !!routeConfig && (!!routeConfig.canActivate?.length || hasResolvers(routeConfig));
const runsOnEveryMatch = ({ routeConfig }) => !!routeConfig &&
(!!routeConfig.canMatch?.length ||
!!routeConfig.canActivateChild?.length ||
(routeConfig.runGuardsAndResolvers !== undefined &&
routeConfig.runGuardsAndResolvers !== 'paramsChange' &&
(!!routeConfig.canActivate?.length || !!routeConfig.canDeactivate?.length || hasResolvers(routeConfig))));
export const getUrl = (router, activatedRoute) => {
const urlTree = router.createUrlTree(['.'], { relativeTo: activatedRoute });
return router.serializeUrl(urlTree);
};
export const isTabSwitch = (enteringView, leavingView) => {
if (!leavingView) {
return true;
}
return enteringView.stackId !== leavingView.stackId;
};
export const computeStackId = (prefixUrl, url) => {
if (!prefixUrl) {
return undefined;
}
const segments = toSegments(url);
for (let i = 0; i < segments.length; i++) {
if (i >= prefixUrl.length) {
return segments[i];
}
if (segments[i] !== prefixUrl[i]) {
return undefined;
}
}
return undefined;
};
export const toSegments = (path) => {
return path
.split('/')
.map((s) => s.trim())
.filter((s) => s !== '');
};
export const destroyView = (view) => {
if (view) {
view.ref.destroy();
view.unlistenEvents();
}
};
//# sourceMappingURL=stack-utils.js.map