mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 18:17:31 +08:00
chore(vue-router): add eslint and prettier (#26636)
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { RouteInfo } from './types';
|
||||
import type { RouteInfo } from "./types";
|
||||
|
||||
export const createLocationHistory = () => {
|
||||
const locationHistory: RouteInfo[] = [];
|
||||
@ -14,20 +14,22 @@ export const createLocationHistory = () => {
|
||||
break;
|
||||
}
|
||||
|
||||
if (routeInfo.routerDirection === 'root') {
|
||||
if (routeInfo.routerDirection === "root") {
|
||||
clearHistory();
|
||||
addRoute(routeInfo);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const update = (routeInfo: RouteInfo) => {
|
||||
const locationIndex = locationHistory.findIndex(x => x.id === routeInfo.id);
|
||||
const locationIndex = locationHistory.findIndex(
|
||||
(x) => x.id === routeInfo.id
|
||||
);
|
||||
if (locationIndex > -1) {
|
||||
locationHistory.splice(locationIndex, 1, routeInfo);
|
||||
}
|
||||
const tabArray = tabsHistory[routeInfo.tab || ''];
|
||||
const tabArray = tabsHistory[routeInfo.tab || ""];
|
||||
if (tabArray) {
|
||||
const tabIndex = tabArray.findIndex(x => x.id === routeInfo.id);
|
||||
const tabIndex = tabArray.findIndex((x) => x.id === routeInfo.id);
|
||||
if (tabIndex > -1) {
|
||||
tabArray.splice(tabIndex, 1, routeInfo);
|
||||
} else {
|
||||
@ -36,7 +38,7 @@ export const createLocationHistory = () => {
|
||||
} else if (routeInfo.tab) {
|
||||
tabsHistory[routeInfo.tab] = [routeInfo];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const pop = (routeInfo: RouteInfo) => {
|
||||
const tabHistory = getTabsHistory(routeInfo.tab);
|
||||
@ -61,19 +63,22 @@ export const createLocationHistory = () => {
|
||||
// Replace with updated route
|
||||
locationHistory.pop();
|
||||
locationHistory.push(routeInfo);
|
||||
}
|
||||
};
|
||||
|
||||
const addRoute = (routeInfo: RouteInfo) => {
|
||||
const tabHistory = getTabsHistory(routeInfo.tab);
|
||||
if (tabHistory) {
|
||||
// If the latest routeInfo is the same (going back and forth between tabs), replace it
|
||||
if (tabHistory[tabHistory.length - 1] && tabHistory[tabHistory.length - 1].id === routeInfo.id) {
|
||||
if (
|
||||
tabHistory[tabHistory.length - 1] &&
|
||||
tabHistory[tabHistory.length - 1].id === routeInfo.id
|
||||
) {
|
||||
tabHistory.pop();
|
||||
}
|
||||
tabHistory.push(routeInfo);
|
||||
}
|
||||
locationHistory.push(routeInfo);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wipes the location history arrays.
|
||||
@ -90,17 +95,21 @@ export const createLocationHistory = () => {
|
||||
* then there will not be any route index in
|
||||
* tabs either.
|
||||
*/
|
||||
const existingRouteIndex = locationHistory.findIndex(r => r.position === position);
|
||||
const existingRouteIndex = locationHistory.findIndex(
|
||||
(r) => r.position === position
|
||||
);
|
||||
if (existingRouteIndex === -1) return;
|
||||
|
||||
locationHistory.splice(existingRouteIndex);
|
||||
|
||||
const clearTabHistory = (tab: string) => {
|
||||
const existingTabRouteIndex = tabsHistory[tab].findIndex(r => r.position === position);
|
||||
const existingTabRouteIndex = tabsHistory[tab].findIndex(
|
||||
(r) => r.position === position
|
||||
);
|
||||
if (existingTabRouteIndex === -1) return;
|
||||
|
||||
tabsHistory[tab].splice(existingTabRouteIndex);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* We also need to search the current tab
|
||||
@ -112,19 +121,19 @@ export const createLocationHistory = () => {
|
||||
const tabHistory = tabsHistory[tab];
|
||||
if (tab && tabHistory) {
|
||||
clearTabHistory(tab);
|
||||
/**
|
||||
* If we are not clearing items after
|
||||
* a tabs page, it is still possible
|
||||
* that there are future tabs pages to clear.
|
||||
* As a result, we need to search through
|
||||
* all the tab stacks and remove views that appear
|
||||
* after the given routeInfo.
|
||||
*
|
||||
* Example: /non-tabs-page --> /tabs/tab1 --> /non-tabs-page
|
||||
* (via router.go(-1)) --> /tabs/tab2. The /tabs/tab1 history
|
||||
* has been overwritten with /tabs/tab2. As a result,
|
||||
* the /tabs/tab1 route info in the Tab 1 stack should be removed.
|
||||
*/
|
||||
/**
|
||||
* If we are not clearing items after
|
||||
* a tabs page, it is still possible
|
||||
* that there are future tabs pages to clear.
|
||||
* As a result, we need to search through
|
||||
* all the tab stacks and remove views that appear
|
||||
* after the given routeInfo.
|
||||
*
|
||||
* Example: /non-tabs-page --> /tabs/tab1 --> /non-tabs-page
|
||||
* (via router.go(-1)) --> /tabs/tab2. The /tabs/tab1 history
|
||||
* has been overwritten with /tabs/tab2. As a result,
|
||||
* the /tabs/tab1 route info in the Tab 1 stack should be removed.
|
||||
*/
|
||||
} else {
|
||||
for (const tab in tabsHistory) {
|
||||
clearTabHistory(tab);
|
||||
@ -137,7 +146,7 @@ export const createLocationHistory = () => {
|
||||
|
||||
locationHistory.length = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
const getTabsHistory = (tab: string): RouteInfo[] => {
|
||||
let history;
|
||||
if (tab) {
|
||||
@ -148,7 +157,7 @@ export const createLocationHistory = () => {
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
};
|
||||
|
||||
const size = () => locationHistory.length;
|
||||
|
||||
@ -166,7 +175,7 @@ export const createLocationHistory = () => {
|
||||
*/
|
||||
const index = currentHistory - initialHistory;
|
||||
return locationHistory[index] || last();
|
||||
}
|
||||
};
|
||||
const last = () => locationHistory[locationHistory.length - 1];
|
||||
|
||||
/**
|
||||
@ -177,9 +186,13 @@ export const createLocationHistory = () => {
|
||||
* the length of locationHistory, but that only worked since we were pruning
|
||||
* the array.
|
||||
*/
|
||||
const canGoBack = (deep: number = 1, initialHistory: number, currentHistory: number) => {
|
||||
const canGoBack = (
|
||||
deep = 1,
|
||||
initialHistory: number,
|
||||
currentHistory: number
|
||||
) => {
|
||||
return currentHistory - deep >= initialHistory;
|
||||
}
|
||||
};
|
||||
|
||||
const getFirstRouteInfoForTab = (tab: string): RouteInfo | undefined => {
|
||||
const tabHistory = getTabsHistory(tab);
|
||||
@ -187,7 +200,7 @@ export const createLocationHistory = () => {
|
||||
return tabHistory[0];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const getCurrentRouteInfoForTab = (tab: string): RouteInfo | undefined => {
|
||||
const tabHistory = getTabsHistory(tab);
|
||||
@ -195,7 +208,7 @@ export const createLocationHistory = () => {
|
||||
return tabHistory[tabHistory.length - 1];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds and returns the previous view based upon
|
||||
@ -207,7 +220,10 @@ export const createLocationHistory = () => {
|
||||
* do not modify the locationHistory stack so we would
|
||||
* not update pushedByRoute anyways.
|
||||
*/
|
||||
const findLastLocation = (routeInfo: RouteInfo, delta: number = -1): RouteInfo | undefined => {
|
||||
const findLastLocation = (
|
||||
routeInfo: RouteInfo,
|
||||
delta = -1
|
||||
): RouteInfo | undefined => {
|
||||
const routeInfos = getTabsHistory(routeInfo.tab);
|
||||
if (routeInfos) {
|
||||
if (delta < -1) {
|
||||
@ -236,7 +252,7 @@ export const createLocationHistory = () => {
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
current,
|
||||
@ -248,6 +264,6 @@ export const createLocationHistory = () => {
|
||||
getFirstRouteInfoForTab,
|
||||
getCurrentRouteInfoForTab,
|
||||
findLastLocation,
|
||||
clearHistory
|
||||
}
|
||||
}
|
||||
clearHistory,
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user