zhangxiao
2024-08-20 e47b788ff5f5c699c682999c95da17eb284ca21d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<template>
    <div class="tag-item">
        <a-dropdown trigger="contextMenu" :popup-max-height="false" @select="actionSelect">
            <span
                class="arco-tag arco-tag-size-medium arco-tag-checked"
                :class="{ 'link-activated': props.data.path === route.path }"
                @click="goto(props.data)"
            >
                <span class="tag-link">
                    {{ $t(props.data.meta?.title) }}
                </span>
                <span
                    class="arco-icon-hover arco-tag-icon-hover arco-icon-hover-size-medium arco-tag-close-btn"
                    @click.stop="tagClose(props.data, props.index)"
                >
                    <icon-close />
                </span>
            </span>
            <template #content>
                <a-doption :disabled="disabledReload" :value="Eaction.reload">
                    <icon-refresh />
                    <span>重新加载</span>
                </a-doption>
                <a-doption class="sperate-line" :disabled="disabledCurrent" :value="Eaction.current">
                    <icon-close />
                    <span>关闭当前标签页</span>
                </a-doption>
                <a-doption :disabled="disabledLeft" :value="Eaction.left">
                    <icon-to-left />
                    <span>关闭左侧标签页</span>
                </a-doption>
                <a-doption class="sperate-line" :disabled="disabledRight" :value="Eaction.right">
                    <icon-to-right />
                    <span>关闭右侧标签页</span>
                </a-doption>
                <a-doption :value="Eaction.others">
                    <icon-swap />
                    <span>关闭其它标签页</span>
                </a-doption>
                <a-doption :value="Eaction.all">
                    <icon-folder-delete />
                    <span>关闭全部标签页</span>
                </a-doption>
            </template>
        </a-dropdown>
    </div>
</template>
<script lang="ts" setup name="AppTagItem">
import routes from "@/config/pinia/routes";
import { getDefaultRoute } from "@/packages/vue-router";
import { Message } from "@arco-design/web-vue";
import { RouteConfig } from "types";
import "@arco-design/web-vue/es/tag/style/css.js";
 
enum Eaction {
    reload = "reload",
    current = "current",
    left = "left",
    right = "right",
    others = "others",
    all = "all"
}
 
const props = withDefaults(
    defineProps<{
        index: number;
        data: RouteConfig;
    }>(),
    {}
);
 
const router = useRouter();
const route = useRoute();
 
const tagList = computed(() => {
    return routes().navTags;
});
 
const goto = (tag: RouteConfig) => {
    router.push(tag);
};
 
const disabledReload = computed(() => {
    return props.data.path !== route.path;
});
 
const disabledCurrent = computed(() => {
    return props.index === 0;
});
 
const disabledLeft = computed(() => {
    return !tagList.value[props.index - 1];
});
 
const disabledRight = computed(() => {
    return !tagList.value[props.index + 1];
});
 
const tagClose = (tag: RouteConfig, idx: number) => {
    routes().DELETE_NAVTAG(idx);
    if (props.data.path === route.path) {
        const latest = tagList.value[idx - 1]; // 获取队列的前一个tab
        router.push(latest);
    }
};
 
const findCurrentRouteIndex = () => {
    return tagList.value.findIndex((el) => el.path === route.path);
};
 
const actionSelect = async (value: any) => {
    const { data, index } = props;
    if (value === Eaction.current) {
        tagClose(data, index);
    } else if (value === Eaction.left) {
        const currentRouteIdx = findCurrentRouteIndex();
        tagList.value.splice(1, props.index - 1);
        if (currentRouteIdx < index) {
            router.push(data);
        }
    } else if (value === Eaction.right) {
        const currentRouteIdx = findCurrentRouteIndex();
        tagList.value.splice(props.index + 1);
 
        if (currentRouteIdx > index) {
            router.push(data);
        }
    } else if (value === Eaction.others) {
        const filterList = tagList.value.filter((el, idx) => {
            return idx === 0 || idx === props.index;
        });
        routes().navTags = filterList;
        router.push(data);
    } else if (value === Eaction.reload) {
        const name = props.data.meta?.keepAliveName || props.data.name;
        if (name) {
            routes().cachedTags = routes().cachedTags.filter((el) => el !== name);
        }
        router.replace({
            name: "redirect",
            query: {
                url: encodeURIComponent(route.path)
            }
        });
    } else {
        routes().CLEAR_NAVTAGS();
        const defaultRoutes = getDefaultRoute();
        if (!defaultRoutes) {
            Message.error("没有可用的路由");
            return;
        }
        router.push(defaultRoutes);
    }
};
</script>
<style scoped lang="scss">
.tag-link {
    color: var(--color-text-2);
    text-decoration: none;
    user-select: none;
}
.link-activated {
    color: $main;
    .tag-link {
        color: $main;
    }
    & + .arco-tag-close-btn {
        color: $main;
    }
}
:deep(.arco-dropdown-option-content) {
    span {
        margin-left: 10px;
    }
}
.arco-dropdown-open {
    .tag-link {
        color: rgb(var(--danger-6));
    }
    .arco-tag-close-btn {
        color: rgb(var(--danger-6));
    }
}
.sperate-line {
    border-bottom: 1px solid var(--color-neutral-3);
}
</style>