only find parents for parents and children for children
This commit is contained in:
parent
274b86532a
commit
ba230e1eed
1 changed files with 24 additions and 35 deletions
|
@ -26,40 +26,32 @@ export const generateLayoutObject = (topics, synapses, focalTopicId) => {
|
||||||
let newRoot
|
let newRoot
|
||||||
let currentTopic
|
let currentTopic
|
||||||
|
|
||||||
const addParentsAndChildren = (topic) => {
|
const addParentsAndChildren = (topic, getParents, getChildren) => {
|
||||||
if (!topic.id) return topic
|
if (!topic.id) return topic
|
||||||
|
|
||||||
usedTopics[topic.id] = true
|
usedTopics[topic.id] = true
|
||||||
|
|
||||||
topic.parents = []
|
if (getParents) {
|
||||||
topic.children = []
|
topic.parents = []
|
||||||
|
synapses.filter(synapse => {
|
||||||
|
return synapse.topic2_id === topic.id
|
||||||
|
&& !usedTopics[synapse.topic1_id]
|
||||||
|
&& synapse.category === 'from-to'
|
||||||
|
})
|
||||||
|
.map(synapse => synapse.topic1_id)
|
||||||
|
.forEach(parentId => topic.parents.push(addParentsAndChildren({id: parentId}, true, false)))
|
||||||
|
}
|
||||||
|
|
||||||
let filteredParentIds = synapses.filter(synapse => {
|
if (getChildren) {
|
||||||
return synapse.topic2_id === topic.id
|
topic.children = []
|
||||||
&& !usedTopics[synapse.topic1_id]
|
synapses.filter(synapse => {
|
||||||
&& synapse.category === 'from-to'
|
return synapse.topic1_id === topic.id
|
||||||
}).map(synapse => synapse.topic1_id)
|
&& !usedTopics[synapse.topic2_id]
|
||||||
|
&& synapse.category === 'from-to'
|
||||||
let filteredChildrenIds = synapses.filter(synapse => {
|
})
|
||||||
return synapse.topic1_id === topic.id
|
.map(synapse => synapse.topic2_id)
|
||||||
&& !usedTopics[synapse.topic2_id]
|
.forEach(childId => topic.children.push(addParentsAndChildren({id: childId}, false, true)))
|
||||||
&& synapse.category === 'from-to'
|
}
|
||||||
|
|
||||||
}).map(synapse => synapse.topic2_id)
|
|
||||||
|
|
||||||
filteredParentIds.forEach(parentId => {
|
|
||||||
let parent = {
|
|
||||||
id: parentId
|
|
||||||
}
|
|
||||||
topic.parents.push(addParentsAndChildren(parent))
|
|
||||||
})
|
|
||||||
|
|
||||||
filteredChildrenIds.forEach(childId => {
|
|
||||||
let child = {
|
|
||||||
id: childId
|
|
||||||
}
|
|
||||||
topic.children.push(addParentsAndChildren(child))
|
|
||||||
})
|
|
||||||
|
|
||||||
return topic
|
return topic
|
||||||
}
|
}
|
||||||
|
@ -73,17 +65,14 @@ export const generateLayoutObject = (topics, synapses, focalTopicId) => {
|
||||||
newRoot = {
|
newRoot = {
|
||||||
id: currentTopic.id
|
id: currentTopic.id
|
||||||
}
|
}
|
||||||
layout.push(addParentsAndChildren(newRoot))
|
layout.push(addParentsAndChildren(newRoot, true, true))
|
||||||
|
// do the rest
|
||||||
//
|
|
||||||
// go through the the topics again, and build the island for the first topic that isn't
|
|
||||||
// yet in the usedTopics object (in any island). recurse
|
|
||||||
topics.forEach(topic => {
|
topics.forEach(topic => {
|
||||||
if (topic && topic.id && !usedTopics[topic.id]) {
|
if (topic && topic.id && !usedTopics[topic.id]) {
|
||||||
newRoot = {
|
newRoot = {
|
||||||
id: topic.id
|
id: topic.id
|
||||||
}
|
}
|
||||||
layout.push(addParentsAndChildren(newRoot))
|
layout.push(addParentsAndChildren(newRoot, true, true))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return layout
|
return layout
|
||||||
|
|
Loading…
Add table
Reference in a new issue