using System;
using Abp.Collections.Extensions;
namespace Abp.Application.Navigation
{
///
/// Defines extension methods for .
///
public static class HasMenuItemDefinitionsExtensions
{
///
/// Searches and gets a by it's unique name.
/// Throws exception if can not find.
///
/// Source object
/// Unique name of the source
public static MenuItemDefinition GetItemByName(this IHasMenuItemDefinitions source, string name)
{
var item = GetItemByNameOrNull(source, name);
if (item == null)
{
throw new ArgumentException("There is no source item with given name: " + name, "name");
}
return item;
}
///
/// Searches all menu items (recursively) in the source and gets a by it's unique name.
/// Returns null if can not find.
///
/// Source object
/// Unique name of the source
public static MenuItemDefinition GetItemByNameOrNull(this IHasMenuItemDefinitions source, string name)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (source.Items.IsNullOrEmpty())
{
return null;
}
foreach (var subItem in source.Items)
{
if (subItem.Name == name)
{
return subItem;
}
var subItemSearchResult = GetItemByNameOrNull(subItem, name);
if (subItemSearchResult != null)
{
return subItemSearchResult;
}
}
return null;
}
}
}