import html from "html";
import { defineAsyncComponent, ref, nextTick, getCurrentInstance } from "vue";
import { useRoute, onBeforeRouteUpdate, useRouter } from "vue-router";
import { useAppStore } from "../store/index.js";
export default {
components: { SvgIcon: defineAsyncComponent(() => import("../components/icon/index.js")) },
template: html`
{{ item.meta?.title ?? item.fullPath }}
刷新
关闭左侧
关闭右侧
关闭其他
`,
styles: html`
`,
setup() {
const appStore = useAppStore();
const itemRefs = ref([]);
const currentRoute = useRoute();
const router = useRouter();
const model = ref(currentRoute.fullPath);
onBeforeRouteUpdate((to) => {
model.value = to.fullPath;
});
const setRef = (index, el) => {
if (el) {
itemRefs.value[index] = el;
} else {
itemRefs.value.splice(index, 1);
}
};
const showContextMenu = (index, show) => {
if (show) {
itemRefs.value.forEach((item, i) => {
if (i !== index) {
item?.handleClose();
}
});
}
};
const refresh = (index) => {
const currentIndex = appStore.routes.findIndex((o) => o.fullPath === currentRoute.fullPath);
const route = appStore.routes[index];
if (index !== currentIndex) {
router.push({ path: route.fullPath });
}
appStore.isRefreshing = true;
nextTick(() => {
appStore.isRefreshing = false;
});
};
const deleteItem = (start, end) => {
appStore.routes.splice(start, end);
const vue = getCurrentInstance();
console.log(vue);
};
const remove = (name) => {
if (appStore.routes.length > 1) {
const index = appStore.routes.findIndex((o) => o.fullPath === name);
const currentIndex = appStore.routes.findIndex((o) => o.fullPath === currentRoute.fullPath);
deleteItem(index, 1);
if (index === currentIndex) {
if (appStore.routes[index]) {
router.push(appStore.routes[index]);
} else {
router.push(appStore.routes[index - 1]);
}
}
}
};
const removeLeft = (index) => {
const currentIndex = appStore.routes.findIndex((o) => o.fullPath === currentRoute.fullPath);
const route = appStore.routes[index];
deleteItem(0, index);
if (currentIndex < index) {
router.push(route);
}
};
const removeRight = (index) => {
const currentIndex = appStore.routes.findIndex((o) => o.fullPath === currentRoute.fullPath);
deleteItem(index + 1, appStore.routes.length - index);
if (currentIndex > index) {
router.push(appStore.routes[index]);
}
};
const removeOthers = (index) => {
removeRight(index);
removeLeft(index);
if (appStore.routes[0].fullPath !== currentRoute.fullPath) {
router.push(appStore.routes[0]);
}
};
const onClick = (context) => {
if (!context.active) {
router.push(context.props.name);
}
};
return {
model,
appStore,
itemRefs,
onBeforeRouteUpdate,
setRef,
showContextMenu,
refresh,
remove,
removeLeft,
removeRight,
removeOthers,
onClick,
};
},
};