Files
procat2/cateditor/src/components/SelectionMixin.js
2019-05-03 00:04:54 -07:00

112 lines
2.9 KiB
JavaScript

const SelectionMixin = { // eslint-disable-line no-unused-vars
computed: {
selectedSectionID: {
get() {
return this.$store.getters.SELECTED_SECTION_ID
},
set(value) {
this.$store.dispatch('SET_SELECTED_SECTION_ID', value)
}
},
selectedFamilyID: {
get() {
return this.$store.getters.SELECTED_FAMILY_ID
},
set(value) {
this.$store.dispatch('SET_SELECTED_FAMILY_ID', value)
}
},
selectedModelID: {
get() {
return this.$store.getters.SELECTED_MODEL_ID
},
set(value) {
this.$store.dispatch('SET_SELECTED_MODEL_ID', value)
}
},
selectedMaterial: {
get() {
return this.$store.getters.SELECTED_MATERIAL_ID
},
set(value) {
this.$store.dispatch('SET_SELECTED_MATERIAL_ID', value)
}
},
},
methods: {
selectSection: function(id) {
this.selectedSectionID = id
// this.selectedFamilyID = null
// this.selectedModelID = null
// this.selectedMaterial = null
},
selectFamily: function(id) {
this.selectedFamilyID = id
// this.selectedModelID = null
// this.selectedMaterial = null
},
selectModel: function(id) {
this.selectedModelID = id
// this.selectedFamilyID = null
// this.selectedMaterial = null
},
selectMaterial: function(id) {
this.selectedMaterial = id
},
selectedFamilies: function() {
let section = this.$store.getters.CAT_SECTION(this.selectedSectionID)
let ids = this.sectionIDs(section)
let materials = this.findMaterials(ids)
return this.makeFamilies(materials)
},
selectedFamily: function() {
let families = this.selectedFamilies()
return families.find(s => s.id === this.selectedFamilyID)
},
sectionIDs: function(section) {
let ids = []
if (section) {
for (let page of section.pages) {
for (let block of page) {
ids.push(...block.ids)
}
}
}
return ids
},
findMaterials: function(ids) {
return ids.map(id => this.$store.getters.MATERIAL(id))
},
makeFamilies: function(materials) {
let families = []
for (let mat of materials) {
let family = families.length > 0 ? families[families.length - 1] : null
if (family === null || family.id !== mat.family) {
// new family
family = { id: mat.family,
name: mat.name, // TODO where does this come from?
models: [mat.model],
materials: [mat.id] }
families.push(family)
} else {
// add to existing
family.materials.push(mat.id)
family.models.push(mat.model)
family.models = this.uniquify(family.models)
}
}
return families
},
uniquify: function(arr) {
return [...new Set(arr)]
},
}
}
export default SelectionMixin