index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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. routes() {
  37. return this.$store.getters.permission_routes
  38. }
  39. },
  40. watch: {
  41. routes() {
  42. this.searchPool = this.generateRoutes(this.routes)
  43. },
  44. searchPool(list) {
  45. this.initFuse(list)
  46. },
  47. show(value) {
  48. if (value) {
  49. document.body.addEventListener('click', this.close)
  50. } else {
  51. document.body.removeEventListener('click', this.close)
  52. }
  53. }
  54. },
  55. mounted() {
  56. this.searchPool = this.generateRoutes(this.routes)
  57. },
  58. methods: {
  59. click() {
  60. this.show = !this.show
  61. if (this.show) {
  62. this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
  63. }
  64. },
  65. close() {
  66. this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
  67. this.options = []
  68. this.show = false
  69. },
  70. change(val) {
  71. const path = val.path;
  72. const query = val.query;
  73. if(this.ishttp(val.path)) {
  74. // http(s):// 路径新窗口打开
  75. const pindex = path.indexOf("http");
  76. window.open(path.substr(pindex, path.length), "_blank");
  77. } else {
  78. if (query) {
  79. this.$router.push({ path: path, query: JSON.parse(query) });
  80. } else {
  81. this.$router.push(path)
  82. }
  83. }
  84. this.search = ''
  85. this.options = []
  86. this.$nextTick(() => {
  87. this.show = false
  88. })
  89. },
  90. initFuse(list) {
  91. this.fuse = new Fuse(list, {
  92. shouldSort: true,
  93. threshold: 0.4,
  94. location: 0,
  95. distance: 100,
  96. minMatchCharLength: 1,
  97. keys: [{
  98. name: 'title',
  99. weight: 0.7
  100. }, {
  101. name: 'path',
  102. weight: 0.3
  103. }]
  104. })
  105. },
  106. // Filter out the routes that can be displayed in the sidebar
  107. // And generate the internationalized title
  108. generateRoutes(routes, basePath = '/', prefixTitle = [], query = {}) {
  109. let res = []
  110. for (const router of routes) {
  111. // skip hidden router
  112. if (router.hidden) { continue }
  113. const data = {
  114. path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
  115. title: [...prefixTitle]
  116. }
  117. if (router.meta && router.meta.title) {
  118. data.title = [...data.title, router.meta.title]
  119. if (router.redirect !== 'noRedirect') {
  120. // only push the routes with title
  121. // special case: need to exclude parent router without redirect
  122. res.push(data)
  123. }
  124. }
  125. if (router.query) {
  126. data.query = router.query
  127. }
  128. // recursive child routes
  129. if (router.children) {
  130. const tempRoutes = this.generateRoutes(router.children, data.path, data.title, data.query)
  131. if (tempRoutes.length >= 1) {
  132. res = [...res, ...tempRoutes]
  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. ishttp(url) {
  146. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .header-search {
  153. font-size: 0 !important;
  154. .search-icon {
  155. cursor: pointer;
  156. font-size: 18px;
  157. vertical-align: middle;
  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. ::v-deep .el-input__inner {
  169. border-radius: 0;
  170. border: 0;
  171. padding-left: 0;
  172. padding-right: 0;
  173. box-shadow: none !important;
  174. border-bottom: 1px solid #d9d9d9;
  175. vertical-align: middle;
  176. }
  177. }
  178. &.show {
  179. .header-search-select {
  180. width: 210px;
  181. margin-left: 10px;
  182. }
  183. }
  184. }
  185. </style>