2018-01-24 11:44:44 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2020-11-17 20:27:10 +01:00
|
|
|
|
using System.Runtime.Serialization;
|
2018-03-05 14:59:23 +01:00
|
|
|
|
using Umbraco.Core.Serialization;
|
2018-01-24 11:44:44 +01:00
|
|
|
|
|
2020-11-19 15:09:25 +01:00
|
|
|
|
namespace Umbraco.Core.PropertyEditors
|
2018-01-24 11:44:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a data type configuration editor.
|
|
|
|
|
|
/// </summary>
|
2018-02-15 14:49:32 +01:00
|
|
|
|
public class ConfigurationEditor : IConfigurationEditor
|
2018-01-24 11:44:44 +01:00
|
|
|
|
{
|
2019-04-16 11:23:22 +02:00
|
|
|
|
private IDictionary<string, object> _defaultConfiguration;
|
|
|
|
|
|
|
2018-01-24 11:44:44 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="ConfigurationEditor"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ConfigurationEditor()
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields = new List<ConfigurationField>();
|
2019-04-16 11:23:22 +02:00
|
|
|
|
_defaultConfiguration = new Dictionary<string, object>();
|
2018-01-24 11:44:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="ConfigurationEditor"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected ConfigurationEditor(List<ConfigurationField> fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields = fields;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the fields.
|
|
|
|
|
|
/// </summary>
|
2020-11-17 20:27:10 +01:00
|
|
|
|
[DataMember(Name = "fields")]
|
2018-01-24 11:44:44 +01:00
|
|
|
|
public List<ConfigurationField> Fields { get; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// Gets a field by its property name.
|
2018-01-24 11:44:44 +01:00
|
|
|
|
/// </summary>
|
2018-02-05 17:48:54 +01:00
|
|
|
|
/// <remarks>Can be used in constructors to add infos to a field that has been defined
|
|
|
|
|
|
/// by a property marked with the <see cref="ConfigurationFieldAttribute"/>.</remarks>
|
|
|
|
|
|
protected ConfigurationField Field(string name)
|
|
|
|
|
|
=> Fields.First(x => x.PropertyName == name);
|
2018-01-24 11:44:44 +01:00
|
|
|
|
|
2018-01-29 19:23:25 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the configuration as a typed object.
|
|
|
|
|
|
/// </summary>
|
2018-01-24 11:44:44 +01:00
|
|
|
|
public static TConfiguration ConfigurationAs<TConfiguration>(object obj)
|
|
|
|
|
|
{
|
2018-01-29 19:23:25 +01:00
|
|
|
|
if (obj == null) return default;
|
2018-01-24 11:44:44 +01:00
|
|
|
|
if (obj is TConfiguration configuration) return configuration;
|
2020-11-17 20:27:10 +01:00
|
|
|
|
throw new InvalidCastException(
|
|
|
|
|
|
$"Cannot cast configuration of type {obj.GetType().Name} to {typeof(TConfiguration).Name}.");
|
2018-01-24 11:44:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-15 08:13:40 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a configuration object into a serialized database value.
|
|
|
|
|
|
/// </summary>
|
2020-11-17 20:27:10 +01:00
|
|
|
|
public static string ToDatabase(object configuration, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
|
|
|
|
|
|
=> configuration == null ? null : configurationEditorJsonSerializer.Serialize(configuration);
|
2018-11-15 08:13:40 +01:00
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-11-17 20:27:10 +01:00
|
|
|
|
[DataMember(Name = "defaultConfig")]
|
|
|
|
|
|
public virtual IDictionary<string, object> DefaultConfiguration
|
|
|
|
|
|
{
|
2019-04-16 11:23:22 +02:00
|
|
|
|
get => _defaultConfiguration;
|
2019-12-09 09:00:00 +01:00
|
|
|
|
set => _defaultConfiguration = value;
|
2019-04-16 11:23:22 +02:00
|
|
|
|
}
|
2018-02-05 17:48:54 +01:00
|
|
|
|
|
2018-09-17 12:54:22 +02:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual object DefaultConfigurationObject => DefaultConfiguration;
|
|
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual bool IsConfiguration(object obj) => obj is IDictionary<string, object>;
|
2018-02-07 13:35:59 +01:00
|
|
|
|
|
2020-11-17 20:27:10 +01:00
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-11-17 20:27:10 +01:00
|
|
|
|
public virtual object FromDatabase(string configurationJson, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
|
2018-02-05 17:48:54 +01:00
|
|
|
|
=> string.IsNullOrWhiteSpace(configurationJson)
|
|
|
|
|
|
? new Dictionary<string, object>()
|
2020-11-17 20:27:10 +01:00
|
|
|
|
: configurationEditorJsonSerializer.Deserialize<Dictionary<string, object>>(configurationJson);
|
2018-02-05 17:48:54 +01:00
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
2018-04-04 13:11:12 +10:00
|
|
|
|
public virtual object FromConfigurationEditor(IDictionary<string, object> editorValues, object configuration)
|
2018-01-24 11:44:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
// by default, return the posted dictionary
|
2018-02-05 17:48:54 +01:00
|
|
|
|
// but only keep entries that have a non-null/empty value
|
|
|
|
|
|
// rest will fall back to default during ToConfigurationEditor()
|
|
|
|
|
|
|
2018-05-03 13:14:19 +02:00
|
|
|
|
var keys = editorValues.Where(x =>
|
|
|
|
|
|
x.Value == null || x.Value is string stringValue && string.IsNullOrWhiteSpace(stringValue))
|
|
|
|
|
|
.Select(x => x.Key).ToList();
|
|
|
|
|
|
|
2018-02-05 17:48:54 +01:00
|
|
|
|
foreach (var key in keys) editorValues.Remove(key);
|
|
|
|
|
|
|
|
|
|
|
|
return editorValues;
|
2018-01-24 11:44:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
2018-04-04 13:11:12 +10:00
|
|
|
|
public virtual IDictionary<string, object> ToConfigurationEditor(object configuration)
|
2018-01-24 11:44:44 +01:00
|
|
|
|
{
|
|
|
|
|
|
// editors that do not override ToEditor/FromEditor have their configuration
|
|
|
|
|
|
// as a dictionary of <string, object> and, by default, we merge their default
|
|
|
|
|
|
// configuration with their current configuration
|
|
|
|
|
|
|
2018-01-26 17:55:20 +01:00
|
|
|
|
if (configuration == null)
|
|
|
|
|
|
configuration = new Dictionary<string, object>();
|
|
|
|
|
|
|
2018-01-24 11:44:44 +01:00
|
|
|
|
if (!(configuration is IDictionary<string, object> c))
|
2020-11-17 20:27:10 +01:00
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
|
$"Expecting a {typeof(Dictionary<string, object>).Name} instance but got {configuration.GetType().Name}.",
|
|
|
|
|
|
nameof(configuration));
|
2018-01-24 11:44:44 +01:00
|
|
|
|
|
|
|
|
|
|
// clone the default configuration, and apply the current configuration values
|
2018-02-05 17:48:54 +01:00
|
|
|
|
var d = new Dictionary<string, object>(DefaultConfiguration);
|
2018-09-17 12:54:22 +02:00
|
|
|
|
foreach (var (key, value) in c)
|
2018-02-05 17:48:54 +01:00
|
|
|
|
d[key] = value;
|
|
|
|
|
|
return d;
|
2018-01-24 11:44:44 +01:00
|
|
|
|
}
|
2018-02-05 17:48:54 +01:00
|
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
|
/// <inheritdoc />
|
2018-04-04 13:11:12 +10:00
|
|
|
|
public virtual IDictionary<string, object> ToValueEditor(object configuration)
|
2018-02-05 17:48:54 +01:00
|
|
|
|
=> ToConfigurationEditor(configuration);
|
2018-03-05 14:59:23 +01:00
|
|
|
|
|
2018-01-24 11:44:44 +01:00
|
|
|
|
}
|
2018-04-04 13:11:12 +10:00
|
|
|
|
}
|