useVoluntaryMajorHighlightInjection.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {ref, computed} from 'vue';
  2. import {injectLocal, provideLocal} from "@vueuse/core";
  3. import {useCacheStore} from "@/hooks/useCacheStore";
  4. import {cacheActions} from "@/hooks/defineCacheActions";
  5. const key = Symbol('VOLUNTARY_MAJOR_HIGHLIGHT')
  6. export const useProvideVoluntaryMajorHighlight = (readonly) => {
  7. const {dispatchCache} = useCacheStore()
  8. const checkedList = ref([])
  9. const formedMajors = ref({})
  10. const majorTree = ref([])
  11. const resetHighlight = () => {
  12. checkedList.value = []
  13. formedMajors.value = {}
  14. }
  15. const majorTreeChildren = computed(() => {
  16. // extend majorTree with children property. use name as key, and children as value.
  17. // put it here to avoid re-compute and re-build big-data instead of in InjectionMixin.
  18. const categoryMap = {}
  19. majorTree.value.forEach(category => {
  20. category.children.forEach(group => {
  21. categoryMap[group.name] = group.children.map(item => item.name)
  22. })
  23. })
  24. return categoryMap
  25. })
  26. const updateCheckedList = (conditionMajors) => {
  27. checkedList.value = conditionMajors
  28. }
  29. const snapshotSearchingMajorWhenApply = (major) => {
  30. // keep currentSearching if major selected, remove if not.
  31. if (major.selected) {
  32. if (isSearchingMajorFired(major)) {
  33. // make a copy of currentSearching, and keep it.
  34. return formedMajors.value[major.id] = [...checkedList.value]
  35. }
  36. }
  37. // remove it if exists.
  38. delete formedMajors.value[major.id]
  39. }
  40. const isSearchingMajorFired = (major) => {
  41. // 编辑模式下,列表的高亮即是表单的高亮(详情列表)
  42. if (readonly) return isFormedMajorFired(major)
  43. return checkedList.value.includes(major.marjorName) ||
  44. checkedList.value.some(item => majorTreeChildren.value[item]?.includes(major.marjorName))
  45. }
  46. const isFormedMajorFired = (major) => {
  47. return formedMajors.value[major.id]?.includes(major.marjorName) ||
  48. formedMajors.value[major.id]?.some(item => majorTreeChildren.value[item]?.includes(major.marjorName))
  49. }
  50. const ensureMajorFullTree = async (batch) => {
  51. const params = {level: 3, batch}
  52. majorTree.value = dispatchCache(cacheActions.getMajorTree, params)
  53. }
  54. const resolveFormedMajorsFromSavedData = (data) => {
  55. const formed = {}
  56. const highlighted = data.detail?.batch?.highlightMajorIds || []
  57. data.detail?.batch?.wishes?.forEach(group => {
  58. group.marjors.forEach(major => {
  59. // Do not use ===, id is not concerned with type of string or number
  60. if (highlighted.some(id => id == major.id)) {
  61. formed[major.id] = [major.name]
  62. }
  63. })
  64. })
  65. formedMajors.value = formed
  66. return formed
  67. }
  68. const options = {
  69. checkedList, formedMajors, majorTree, majorTreeChildren,
  70. updateCheckedList,
  71. snapshotSearchingMajorWhenApply,
  72. isSearchingMajorFired,
  73. isFormedMajorFired,
  74. ensureMajorFullTree,
  75. resolveFormedMajorsFromSavedData,
  76. resetHighlight
  77. }
  78. provideLocal(key, options)
  79. return options
  80. }
  81. export const useInjectVoluntaryMajorHighlight = () => {
  82. return injectLocal(key)
  83. }