uComponents: Added base classes for PrevalueEditors and Data.

This commit is contained in:
leekelleher
2012-04-28 13:35:16 -01:00
parent 551f1988ce
commit 113ecfd3cf
7 changed files with 478 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
using System;
using System.Web.Script.Serialization;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Abstract class for the PreValue Editor.
/// Specifically designed to serialize/deserialize the options as JSON.
/// </summary>
public abstract class AbstractJsonPrevalueEditor : AbstractPrevalueEditor
{
/// <summary>
/// The underlying base data-type.
/// </summary>
protected readonly BaseDataType m_DataType;
/// <summary>
/// An object to temporarily lock writing to the database.
/// </summary>
private static readonly object m_Locker = new object();
/// <summary>
/// Initializes a new instance of the <see cref="AbstractJsonPrevalueEditor"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
public AbstractJsonPrevalueEditor(BaseDataType dataType)
{
this.m_DataType = dataType;
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractJsonPrevalueEditor"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
/// <param name="dbType">Type of the database field.</param>
public AbstractJsonPrevalueEditor(BaseDataType dataType, DBTypes dbType)
: base()
{
this.m_DataType = dataType;
this.m_DataType.DBType = dbType;
}
/// <summary>
/// Gets the PreValue options for the data-type.
/// </summary>
/// <typeparam name="T">The type of the resulting object.</typeparam>
/// <returns>
/// Returns the options for the PreValue Editor
/// </returns>
public T GetPreValueOptions<T>()
{
var prevalues = PreValues.GetPreValues(this.m_DataType.DataTypeDefinitionId);
if (prevalues.Count > 0)
{
var prevalue = (PreValue)prevalues[0];
if (!string.IsNullOrEmpty(prevalue.Value))
{
try
{
// deserialize the options
var serializer = new JavaScriptSerializer();
// return the options
return serializer.Deserialize<T>(prevalue.Value);
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, this.m_DataType.DataTypeDefinitionId, string.Concat("uComponents: Execption thrown: ", ex.Message));
}
}
}
// if all else fails, return default options
return default(T);
}
/// <summary>
/// Saves the data-type PreValue options.
/// </summary>
public void SaveAsJson(object options)
{
// serialize the options into JSON
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(options);
lock (m_Locker)
{
var prevalues = PreValues.GetPreValues(this.m_DataType.DataTypeDefinitionId);
if (prevalues.Count > 0)
{
PreValue prevalue = (PreValue)prevalues[0];
// update
prevalue.Value = json;
prevalue.Save();
}
else
{
// insert
PreValue.MakeNew(this.m_DataType.DataTypeDefinitionId, json);
}
}
}
}
}

View File

@@ -0,0 +1,42 @@
using System.ComponentModel;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Abstract class for the Prevalue Editor options.
/// </summary>
public abstract class AbstractOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="AbstractOptions"/> class.
/// </summary>
public AbstractOptions()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractOptions"/> class.
/// </summary>
/// <param name="loadDefaults">if set to <c>true</c> [load defaults].</param>
public AbstractOptions(bool loadDefaults)
: this()
{
if (loadDefaults)
{
// get the type of the object.
var type = this.GetType();
// iterate through the properties.
foreach (var property in type.GetProperties())
{
// iterate through the DefaultValue attributes.
foreach (DefaultValueAttribute attribute in property.GetCustomAttributes(typeof(DefaultValueAttribute), true))
{
// set the default value of the property.
property.SetValue(this, attribute.Value, null);
}
}
}
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClientDependency.Core;
using umbraco.interfaces;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Abstract class for the PreValue Editor.
/// </summary>
public abstract class AbstractPrevalueEditor : WebControl, IDataPrevalue
{
/// <summary>
/// Initializes a new instance of the <see cref="AbstractPrevalueEditor"/> class.
/// </summary>
public AbstractPrevalueEditor()
: base()
{
}
/// <summary>
/// Gets the editor.
/// </summary>
/// <value>The editor.</value>
public virtual Control Editor
{
get
{
return this;
}
}
/// <summary>
/// Saves this instance.
/// </summary>
public virtual void Save()
{
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.EnsureChildControls();
// Adds the client dependencies.
this.AddResourceToClientDependency("uComponents.DataTypes.Shared.Resources.Styles.PrevalueEditor.css", ClientDependencyType.Css);
}
/// <summary>
/// Renders the HTML opening tag of the control to the specified writer. This method is used primarily by control developers.
/// </summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "uComponents");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
base.RenderBeginTag(writer);
}
/// <summary>
/// Renders the HTML closing tag of the control into the specified writer. This method is used primarily by control developers.
/// </summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
public override void RenderEndTag(HtmlTextWriter writer)
{
base.RenderEndTag(writer);
writer.RenderEndTag();
}
}
}

View File

@@ -0,0 +1,96 @@
using System.Xml;
using umbraco;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Overrides the <see cref="umbraco.cms.businesslogic.datatype.DefaultData"/> object to return the value as XML.
/// </summary>
public class CsvToXmlData : DefaultData
{
/// <summary>
/// The separators to split the delimited string.
/// </summary>
private string[] separator;
/// <summary>
/// Name for the root node.
/// </summary>
private string rootName;
/// <summary>
/// Name for the element/node that contains the value.
/// </summary>
private string elementName;
/// <summary>
/// Initializes a new instance of the <see cref="CsvToXmlData"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
public CsvToXmlData(BaseDataType dataType)
: this(dataType, "values")
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvToXmlData"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
/// <param name="rootName">Name of the root.</param>
public CsvToXmlData(BaseDataType dataType, string rootName)
: this(dataType, rootName, "value")
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvToXmlData"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
/// <param name="rootName">Name of the root.</param>
/// <param name="elementName">Name of the element.</param>
public CsvToXmlData(BaseDataType dataType, string rootName, string elementName)
: this(dataType, rootName, elementName, new[] { "," })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvToXmlData"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
/// <param name="rootName">Name of the root.</param>
/// <param name="elementName">Name of the element.</param>
/// <param name="separator">The separator.</param>
public CsvToXmlData(BaseDataType dataType, string rootName, string elementName, string[] separator)
: base(dataType)
{
this.rootName = rootName;
this.elementName = elementName;
this.separator = separator;
}
/// <summary>
/// Converts the data value to XML.
/// </summary>
/// <param name="data">The data to convert to XML.</param>
/// <returns>Returns the data value as an XmlNode</returns>
public override XmlNode ToXMl(XmlDocument data)
{
// check that the value isn't null
if (this.Value != null)
{
// split the CSV data into an XML document.
var xml = xmlHelper.Split(new XmlDocument(), this.Value.ToString(), this.separator, this.rootName, this.elementName);
// return the XML node.
return data.ImportNode(xml.DocumentElement, true);
}
else
{
// otherwise render the value as default (in CDATA)
return base.ToXMl(data);
}
}
}
}

View File

@@ -0,0 +1,105 @@
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Extension methods for the Prevalue Editor
/// </summary>
public static class PrevalueEditorExtensions
{
/// <summary>
/// Adds the prevalue controls.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="controls">The controls.</param>
public static void AddPrevalueControls(this ControlCollection collection, params Control[] controls)
{
foreach (var control in controls)
{
collection.Add(control);
}
}
/// <summary>
/// Adds the prevalue row heading.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="heading">The heading.</param>
public static void AddPrevalueHeading(this HtmlTextWriter writer, string heading)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "row clearfix");
writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'row'
writer.RenderBeginTag(HtmlTextWriterTag.H3); // start 'h3'
writer.Write(heading);
writer.RenderEndTag(); // end 'h3'
writer.RenderEndTag(); // end 'row'
}
/// <summary>
/// Adds a new row to the Prevalue Editor.
/// </summary>
/// <param name="writer">The HtmlTextWriter.</param>
/// <param name="label">The label for the field.</param>
/// <param name="controls">The controls for the field.</param>
public static void AddPrevalueRow(this HtmlTextWriter writer, string label, params Control[] controls)
{
writer.AddPrevalueRow(label, string.Empty, controls);
}
/// <summary>
/// Adds a new row to the Prevalue Editor, (with an optional description).
/// </summary>
/// <param name="writer">The HtmlTextWriter.</param>
/// <param name="label">The label for the field.</param>
/// <param name="description">The description for the field.</param>
/// <param name="controls">The controls for the field.</param>
public static void AddPrevalueRow(this HtmlTextWriter writer, string label, string description, params Control[] controls)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "row clearfix");
writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'row'
writer.AddAttribute(HtmlTextWriterAttribute.Class, "label");
writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'label'
var lbl = new HtmlGenericControl("label") { InnerText = label };
if (controls.Length > 0 && !string.IsNullOrEmpty(controls[0].ClientID))
{
lbl.Attributes.Add("for", controls[0].ClientID);
}
lbl.RenderControl(writer);
writer.RenderEndTag(); // end 'label'
writer.AddAttribute(HtmlTextWriterAttribute.Class, "field");
writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'field'
foreach (var control in controls)
{
control.RenderControl(writer);
}
writer.RenderEndTag(); // end 'field'
if (!string.IsNullOrEmpty(description))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "description");
writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'description'
Label desc = new Label() { Text = description };
desc.RenderControl(writer);
writer.RenderEndTag(); // end 'description'
}
writer.RenderEndTag(); // end 'row'
}
}
}

View File

@@ -0,0 +1,44 @@
using System.Xml;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// Overrides the <see cref="umbraco.cms.businesslogic.datatype.DefaultData"/> object to return the value as XML.
/// </summary>
public class XmlData : DefaultData
{
/// <summary>
/// Initializes a new instance of the <see cref="XmlData"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
public XmlData(BaseDataType dataType)
: base(dataType)
{
}
/// <summary>
/// Converts the data value to XML.
/// </summary>
/// <param name="data">The data to convert to XML.</param>
/// <returns></returns>
public override XmlNode ToXMl(XmlDocument data)
{
// check that the value isn't null and starts with an opening angle-bracket.
if (this.Value != null && xmlHelper.CouldItBeXml(this.Value.ToString()))
{
// load the value into an XML document.
var xd = new XmlDocument();
xd.LoadXml(this.Value.ToString());
// return the XML node.
return data.ImportNode(xd.DocumentElement, true);
}
else
{
// otherwise render the value as default (in CDATA)
return base.ToXMl(data);
}
}
}
}

View File

@@ -200,7 +200,11 @@
</Compile>
<Compile Include="Actions\IActionHandler.cs" />
<Compile Include="businesslogic\CMSPreviewNode.cs" />
<Compile Include="businesslogic\datatype\AbstractJsonPrevalueEditor.cs" />
<Compile Include="businesslogic\datatype\AbstractOptions.cs" />
<Compile Include="businesslogic\datatype\AbstractPrevalueEditor.cs" />
<Compile Include="businesslogic\datatype\ClientDependencyAttribute.cs" />
<Compile Include="businesslogic\datatype\CsvToXmlData.cs" />
<Compile Include="businesslogic\datatype\DataEditorSetting.cs" />
<Compile Include="businesslogic\datatype\DataEditorSettingsStorage.cs" />
<Compile Include="businesslogic\datatype\DataEditorSettingType.cs" />
@@ -208,6 +212,9 @@
<Compile Include="businesslogic\datatype\DBTypes.cs" />
<Compile Include="businesslogic\datatype\IDataEditorSettingType.cs" />
<Compile Include="businesslogic\datatype\PreValue.cs" />
<Compile Include="businesslogic\datatype\PrevalueEditorExtensions.cs" />
<Compile Include="businesslogic\datatype\ResourceExtensions.cs" />
<Compile Include="businesslogic\datatype\XmlData.cs" />
<Compile Include="businesslogic\installer\IInstallerStep.cs" />
<Compile Include="businesslogic\installer\InstallerStep.cs" />
<Compile Include="businesslogic\installer\InstallStepCollection.cs">