Skip to content

Commit

Permalink
Cleaned up Descendants and Ancestors code so it will function correct…
Browse files Browse the repository at this point in the history
…ly for any node.
  • Loading branch information
NightOwl888 committed Aug 17, 2013
1 parent e5286db commit 99f48b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
44 changes: 20 additions & 24 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,41 +366,37 @@ public virtual ISiteMapNodeCollection GetChildNodes(ISiteMapNode node)
}
return siteMapChildStateFactory.CreateReadOnlySiteMapNodeCollection(secureCollection);
}
private ISiteMapNodeCollection _descendants;
private ISiteMapNodeCollection _ancestors;
private void FindAllDescendants(ISiteMapNode node)

public virtual ISiteMapNodeCollection GetDescendants(ISiteMapNode node)
{
foreach (var child in node.ChildNodes)
{
_descendants.Add(child);
FindAllDescendants(child);
}
var descendants = siteMapChildStateFactory.CreateSiteMapNodeCollection();
GetDescendantsInternal(node, descendants);
return siteMapChildStateFactory.CreateReadOnlySiteMapNodeCollection(descendants);
}
private void FindAllAncestors(ISiteMapNode node)

public virtual ISiteMapNodeCollection GetAncestors(ISiteMapNode node)
{
if (node.ParentNode != null)
{
_ancestors.Add(node.ParentNode);
FindAllAncestors(node.ParentNode);
}
var ancestors = siteMapChildStateFactory.CreateSiteMapNodeCollection();
GetAncestorsInternal(node, ancestors);
return siteMapChildStateFactory.CreateReadOnlySiteMapNodeCollection(ancestors);
}
public virtual ISiteMapNodeCollection GetDescendants(ISiteMapNode node)

protected virtual void GetDescendantsInternal(ISiteMapNode node, ISiteMapNodeCollection descendants)
{
if (_descendants == null)
foreach (var child in node.ChildNodes)
{
_descendants = siteMapChildStateFactory.CreateSiteMapNodeCollection();
FindAllDescendants(node);
descendants.Add(child);
GetDescendantsInternal(child, descendants);
}
return siteMapChildStateFactory.CreateReadOnlySiteMapNodeCollection(_descendants);
}
public virtual ISiteMapNodeCollection GetAncestors(ISiteMapNode node)

protected virtual void GetAncestorsInternal(ISiteMapNode node, ISiteMapNodeCollection ancestors)
{
if (_ancestors == null)
if (node.ParentNode != null)
{
_ancestors = siteMapChildStateFactory.CreateSiteMapNodeCollection();
FindAllAncestors(node);
ancestors.Add(node.ParentNode);
GetAncestorsInternal(node.ParentNode, ancestors);
}
return siteMapChildStateFactory.CreateReadOnlySiteMapNodeCollection(_ancestors);
}

public virtual ISiteMapNode GetCurrentNodeAndHintAncestorNodes(int upLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public abstract class SiteMapNodePositioningBase
/// </value>
public override ISiteMapNode ParentNode
{
get
{
return this.SiteMap.GetParentNode(this);
}
get { return this.SiteMap.GetParentNode(this); }
}

/// <summary>
Expand All @@ -36,10 +33,24 @@ public override ISiteMapNodeCollection ChildNodes
{
get { return this.SiteMap.GetChildNodes(this); }
}

/// <summary>
/// Gets the descendant nodes.
/// </summary>
/// <value>
/// The descendant nodes.
/// </value>
public override ISiteMapNodeCollection Descendants
{
get { return this.SiteMap.GetDescendants(this); }
}

/// <summary>
/// Gets the ancestor nodes.
/// </summary>
/// <value>
/// The ancestor nodes.
/// </value>
public override ISiteMapNodeCollection Ancestors
{
get { return this.SiteMap.GetAncestors(this); }
Expand All @@ -52,9 +63,9 @@ public override ISiteMapNodeCollection Ancestors
/// <returns>true if the current node is a child or descendant of the specified node; otherwise, false.</returns>
public override bool IsDescendantOf(ISiteMapNode node)
{
for (var node2 = this.ParentNode; node2 != null; node2 = node2.ParentNode)
for (var parent = this.ParentNode; parent != null; parent = parent.ParentNode)
{
if (node2.Equals(node))
if (parent.Equals(node))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ public SiteMapNodeModel(ISiteMapNode node, IDictionary<string, object> sourceMet
/// <c>true</c> if this instance is clickable; otherwise, <c>false</c>.
/// </value>
public bool IsClickable { get; protected set; }

/// <summary>
/// Gets or sets a value indicating whether the visibility property of the current node
/// will affect the descendant nodes.
/// </summary>
/// <value>
/// <c>true</c> if visibility should affect descendants; otherwise, <c>false</c>.
/// </value>
public bool VisibilityAffectsDescendants { get; protected set; }

/// <summary>
/// Gets or sets the route values.
/// </summary>
Expand Down Expand Up @@ -203,7 +212,7 @@ public SiteMapNodeModelList Children
}
else
{
NVDList = new List<SiteMapNodeModel>();
nearestVisibleDescendantsList = new List<SiteMapNodeModel>();
foreach (var child in node.ChildNodes)
{
if (child.IsAccessibleToUser())
Expand All @@ -215,7 +224,7 @@ public SiteMapNodeModelList Children
else if (maxDepth > 0)
{
FindNearestVisibleDescendants(child, maxDepth - 1);
children.AddRange(NVDList);
children.AddRange(nearestVisibleDescendantsList);
}
}
}
Expand All @@ -233,7 +242,7 @@ public SiteMapNodeModelList Children
}
}

private List<SiteMapNodeModel> NVDList;
private List<SiteMapNodeModel> nearestVisibleDescendantsList;
private void FindNearestVisibleDescendants(ISiteMapNode node, int maxDepth)
{
foreach (var child in node.ChildNodes)
Expand All @@ -242,7 +251,7 @@ private void FindNearestVisibleDescendants(ISiteMapNode node, int maxDepth)
{
if (child.IsVisible(SourceMetadata))
{
NVDList.Add(new SiteMapNodeModel(child, SourceMetadata, maxDepth - 1, drillDownToCurrent, false, VisibilityAffectsDescendants));
nearestVisibleDescendantsList.Add(new SiteMapNodeModel(child, SourceMetadata, maxDepth - 1, drillDownToCurrent, false, VisibilityAffectsDescendants));
}
else if (maxDepth > 0)
{
Expand Down

0 comments on commit 99f48b9

Please sign in to comment.