using System; using System.Collections.Generic; using System.Linq; using System.Text; using Umbraco.Core; using Umbraco.Core.Logging; using umbraco.cms.presentation.Trees; namespace Umbraco.Web.Trees { /// /// A class used to render the legacy JS requirements for trees and IActions. /// internal static class LegacyTreeJavascript { /// /// If any legacy tree requires any JS rendering then we will compile a JS output of the combination. /// /// public static string GetLegacyTreeJavascript() { //find all tree defs that exists for the current application regardless of if they are active List appTreeDefs = TreeDefinitionCollection.Instance; //Create the BaseTree's based on the tree definitions found var legacyTrees = appTreeDefs.Select(treeDef => treeDef.CreateInstance()).ToList(); var javascript = new StringBuilder(); foreach (var bTree in legacyTrees) { bTree.RenderJS(ref javascript); } return javascript.ToString(); } /// /// Returns a string with javascript proxy methods for IActions that are using old javascript /// /// public static string GetLegacyIActionJavascript() { var js = new StringBuilder(); foreach (var a in ActionsResolver.Current.Actions.ToList()) { // NH: Added a try/catch block to this as an error in a 3rd party action can crash the whole menu initialization try { if (string.IsNullOrEmpty(a.Alias) == false && (string.IsNullOrEmpty(a.JsFunctionName) == false || string.IsNullOrEmpty(a.JsSource) == false)) { // if the action is using invalid javascript we need to do something about this if (global::umbraco.BusinessLogic.Actions.Action.ValidateActionJs(a) == false) { js.AppendLine("function IActionProxy_" + a.Alias.ToSafeAlias() + "() {"); js.AppendLine(global::umbraco.BusinessLogic.Actions.Action.ConvertLegacyJs(a.JsFunctionName)); js.AppendLine("}"); } } } catch (Exception ee) { LogHelper.Error(typeof(LegacyTreeJavascript), "Error initializing tree action", ee); } } if (js.Length != 0) { js.Insert(0, "// This javascript is autogenerated by Umbraco to ensure legacy compatiblity with old context menu items\n\n"); } return js.ToString(); } } }