using Umbraco.Core; using Umbraco.Core.Models.EntityBase; using umbraco; namespace Umbraco.Web.Trees.Menu { public static class MenuItemExtensions { /// /// Used as a key for the AdditionalData to specify a specific dialog title instead of the menu title /// internal const string DialogTitleKey = "dialogTitle"; /// /// Used to specify the URL that the dialog will launch to in an iframe /// internal const string ActionUrlKey = "actionUrl"; internal const string ActionUrlMethodKey = "actionUrlMethod"; /// /// Used to specify the angular view that the dialog will launch /// internal const string ActionViewKey = "actionView"; /// /// Sets the menu item to display a dialog based on an angular view path /// /// /// /// public static void LaunchDialogView(this MenuItem menuItem, string view, string dialogTitle) { menuItem.SetDialogTitle(dialogTitle); menuItem.AdditionalData[ActionViewKey] = view; } /// /// Sets the menu item to display a dialog based on a url path in an iframe /// /// /// /// public static void LaunchDialogUrl(this MenuItem menuItem, string url, string dialogTitle) { menuItem.SetDialogTitle(dialogTitle); menuItem.SetActionUrl(url); } /// /// Puts a dialog title into the meta data to be displayed on the dialog of the menu item (if there is one) /// instead of the menu name /// /// /// private static void SetDialogTitle(this MenuItem menuItem, string dialogTitle) { menuItem.AdditionalData[DialogTitleKey] = dialogTitle; } /// /// Configures the menu item to launch a URL with the specified action (dialog or new window) /// /// /// /// private static void SetActionUrl(this MenuItem menuItem, string url, ActionUrlMethod method = ActionUrlMethod.Dialog) { menuItem.AdditionalData[ActionUrlKey] = url; menuItem.AdditionalData[ActionUrlMethodKey] = url; } internal static void ConvertLegacyMenuItem(this MenuItem menuItem, IUmbracoEntity item, string nodeType, string currentSection) { //First try to get a URL/title from the legacy action, // if that doesn't work, try to get the legacy confirm view //in some edge cases, item can be null so we'll just convert those to "-1" and "" for id and name since these edge cases don't need that. Attempt .Try(LegacyTreeDataConverter.GetUrlAndTitleFromLegacyAction(menuItem.Action, item == null ? "-1" : item.Id.ToInvariantString(), nodeType, item == null ? "" : item.Name, currentSection), action => menuItem.LaunchDialogUrl(action.Url, action.DialogTitle)) .OnFailure(() => LegacyTreeDataConverter.GetLegacyConfirmView(menuItem.Action, currentSection), view => menuItem.LaunchDialogView( view, ui.GetText("defaultdialogs", "confirmdelete") + " '" + item.Name + "' ?")); } } }