index.vue 4.8 KB

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