SearchMajorProviderMixin.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // noinspection JSIgnoredPromiseFromCall
  2. export default {
  3. data() {
  4. return {
  5. checkedList: [], // searching majors of level-2, for search result major highlight mark.
  6. formedMajors: {} // Key: majorId, Value: currentSearching copy while major is selected.
  7. }
  8. },
  9. computed: {
  10. majorTree() {
  11. return this.$store.state.cachedData.majorFullTree
  12. },
  13. majorTreeChildren() {
  14. // extend majorTree with children property. use name as key, and children as value.
  15. // put it here to avoid re-compute and re-build big-data instead of in InjectionMixin.
  16. const result = {}
  17. this.majorTree.forEach(category => {
  18. category.children.forEach(group => {
  19. result[group.code] = group.children.map(item => item.code)
  20. })
  21. })
  22. return result
  23. },
  24. searchingMajors() {
  25. // below is a structure of searchingMajors, waiting for override.
  26. return {
  27. // top search condition checked major list, for search result major highlight mark.
  28. checkedList: this.checkedList,
  29. majorTreeChildren: this.majorTreeChildren,
  30. // Key: majorId, Value: currentSearching copy while major is selected.
  31. formedMajors: this.formedMajors
  32. }
  33. }
  34. },
  35. provide() {
  36. return {
  37. fetchSearchingMajors: () => this.searchingMajors,
  38. updateCheckedList: this.updateCheckedList,
  39. snapshotSearchingMajorWhenApply: this.snapshotSearchingMajorWhenApply,
  40. isSearchingMajorFired: this.isSearchingMajorFired,
  41. isFormedMajorFired: this.isFormedMajorFired,
  42. ensureMajorFullTree: this.ensureMajorFullTree
  43. }
  44. },
  45. methods: {
  46. updateCheckedList(checkedList) {
  47. this.checkedList = checkedList
  48. },
  49. snapshotSearchingMajorWhenApply(major) {
  50. // keep currentSearching if major selected, remove if not.
  51. if (major.selected) {
  52. if (this.isSearchingMajorFired(major)) {
  53. // make a copy of currentSearching, and keep it.
  54. this.$set(this.formedMajors, major.id, [...this.checkedList])
  55. return
  56. }
  57. }
  58. // remove it if exists.
  59. this.$delete(this.formedMajors, major.id)
  60. },
  61. isSearchingMajorFired(major) {
  62. return this.checkedList.includes(major.marjorCode) ||
  63. this.checkedList.some(item => this.majorTreeChildren[item]?.includes(major.marjorCode))
  64. },
  65. isFormedMajorFired(major) {
  66. return this.formedMajors[major.id]?.includes(major.marjorCode) ||
  67. this.formedMajors[major.id]?.some(item => this.majorTreeChildren[item]?.includes(major.marjorCode))
  68. },
  69. ensureMajorFullTree(batch) {
  70. const params = {
  71. level: 3,
  72. batch
  73. }
  74. // noinspection JSIgnoredPromiseFromCall
  75. this.$store.dispatch('cachedData/ensureMajorFullTree', params)
  76. },
  77. resolveFormedMajorsFromSavedData(data) {
  78. const results = {}
  79. const highlighted = data.detail?.batch?.highlightMajorIds || []
  80. data.detail?.batch?.wishes?.forEach(group => {
  81. group.marjors.forEach(major => {
  82. // Do not use ===, id is not concerned with type of string or number
  83. if (highlighted.some(id => id == major.id)) {
  84. results[major.id] = [major.marjorCode]
  85. }
  86. })
  87. })
  88. return results
  89. }
  90. }
  91. }