uComponents: Added MultipleTextstring to the core

This commit is contained in:
leekelleher
2012-04-28 13:54:51 -01:00
parent c2d45adcb9
commit a0d9e668bd
4 changed files with 481 additions and 0 deletions

View File

@@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.cms.businesslogic.datatype;
[assembly: WebResource("umbraco.editorControls.MultipleTextstring.MultipleTextstring.css", "text/css")]
[assembly: WebResource("umbraco.editorControls.MultipleTextstring.MultipleTextstring.js", "application/x-javascript")]
namespace umbraco.editorControls.MultipleTextstring
{
/// <summary>
/// The MultipleTextstring control sets a character limit on a TextBox.
/// </summary>
[ValidationProperty("IsValid")]
public class MultipleTextstringControl : PlaceHolder
{
/// <summary>
/// Field for the list of values.
/// </summary>
private List<string> values;
/// <summary>
/// The HiddenField to store the selected values.
/// </summary>
private HiddenField SelectedValues = new HiddenField();
/// <summary>
/// Gets or sets the options.
/// </summary>
/// <value>The options.</value>
public MultipleTextstringOptions Options { get; set; }
/// <summary>
/// Gets the value of IsValid.
/// </summary>
/// <value>Returns 'Valid' if valid, otherwise an empty string.</value>
public string IsValid
{
get
{
if (!string.IsNullOrEmpty(this.Values))
{
return "Valid";
}
return string.Empty;
}
}
/// <summary>
/// Gets or sets the values.
/// </summary>
/// <value>The values.</value>
public string Values
{
get
{
return this.SelectedValues.Value;
}
set
{
this.SelectedValues.Value = value;
}
}
/// <summary>
/// Initialize the control, make sure children are created
/// </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();
}
/// <summary>
/// Add the resources (sytles/scripts)
/// </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);
// Adds the client dependencies.
this.AddResourceToClientDependency("umbraco.editorControls.MultipleTextstring.MultipleTextstring.css", ClientDependencyType.Css);
this.AddResourceToClientDependency("umbraco.editorControls.MultipleTextstring.MultipleTextstring.js", ClientDependencyType.Javascript);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// initalise the string array/list.
this.values = new List<string>();
// load the values into a string array/list.
if (!string.IsNullOrEmpty(this.Values))
{
this.values.AddRange(this.Values.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
}
// check the minimum number allowed, add extra fields.
if (this.values.Count < this.Options.Minimum && this.Options.Minimum > 1)
{
this.values.AddRange(new string(',', this.Options.Minimum - 1).Split(new[] { ',' }, StringSplitOptions.None));
}
// check the maxmimum number allowed, remove the excess.
if (this.values.Count > this.Options.Maximum && this.Options.Maximum > 0)
{
this.values.RemoveRange(this.Options.Maximum, this.values.Count - this.Options.Maximum);
}
// if there are no selected values...
if (this.values.Count == 0)
{
// ... then add an empty string to display a single textstring box.
this.values.Add(string.Empty);
}
}
/// <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()
{
base.CreateChildControls();
this.EnsureChildControls();
// populate the control's attributes.
this.SelectedValues.ID = this.SelectedValues.ClientID;
// add the controls.
this.Controls.Add(this.SelectedValues);
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "MultipleTextstring");
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
// loop through each value
foreach (string value in this.values)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
// input tag
writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row-field");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.WriteLine("<input type='text' class='umbEditorTextField' value='{0}' />", value.Replace("'", "&#39;"));
// append the add/remove buttons
writer.WriteLine(" <a href='#add' class='add_row' title='Add a new row'><img src='{0}/images/small_plus.png' /></a>", GlobalSettings.Path);
writer.WriteLine(" <a href='#remove' class='remove_row' title='Remove this row'><img src='{0}/images/small_minus.png' /></a>", GlobalSettings.Path);
writer.RenderEndTag(); // </div> .textstring-row-field
writer.WriteLine("<div class='textstring-row-sort' title='Re-order this row' style='background: url({0}/images/sort.png) no-repeat 0 2px;'></div>", GlobalSettings.Path);
writer.RenderEndTag(); // </div> .textstring-row
}
this.SelectedValues.RenderControl(writer);
writer.RenderEndTag(); // </div> .MultipleTextstring
// add jquery window load event
var javascriptMethod = string.Format("jQuery('#{0}').MultipleTextstring('#{1}', {2}, {3});", this.ClientID, this.SelectedValues.ClientID, this.Options.Minimum, this.Options.Maximum);
var javascript = string.Concat("<script type='text/javascript'>jQuery(window).load(function(){", javascriptMethod, "});</script>");
writer.WriteLine(javascript);
}
}
}

View File

@@ -0,0 +1,135 @@
using System;
using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;
namespace umbraco.editorControls.MultipleTextstring
{
/// <summary>
/// Data Editor for the Multiple Textstring data type.
/// </summary>
public class MultipleTextstringDataType : AbstractDataEditor
{
/// <summary>
/// The control for the Multiple Textstring data-editor.
/// </summary>
private MultipleTextstringControl m_Control = new MultipleTextstringControl();
/// <summary>
/// The Data object for the data-type.
/// </summary>
private IData m_Data;
/// <summary>
/// The PreValue Editor for the data-type.
/// </summary>
private IDataPrevalue m_PreValueEditor;
/// <summary>
/// Initializes a new instance of the <see cref="MultipleTextstringDataType"/> class.
/// </summary>
public MultipleTextstringDataType()
{
// set the render control as the placeholder
this.RenderControl = this.m_Control;
// assign the initialise event for the control
this.m_Control.Init += new EventHandler(this.m_Control_Init);
// assign the save event for the data-type/editor
this.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(this.DataEditorControl_OnSave);
}
/// <summary>
/// Gets the id of the data-type.
/// </summary>
/// <value>The id of the data-type.</value>
public override Guid Id
{
get
{
return new Guid(DataTypeGuids.MultipleTextstringId);
}
}
/// <summary>
/// Gets the name of the data type.
/// </summary>
/// <value>The name of the data type.</value>
public override string DataTypeName
{
get
{
return "Multiple Textstring";
}
}
/// <summary>
/// Gets the data for the data-type.
/// </summary>
/// <value>The data for the data-type.</value>
public override IData Data
{
get
{
if (this.m_Data == null)
{
this.m_Data = new CsvToXmlData(this, "values", "value", new[] { Environment.NewLine });
}
return this.m_Data;
}
}
/// <summary>
/// Gets the prevalue editor.
/// </summary>
/// <value>The prevalue editor.</value>
public override IDataPrevalue PrevalueEditor
{
get
{
if (this.m_PreValueEditor == null)
{
this.m_PreValueEditor = new MultipleTextstringPrevalueEditor(this);
}
return this.m_PreValueEditor;
}
}
/// <summary>
/// Handles the Init event of the control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void m_Control_Init(object sender, EventArgs e)
{
var options = ((MultipleTextstringPrevalueEditor)this.PrevalueEditor).GetPreValueOptions<MultipleTextstringOptions>();
if (options == null)
{
// load defaults
options = new MultipleTextstringOptions(true);
}
// check if the data value is available...
if (this.Data.Value != null)
{
// set the value of the control
this.m_Control.Values = this.Data.Value.ToString();
}
// set the controls options
this.m_Control.Options = options;
}
/// <summary>
/// Saves the data for the editor control.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void DataEditorControl_OnSave(EventArgs e)
{
this.Data.Value = this.m_Control.Values;
}
}
}

View File

@@ -0,0 +1,41 @@
using System.ComponentModel;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.editorControls.MultipleTextstring
{
/// <summary>
/// The options for the Multiple Textstring data-type.
/// </summary>
public class MultipleTextstringOptions : AbstractOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="MultipleTextstringOptions"/> class.
/// </summary>
public MultipleTextstringOptions()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MultipleTextstringOptions"/> class.
/// </summary>
/// <param name="loadDefaults">if set to <c>true</c> [load defaults].</param>
public MultipleTextstringOptions(bool loadDefaults)
: base(loadDefaults)
{
}
/// <summary>
/// Gets or sets the maximum.
/// </summary>
/// <value>The maximum.</value>
[DefaultValue(-1)]
public int Maximum { get; set; }
/// <summary>
/// Gets or sets the minimum.
/// </summary>
/// <value>The minimum.</value>
[DefaultValue(1)]
public int Minimum { get; set; }
}
}

View File

@@ -0,0 +1,118 @@
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
// using uComponents.DataTypes.Shared.Extensions;
// using uComponents.DataTypes.Shared.PrevalueEditors;
using umbraco.cms.businesslogic.datatype;
namespace umbraco.editorControls.MultipleTextstring
{
/// <summary>
/// The PreValue Editor for the Multiple Textstring data-type.
/// </summary>
public class MultipleTextstringPrevalueEditor : AbstractJsonPrevalueEditor
{
/// <summary>
/// The TextBox control for the maximum value of the control.
/// </summary>
private TextBox TextBoxMaximum;
/// <summary>
/// The TextBox control for the minimum value of the control.
/// </summary>
private TextBox TextBoxMinimum;
/// <summary>
/// Initializes a new instance of the <see cref="MultipleTextstringPrevalueEditor"/> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
public MultipleTextstringPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType dataType)
: base(dataType, umbraco.cms.businesslogic.datatype.DBTypes.Ntext)
{
}
/// <summary>
/// Saves this instance.
/// </summary>
public override void Save()
{
// set the options
var options = new MultipleTextstringOptions(true);
// parse the maximum
int maximum;
if (int.TryParse(this.TextBoxMaximum.Text, out maximum))
{
if (maximum == 0)
{
maximum = -1;
}
options.Maximum = maximum;
}
// parse the minimum
int minimum;
if (int.TryParse(this.TextBoxMinimum.Text, out minimum))
{
if (minimum == 0)
{
minimum = -1;
}
options.Minimum = minimum;
}
// save the options as JSON
this.SaveAsJson(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()
{
base.CreateChildControls();
// set-up child controls
this.TextBoxMaximum = new TextBox() { ID = "Maximum", CssClass = "guiInputText" };
this.TextBoxMinimum = new TextBox() { ID = "Minimum", CssClass = "guiInputText" };
// add the child controls
this.Controls.AddPrevalueControls(this.TextBoxMaximum, this.TextBoxMinimum);
}
/// <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);
// get PreValues, load them into the controls.
var options = this.GetPreValueOptions<MultipleTextstringOptions>();
// no options? use the default ones.
if (options == null)
{
options = new MultipleTextstringOptions(true);
}
// set the values
this.TextBoxMaximum.Text = options.Maximum.ToString();
this.TextBoxMinimum.Text = options.Minimum.ToString();
}
/// <summary>
/// Renders the contents 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>
protected override void RenderContents(HtmlTextWriter writer)
{
// add property fields
writer.AddPrevalueRow("Minimum:", "Minimum number of rows to display.", this.TextBoxMinimum);
writer.AddPrevalueRow("Maximum:", "Maximum number of rows to display.", this.TextBoxMaximum);
}
}
}