namespace Nop.Web.Framework.Menu; /// /// Admin menu item /// public partial class AdminMenuItem { #region Fields protected IList _permissionNames; protected string _url; #endregion #region Ctor /// /// Initializes a new instance of the class. /// public AdminMenuItem() { ChildNodes = new List(); } #endregion #region Utilities /// /// Inserts new menu item /// /// Menu item to get place for insert /// New menu item /// The flag which indicates where to place new menu item, before (when true) or after (when false) the exist one /// True if a new menu item has been inserted protected virtual bool Insert(string itemSystemName, AdminMenuItem newMenuItem, bool before) { var position = 0; var inserted = false; foreach (var adminMenuItem in ChildNodes.ToList()) if (!adminMenuItem.SystemName.Equals(itemSystemName)) position += 1; else { ChildNodes.Insert(position + (before ? 0 : 1), newMenuItem); inserted = true; break; } if (inserted) return true; foreach (var adminMenuItem in ChildNodes) { inserted = adminMenuItem.Insert(itemSystemName, newMenuItem, before); if (inserted) break; } return inserted; } #endregion #region Methods /// /// Checks whether this node or child ones has a specified system name /// /// System name /// Result public bool ContainsSystemName(string systemName) { return GetItemBySystem(systemName) != null; } /// /// Gets the menu item by the system name /// /// Menu item system name /// Menu item if found, otherwise null public AdminMenuItem GetItemBySystem(string systemName) { if (string.IsNullOrEmpty(systemName)) return null; return SystemName.Equals(systemName) ? this : ChildNodes.Select(n => n.GetItemBySystem(systemName)).FirstOrDefault(n => n != null); } /// /// Inserts new menu item before the existing one /// /// Menu item to get place for insert /// New menu item /// True if a new menu item has been inserted public virtual bool InsertBefore(string itemSystemName, AdminMenuItem newMenuItem) { return Insert(itemSystemName, newMenuItem, true); } /// /// Inserts new menu item after the existing one /// /// Menu item to get place for insert /// New menu item /// True if a new menu item has been inserted public virtual bool InsertAfter(string itemSystemName, AdminMenuItem newMenuItem) { return Insert(itemSystemName, newMenuItem, false); } #endregion #region Properties /// /// Gets permission names /// public IList PermissionNames { get { if (_permissionNames != null) return _permissionNames; if (ChildNodes.Any()) return ChildNodes.SelectMany(p => p.PermissionNames).ToList(); return []; } set => _permissionNames = value; } /// /// Gets or sets the system name. /// public string SystemName { get; set; } /// /// Gets or sets the title. /// public string Title { get; set; } /// /// Gets or sets the URL. /// public string Url { get => _url; set { _url = value; var parts = value?.TrimEnd('/').Split('/') ?? []; var len = parts.Length; if (len < 3) { ControllerName = string.Empty; ActionName = string.Empty; } else { ControllerName = parts[len - 2]; ActionName = parts[len - 1]; } } } /// /// Gets or sets the child nodes. /// public IList ChildNodes { get; set; } /// /// Gets or sets the icon class (Font Awesome: http://fontawesome.io/) /// public string IconClass { get; set; } /// /// Gets or sets the item is visible /// public bool Visible { get; set; } = true; /// /// Gets or sets a value indicating whether to open url in new tab (window) or not /// public bool OpenUrlInNewTab { get; set; } /// /// Gets the controller name from URL /// public string ControllerName { get; protected set; } /// /// Gets the action name from URL /// public string ActionName { get; protected set; } #endregion }