mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-10-13 13:23:42 +08:00
feat(projects): 1.0 beta
This commit is contained in:
170
src/store/modules/tab/shared.ts
Normal file
170
src/store/modules/tab/shared.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import type { Router } from 'vue-router';
|
||||
import type { RouteMap, LastLevelRouteKey } from '@elegant-router/types';
|
||||
import { $t } from '@/locales';
|
||||
import { getRoutePath } from '@/router/elegant/transform';
|
||||
|
||||
/**
|
||||
* get all tabs
|
||||
* @param tabs tabs
|
||||
* @param homeTab home tab
|
||||
*/
|
||||
export function getAllTabs(tabs: App.Global.Tab[], homeTab?: App.Global.Tab) {
|
||||
if (!homeTab) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const fixedTabs = tabs.filter(tab => tab.fixedIndex !== undefined).sort((a, b) => a.fixedIndex! - b.fixedIndex!);
|
||||
|
||||
const remainTabs = tabs.filter(tab => tab.fixedIndex === undefined);
|
||||
|
||||
const allTabs = [homeTab, ...fixedTabs, ...remainTabs];
|
||||
|
||||
return updateTabsLabel(allTabs);
|
||||
}
|
||||
|
||||
/**
|
||||
* get tab id by route
|
||||
* @param route
|
||||
*/
|
||||
export function getTabIdByRoute(route: App.Global.TabRoute) {
|
||||
const { path, query = {}, meta } = route;
|
||||
|
||||
let id = path;
|
||||
|
||||
if (meta.multiTab) {
|
||||
const queryKeys = Object.keys(query).sort();
|
||||
const qs = queryKeys.map(key => `${key}=${query[key]}`).join('&');
|
||||
|
||||
id = `${path}?${qs}`;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* get tab by route
|
||||
* @param route
|
||||
*/
|
||||
export function getTabByRoute(route: App.Global.TabRoute) {
|
||||
const { name, path, fullPath = path, meta } = route;
|
||||
const { title, i18nKey, fixedIndexInTab, icon = import.meta.env.VITE_MENU_ICON, localIcon } = meta;
|
||||
|
||||
const label = i18nKey ? $t(i18nKey) : title;
|
||||
|
||||
const tab: App.Global.Tab = {
|
||||
id: getTabIdByRoute(route),
|
||||
label,
|
||||
routeKey: name as LastLevelRouteKey,
|
||||
routePath: path as RouteMap[LastLevelRouteKey],
|
||||
fullPath,
|
||||
fixedIndex: fixedIndexInTab,
|
||||
icon,
|
||||
localIcon,
|
||||
i18nKey
|
||||
};
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
/**
|
||||
* get default home tab
|
||||
* @param router
|
||||
*/
|
||||
export function getDefaultHomeTab(router: Router) {
|
||||
const homeRouteName = import.meta.env.VITE_ROUTE_HOME;
|
||||
const homeRoutePath = getRoutePath(homeRouteName);
|
||||
const i18nLabel = $t(`route.${homeRouteName}`);
|
||||
|
||||
let homeTab: App.Global.Tab = {
|
||||
id: getRoutePath(homeRouteName),
|
||||
label: i18nLabel || homeRouteName,
|
||||
routeKey: homeRouteName,
|
||||
routePath: homeRoutePath,
|
||||
fullPath: homeRoutePath
|
||||
};
|
||||
|
||||
const routes = router.getRoutes();
|
||||
const homeRoute = routes.find(route => route.name === homeRouteName);
|
||||
if (homeRoute) {
|
||||
homeTab = getTabByRoute(homeRoute);
|
||||
}
|
||||
|
||||
return homeTab;
|
||||
}
|
||||
|
||||
/**
|
||||
* is tab in tabs
|
||||
* @param tab
|
||||
* @param tabs
|
||||
*/
|
||||
export function isTabInTabs(tabId: string, tabs: App.Global.Tab[]) {
|
||||
return tabs.some(tab => tab.id === tabId);
|
||||
}
|
||||
|
||||
/**
|
||||
* filter tabs by id
|
||||
* @param tabId
|
||||
* @param tabs
|
||||
*/
|
||||
export function filterTabsById(tabId: string, tabs: App.Global.Tab[]) {
|
||||
return tabs.filter(tab => tab.id !== tabId);
|
||||
}
|
||||
|
||||
/**
|
||||
* filter tabs by ids
|
||||
* @param tabIds
|
||||
* @param tabs
|
||||
*/
|
||||
export function filterTabsByIds(tabIds: string[], tabs: App.Global.Tab[]) {
|
||||
return tabs.filter(tab => !tabIds.includes(tab.id));
|
||||
}
|
||||
|
||||
/**
|
||||
* get fixed tabs
|
||||
* @param tabs
|
||||
*/
|
||||
export function getFixedTabs(tabs: App.Global.Tab[]) {
|
||||
return tabs.filter(tab => tab.fixedIndex !== undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* get fixed tab ids
|
||||
* @param tabs
|
||||
*/
|
||||
export function getFixedTabIds(tabs: App.Global.Tab[]) {
|
||||
const fixedTabs = getFixedTabs(tabs);
|
||||
|
||||
return fixedTabs.map(tab => tab.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* update tabs label
|
||||
* @param tabs
|
||||
*/
|
||||
function updateTabsLabel(tabs: App.Global.Tab[]) {
|
||||
return tabs.map(tab => ({
|
||||
...tab,
|
||||
label: tab.newLabel || tab.label
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* update tab by i18n key
|
||||
* @param tab
|
||||
*/
|
||||
export function updateTabByI18nKey(tab: App.Global.Tab) {
|
||||
const { i18nKey, label } = tab;
|
||||
|
||||
return {
|
||||
...tab,
|
||||
label: i18nKey ? $t(i18nKey) : label
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* update tabs by i18n key
|
||||
* @param tabs
|
||||
*/
|
||||
export function updateTabsByI18nKey(tabs: App.Global.Tab[]) {
|
||||
return tabs.map(tab => updateTabByI18nKey(tab));
|
||||
}
|
Reference in New Issue
Block a user