Rap 原分销系统代码Web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.4KB

  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="item in options" :key="item.path" :value="item" :label="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'
  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.state.permission.routers
  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. if (this.ishttp(val.path)) {
  72. // http(s):// 路径新窗口打开
  73. window.open(val.path, '_blank')
  74. } else {
  75. this.$router.push(val.path)
  76. }
  77. this.search = ''
  78. this.options = []
  79. this.$nextTick(() => {
  80. this.show = false
  81. })
  82. },
  83. initFuse(list) {
  84. this.fuse = new Fuse(list, {
  85. shouldSort: true,
  86. threshold: 0.4,
  87. location: 0,
  88. distance: 100,
  89. maxPatternLength: 32,
  90. minMatchCharLength: 1,
  91. keys: [{
  92. name: 'title',
  93. weight: 0.7
  94. }, {
  95. name: 'path',
  96. weight: 0.3
  97. }]
  98. })
  99. },
  100. // Filter out the routes that can be displayed in the sidebar
  101. // And generate the internationalized title
  102. generateRoutes(routes, basePath = '/', prefixTitle = []) {
  103. let res = []
  104. for (const router of routes) {
  105. // skip hidden router
  106. if (router.hidden) { continue }
  107. const data = {
  108. path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
  109. title: [...prefixTitle]
  110. }
  111. if (router.meta && router.meta.title) {
  112. data.title = [...data.title, router.meta.title]
  113. if (router.redirect !== 'noRedirect') {
  114. // only push the routes with title
  115. // special case: need to exclude parent router without redirect
  116. res.push(data)
  117. }
  118. }
  119. // recursive child routes
  120. if (router.children) {
  121. const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
  122. if (tempRoutes.length >= 1) {
  123. res = [...res, ...tempRoutes]
  124. }
  125. }
  126. }
  127. return res
  128. },
  129. querySearch(query) {
  130. if (query !== '') {
  131. this.options = this.fuse.search(query)
  132. } else {
  133. this.options = []
  134. }
  135. },
  136. ishttp(url) {
  137. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. .header-search {
  144. font-size: 0 !important;
  145. .search-icon {
  146. cursor: pointer;
  147. font-size: 18px;
  148. vertical-align: middle;
  149. }
  150. .header-search-select {
  151. font-size: 18px;
  152. transition: width 0.2s;
  153. width: 0;
  154. overflow: hidden;
  155. background: transparent;
  156. border-radius: 0;
  157. display: inline-block;
  158. vertical-align: middle;
  159. ::v-deep .el-input__inner {
  160. border-radius: 0;
  161. border: 0;
  162. padding-left: 0;
  163. padding-right: 0;
  164. box-shadow: none !important;
  165. border-bottom: 1px solid #d9d9d9;
  166. vertical-align: middle;
  167. }
  168. }
  169. &.show {
  170. .header-search-select {
  171. width: 210px;
  172. margin-left: 10px;
  173. }
  174. }
  175. }
  176. </style>