diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/Trees/XmlTree.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/Trees/XmlTree.cs index c6dfd4c758..9b62e5ea1f 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/Trees/XmlTree.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/Trees/XmlTree.cs @@ -11,13 +11,6 @@ using Action = Umbraco.Web._Legacy.Actions.Action; namespace umbraco.cms.presentation.Trees { - - public enum SerializedTreeType - { - JSONTree, - JsTree - } - /// /// Used for serializing data to XML as the data structure for the JavaScript tree /// @@ -27,73 +20,13 @@ namespace umbraco.cms.presentation.Trees public XmlTree() { - //set to the XTree provider by default. - //m_TreeType = SerializedTreeType.XmlTree; - //m_TreeType = SerializedTreeType.JSONTree; - m_TreeType = SerializedTreeType.JsTree; - Init(); } - - /// - /// Use this constructor to force a tree provider to be used - /// - /// - public XmlTree(SerializedTreeType treeType) - { - m_TreeType = treeType; - Init(); - } - + private void Init() { - m_JSSerializer = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }; - - switch (m_TreeType) - { - case SerializedTreeType.JSONTree: - m_JSSerializer.RegisterConverters(new List() - { - new JSONTreeConverter(), - new JSONTreeNodeConverter() - }); - break; - case SerializedTreeType.JsTree: - m_JSSerializer.RegisterConverters(new List() - { - new JsTreeNodeConverter() - }); - break; - } - - } - - private JavaScriptSerializer m_JSSerializer; - private SerializedTreeType m_TreeType; - - /// - /// Returns the string representation of the tree structure depending on the SerializedTreeType - /// specified. - /// - /// - public override string ToString() - { - return ToString(m_TreeType); - } - - public string ToString(SerializedTreeType type) - { - switch (type) - { - case SerializedTreeType.JsTree: - return m_JSSerializer.Serialize(this.treeCollection); - case SerializedTreeType.JSONTree: - return m_JSSerializer.Serialize(this); - } - return ""; - } - + public void Add(XmlTreeNode obj) { treeCollection.Add(obj); @@ -532,231 +465,4 @@ namespace umbraco.cms.presentation.Trees } - /// - /// Used to serialize an XmlTree object to JSON for supporting a JSON Tree View control. - /// - internal class JSONTreeConverter : JavaScriptConverter - { - /// - /// Not implemented as we never need to Deserialize - /// - /// - /// - /// - /// - public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) - { - throw new NotImplementedException(); - } - - /// - /// Serializes an XmlTree object with the relevant values. - /// - /// - /// - /// - public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) - { - XmlTree tree = obj as XmlTree; - - Dictionary resultSet = new Dictionary(); - Dictionary resultTree = new Dictionary(); - - if (tree != null) - { - //add a count property for the count of total nodes - resultTree.Add("Count", tree.Count); - - List nodes = new List(); - foreach (XmlTreeNode node in tree.treeCollection) - nodes.Add(node); - - resultTree.Add("Nodes", nodes); - } - - resultSet.Add("Tree", resultTree); - - return resultSet; - } - - public override IEnumerable SupportedTypes - { - get { return new Type[] { typeof(XmlTree) }; } - } - } - - /// - /// Used to serialize an XmlTreeNode object to JSON for supporting a JS Tree View control. - /// - internal class JSONTreeNodeConverter : JavaScriptConverter - { - - /// - /// Not implemented as we never need to Deserialize - /// - /// - /// - /// - /// - public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) - { - throw new NotImplementedException(); - } - - /// - /// Serializes an XmlTreeNode object with the relevant values. - /// - /// - /// - /// - public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) - { - XmlTreeNode node = obj as XmlTreeNode; - - Dictionary result = new Dictionary(); - - if (node != null) - { - //add the properties - result.Add(XmlTreeNode.TreeAttributes.notPublished.ToString(), node.NotPublished); - result.Add(XmlTreeNode.TreeAttributes.isProtected.ToString(), node.IsProtected); - result.Add(XmlTreeNode.TreeAttributes.text.ToString(), node.Text); - result.Add(XmlTreeNode.TreeAttributes.action.ToString(), node.Action); - result.Add(XmlTreeNode.TreeAttributes.src.ToString(), node.Source); - result.Add(XmlTreeNode.TreeAttributes.iconClass.ToString(), node.IconClass); - result.Add(XmlTreeNode.TreeAttributes.icon.ToString(), node.Icon); - result.Add(XmlTreeNode.TreeAttributes.openIcon.ToString(), node.OpenIcon); - result.Add(XmlTreeNode.TreeAttributes.nodeType.ToString(), node.NodeType); - result.Add(XmlTreeNode.TreeAttributes.nodeID.ToString(), node.NodeID); - - //Add the menu as letters. - result.Add(XmlTreeNode.TreeAttributes.menu.ToString(), node.Menu != null && node.Menu.Count > 0 ? Action.ToString(node.Menu) : ""); - - return result; - } - - return new Dictionary(); - - } - - public override IEnumerable SupportedTypes - { - get { return new Type[] { typeof(XmlTreeNode) }; } - } - } - - /// - /// Used to serialize an XmlTreeNode object to JSON for supporting a JS Tree View control. - /// - internal class JsTreeNodeConverter : JavaScriptConverter - { - - /// - /// A reference path to where the icons are actually stored as compared to where the tree themes folder is - /// - private static string IconPath = IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/images/umbraco/"; - - /// - /// Not implemented as we never need to Deserialize - /// - /// - /// - /// - /// - public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) - { - throw new NotImplementedException(); - } - - /// - /// Serializes an XmlTreeNode object with the relevant values. - /// - /// - /// - /// - public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) - { - XmlTreeNode node = obj as XmlTreeNode; - - Dictionary result = new Dictionary(); - - if (node != null) - { - //the data object to build the node - Dictionary data = new Dictionary(); - - data.Add("title", node.Text); - - - - //the attributes object fot the tree node link (a) object created - Dictionary dataAttributes = new Dictionary(); - 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 metadata = new Dictionary(); - //the menu: - metadata.Add("menu", node.Menu == null ? "" : 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 - JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); - 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 attributes = new Dictionary(); - 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; - - } - - public override IEnumerable SupportedTypes - { - get { return new Type[] { typeof(XmlTreeNode) }; } - } - } }