search.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import '@/utils/objects'
  2. import { defineStore } from 'pinia'
  3. export const searchStore = defineStore('search', () => {
  4. // 定义state
  5. const pool = ref([])
  6. const hotkey = ref({
  7. open: 's',
  8. close: 'esc'
  9. })
  10. const active = ref(false)
  11. // 定义action
  12. const toggleActive = () => {
  13. active.value = !active.value
  14. }
  15. const setActive = (val) => {
  16. active.value = val
  17. }
  18. const init = (menu) => {
  19. const poolList = []
  20. const getFullName = function (meta) {
  21. if (meta.breadcrumb) {
  22. let list = []
  23. meta.breadcrumb.forEach((item) => {
  24. list.push(item.meta.title)
  25. })
  26. return list.join(' / ')
  27. }
  28. return meta.title
  29. }
  30. const push = function (menu) {
  31. menu.forEach((m) => {
  32. if ('menu' === m.meta.type) {
  33. if (m.children) {
  34. push(m.children)
  35. } else if (m.children === null) {
  36. poolList.push({
  37. icon: m.meta.icon,
  38. path: m.path,
  39. fullPath: m.path,
  40. name: m.meta.title,
  41. fullName: getFullName(m.meta),
  42. namePinyin: m.meta.title.toPinyin(),
  43. namePinyinFirst: m.meta.title.toPinyin(true)
  44. })
  45. }
  46. }
  47. })
  48. }
  49. push(menu)
  50. pool.value = poolList
  51. }
  52. return {
  53. pool,
  54. hotkey,
  55. active,
  56. toggleActive,
  57. setActive,
  58. init
  59. }
  60. })