yuang-framework-ui-pc
Version:
yuang-framework-ui-pc Library
172 lines (171 loc) • 5.3 kB
JavaScript
import { inject, computed } from "vue";
import { findTree, mapTree, omit } from "../utils/core";
import { LAYOUT_KEY } from "../ele-admin-layout/props";
import { PRO_LAYOUT_KEY } from "./props";
function findMenuByPath(path, menus) {
return path == null ? void 0 : findTree(menus, (d) => path === d.path);
}
function findTabByPath(path, tabs) {
if (path != null && tabs != null) {
return tabs.find((d) => path === d.key || path === d.fullPath);
}
}
function findTabByKey(key, tabs) {
if (key != null && tabs != null) {
return tabs.find((d) => key === d.key);
}
}
function getMatchedMenus(path, menus, parents) {
var _a;
for (const m of menus) {
const p = parents ? [...parents, m] : [m];
if (m.path === path) {
return p;
} else if ((_a = m.children) == null ? void 0 : _a.length) {
const result = getMatchedMenus(path, m.children, p);
if (result) {
return result;
}
}
}
}
function getRouteMatched(route, menus) {
var _a, _b, _c;
const { path, fullPath, meta } = route;
if (meta == null ? void 0 : meta.active) {
const m2 = findMenuByPath(fullPath, menus) ?? findMenuByPath(path, menus);
return {
active: meta.active,
activeOther: true,
title: ((_a = m2 == null ? void 0 : m2.meta) == null ? void 0 : _a.title) ?? meta.title,
matched: getMatchedMenus(meta.active, menus)
};
}
const fm = findMenuByPath(fullPath, menus);
if (fm) {
return {
active: fullPath,
title: ((_b = fm.meta) == null ? void 0 : _b.title) ?? meta.title,
matched: getMatchedMenus(fullPath, menus)
};
}
const m = findMenuByPath(path, menus);
return {
active: path,
title: ((_c = m == null ? void 0 : m.meta) == null ? void 0 : _c.title) ?? meta.title,
matched: getMatchedMenus(path, menus)
};
}
function getMatchedLevels(matched, activeOther, route, menus, tabs) {
var _a;
const levels = [];
if (matched) {
matched.forEach((m) => {
var _a2;
if (m.meta && m.meta.title && m.meta.breadcrumb !== false) {
const title = ((_a2 = findTabByPath(m.path, tabs)) == null ? void 0 : _a2.title) || m.meta.title;
levels.push({ path: m.path, title });
}
});
}
if (activeOther) {
const { path, fullPath, meta } = route;
const notIn = !levels.length || fullPath !== levels[levels.length - 1].path;
if (notIn && meta.title) {
const m = findMenuByPath(fullPath, menus) ?? findMenuByPath(path, menus);
const t = findTabByPath(fullPath, tabs) ?? findTabByPath(path, tabs);
const title = (t == null ? void 0 : t.title) || ((_a = m == null ? void 0 : m.meta) == null ? void 0 : _a.title) || meta.title;
levels.push({ path: fullPath, title });
}
}
return levels;
}
function getMatchedComponents(matched) {
const components = [];
matched.forEach((m) => {
var _a, _b;
if ((_b = (_a = m.components) == null ? void 0 : _a.default) == null ? void 0 : _b.name) {
components.push(m.components.default.name);
}
});
return components;
}
function getRouteTab(route, tabs, homePath, routeTitle) {
const { path, fullPath, meta, matched } = route;
const key = meta.tabUnique === false ? fullPath : path;
const t = findTabByPath(key, tabs);
const title = (t == null ? void 0 : t.title) || routeTitle;
const home = path === homePath || fullPath === homePath;
const closable = (t == null ? void 0 : t.closable) ?? meta.closable !== false;
const components = getMatchedComponents(matched);
return { key, path, fullPath, title, closable, home, components, meta };
}
function getActiveChilds(menus, active, childrenName) {
const field = childrenName || "children";
if (!menus.length) {
return [];
}
if (!active) {
return menus[0][field] || [];
}
const m = menus.find((m2) => m2.path === active);
if (m == null) {
return menus[0][field] || [];
}
return m[field] || [];
}
function getIframeSrc(routePath, iframeUrl) {
const [_path1, query1 = ""] = (routePath ?? "").split("?");
const [path2, query2 = ""] = (iframeUrl ?? "").split("?");
const params1 = new URLSearchParams(query1);
const params2 = new URLSearchParams(query2);
for (const [key, value] of params2.entries()) {
params1.append(key, value);
}
const newQuery = params1.toString();
return `${path2}${newQuery ? `?${newQuery}` : ""}`;
}
function getMenuItems(menus, link) {
return mapTree(menus, (m) => {
const { path, meta } = m;
const { hide, icon, title, props } = meta ?? {};
if (hide) {
return;
}
const item = {
title,
icon,
path: link ? path : void 0,
index: path,
...omit(props, ["title", "icon", "path", "index"]),
meta
};
return item;
});
}
function useLayoutState() {
return inject(LAYOUT_KEY, {});
}
function useProLayoutState() {
return inject(PRO_LAYOUT_KEY, {});
}
function useResponsive(props) {
const state = useProLayoutState();
return computed(() => props.responsive ?? state.responsive ?? true);
}
export {
findMenuByPath,
findTabByKey,
findTabByPath,
getActiveChilds,
getIframeSrc,
getMatchedComponents,
getMatchedLevels,
getMatchedMenus,
getMenuItems,
getRouteMatched,
getRouteTab,
useLayoutState,
useProLayoutState,
useResponsive
};