Creating the internal GetBaseCropUrl method and moving the logic from the Model GetCropUrl method to ImageCropperBaseExtensions so that it can be used by both the Model method and the template extensions

Also added some new tests
This commit is contained in:
Jeavon Leopold
2014-04-21 08:38:53 +01:00
parent 1cf20597a4
commit 7787c51156
4 changed files with 63 additions and 40 deletions

View File

@@ -41,5 +41,26 @@ namespace Umbraco.Tests.PropertyEditors
Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
}
/// <summary>
/// Test that if a crop alias has been specified that doesn't exist the method returns null
/// </summary>
[Test]
public void GetCropUrlNullTest()
{
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "Banner", useCropDimensions: true);
Assert.AreEqual(null, urlString);
}
/// <summary>
/// Test the GetCropUrl method on the ImageCropDataSet Model
/// </summary>
[Test]
public void GetBaseCropUrlFromModelTest()
{
var cropDataSet = cropperJson.SerializeToCropDataSet();
var urlString = cropDataSet.GetCropUrl("thumb");
Assert.AreEqual("?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString);
}
}
}

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Globalization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -70,5 +71,38 @@ namespace Umbraco.Web
return dataset.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
}
internal static string GetCropBaseUrl(this ImageCropDataSet cropDataSet, string cropAlias, bool preferFocalPoint)
{
var cropUrl = new StringBuilder();
var crop = cropDataSet.GetCrop(cropAlias);
// if crop alias has been specified but not found in the Json we should return null
if (string.IsNullOrEmpty(cropAlias) == false && crop == null)
{
return null;
}
if ((preferFocalPoint && cropDataSet.HasFocalPoint()) || (crop != null && crop.Coordinates == null && cropDataSet.HasFocalPoint()) || (string.IsNullOrEmpty(cropAlias) && cropDataSet.HasFocalPoint()))
{
cropUrl.Append("?center=" + cropDataSet.FocalPoint.Top.ToString(CultureInfo.InvariantCulture) + "," + cropDataSet.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
cropUrl.Append("&mode=crop");
}
else if (crop != null && crop.Coordinates != null)
{
cropUrl.Append("?crop=");
cropUrl.Append(crop.Coordinates.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
cropUrl.Append(crop.Coordinates.Y2.ToString(CultureInfo.InvariantCulture));
cropUrl.Append("&cropmode=percentage");
}
else
{
cropUrl.Append("?anchor=center");
cropUrl.Append("&mode=crop");
}
return cropUrl.ToString();
}
}
}

View File

@@ -214,32 +214,16 @@ namespace Umbraco.Web
{
var crop = cropDataSet.GetCrop(cropAlias);
// if crop alias has been specified but not found in the Json we should return null
if (string.IsNullOrEmpty(cropAlias) == false && crop == null)
{
return null;
}
imageResizerUrl.Append(cropDataSet.Src);
if ((preferFocalPoint && cropDataSet.HasFocalPoint()) || (crop != null && crop.Coordinates == null && cropDataSet.HasFocalPoint()) || (string.IsNullOrEmpty(cropAlias) && cropDataSet.HasFocalPoint()))
var cropBaseUrl = cropDataSet.GetCropBaseUrl(cropAlias, preferFocalPoint);
if (cropBaseUrl != null)
{
imageResizerUrl.Append("?center=" + cropDataSet.FocalPoint.Top.ToString(CultureInfo.InvariantCulture) + "," + cropDataSet.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
imageResizerUrl.Append("&mode=crop");
}
else if (crop != null && crop.Coordinates != null)
{
imageResizerUrl.Append("?crop=");
imageResizerUrl.Append(crop.Coordinates.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageResizerUrl.Append(crop.Coordinates.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
imageResizerUrl.Append(crop.Coordinates.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
imageResizerUrl.Append(crop.Coordinates.Y2.ToString(CultureInfo.InvariantCulture));
imageResizerUrl.Append("&cropmode=percentage");
imageResizerUrl.Append(cropBaseUrl);
}
else
{
imageResizerUrl.Append("?anchor=center");
imageResizerUrl.Append("&mode=crop");
return null;
}
if (crop!= null & useCropDimensions)
@@ -247,12 +231,10 @@ namespace Umbraco.Web
width = crop.Width;
height = crop.Height;
}
}
}
else
{
imageResizerUrl.Append(imageUrl);
if (imageCropMode == null)

View File

@@ -32,24 +32,10 @@ namespace Umbraco.Web.Models
var sb = new StringBuilder();
if ((useFocalPoint && HasFocalPoint()) || (crop != null && crop.Coordinates == null && HasFocalPoint()) || (string.IsNullOrEmpty(alias) && HasFocalPoint()))
var cropBaseUrl = this.GetCropBaseUrl(alias, useFocalPoint);
if (cropBaseUrl != null)
{
sb.Append("?center=" + FocalPoint.Top.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + FocalPoint.Left.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append("&mode=crop");
}
else if (crop != null && crop.Coordinates != null)
{
sb.Append("?crop=");
sb.Append(crop.Coordinates.X1.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
sb.Append(crop.Coordinates.Y1.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
sb.Append(crop.Coordinates.X2.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(",");
sb.Append(crop.Coordinates.Y2.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append("&cropmode=percentage");
}
else
{
sb.Append("?anchor=center");
sb.Append("&mode=crop");
sb.Append(cropBaseUrl);
}
if (crop != null && useCropDimensions)