Removes old serializers for old tree formats

This commit is contained in:
Shannon
2016-03-31 18:50:25 +02:00
parent cf822be04f
commit 4df5ddcb4c

View File

@@ -11,13 +11,6 @@ using Action = Umbraco.Web._Legacy.Actions.Action;
namespace umbraco.cms.presentation.Trees
{
public enum SerializedTreeType
{
JSONTree,
JsTree
}
/// <summary>
/// Used for serializing data to XML as the data structure for the JavaScript tree
/// </summary>
@@ -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();
}
/// <summary>
/// Use this constructor to force a tree provider to be used
/// </summary>
/// <param name="treeType"></param>
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<JavaScriptConverter>()
{
new JSONTreeConverter(),
new JSONTreeNodeConverter()
});
break;
case SerializedTreeType.JsTree:
m_JSSerializer.RegisterConverters(new List<JavaScriptConverter>()
{
new JsTreeNodeConverter()
});
break;
}
}
private JavaScriptSerializer m_JSSerializer;
private SerializedTreeType m_TreeType;
/// <summary>
/// Returns the string representation of the tree structure depending on the SerializedTreeType
/// specified.
/// </summary>
/// <returns></returns>
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
}
/// <summary>
/// Used to serialize an XmlTree object to JSON for supporting a JSON Tree View control.
/// </summary>
internal class JSONTreeConverter : JavaScriptConverter
{
/// <summary>
/// Not implemented as we never need to Deserialize
/// </summary>
/// <param name="dictionary"></param>
/// <param name="type"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
/// <summary>
/// Serializes an XmlTree 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)
{
XmlTree tree = obj as XmlTree;
Dictionary<string, object> resultSet = new Dictionary<string, object>();
Dictionary<string, object> resultTree = new Dictionary<string, object>();
if (tree != null)
{
//add a count property for the count of total nodes
resultTree.Add("Count", tree.Count);
List<object> nodes = new List<object>();
foreach (XmlTreeNode node in tree.treeCollection)
nodes.Add(node);
resultTree.Add("Nodes", nodes);
}
resultSet.Add("Tree", resultTree);
return resultSet;
}
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(XmlTree) }; }
}
}
/// <summary>
/// Used to serialize an XmlTreeNode object to JSON for supporting a JS Tree View control.
/// </summary>
internal class JSONTreeNodeConverter : JavaScriptConverter
{
/// <summary>
/// Not implemented as we never need to Deserialize
/// </summary>
/// <param name="dictionary"></param>
/// <param name="type"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
/// <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)
{
//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<string, object>();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(XmlTreeNode) }; }
}
}
/// <summary>
/// Used to serialize an XmlTreeNode object to JSON for supporting a JS Tree View control.
/// </summary>
internal class JsTreeNodeConverter : JavaScriptConverter
{
/// <summary>
/// A reference path to where the icons are actually stored as compared to where the tree themes folder is
/// </summary>
private static string IconPath = IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/images/umbraco/";
/// <summary>
/// Not implemented as we never need to Deserialize
/// </summary>
/// <param name="dictionary"></param>
/// <param name="type"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
/// <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 ? "" : 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<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;
}
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(XmlTreeNode) }; }
}
}
}