Add TreeController.normalizeSelectedNodes and supporting functions in Node.

This commit is contained in:
Brent Simmons 2017-10-21 12:14:15 -07:00
parent e0f872a8c7
commit edea37dba6
2 changed files with 43 additions and 1 deletions

View File

@ -103,6 +103,34 @@ public final class Node: Equatable {
}
return nil
}
public func hasAncestor(in nodes: [Node]) -> Bool {
for node in nodes {
if node.isAncestor(of: self) {
return true
}
}
return false
}
public func isAncestor(of node: Node) -> Bool {
if node == self {
return false
}
var nomad = node
while true {
guard let parent = nomad.parent else {
return false
}
if parent == self {
return true
}
nomad = parent
}
}
}

View File

@ -67,6 +67,21 @@ public final class TreeController {
return nodeInArrayRepresentingObject(nodes: [rootNode], representedObject: representedObject, recurse: true)
}
public func normalizedSelectedNodes(_ nodes: [Node]) -> [Node] {
// An array of nodes might include a leaf node and its parent. Remove the leaf node.
var normalizedNodes = [Node]()
for node in nodes {
if !node.hasAncestor(in: nodes) {
normalizedNodes += [node]
}
}
return normalizedNodes
}
}
private extension TreeController {
@ -117,5 +132,4 @@ private extension TreeController {
return childNodesDidChange
}
}