|
| 1 | +<template> |
| 2 | + <div :class="{'show':show}" class="header-search"> |
| 3 | + <svg-icon class-name="search-icon" icon-class="search" @click="click" /> |
| 4 | + <el-select |
| 5 | + ref="headerSearchSelect" |
| 6 | + v-model="search" |
| 7 | + :remote-method="querySearch" |
| 8 | + filterable |
| 9 | + default-first-option |
| 10 | + remote |
| 11 | + placeholder="Search" |
| 12 | + class="header-search-select" |
| 13 | + @change="change"> |
| 14 | + <el-option v-for="item in options" :key="item.path" :value="item" :label="item.title.join(' > ')"/> |
| 15 | + </el-select> |
| 16 | + </div> |
| 17 | +</template> |
| 18 | + |
| 19 | +<script> |
| 20 | +import Fuse from 'fuse.js' |
| 21 | +import path from 'path' |
| 22 | +import i18n from '@/lang' |
| 23 | +
|
| 24 | +export default { |
| 25 | + name: 'HeaderSearch', |
| 26 | + data() { |
| 27 | + return { |
| 28 | + search: '', |
| 29 | + options: [], |
| 30 | + searchPool: [], |
| 31 | + show: false, |
| 32 | + fuse: undefined |
| 33 | + } |
| 34 | + }, |
| 35 | + computed: { |
| 36 | + routers() { |
| 37 | + return this.$store.getters.permission_routers |
| 38 | + }, |
| 39 | + lang() { |
| 40 | + return this.$store.getters.language |
| 41 | + } |
| 42 | + }, |
| 43 | + watch: { |
| 44 | + lang() { |
| 45 | + this.searchPool = this.generateRouters(this.routers) |
| 46 | + }, |
| 47 | + routers() { |
| 48 | + this.searchPool = this.generateRouters(this.routers) |
| 49 | + }, |
| 50 | + searchPool(list) { |
| 51 | + this.initFuse(list) |
| 52 | + }, |
| 53 | + show(value) { |
| 54 | + if (value) { |
| 55 | + document.body.addEventListener('click', this.close) |
| 56 | + } else { |
| 57 | + document.body.removeEventListener('click', this.close) |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + mounted() { |
| 62 | + this.searchPool = this.generateRouters(this.routers) |
| 63 | + }, |
| 64 | + methods: { |
| 65 | + click() { |
| 66 | + this.show = !this.show |
| 67 | + if (this.show) { |
| 68 | + this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus() |
| 69 | + } |
| 70 | + }, |
| 71 | + close() { |
| 72 | + this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur() |
| 73 | + this.options = [] |
| 74 | + this.show = false |
| 75 | + }, |
| 76 | + change(val) { |
| 77 | + this.$router.push(val.path) |
| 78 | + this.search = '' |
| 79 | + this.options = [] |
| 80 | + this.$nextTick(() => { |
| 81 | + this.show = false |
| 82 | + }) |
| 83 | + }, |
| 84 | + initFuse(list) { |
| 85 | + this.fuse = new Fuse(list, { |
| 86 | + shouldSort: true, |
| 87 | + threshold: 0.4, |
| 88 | + location: 0, |
| 89 | + distance: 100, |
| 90 | + maxPatternLength: 32, |
| 91 | + minMatchCharLength: 1, |
| 92 | + keys: [{ |
| 93 | + name: 'title', |
| 94 | + weight: 0.7 |
| 95 | + }, { |
| 96 | + name: 'path', |
| 97 | + weight: 0.3 |
| 98 | + }] |
| 99 | + }) |
| 100 | + }, |
| 101 | + // Filter out the routes that can be displayed in the sidebar |
| 102 | + // And generate the internationalized title |
| 103 | + generateRouters(routers, basePath = '/', prefixTitle = []) { |
| 104 | + let res = [] |
| 105 | +
|
| 106 | + for (const router of routers) { |
| 107 | + // skip hidden router |
| 108 | + if (router.hidden) { continue } |
| 109 | +
|
| 110 | + const data = { |
| 111 | + path: path.resolve(basePath, router.path), |
| 112 | + title: [...prefixTitle] |
| 113 | + } |
| 114 | +
|
| 115 | + if (router.meta && router.meta.title) { |
| 116 | + // generate internationalized title |
| 117 | + const i18ntitle = i18n.t(`route.${router.meta.title}`) |
| 118 | +
|
| 119 | + data.title = [...data.title, i18ntitle] |
| 120 | +
|
| 121 | + if (router.redirect !== 'noredirect') { |
| 122 | + // only push the routes with title |
| 123 | + // special case: need to exclude parent router without redirect |
| 124 | + res.push(data) |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + // recursive child routers |
| 129 | + if (router.children) { |
| 130 | + const tempRouters = this.generateRouters(router.children, data.path, data.title) |
| 131 | + if (tempRouters.length >= 1) { |
| 132 | + res = [...res, ...tempRouters] |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return res |
| 137 | + }, |
| 138 | + querySearch(query) { |
| 139 | + if (query !== '') { |
| 140 | + this.options = this.fuse.search(query) |
| 141 | + } else { |
| 142 | + this.options = [] |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +</script> |
| 148 | + |
| 149 | +<style lang="scss" scoped> |
| 150 | +.header-search { |
| 151 | + font-size: 0 !important; |
| 152 | +
|
| 153 | + .search-icon { |
| 154 | + cursor: pointer; |
| 155 | + font-size: 18px; |
| 156 | + vertical-align: middle; |
| 157 | + } |
| 158 | +
|
| 159 | + .header-search-select { |
| 160 | + font-size: 18px; |
| 161 | + transition: width 0.2s; |
| 162 | + width: 0; |
| 163 | + overflow: hidden; |
| 164 | + background: transparent; |
| 165 | + border-radius: 0; |
| 166 | + display: inline-block; |
| 167 | + vertical-align: middle; |
| 168 | +
|
| 169 | + /deep/ .el-input__inner { |
| 170 | + border-radius: 0; |
| 171 | + border: 0; |
| 172 | + padding-left: 0; |
| 173 | + padding-right: 0; |
| 174 | + box-shadow: none !important; |
| 175 | + border-bottom: 1px solid #d9d9d9; |
| 176 | + vertical-align: middle; |
| 177 | + } |
| 178 | + } |
| 179 | +
|
| 180 | + &.show { |
| 181 | + .header-search-select { |
| 182 | + width: 210px; |
| 183 | + margin-left: 10px; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | +</style> |
0 commit comments