A more efficient way of detecting the entity groups to display.

Instead of iterating over each of the entities in a grouping to find out
which groups should be displayed based on the selector's policy, the
selector can directly keep its `selectedGroups` attribute in sync with
the index.
This commit is contained in:
Fabio Manganiello 2023-05-05 02:33:34 +02:00
parent 373788377b
commit 8af3ae17b8
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 13 additions and 21 deletions

View file

@ -127,6 +127,7 @@ export default {
selector: { selector: {
grouping: 'plugin', grouping: 'plugin',
selectedEntities: {}, selectedEntities: {},
selectedGroups: {},
}, },
} }
}, },
@ -148,14 +149,9 @@ export default {
}, },
displayGroups() { displayGroups() {
return Object.entries(this.entityGroups[this.selector.grouping]). return Object.entries(this.entityGroups[this.selector.grouping])
filter( .filter((entry) => this.selector.selectedGroups[entry[0]])
(entry) => Object.values(entry[1]).filter( .map(
(e) =>
!!this.selector.selectedEntities[e.id] && e.parent_id == null
).length > 0
).
map(
([grouping, entities]) => { ([grouping, entities]) => {
return { return {
name: grouping, name: grouping,
@ -164,8 +160,9 @@ export default {
), ),
} }
} }
). )
sort((a, b) => a.name.localeCompare(b.name)) .filter((group) => group.entities?.length > 0)
.sort((a, b) => a.name.localeCompare(b.name))
}, },
}, },

View file

@ -143,16 +143,11 @@ export default {
return {} return {}
}, },
synchronizeSelectedEntities() { sync() {
const value = {...this.value}
value.selectedEntities = this.selectedEntities
this.$emit('input', value)
},
updateSearchTerm() {
const value = {...this.value} const value = {...this.value}
value.searchTerm = this.searchTerm value.searchTerm = this.searchTerm
value.selectedEntities = this.selectedEntities value.selectedEntities = this.selectedEntities
value.selectedGroups = this.selectedGroups
this.$emit('input', value) this.$emit('input', value)
}, },
@ -166,17 +161,17 @@ export default {
}, {} }, {}
) )
this.synchronizeSelectedEntities() this.sync()
}, },
toggleGroup(group) { toggleGroup(group) {
this.selectedGroups[group] = !this.selectedGroups[group] this.selectedGroups[group] = !this.selectedGroups[group]
this.synchronizeSelectedEntities() this.sync()
}, },
processEntityUpdate(entity) { processEntityUpdate(entity) {
const group = entity[this.value?.grouping] const group = entity[this.value?.grouping]
if (group && this.selectedGroups[entity[group]] == null) { if (group && this.selectedGroups[group] == null) {
this.selectedGroups[group] = true this.selectedGroups[group] = true
} }
}, },
@ -194,7 +189,7 @@ export default {
mounted() { mounted() {
this.refreshGroupFilter() this.refreshGroupFilter()
this.$watch(() => this.value?.grouping, () => { this.refreshGroupFilter() }) this.$watch(() => this.value?.grouping, () => { this.refreshGroupFilter() })
this.$watch(() => this.searchTerm, this.updateSearchTerm) this.$watch(() => this.searchTerm, this.sync)
bus.onEntity(this.processEntityUpdate) bus.onEntity(this.processEntityUpdate)
}, },
} }