Files
jwl-manage-web/src/components/HeaderSearch/index.vue

186 lines
4.5 KiB
Vue
Raw Normal View History

2023-02-15 09:17:05 +08:00
<template>
<div :class="{'show':show}" class="header-search">
<svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />
2023-03-21 00:53:28 +08:00
<el-select ref="headerSearchSelect" v-model="search" :remote-method="querySearch" filterable default-first-option remote placeholder="Search" class="header-search-select" @change="change">
2023-02-15 09:17:05 +08:00
<el-option v-for="option in options" :key="option.item.path" :value="option.item" :label="option.item.title.join(' > ')" />
</el-select>
</div>
</template>
<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
2023-03-21 00:53:28 +08:00
import Fuse from 'fuse.js/dist/fuse.min.js';
import path from 'path';
2023-02-15 09:17:05 +08:00
export default {
name: 'HeaderSearch',
data() {
return {
search: '',
options: [],
searchPool: [],
show: false,
fuse: undefined
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
},
computed: {
routes() {
2023-03-21 00:53:28 +08:00
return this.$store.getters.permission_routes;
2023-02-15 09:17:05 +08:00
}
},
watch: {
routes() {
2023-03-21 00:53:28 +08:00
this.searchPool = this.generateRoutes(this.routes);
2023-02-15 09:17:05 +08:00
},
searchPool(list) {
2023-03-21 00:53:28 +08:00
this.initFuse(list);
2023-02-15 09:17:05 +08:00
},
show(value) {
if (value) {
2023-03-21 00:53:28 +08:00
document.body.addEventListener('click', this.close);
2023-02-15 09:17:05 +08:00
} else {
2023-03-21 00:53:28 +08:00
document.body.removeEventListener('click', this.close);
2023-02-15 09:17:05 +08:00
}
}
},
mounted() {
2023-03-21 00:53:28 +08:00
this.searchPool = this.generateRoutes(this.routes);
2023-02-15 09:17:05 +08:00
},
methods: {
click() {
2023-03-21 00:53:28 +08:00
this.show = !this.show;
2023-02-15 09:17:05 +08:00
if (this.show) {
2023-03-21 00:53:28 +08:00
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus();
2023-02-15 09:17:05 +08:00
}
},
close() {
2023-03-21 00:53:28 +08:00
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur();
this.options = [];
this.show = false;
2023-02-15 09:17:05 +08:00
},
change(val) {
const path = val.path;
2023-03-21 00:53:28 +08:00
if (this.ishttp(val.path)) {
2023-02-15 09:17:05 +08:00
// http(s):// 路径新窗口打开
2023-03-21 00:53:28 +08:00
const pindex = path.indexOf('http');
window.open(path.substr(pindex, path.length), '_blank');
2023-02-15 09:17:05 +08:00
} else {
2023-03-21 00:53:28 +08:00
this.$router.push(val.path);
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
this.search = '';
this.options = [];
2023-02-15 09:17:05 +08:00
this.$nextTick(() => {
2023-03-21 00:53:28 +08:00
this.show = false;
});
2023-02-15 09:17:05 +08:00
},
initFuse(list) {
this.fuse = new Fuse(list, {
shouldSort: true,
threshold: 0.4,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
2023-03-21 00:53:28 +08:00
keys: [
{
name: 'title',
weight: 0.7
},
{
name: 'path',
weight: 0.3
}
]
});
2023-02-15 09:17:05 +08:00
},
// Filter out the routes that can be displayed in the sidebar
// And generate the internationalized title
generateRoutes(routes, basePath = '/', prefixTitle = []) {
2023-03-21 00:53:28 +08:00
let res = [];
2023-02-15 09:17:05 +08:00
for (const router of routes) {
// skip hidden router
2023-03-21 00:53:28 +08:00
if (router.hidden) {
continue;
}
2023-02-15 09:17:05 +08:00
const data = {
path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
title: [...prefixTitle]
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
if (router.meta && router.meta.title) {
2023-03-21 00:53:28 +08:00
data.title = [...data.title, router.meta.title];
2023-02-15 09:17:05 +08:00
if (router.redirect !== 'noRedirect') {
// only push the routes with title
// special case: need to exclude parent router without redirect
2023-03-21 00:53:28 +08:00
res.push(data);
2023-02-15 09:17:05 +08:00
}
}
// recursive child routes
if (router.children) {
2023-03-21 00:53:28 +08:00
const tempRoutes = this.generateRoutes(router.children, data.path, data.title);
2023-02-15 09:17:05 +08:00
if (tempRoutes.length >= 1) {
2023-03-21 00:53:28 +08:00
res = [...res, ...tempRoutes];
2023-02-15 09:17:05 +08:00
}
}
}
2023-03-21 00:53:28 +08:00
return res;
2023-02-15 09:17:05 +08:00
},
querySearch(query) {
if (query !== '') {
2023-03-21 00:53:28 +08:00
this.options = this.fuse.search(query);
2023-02-15 09:17:05 +08:00
} else {
2023-03-21 00:53:28 +08:00
this.options = [];
2023-02-15 09:17:05 +08:00
}
},
ishttp(url) {
2023-03-21 00:53:28 +08:00
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
2023-02-15 09:17:05 +08:00
}
}
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
</script>
<style lang="scss" scoped>
.header-search {
font-size: 0 !important;
.search-icon {
cursor: pointer;
font-size: 18px;
vertical-align: middle;
}
.header-search-select {
font-size: 18px;
transition: width 0.2s;
width: 0;
overflow: hidden;
background: transparent;
border-radius: 0;
display: inline-block;
vertical-align: middle;
::v-deep .el-input__inner {
border-radius: 0;
border: 0;
padding-left: 0;
padding-right: 0;
box-shadow: none !important;
border-bottom: 1px solid #d9d9d9;
vertical-align: middle;
}
}
&.show {
.header-search-select {
width: 210px;
margin-left: 10px;
}
}
}
</style>