Partial revert of #17854 (#18040)

This commit is contained in:
Kenn Jacobsen
2025-01-21 08:48:57 +01:00
committed by GitHub
parent c00e86e0ea
commit 2a73cfefbb
7 changed files with 30 additions and 137 deletions

View File

@@ -1,5 +1,3 @@
using System.Text.Json.Serialization;
namespace Umbraco.Cms.Core.PropertyEditors;
/// <summary>
@@ -15,10 +13,4 @@ public class SliderConfiguration
[ConfigurationField("maxVal")]
public decimal MaximumValue { get; set; }
[ConfigurationField("initVal1")]
public decimal InitialValue1 { get; set; }
[ConfigurationField("initVal2")]
public decimal InitialValue2 { get; set; }
}

View File

@@ -1,12 +0,0 @@
using System.Text.Json.Serialization;
namespace Umbraco.Cms.Core.PropertyEditors;
/// <summary>
/// Represents the configuration for the true/false (toggle) value editor.
/// </summary>
public class TrueFalseConfiguration
{
[ConfigurationField("default")]
public bool InitialState { get; set; }
}

View File

@@ -1,17 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.IO;
namespace Umbraco.Cms.Core.PropertyEditors;
/// <summary>
/// Represents the configuration editor for the true/false (toggle) value editor.
/// </summary>
public class TrueFalseConfigurationEditor : ConfigurationEditor<TrueFalseConfiguration>
{
public TrueFalseConfigurationEditor(IIOHelper ioHelper)
: base(ioHelper)
{
}
}

View File

@@ -49,29 +49,16 @@ public class SliderValueConverter : PropertyValueConverterBase
/// <inheritdoc />
public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object? source, bool preview)
{
SliderConfiguration? configuration = propertyType.DataType.ConfigurationAs<SliderConfiguration>();
bool isRange = IsRange(configuration);
bool isRange = IsRange(propertyType);
var sourceString = source?.ToString();
// If source is null, the returned value depends on the configured initial values.
if (string.IsNullOrEmpty(sourceString))
{
return isRange
? new Range<decimal>
{
Minimum = configuration?.InitialValue1 ?? 0M,
Maximum = configuration?.InitialValue2 ?? 0M
}
: configuration?.InitialValue1 ?? 0M;
}
return isRange
? HandleRange(sourceString)
: HandleDecimal(sourceString);
}
private static Range<decimal> HandleRange(string sourceString)
private static Range<decimal> HandleRange(string? sourceString)
{
if (sourceString is null)
{
@@ -105,8 +92,13 @@ public class SliderValueConverter : PropertyValueConverterBase
return new Range<decimal>();
}
private static decimal HandleDecimal(string sourceString)
private static decimal HandleDecimal(string? sourceString)
{
if (string.IsNullOrEmpty(sourceString))
{
return default;
}
// This used to be a range slider, so we'll assign the minimum value as the new value
if (sourceString.Contains(','))
{
@@ -131,9 +123,7 @@ public class SliderValueConverter : PropertyValueConverterBase
private static bool TryParseDecimal(string? representation, out decimal value)
=> decimal.TryParse(representation, NumberStyles.Number, CultureInfo.InvariantCulture, out value);
private static bool IsRange(IPublishedPropertyType propertyType)
=> IsRange(propertyType.DataType.ConfigurationAs<SliderConfiguration>());
private static bool IsRange(SliderConfiguration? configuration)
=> configuration?.EnableRange == true;
private static bool IsRange(IPublishedPropertyType propertyType)
=> propertyType.DataType.ConfigurationAs<SliderConfiguration>()?.EnableRange == true;
}

View File

@@ -18,7 +18,7 @@ public class YesNoValueConverter : PropertyValueConverterBase
{
if (source is null)
{
return null;
return false;
}
// in xml a boolean is: string
@@ -59,17 +59,4 @@ public class YesNoValueConverter : PropertyValueConverterBase
// false for any other value
return false;
}
/// <inheritdoc />
public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object? source, bool preview)
{
// If source is null, whether we return true or false depends on the configured default value (initial state).
if (source is null)
{
TrueFalseConfiguration? configuration = propertyType.DataType.ConfigurationAs<TrueFalseConfiguration>();
return configuration?.InitialState ?? false;
}
return (bool)source;
}
}

View File

@@ -1,8 +1,6 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
@@ -20,37 +18,17 @@ namespace Umbraco.Cms.Core.PropertyEditors;
ValueEditorIsReusable = true)]
public class TrueFalsePropertyEditor : DataEditor
{
private readonly IIOHelper _ioHelper;
/// <summary>
/// Initializes a new instance of the <see cref="TrueFalsePropertyEditor" /> class.
/// </summary>
[Obsolete("Please use the constructor taking all parameters. This constructor will be removed in V17.")]
public TrueFalsePropertyEditor(IDataValueEditorFactory dataValueEditorFactory)
: this(
dataValueEditorFactory,
StaticServiceProvider.Instance.GetRequiredService<IIOHelper>())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TrueFalsePropertyEditor" /> class.
/// </summary>
public TrueFalsePropertyEditor(IDataValueEditorFactory dataValueEditorFactory, IIOHelper ioHelper)
: base(dataValueEditorFactory)
{
_ioHelper = ioHelper;
SupportsReadOnly = true;
}
=> SupportsReadOnly = true;
/// <inheritdoc />
protected override IDataValueEditor CreateValueEditor()
=> DataValueEditorFactory.Create<TrueFalsePropertyValueEditor>(Attribute!);
/// <inheritdoc />
protected override IConfigurationEditor CreateConfigurationEditor() =>
new TrueFalseConfigurationEditor(_ioHelper);
internal class TrueFalsePropertyValueEditor : DataValueEditor
{
public TrueFalsePropertyValueEditor(

View File

@@ -9,7 +9,6 @@ using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Serialization;
using Umbraco.Cms.Tests.Common.Builders;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors;
@@ -47,54 +46,30 @@ public class PropertyEditorValueConverterTests
}
}
[TestCase("TRUE", null, true)]
[TestCase("True", null, true)]
[TestCase("true", null, true)]
[TestCase("1", null, true)]
[TestCase(1, null, true)]
[TestCase(true, null, true)]
[TestCase("FALSE", null, false)]
[TestCase("False", null, false)]
[TestCase("false", null, false)]
[TestCase("0", null, false)]
[TestCase(0, null, false)]
[TestCase(false, null, false)]
[TestCase("", null, false)]
[TestCase("blah", null, false)]
[TestCase(null, false, false)]
[TestCase(null, true, true)]
public void CanConvertTrueFalsePropertyEditor(object value, bool initialStateConfigurationValue, bool expected)
[TestCase("TRUE", true)]
[TestCase("True", true)]
[TestCase("true", true)]
[TestCase("1", true)]
[TestCase(1, true)]
[TestCase(true, true)]
[TestCase("FALSE", false)]
[TestCase("False", false)]
[TestCase("false", false)]
[TestCase("0", false)]
[TestCase(0, false)]
[TestCase(false, false)]
[TestCase("", false)]
[TestCase(null, false)]
[TestCase("blah", false)]
public void CanConvertYesNoPropertyEditor(object value, bool expected)
{
var publishedDataType = CreatePublishedDataType(initialStateConfigurationValue);
var publishedPropertyTypeMock = new Mock<IPublishedPropertyType>();
publishedPropertyTypeMock
.SetupGet(p => p.DataType)
.Returns(publishedDataType);
var converter = new YesNoValueConverter();
var intermediateResult = converter.ConvertSourceToIntermediate(null, publishedPropertyTypeMock.Object, value, false);
var result = converter.ConvertIntermediateToObject(null, publishedPropertyTypeMock.Object, PropertyCacheLevel.Element, intermediateResult, false);
var result =
converter.ConvertSourceToIntermediate(null, null, value, false); // does not use type for conversion
Assert.AreEqual(expected, result);
}
private static PublishedDataType CreatePublishedDataType(bool initialStateConfigurationValue)
{
var dataTypeConfiguration = new TrueFalseConfiguration
{
InitialState = initialStateConfigurationValue
};
var dateTypeMock = new Mock<IDataType>();
dateTypeMock.SetupGet(x => x.Id).Returns(1000);
dateTypeMock.SetupGet(x => x.EditorAlias).Returns(global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Boolean);
dateTypeMock.SetupGet(x => x.EditorUiAlias).Returns("Umb.PropertyEditorUi.Toggle");
dateTypeMock.SetupGet(x => x.ConfigurationObject).Returns(dataTypeConfiguration);
return new PublishedDataType(dateTypeMock.Object.Id, dateTypeMock.Object.EditorAlias, dateTypeMock.Object.EditorUiAlias, new Lazy<object>(() => dataTypeConfiguration));
}
[TestCase("[\"apples\"]", new[] { "apples" })]
[TestCase("[\"apples\",\"oranges\"]", new[] { "apples", "oranges" })]
[TestCase("[\"apples\",\"oranges\",\"pears\"]", new[] { "apples", "oranges", "pears" })]