uComponents: Added XPathDropDownList to the core

This commit is contained in:
leekelleher
2012-04-28 13:52:25 -01:00
parent bb8d17b741
commit 16cd283462
4 changed files with 437 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
namespace umbraco.editorControls.XPathDropDownList
{
/// <summary>
/// XPath configurabale DropDownList Data Type
/// </summary>
public class XPathDropDownListDataEditor : CompositeControl, IDataEditor
{
/// <summary>
/// Field for the data.
/// </summary>
private IData m_Data;
/// <summary>
/// Field for the options.
/// </summary>
private XPathDropDownListOptions m_Options;
/// <summary>
/// Field for the CustomValidator.
/// </summary>
private CustomValidator m_CustomValidator = new CustomValidator();
/// <summary>
/// Field for the DropDownList.
/// </summary>
private DropDownList m_DropDownList = new DropDownList();
/// <summary>
/// Gets a value indicating whether [treat as rich text editor].
/// </summary>
/// <value>
/// <c>true</c> if [treat as rich text editor]; otherwise, <c>false</c>.
/// </value>
public virtual bool TreatAsRichTextEditor
{
get
{
return false;
}
}
/// <summary>
/// Gets a value indicating whether [show label].
/// </summary>
/// <value><c>true</c> if [show label]; otherwise, <c>false</c>.</value>
public virtual bool ShowLabel
{
get
{
return true;
}
}
/// <summary>
/// Gets the editor.
/// </summary>
/// <value>The editor.</value>
public Control Editor
{
get
{
return this;
}
}
/// <summary>
/// Initializes a new instance of XPathCheckBoxListDataEditor
/// </summary>
/// <param name="data"></param>
/// <param name="options"></param>
internal XPathDropDownListDataEditor(IData data, XPathDropDownListOptions options)
{
this.m_Data = data;
this.m_Options = options;
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
/// </summary>
protected override void CreateChildControls()
{
this.m_DropDownList.DataSource = uQuery.GetNodesByXPath(this.m_Options.XPath).ToNameIds();
this.m_DropDownList.DataTextField = "Value";
this.m_DropDownList.DataValueField = this.m_Options.UseId ? "Key" : "Value";
this.m_DropDownList.DataBind();
// Add a default please select value
this.m_DropDownList.Items.Insert(0, new ListItem(string.Empty, "-1"));
this.Controls.Add(this.m_CustomValidator);
this.Controls.Add(this.m_DropDownList);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
if (!this.Page.IsPostBack)
{
// Get selected items from Node Name or Node Id
var dropDownListItem = this.m_DropDownList.Items.FindByValue(this.m_Data.Value.ToString());
if (dropDownListItem != null)
{
dropDownListItem.Selected = true;
}
}
}
/// <summary>
/// Called by Umbraco when saving the node
/// </summary>
public void Save()
{
Property property = new Property(((DefaultData)this.m_Data).PropertyId);
if (property.PropertyType.Mandatory && this.m_DropDownList.SelectedValue == "-1")
{
// Property is mandatory, but no value selected in the DropDownList
this.m_CustomValidator.IsValid = false;
DocumentType documentType = new DocumentType(property.PropertyType.ContentTypeId);
ContentType.TabI tab = documentType.getVirtualTabs.Where(x => x.Id == property.PropertyType.TabId).FirstOrDefault();
if (tab != null)
{
this.m_CustomValidator.ErrorMessage = ui.Text("errorHandling", "errorMandatory", new string[] { property.PropertyType.Alias, tab.Caption }, User.GetCurrent());
}
}
this.m_Data.Value = this.m_DropDownList.SelectedValue;
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;
namespace umbraco.editorControls.XPathDropDownList
{
/// <summary>
///
/// </summary>
public class XPathDropDownListDataType : umbraco.cms.businesslogic.datatype.BaseDataType, IDataType
{
/// <summary>
///
/// </summary>
private XPathDropDownListPreValueEditor preValueEditor;
/// <summary>
///
/// </summary>
private IDataEditor dataEditor;
/// <summary>
///
/// </summary>
private IData data;
/// <summary>
/// Gets the name of the data type.
/// </summary>
/// <value>The name of the data type.</value>
public override string DataTypeName
{
get
{
return "XPath DropDownList";
}
}
/// <summary>
/// Gets the id.
/// </summary>
/// <value>The id.</value>
public override Guid Id
{
get
{
return new Guid(DataTypeGuids.XPathDropDownListId);
}
}
/// <summary>
/// Lazy load the associated PreValueEditor instance,
/// this is constructed supplying 'this'
/// </summary>
public override IDataPrevalue PrevalueEditor
{
get
{
if (this.preValueEditor == null)
{
this.preValueEditor = new XPathDropDownListPreValueEditor(this);
}
return this.preValueEditor;
}
}
/// <summary>
/// Lazy load the assocated DataEditor,
/// this is constructed supplying the data value stored by the PreValueEditor, and also the configuration settings of the PreValueEditor
/// </summary>
public override IDataEditor DataEditor
{
get
{
if (this.dataEditor == null)
{
this.dataEditor = new XPathDropDownListDataEditor(this.Data, ((XPathDropDownListPreValueEditor)this.PrevalueEditor).Options);
}
return this.dataEditor;
}
}
/// <summary>
/// Lazy load an empty DefaultData object, this is used to pass data between the PreValueEditor and the DataEditor
/// </summary>
public override IData Data
{
get
{
if (this.data == null)
{
this.data = new umbraco.cms.businesslogic.datatype.DefaultData(this);
}
return this.data;
}
}
}
}

View File

@@ -0,0 +1,25 @@
namespace umbraco.editorControls.XPathDropDownList
{
internal class XPathDropDownListOptions
{
/// <summary>
/// XPath string used to get Nodes to be used as CheckBox options in a CheckBoxList
/// </summary>
public string XPath { get; set; }
/// <summary>
/// Defaults to true, where property value is a csv of NodeIds, else if false, then csv of Node names is stored
/// </summary>
public bool UseId { get; set; }
/// <summary>
/// Initializes an instance of XPathDropDownListOptions
/// </summary>
public XPathDropDownListOptions()
{
this.XPath = string.Empty;
this.UseId = true; // Default to storing Node Id
}
}
}

View File

@@ -0,0 +1,162 @@
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.XPath;
// using uComponents.DataTypes.Shared.Extensions;
// using uComponents.DataTypes.Shared.PrevalueEditors;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.editorControls.XPathDropDownList
{
class XPathDropDownListPreValueEditor : AbstractJsonPrevalueEditor
{
/// <summary>
/// TextBox control to get the XPath expression
/// </summary>
private TextBox xPathTextBox = new TextBox();
/// <summary>
/// RequiredFieldValidator to ensure an XPath expression has been entered
/// </summary>
private RequiredFieldValidator xPathRequiredFieldValidator = new RequiredFieldValidator();
/// <summary>
/// Server side validation of XPath expression, to ensure some nodes are returned
/// </summary>
private CustomValidator xPathCustomValidator = new CustomValidator();
/// <summary>
/// Drop Down List to pick either Node Name or Node Id
/// </summary>
private DropDownList valueTypeDropDownList = new DropDownList();
/// <summary>
/// Data object used to define the configuration status of this PreValueEditor
/// </summary>
private XPathDropDownListOptions options = null;
/// <summary>
/// Gets the options data object that represents the current state of this datatypes configuration
/// </summary>
internal XPathDropDownListOptions Options
{
get
{
if (this.options == null)
{
// Deserialize any stored settings for this PreValueEditor instance
this.options = this.GetPreValueOptions<XPathDropDownListOptions>();
// If still null, ie, object couldn't be de-serialized from PreValue[0] string value
if (this.options == null)
{
// Create a new Options data object with the default values
this.options = new XPathDropDownListOptions();
}
}
return this.options;
}
}
/// <summary>
/// Initialize a new instance of XPathCheckBoxlistPreValueEditor
/// </summary>
/// <param name="dataType">XPathCheckBoxListDataType</param>
public XPathDropDownListPreValueEditor(umbraco.cms.businesslogic.datatype.BaseDataType dataType)
: base(dataType, umbraco.cms.businesslogic.datatype.DBTypes.Nvarchar)
{
}
/// <summary>
/// Creates all of the controls and assigns all of their properties
/// </summary>
protected override void CreateChildControls()
{
this.xPathTextBox.ID = "xPathTextBox";
this.xPathTextBox.CssClass = "umbEditorTextField";
this.xPathRequiredFieldValidator.ControlToValidate = this.xPathTextBox.ID;
this.xPathRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
this.xPathRequiredFieldValidator.ErrorMessage = " XPath expression required";
this.xPathCustomValidator.ControlToValidate = this.xPathTextBox.ID;
this.xPathCustomValidator.Display = ValidatorDisplay.Dynamic;
this.xPathCustomValidator.ServerValidate += new ServerValidateEventHandler(XPathCustomValidator_ServerValidate);
this.valueTypeDropDownList.ID = "valueTypeDropDownList";
this.valueTypeDropDownList.Items.Add(new ListItem("Node Id", bool.TrueString));
this.valueTypeDropDownList.Items.Add(new ListItem("Node Name", bool.FalseString));
this.Controls.Add(this.xPathTextBox);
this.Controls.Add(this.xPathRequiredFieldValidator);
this.Controls.Add(this.xPathCustomValidator);
this.Controls.Add(this.valueTypeDropDownList);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//if (!this.Page.IsPostBack)
//{
// Read in stored configuration values
this.xPathTextBox.Text = this.Options.XPath;
this.valueTypeDropDownList.SelectedValue = this.Options.UseId.ToString();
//}
}
/// <summary>
/// Will run the entered XPath expression to ensure it returns at least 1 node
/// </summary>
/// <param name="source">xPathCustomValidator</param>
/// <param name="args"></param>
private void XPathCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
string xPath = args.Value;
bool isValid = false;
try
{
if (uQuery.GetNodesByXPath(xPath).Count >= 0)
{
isValid = true;
}
}
catch (XPathException)
{
this.xPathCustomValidator.ErrorMessage = " Syntax error in XPath expression";
}
args.IsValid = isValid;
}
/// <summary>
/// Saves the pre value data to Umbraco
/// </summary>
public override void Save()
{
if (this.Page.IsValid)
{
this.Options.XPath = this.xPathTextBox.Text;
this.Options.UseId = bool.Parse(this.valueTypeDropDownList.SelectedValue);
this.SaveAsJson(this.Options); // Serialize to Umbraco database field
}
}
/// <summary>
/// Replaces the base class writer and instead uses the shared uComponents extension method, to inject consistant markup
/// </summary>
/// <param name="writer"></param>
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddPrevalueRow("XPath Expression", this.xPathTextBox, this.xPathRequiredFieldValidator, this.xPathCustomValidator);
writer.AddPrevalueRow("Value", this.valueTypeDropDownList);
}
}
}