public string RenderJSONMenu() { JSONSerializer jSSerializer = new JSONSerializer(); jSSerializer.RegisterConverters(new List<JavaScriptConverter>() { new JTreeContextMenuItem() }); List<IAction> allActions = new List<IAction>(); foreach (IAction a in global::umbraco.BusinessLogic.Actions.Action.GetAll()) { // 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) && (!string.IsNullOrEmpty(a.JsFunctionName) || !string.IsNullOrEmpty(a.JsSource))) { // if the action is using invalid javascript we need to do something about this if (!umbraco.BusinessLogic.Actions.Action.ValidateActionJs(a)) { // Make new Iaction PlaceboAction pa = new PlaceboAction(a); pa.JsFunctionName = "IActionProxy_" + umbraco.cms.helpers.Casing.SafeAlias(pa.Alias) + "()"; allActions.Add(pa); } else { allActions.Add(a); } } } catch (Exception ee) { LogHelper.Error<JTreeContextMenu>("Error initializing tree action", ee); } } return jSSerializer.Serialize(allActions); }
/// <summary> /// Serializes an XmlTreeNode object with the relevant values. /// </summary> /// <param name="obj"></param> /// <param name="serializer"></param> /// <returns></returns> public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { XmlTreeNode node = obj as XmlTreeNode; Dictionary<string, object> result = new Dictionary<string, object>(); if (node != null) { //the data object to build the node Dictionary<string, object> data = new Dictionary<string, object>(); data.Add("title", node.Text); //the attributes object fot the tree node link (a) object created Dictionary<string, object> dataAttributes = new Dictionary<string, object>(); string cssClass = ""; if (node.Icon.StartsWith(".spr")) cssClass = "sprTree " + node.Icon.TrimStart('.'); else { //there is no sprite so add the noSpr class cssClass = "sprTree noSpr"; data.Add("icon", IconPath + node.Icon); } dataAttributes.Add("class", cssClass + (string.IsNullOrEmpty(node.IconClass) ? "" : " " + node.IconClass)); dataAttributes.Add("href", node.Action); //add a metadata dictionary object, we can store whatever we want in this! //in this case we're going to store permissions & the tree type & the child node source url //For whatever reason jsTree requires this JSON output to be quoted!?! //This also needs to be stored in the attributes object with the class above. Dictionary<string, object> metadata = new Dictionary<string, object>(); //the menu: metadata.Add("menu", node.Menu == null ? "" : umbraco.BusinessLogic.Actions.Action.ToString(node.Menu)); //the tree type: metadata.Add("nodeType", node.NodeType); //the data url for child nodes: metadata.Add("source", node.Source); //the metadata/jsTree requires this property to be in a quoted JSON syntax JSONSerializer jsSerializer = new JSONSerializer(); string strMetaData = jsSerializer.Serialize(metadata).Replace("\"", "'"); dataAttributes.Add("umb:nodedata", strMetaData); data.Add("attributes", dataAttributes); //add the data structure result.Add("data", data); //state is nothing if no children if ((node.HasChildren || node.IsRoot) && !string.IsNullOrEmpty(node.Source)) result.Add("state", "closed"); //the attributes object for the tree node (li) object created Dictionary<string, object> attributes = new Dictionary<string, object>(); attributes.Add("id", node.NodeID); attributes.Add("class", string.Join(" ", node.Style.AppliedClasses.ToArray())); if (node.IsRoot) attributes.Add("rel", "rootNode"); else attributes.Add("rel", "dataNode"); //the tree type should always be set, however if some developers have serialized their tree into an XmlTree //then there is no gaurantees that this will be set if they didn't use the create methods of XmlTreeNode. //So, we'll set the treetype to the nodetype if it is null, this means that the tree on the front end may //not function as expected when reloding a node. attributes.Add("umb:type", string.IsNullOrEmpty(node.TreeType) ? node.NodeType : node.TreeType); result.Add("attributes", attributes); return result; } return null; }