// noinspection JSIgnoredPromiseFromCall export default { data() { return { checkedList: [], // searching majors of level-2, for search result major highlight mark. formedMajors: {} // Key: majorId, Value: currentSearching copy while major is selected. } }, computed: { majorTree() { return this.$store.state.cachedData.majorFullTree }, majorTreeChildren() { // extend majorTree with children property. use name as key, and children as value. // put it here to avoid re-compute and re-build big-data instead of in InjectionMixin. const result = {} this.majorTree.forEach(category => { category.children.forEach(group => { result[group.code] = group.children.map(item => item.code) }) }) return result }, searchingMajors() { // below is a structure of searchingMajors, waiting for override. return { // top search condition checked major list, for search result major highlight mark. checkedList: this.checkedList, majorTreeChildren: this.majorTreeChildren, // Key: majorId, Value: currentSearching copy while major is selected. formedMajors: this.formedMajors } } }, provide() { return { fetchSearchingMajors: () => this.searchingMajors, updateCheckedList: this.updateCheckedList, snapshotSearchingMajorWhenApply: this.snapshotSearchingMajorWhenApply, isSearchingMajorFired: this.isSearchingMajorFired, isFormedMajorFired: this.isFormedMajorFired, ensureMajorFullTree: this.ensureMajorFullTree } }, methods: { updateCheckedList(checkedList) { this.checkedList = checkedList }, snapshotSearchingMajorWhenApply(major) { // keep currentSearching if major selected, remove if not. if (major.selected) { if (this.isSearchingMajorFired(major)) { // make a copy of currentSearching, and keep it. this.$set(this.formedMajors, major.id, [...this.checkedList]) return } } // remove it if exists. this.$delete(this.formedMajors, major.id) }, isSearchingMajorFired(major) { return this.checkedList.includes(major.marjorCode) || this.checkedList.some(item => this.majorTreeChildren[item]?.includes(major.marjorCode)) }, isFormedMajorFired(major) { return this.formedMajors[major.id]?.includes(major.marjorCode) || this.formedMajors[major.id]?.some(item => this.majorTreeChildren[item]?.includes(major.marjorCode)) }, ensureMajorFullTree(batch) { const params = { level: 3, batch } // noinspection JSIgnoredPromiseFromCall this.$store.dispatch('cachedData/ensureMajorFullTree', params) }, resolveFormedMajorsFromSavedData(data) { const results = {} const highlighted = data.detail?.batch?.highlightMajorIds || [] data.detail?.batch?.wishes?.forEach(group => { group.marjors.forEach(major => { // Do not use ===, id is not concerned with type of string or number if (highlighted.some(id => id == major.id)) { results[major.id] = [major.marjorCode] } }) }) return results } } }