Blame view

src/components/HeaderSearch/index.vue 4.43 KB
be009217   monkeyhouyi   处理,通知公告,任务中心
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="{'show':show}" class="header-search">
      <i class="el-icon-search search-icon" @click.stop="click"></i>
      <el-select ref="headerSearchSelect" v-model="search" :remote-method="querySearch" filterable
        default-first-option remote placeholder="搜索:导航菜单" class="header-search-select"
        @change="change">
        <el-option v-for="item in options" :key="item.path" :value="item"
          :label="item.title.join(' > ')" />
      </el-select>
    </div>
  </template>
  
  <script>
  // fuse is a lightweight fuzzy-search module
  // make search results more in line with expectations
  import Fuse from 'fuse.js'
  import path from 'path'
  import i18n from '@/lang'
  
  export default {
    name: 'HeaderSearch',
    data() {
      return {
        search: '',
        options: [],
        searchPool: [],
        show: false,
        fuse: undefined
      }
    },
    computed: {
      routes() {
        return this.$store.getters.permission_routes
      },
      lang() {
        return this.$store.getters.language
      }
    },
    watch: {
      lang() {
        this.searchPool = this.generateRoutes(this.routes)
      },
      routes() {
        this.searchPool = this.generateRoutes(this.routes)
      },
      searchPool(list) {
        this.initFuse(list)
      },
      show(value) {
        if (value) {
          document.body.addEventListener('click', this.close)
        } else {
          document.body.removeEventListener('click', this.close)
        }
      }
    },
    mounted() {
      this.searchPool = this.generateRoutes(this.routes)
    },
    methods: {
      click() {
        this.show = !this.show
        if (this.show) {
          this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
        }
      },
      close() {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
        this.options = []
        this.show = false
      },
      change(val) {
        this.$router.push(val.path)
        this.search = ''
        this.options = []
        this.$nextTick(() => {
          this.show = false
        })
      },
      initFuse(list) {
        this.fuse = new Fuse(list, {
          shouldSort: true,
          threshold: 0.4,
          location: 0,
          distance: 100,
          maxPatternLength: 32,
          minMatchCharLength: 1,
          keys: [{
            name: 'title',
            weight: 0.7
          }, {
            name: 'path',
            weight: 0.3
          }]
        })
      },
      // Filter out the routes that can be displayed in the sidebar
      // And generate the internationalized title
      generateRoutes(routes, basePath = '/', prefixTitle = []) {
        let res = []
  
        for (const router of routes) {
          // console.log(router);
  
          // skip hidden router
          if (router.hidden) { continue }
  
          const data = {
            path: path.resolve(basePath, router.path),
            title: [...prefixTitle]
          }
  
          if (router.meta && router.meta.title) {
            // generate internationalized title
            const i18ntitle = i18n.t(`route.${router.meta.title}`)
  
            let title = i18ntitle
            if (i18ntitle.indexOf('route.') > -1) title = router.meta.zhTitle
            data.title = [...data.title, title]
  
            if (router.redirect !== 'noRedirect') {
              // only push the routes with title
              // special case: need to exclude parent router without redirect
              res.push(data)
            }
          }
  
          // recursive child routes
          if (router.children) {
            const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
            if (tempRoutes.length >= 1) {
              res = [...res, ...tempRoutes]
            }
          }
        }
        return res
      },
      querySearch(query) {
        if (query !== '') {
          this.options = this.fuse.search(query)
        } else {
          this.options = []
        }
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .header-search {
    font-size: 0 !important;
  
    .search-icon {
      cursor: pointer;
      font-size: 16px;
      vertical-align: middle;
    }
  
    .header-search-select {
      font-size: 20px;
      transition: width 0.2s;
      width: 0;
      overflow: hidden;
      background: transparent;
      border-radius: 0;
      display: inline-block;
      vertical-align: middle;
  
      >>> .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>