Also delete child nodes when deleting

This commit is contained in:
simon 2019-06-14 22:36:42 -04:00
parent a531bb20ee
commit 55b47450df
2 changed files with 37 additions and 11 deletions

View File

@ -364,11 +364,15 @@ export function MusicGraph(data) {
weight: weight weight: weight
})) }))
// Update source/targetLinks // Update source/targetLinks, avoid bidirectional links
for (const {source, target} of linksToAdd) { for (const {source, target} of linksToAdd) {
if (!target.sourceLinks.has(source.id)) {
source.sourceLinks.add(target.id) source.sourceLinks.add(target.id)
}
if (!source.targetLinks.has(target.id)) {
target.targetLinks.add(source.id) target.targetLinks.add(source.id)
} }
}
this.nodes.push(...nodesToAdd) this.nodes.push(...nodesToAdd)
this.links.push(...linksToAdd) this.links.push(...linksToAdd)
@ -384,20 +388,24 @@ export function MusicGraph(data) {
this.removeNodes = function (idsToRemove) { this.removeNodes = function (idsToRemove) {
let idSetToRemove = new Set(idsToRemove) let idSetToRemove = new Set(idsToRemove)
// Remove child nodes
idsToRemove.forEach(id => { idsToRemove.forEach(id => {
// Update targetLinks this.nodeById.get(id).sourceLinks.forEach(childId => idSetToRemove.add(childId))
Array.from(this.nodeById.get(id).sourceLinks) })
// Update node link cache
idSetToRemove.forEach(id => {
let node = this.nodeById.get(id)
Array.from(node.sourceLinks)
.map(srcId => this.nodeById.get(srcId)) .map(srcId => this.nodeById.get(srcId))
.forEach(target => { .forEach(target => {
target.targetLinks.delete(id) target.targetLinks.delete(id)
}) })
Array.from(this.nodeById.get(id).targetLinks) Array.from(node.targetLinks)
.map(srcId => this.nodeById.get(srcId)) .map(srcId => this.nodeById.get(srcId))
.forEach(target => { .forEach(target => {
target.sourceLinks.delete(id) target.sourceLinks.delete(id)
}) })
this.nodeById.delete(id)
}) })
// Remove links // Remove links
@ -407,6 +415,10 @@ export function MusicGraph(data) {
) )
// Remove nodes // Remove nodes
idSetToRemove.forEach(id => {
this.nodeById.delete(id)
this.expandedNodes.delete(id)
})
this.nodes = this.nodes.filter(d => !idSetToRemove.has(d.id)) this.nodes = this.nodes.filter(d => !idSetToRemove.has(d.id))
this._update() this._update()

View File

@ -136,10 +136,24 @@ export function MusicGraphApi() {
this.getRelatedByMbid = function (mbid) { this.getRelatedByMbid = function (mbid) {
return d3.json(this.url + '/artist/related/' + mbid) return d3.json(this.url + '/artist/related/' + mbid)
.then((r) => { .then((r) => {
let node = nodeUtils.fromRawDict(r.artists.find(a => a.mbid === mbid))
let directedRelations = r.relations.map(rel => {
// Make new nodes children of the expanded nodes, no matter the original direction
if (rel.source === node.id) {
return rel
} else {
return { return {
node: nodeUtils.fromRawDict(r.artists.find(a => a.mbid === mbid)), source: rel.target,
target: rel.source,
weight: rel.weight
}
}
})
return {
node: node,
newNodes: r.artists.map(nodeUtils.fromRawDict), newNodes: r.artists.map(nodeUtils.fromRawDict),
relations: r.relations relations: directedRelations
} }
}) })
} }