马宇豪
2023-08-07 7ed4982b8f2a345bfad95a96070143c32142bc61
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
<template>
    <div class="layout-search-dialog">
        <el-dialog v-model="isShowSearch" width="300px" destroy-on-close :modal="false" fullscreen :show-close="false">
            <el-autocomplete
                v-model="menuQuery"
                :fetch-suggestions="menuSearch"
                :placeholder="$t('message.user.searchPlaceholder')"
                ref="layoutMenuAutocompleteRef"
                @select="onHandleSelect"
                @blur="onSearchBlur"
            >
                <template #prefix>
                    <el-icon class="el-input__icon">
                        <ele-Search />
                    </el-icon>
                </template>
                <template #default="{ item }">
                    <div>
                        <SvgIcon :name="item.meta.icon" class="mr5" />
                        {{ $t(item.meta.title) }}
                    </div>
                </template>
            </el-autocomplete>
        </el-dialog>
    </div>
</template>
 
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, nextTick } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
 
// 定义接口来定义对象的类型
interface SearchState {
    isShowSearch: boolean;
    menuQuery: string;
    tagsViewList: object[];
}
interface Restaurant {
    path: string;
    meta: {
        title: string;
    };
}
 
export default defineComponent({
    name: 'layoutBreadcrumbSearch',
    setup() {
        const storesTagsViewRoutes = useTagsViewRoutes();
        const { tagsViewRoutes } = storeToRefs(storesTagsViewRoutes);
        const layoutMenuAutocompleteRef = ref();
        const { t } = useI18n();
        const router = useRouter();
        const state = reactive<SearchState>({
            isShowSearch: false,
            menuQuery: '',
            tagsViewList: [],
        });
        // 搜索弹窗打开
        const openSearch = () => {
            state.menuQuery = '';
            state.isShowSearch = true;
            initTageView();
            nextTick(() => {
                setTimeout(() => {
                    layoutMenuAutocompleteRef.value.focus();
                });
            });
        };
        // 搜索弹窗关闭
        const closeSearch = () => {
            state.isShowSearch = false;
        };
        // 菜单搜索数据过滤
        const menuSearch = (queryString: string, cb: Function) => {
            let results = queryString ? state.tagsViewList.filter(createFilter(queryString)) : state.tagsViewList;
            cb(results);
        };
        // 菜单搜索过滤
        const createFilter: any = (queryString: string) => {
            return (restaurant: Restaurant) => {
                return (
                    restaurant.path.toLowerCase().indexOf(queryString.toLowerCase()) > -1 ||
                    restaurant.meta.title.toLowerCase().indexOf(queryString.toLowerCase()) > -1 ||
                    t(restaurant.meta.title).indexOf(queryString.toLowerCase()) > -1
                );
            };
        };
        // 初始化菜单数据
        const initTageView = () => {
            if (state.tagsViewList.length > 0) return false;
            tagsViewRoutes.value.map((v: any) => {
                if (!v.meta.isHide) state.tagsViewList.push({ ...v });
            });
        };
        // 当前菜单选中时
        const onHandleSelect = (item: any) => {
            let { path, redirect } = item;
            if (item.meta.isLink && !item.meta.isIframe) window.open(item.meta.isLink);
            else if (redirect) router.push(redirect);
            else router.push(path);
            closeSearch();
        };
        // input 失去焦点时
        const onSearchBlur = () => {
            closeSearch();
        };
        return {
            layoutMenuAutocompleteRef,
            openSearch,
            closeSearch,
            menuSearch,
            onHandleSelect,
            onSearchBlur,
            ...toRefs(state),
        };
    },
});
</script>
 
<style scoped lang="scss">
.layout-search-dialog {
    ::v-deep(.el-dialog) {
        box-shadow: unset !important;
        border-radius: 0 !important;
        background: rgba(0, 0, 0, 0.5);
    }
    ::v-deep(.el-autocomplete) {
        width: 560px;
        position: absolute;
        top: 100px;
        left: 50%;
        transform: translateX(-50%);
    }
}
</style>