Beispiel #1
0
        public static SiteMapNode GetParentSiteMapNodeByLevel(this SiteMapNode childNode, int level)
        {
            //TODO: Unit test
            // Pre-conditions
            if (level < 0)
            {
                throw new ArgumentOutOfRangeException("level", "level cannot be negative");
            }
            if (childNode == null)
            {
                return(null);
            }

            const int rootLevel = 0;

            if (level == rootLevel)
            {
                return(childNode.RootNode);
            }

            if (level == 1) // Short-cut for first non-root parent
            {
                if (childNode == SiteMap.RootNode)
                {
                    return(null);
                }
                SiteMapNode result = childNode;

                while ((result.ParentNode != null) && (result.ParentNode != childNode.RootNode))
                {
                    result = result.ParentNode;
                }
                return(result);
            }
            int         childNodeLevel   = childNode.GetDepthLevel();
            SiteMapNode currentLevelNode = childNode;

            while (childNodeLevel > level)
            {
                currentLevelNode = currentLevelNode.ParentNode;
                childNodeLevel--;
            }

            return(currentLevelNode);
        }