using System; using System.Collections.Generic; using Umbraco.Core; using Umbraco.Core.Services; using Umbraco.Web.Actions; using Umbraco.Web.Composing; namespace Umbraco.Web.Models.Trees { /// /// A custom menu list /// /// /// NOTE: We need a sub collection to the MenuItemCollection object due to how json serialization works. /// public class MenuItemList : List { public MenuItemList() { } public MenuItemList(IEnumerable items) : base(items) { } /// /// Adds a menu item based on a /// /// /// The text to display for the menu item, will default to the IAction alias if not specified internal MenuItem Add(IAction action, string name) { var item = new MenuItem(action, name); Add(item); return item; } /// /// Adds a menu item with a dictionary which is merged to the AdditionalData bag /// /// /// /// The text to display for the menu item, will default to the IAction alias if not specified /// Whether or not this action opens a dialog public MenuItem Add(string name, bool hasSeparator = false, bool opensDialog = false) where T : IAction { var item = CreateMenuItem(name, hasSeparator, opensDialog); if (item != null) { Add(item); return item; } return null; } /// /// Adds a menu item with a dictionary which is merged to the AdditionalData bag /// /// /// /// The used to localize the action name based on it's alias /// Whether or not this action opens a dialog public MenuItem Add(ILocalizedTextService textService, bool hasSeparator = false, bool opensDialog = false) where T : IAction { var item = CreateMenuItem(textService, hasSeparator, opensDialog); if (item != null) { Add(item); return item; } return null; } internal MenuItem CreateMenuItem(string name, bool hasSeparator = false, bool opensDialog = false) where T : IAction { var item = Current.Actions.GetAction(); if (item == null) return null; var menuItem = new MenuItem(item, name) { SeparatorBefore = hasSeparator, OpensDialog = opensDialog }; return menuItem; } internal MenuItem CreateMenuItem(ILocalizedTextService textService, bool hasSeparator = false, bool opensDialog = false) where T : IAction { var item = Current.Actions.GetAction(); if (item == null) return null; var menuItem = new MenuItem(item, textService.Localize($"actions/{item.Alias}")) { SeparatorBefore = hasSeparator, OpensDialog = opensDialog }; return menuItem; } } }