ImageCropper property alias now set via a drop down of all available aliases, and a safety check on the dataeditor to ensure the selected property was found

This commit is contained in:
Hendy
2012-10-25 10:14:24 -01:00
parent 05db49c66d
commit da3d20a5bb
2 changed files with 130 additions and 116 deletions

View File

@@ -1,14 +1,13 @@
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.media;
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.web;
using System.Xml;
using System.Text;
using umbraco.editorControls.imagecropper;
using umbraco.cms.businesslogic.media;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.member;
namespace umbraco.editorControls.imagecropper
{
@@ -36,7 +35,6 @@ namespace umbraco.editorControls.imagecropper
public Control Editor { get { return this; } }
protected override void OnInit(EventArgs e)
//protected override void OnLoad(EventArgs e)
{
this.ID = "ImageCropper";
//base.OnInit(e);
@@ -50,133 +48,133 @@ namespace umbraco.editorControls.imagecropper
CMSNode node = new CMSNode(currentDocumentId);
if (node.nodeObjectType == Document._objectType)
{
uploadProperty =
new Document(currentDocumentId).getProperty(config.UploadPropertyAlias);
uploadProperty = new Document(currentDocumentId).getProperty(config.UploadPropertyAlias);
}
else if (node.nodeObjectType == umbraco.cms.businesslogic.media.Media._objectType)
{
uploadProperty =
new Media(currentDocumentId).getProperty(config.UploadPropertyAlias);
uploadProperty = new Media(currentDocumentId).getProperty(config.UploadPropertyAlias);
}
else if (node.nodeObjectType == Member._objectType)
{
uploadProperty =
new Member(currentDocumentId).getProperty(config.UploadPropertyAlias);
uploadProperty = new Member(currentDocumentId).getProperty(config.UploadPropertyAlias);
}
else
{
throw new Exception("Unsupported Umbraco Node type for Image Cropper (only Document, Media and Members are supported.");
}
string relativeImagePath = uploadProperty.Value.ToString();
ImageInfo imageInfo = new ImageInfo(relativeImagePath);
imgImage.ImageUrl = relativeImagePath;
imgImage.ID = String.Format("cropBox_{0}", propertyId);
StringBuilder sbJson = new StringBuilder();
StringBuilder sbRaw = new StringBuilder();
try
// upload property could be null here if the property wasn't found
if (uploadProperty != null)
{
_xml = new XmlDocument();
_xml.LoadXml(data.Value.ToString());
}
catch
{
_xml = createBaseXmlDocument();
}
string relativeImagePath = uploadProperty.Value.ToString();
sbJson.Append("{ \"current\": 0, \"crops\": [");
ImageInfo imageInfo = new ImageInfo(relativeImagePath);
for (int i = 0; i < config.presets.Count; i++)
{
Preset preset = (Preset)config.presets[i];
Crop crop;
imgImage.ImageUrl = relativeImagePath;
imgImage.ID = String.Format("cropBox_{0}", propertyId);
sbJson.Append("{\"name\":'" + preset.Name + "'");
StringBuilder sbJson = new StringBuilder();
StringBuilder sbRaw = new StringBuilder();
sbJson.Append(",\"config\":{" +
String.Format("\"targetWidth\":{0},\"targetHeight\":{1},\"keepAspect\":{2}",
preset.TargetWidth, preset.TargetHeight,
(preset.KeepAspect ? "true" : "false") + "}"));
if (imageInfo.Exists)
try
{
crop = preset.Fit(imageInfo);
_xml = new XmlDocument();
_xml.LoadXml(data.Value.ToString());
}
else
catch
{
crop.X = 0;
crop.Y = 0;
crop.X2 = preset.TargetWidth;
crop.Y2 = preset.TargetHeight;
_xml = createBaseXmlDocument();
}
// stored
if (_xml.DocumentElement != null && _xml.DocumentElement.ChildNodes.Count == config.presets.Count)
sbJson.Append("{ \"current\": 0, \"crops\": [");
for (int i = 0; i < config.presets.Count; i++)
{
XmlNode xmlNode = _xml.DocumentElement.ChildNodes[i];
Preset preset = (Preset)config.presets[i];
Crop crop;
int xml_x = Convert.ToInt32(xmlNode.Attributes["x"].Value);
int xml_y = Convert.ToInt32(xmlNode.Attributes["y"].Value);
int xml_x2 = Convert.ToInt32(xmlNode.Attributes["x2"].Value);
int xml_y2 = Convert.ToInt32(xmlNode.Attributes["y2"].Value);
sbJson.Append("{\"name\":'" + preset.Name + "'");
// only use xml values if image is the same and different from defaults (document is stored inbetween image upload and cropping)
//if (xml_x2 - xml_x != preset.TargetWidth || xml_y2 - xml_y != preset.TargetHeight)
//fileDate == imageInfo.DateStamp && (
sbJson.Append(",\"config\":{" +
String.Format("\"targetWidth\":{0},\"targetHeight\":{1},\"keepAspect\":{2}",
preset.TargetWidth, preset.TargetHeight,
(preset.KeepAspect ? "true" : "false") + "}"));
if (crop.X != xml_x || crop.X2 != xml_x2 || crop.Y != xml_y || crop.Y2 != xml_y2)
if (imageInfo.Exists)
{
crop.X = xml_x;
crop.Y = xml_y;
crop.X2 = xml_x2;
crop.Y2 = xml_y2;
crop = preset.Fit(imageInfo);
}
else
{
crop.X = 0;
crop.Y = 0;
crop.X2 = preset.TargetWidth;
crop.Y2 = preset.TargetHeight;
}
// stored
if (_xml.DocumentElement != null && _xml.DocumentElement.ChildNodes.Count == config.presets.Count)
{
XmlNode xmlNode = _xml.DocumentElement.ChildNodes[i];
int xml_x = Convert.ToInt32(xmlNode.Attributes["x"].Value);
int xml_y = Convert.ToInt32(xmlNode.Attributes["y"].Value);
int xml_x2 = Convert.ToInt32(xmlNode.Attributes["x2"].Value);
int xml_y2 = Convert.ToInt32(xmlNode.Attributes["y2"].Value);
// only use xml values if image is the same and different from defaults (document is stored inbetween image upload and cropping)
//if (xml_x2 - xml_x != preset.TargetWidth || xml_y2 - xml_y != preset.TargetHeight)
//fileDate == imageInfo.DateStamp && (
if (crop.X != xml_x || crop.X2 != xml_x2 || crop.Y != xml_y || crop.Y2 != xml_y2)
{
crop.X = xml_x;
crop.Y = xml_y;
crop.X2 = xml_x2;
crop.Y2 = xml_y2;
}
}
sbJson.Append(",\"value\":{" + String.Format("\"x\":{0},\"y\":{1},\"x2\":{2},\"y2\":{3}", crop.X, crop.Y, crop.X2, crop.Y2) + "}}");
sbRaw.Append(String.Format("{0},{1},{2},{3}", crop.X, crop.Y, crop.X2, crop.Y2));
if (i < config.presets.Count - 1)
{
sbJson.Append(",");
sbRaw.Append(";");
}
}
sbJson.Append(",\"value\":{" + String.Format("\"x\":{0},\"y\":{1},\"x2\":{2},\"y2\":{3}", crop.X, crop.Y, crop.X2, crop.Y2) + "}}");
sbRaw.Append(String.Format("{0},{1},{2},{3}", crop.X, crop.Y, crop.X2, crop.Y2));
sbJson.Append("]}");
hdnJson.Value = sbJson.ToString();
//hdnJson.ID = String.Format("json_{0}", propertyId);
hdnRaw.Value = sbRaw.ToString();
//hdnRaw.ID = String.Format("raw_{0}", propertyId);
Controls.Add(imgImage);
Controls.Add(hdnJson);
Controls.Add(hdnRaw);
string imageCropperInitScript =
"initImageCropper('" +
imgImage.ClientID + "', '" +
hdnJson.ClientID + "', '" +
hdnRaw.ClientID +
"');";
Page.ClientScript.RegisterStartupScript(GetType(), ClientID + "_imageCropper", imageCropperInitScript, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.json2Script.GetType(), "json2Script", Resources.json2Script, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.jCropCSS.GetType(), "jCropCSS", Resources.jCropCSS);
Page.ClientScript.RegisterClientScriptBlock(Resources.jCropScript.GetType(), "jCropScript", Resources.jCropScript, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.imageCropperScript.GetType(), "imageCropperScript", Resources.imageCropperScript, true);
if (i < config.presets.Count - 1)
{
sbJson.Append(",");
sbRaw.Append(";");
}
}
sbJson.Append("]}");
hdnJson.Value = sbJson.ToString();
//hdnJson.ID = String.Format("json_{0}", propertyId);
hdnRaw.Value = sbRaw.ToString();
//hdnRaw.ID = String.Format("raw_{0}", propertyId);
Controls.Add(imgImage);
Controls.Add(hdnJson);
Controls.Add(hdnRaw);
string imageCropperInitScript =
"initImageCropper('" +
imgImage.ClientID + "', '" +
hdnJson.ClientID + "', '" +
hdnRaw.ClientID +
"');";
Page.ClientScript.RegisterStartupScript(GetType(), ClientID + "_imageCropper", imageCropperInitScript, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.json2Script.GetType(), "json2Script", Resources.json2Script, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.jCropCSS.GetType(), "jCropCSS", Resources.jCropCSS);
Page.ClientScript.RegisterClientScriptBlock(Resources.jCropScript.GetType(), "jCropScript", Resources.jCropScript, true);
Page.ClientScript.RegisterClientScriptBlock(Resources.imageCropperScript.GetType(), "imageCropperScript", Resources.imageCropperScript, true);
base.OnInit(e);
}
/// <summary>
/// Store data as string XML (overridden by ToXMl to store "real" XML
/// XML format:
@@ -206,6 +204,5 @@ namespace umbraco.editorControls.imagecropper
doc.AppendChild(root);
return doc;
}
}
}

View File

@@ -5,6 +5,7 @@ using umbraco.BusinessLogic;
using umbraco.editorControls;
using umbraco.DataLayer;
using umbraco.interfaces;
using umbraco.macroRenderings;
namespace umbraco.editorControls.imagecropper
{
@@ -12,7 +13,9 @@ namespace umbraco.editorControls.imagecropper
{
private readonly umbraco.cms.businesslogic.datatype.BaseDataType _dataType;
private TextBox txtPropertyAlias;
private propertyTypePicker imagePropertyTypePicker; // this has replaced txtPropertyAlias (a textbox used to enter a property alias)
private RequiredFieldValidator imagePropertyRequiredFieldValidator;
private CheckBox chkGenerateCrops;
private CheckBox chkShowLabel;
private Literal litQuality;
@@ -44,7 +47,15 @@ namespace umbraco.editorControls.imagecropper
public void SetupChildControls()
{
txtPropertyAlias = new TextBox {ID = "upload", Width = Unit.Pixel(100)};
this.imagePropertyTypePicker = new propertyTypePicker() { ID = "imagePropertyTypePicker" };
this.imagePropertyRequiredFieldValidator = new RequiredFieldValidator()
{
ID = "imagePropertyRequiredFieldValidator",
Text = " Required",
InitialValue = string.Empty,
ControlToValidate = this.imagePropertyTypePicker.ID
};
chkGenerateCrops = new CheckBox {ID = "generateimg", AutoPostBack = true};
litQuality = new Literal {ID = "qualityLiteral", Text = " Quality ", Visible = false};
txtQuality = new TextBox {ID = "quality", Width = Unit.Pixel(30), Visible = false};
@@ -90,7 +101,9 @@ namespace umbraco.editorControls.imagecropper
// AssociatedControlID = txtCropName.ID
// };
Controls.Add(txtPropertyAlias);
Controls.Add(this.imagePropertyTypePicker);
Controls.Add(this.imagePropertyRequiredFieldValidator);
Controls.Add(chkGenerateCrops);
Controls.Add(litQuality);
Controls.Add(txtQuality);
@@ -121,9 +134,7 @@ namespace umbraco.editorControls.imagecropper
//btnGenerate.Click += _generateButton_Click;
chkGenerateCrops.CheckedChanged += _generateImagesCheckBox_CheckedChanged;
chkGenerateCrops.CheckedChanged += _generateImagesCheckBox_CheckedChanged;
}
#if false
@@ -254,7 +265,11 @@ namespace umbraco.editorControls.imagecropper
{
Config config = new Config(Configuration);
txtPropertyAlias.Text = config.UploadPropertyAlias;
if (this.imagePropertyTypePicker.Items.Contains(new ListItem(config.UploadPropertyAlias)))
{
this.imagePropertyTypePicker.SelectedValue = config.UploadPropertyAlias;
}
chkGenerateCrops.Checked = config.GenerateImages;
chkShowLabel.Checked = config.ShowLabel;
txtQuality.Visible = chkShowLabel.Checked;
@@ -297,10 +312,10 @@ namespace umbraco.editorControls.imagecropper
/// </summary>
public void Save()
{
_dataType.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(
typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);
_dataType.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);
string generalData = String.Format("{0},{1},{2},{3}",
txtPropertyAlias.Text,
this.imagePropertyTypePicker.SelectedValue,
chkGenerateCrops.Checked ? "1" : "0",
chkShowLabel.Checked ? "1" : "0",
txtQuality.Text
@@ -328,10 +343,14 @@ namespace umbraco.editorControls.imagecropper
writer.Write("<p><strong>General</strong></p>");
writer.Write("<table>");
writer.Write(" <tr><td>Property alias: (eg. 'umbracoFile'):</td><td>");
txtPropertyAlias.RenderControl(writer);
writer.Write(" </td></tr>");
writer.Write(@" <tr>
<td>Property alias: </td>");
writer.Write(" <td>");
this.imagePropertyTypePicker.RenderControl(writer);
this.imagePropertyRequiredFieldValidator.RenderControl(writer);
writer.Write(@" </td>
</tr>");
writer.Write(" <tr><td>Save crop images (/media/(imageid)/(filename)_(cropname).jpg):</td><td>");
chkGenerateCrops.RenderControl(writer);
litQuality.RenderControl(writer);
@@ -383,9 +402,7 @@ namespace umbraco.editorControls.imagecropper
//_generateButton.RenderControl(writer);
//_vsErrors.RenderControl(writer);
//_revName.RenderControl(writer);
//_revName.RenderControl(writer);
}
public string Configuration