SearchMajorProviderMixin.js 3.0 KB

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