This commit is contained in:
Morten Christensen
2013-05-24 08:14:07 -02:00
11 changed files with 1 additions and 412 deletions

View File

@@ -1,48 +0,0 @@
using System;
namespace Umbraco.Core.PropertyEditors.Attributes
{
/// <summary>
/// Defines a PropertyEditor
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
internal sealed class PropertyEditorAttribute : Attribute
{
public PropertyEditorAttribute(string id, string alias, string name)
{
Mandate.ParameterNotNullOrEmpty(id, "id");
Mandate.ParameterNotNullOrEmpty(alias, "alias");
Mandate.ParameterNotNullOrEmpty(name, "name");
Id = Guid.Parse(id);
Alias = alias;
Name = name;
IsContentPropertyEditor = true;
}
public Guid Id { get; private set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the alias.
/// </summary>
/// <value>The alias.</value>
public string Alias { get; set; }
/// <summary>
/// Flag determining if this property editor is used to edit content
/// </summary>
public bool IsContentPropertyEditor { get; set; }
/// <summary>
/// Flag determining if this property editor is used to edit parameters
/// </summary>
public bool IsParameterEditor { get; set; }
}
}

View File

@@ -1,22 +0,0 @@
using System;
namespace Umbraco.Core.PropertyEditors.Attributes
{
/// <summary>
/// Attribute determining whether or not to hide/show the label of a property
/// </summary>
/// <remarks>
/// This directly affects the meta data property: HideSurroundingHtml
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal class ShowLabelAttribute : Attribute
{
public ShowLabelAttribute(bool show)
{
ShowLabel = show;
}
public bool ShowLabel { get; private set; }
}
}

View File

@@ -1,13 +0,0 @@
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// A class representing a blank, null or empty pre-value editor.
/// </summary>
/// <remarks>
/// This class can be used for Property Editors who do not define a Pre value editor
/// </remarks>
internal class BlankPreValueModel : PreValueModel
{
}
}

View File

@@ -1,147 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
namespace Umbraco.Core.PropertyEditors
{
internal abstract class EditorModel<TValueModel> where TValueModel : IValueModel, new()
{
protected EditorModel()
{
// Set the UI Elements collection to an empty list
//UIElements = new List<UIElement>();
}
[ReadOnly(true)]
public virtual bool ShowUmbracoLabel
{
get { return true; }
}
/// <summary>
/// Gets a list of UI Elements for the property editor.
/// </summary>
//[ScaffoldColumn(false)]
//public virtual IList<UIElement> UIElements { get; protected internal set; }
private ModelMetadata _modelMetadata;
/// <summary>
/// Returns the meta data for the current editor model
/// </summary>
protected internal ModelMetadata MetaData
{
get
{
return _modelMetadata ?? (_modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => this, GetType()));
}
}
/// <summary>
/// Returns the serialized value for the PropertyEditor
/// </summary>
/// <returns></returns>
public virtual IDictionary<string, object> GetSerializedValue()
{
var editableProps = MetaData.Properties.Where(x => x.ShowForEdit && !x.IsReadOnly);
var d = new Dictionary<string, object>();
foreach (var p in editableProps)
{
//by default, we will not support complex modelled properties, developers will need to override
//the GetSerializedValue method if they need support for this.
if (p.IsComplexType)
{
//TODO: We should magically support this
throw new NotSupportedException("The default serialization implementation of EditorModel does not support properties that are complex models");
}
d.Add(p.PropertyName, p.Model);
}
return d;
}
public virtual void SetModelValues(IDictionary<string, object> serializedVal)
{
if (serializedVal == null || serializedVal.Count == 0)
{
return;
}
var modelProperties = GetType().GetProperties();
var editableProps = MetaData.Properties.Where(x => x.ShowForEdit && !x.IsReadOnly);
foreach (var i in serializedVal)
{
if (i.Value == null)
continue;
//get the property with the name
var prop = editableProps.Where(x => x.PropertyName == i.Key).SingleOrDefault();
if (prop != null)
{
//set the property value
var toConverter = TypeDescriptor.GetConverter(prop.ModelType);
if (toConverter != null)
{
//get the model property for this property meta data to set its value
var propInfo = modelProperties.Where(x => x.Name == prop.PropertyName).Single();
object convertedVal;
//if value is already of the same type, just use the current value, otherwise try and convert it
if (i.Value.GetType() == propInfo.PropertyType)
{
convertedVal = i.Value;
}
else
{
try
{
convertedVal = toConverter.ConvertFrom(i.Value);
}
catch (NotSupportedException)
{
//this occurs when the converter doesn't know how, so we can try the opposite way as a last ditch effort
var fromConverter = TypeDescriptor.GetConverter(i.Value.GetType());
if (fromConverter == null)
{
throw;
}
convertedVal = fromConverter.ConvertTo(i.Value, prop.ModelType);
}
}
propInfo.SetValue(this, convertedVal, null);
}
}
}
}
public virtual TValueModel GetValueModel()
{
var editableProps = MetaData.Properties.Where(x => x.ShowForEdit && !x.IsReadOnly);
var d = new TValueModel();
foreach (var p in editableProps)
{
//by default, we will not support complex modelled properties, developers will need to override
//the GetSerializedValue method if they need support for this.
//TODO Test if this exception is still valid
if (p.IsComplexType)
{
throw new NotSupportedException("The default serialization implementation of EditorModel does not support properties that are complex models");
}
var property = d.GetType().GetProperty(p.PropertyName);
if(property != null)
{
property.SetValue(d, p.Model, null);
}
}
return d;
}
}
}

View File

@@ -1,27 +0,0 @@
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// An abstract class representing the model to render a Property Editor's content editor with PreValues
/// </summary>
/// <typeparam name="TPreValueModel">The type of the PreValue model.</typeparam>
/// <typeparam name="TValueModel"> </typeparam>
internal abstract class EditorModel<TValueModel, TPreValueModel> : EditorModel<TValueModel>
where TValueModel : IValueModel, new()
where TPreValueModel : PreValueModel
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="preValues">The pre value options used to construct the editor</param>
protected EditorModel(TPreValueModel preValues)
{
PreValueModel = preValues;
}
/// <summary>
/// The pre value options used to configure the editor
/// </summary>
/// <value>The pre value model.</value>
public TPreValueModel PreValueModel { get; protected internal set; }
}
}

View File

@@ -1,7 +0,0 @@
namespace Umbraco.Core.PropertyEditors
{
internal interface IValueModel
{
}
}

View File

@@ -1,21 +0,0 @@
using System;
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// A class representing a single value of a Pre-Value editor to be saved
/// </summary>
internal class PreValueDefinition
{
public PreValueDefinition(string propertyName, Type modeType, object modelValue)
{
ModelType = modeType;
PropertyName = propertyName;
ModelValue = modelValue;
}
public Type ModelType { get; private set; }
public string PropertyName { get; private set; }
public object ModelValue { get; private set; }
}
}

View File

@@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// Abstract class representing a Property Editor's model to render it's Pre value editor
/// </summary>
internal abstract class PreValueModel
{
protected virtual IEnumerable<PreValueDefinition> GetValueDefinitions()
{
throw new NotImplementedException();
}
public virtual string GetSerializedValue()
{
throw new NotImplementedException();
}
protected virtual void SetModelPropertyValue(PreValueDefinition def, Action<object> setProperty)
{
throw new NotImplementedException();
}
public virtual void SetModelValues(string serializedVal)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,37 +0,0 @@
using System;
using System.Linq;
using Umbraco.Core.PropertyEditors.Attributes;
namespace Umbraco.Core.PropertyEditors
{
internal abstract class PropertyEditor
{
/// <summary>
/// Constructor for a PropertyEditor
/// </summary>
protected PropertyEditor()
{
//Locate the metadata attribute
var docTypeAttributes = GetType()
.GetCustomAttributes(typeof(PropertyEditorAttribute), true)
.OfType<PropertyEditorAttribute>();
if (!docTypeAttributes.Any())
throw new InvalidOperationException(
string.Format("The PropertyEditor of type {0} is missing the {1} attribute", GetType().FullName,
typeof(PropertyEditorAttribute).FullName));
//assign the properties of this object to those of the metadata attribute
var attr = docTypeAttributes.First();
Id = attr.Id;
Name = attr.Name;
Alias = attr.Alias;
}
public virtual Guid Id { get; protected set; }
public virtual string Name { get; protected set; }
public virtual string Alias { get; protected set; }
}
}

View File

@@ -1,49 +0,0 @@
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// Abstract class that all Property editors should inherit from
/// </summary>
/// <typeparam name="TEditorModel"></typeparam>
/// <typeparam name="TPreValueModel"></typeparam>
/// <typeparam name="TValueModel"> </typeparam>
internal abstract class PropertyEditor<TValueModel, TEditorModel, TPreValueModel> : PropertyEditor
where TValueModel : IValueModel, new()
where TEditorModel : EditorModel<TValueModel>
where TPreValueModel : PreValueModel
{
/// <summary>
/// Returns the editor model to be used for the property editor
/// </summary>
/// <returns></returns>
public abstract TEditorModel CreateEditorModel(TPreValueModel preValues);
/// <summary>
/// Returns the editor model to be used for the prevalue editor
/// </summary>
/// <returns></returns>
public abstract TPreValueModel CreatePreValueEditorModel();
}
/// <summary>
/// Abstract class that Property editors should inherit from that don't require a pre-value editor
/// </summary>
/// <typeparam name="TEditorModel"></typeparam>
/// <typeparam name="TValueModel"> </typeparam>
internal abstract class PropertyEditor<TValueModel, TEditorModel> : PropertyEditor<TValueModel, TEditorModel, BlankPreValueModel>
where TValueModel : IValueModel, new()
where TEditorModel : EditorModel<TValueModel>
{
public override BlankPreValueModel CreatePreValueEditorModel()
{
return new BlankPreValueModel();
}
public sealed override TEditorModel CreateEditorModel(BlankPreValueModel preValues)
{
return CreateEditorModel();
}
public abstract TEditorModel CreateEditorModel();
}
}

View File

@@ -530,16 +530,6 @@
<Compile Include="Profiling\ProfilerExtensions.cs" />
<Compile Include="Profiling\ProfilerResolver.cs" />
<Compile Include="Profiling\WebProfiler.cs" />
<Compile Include="PropertyEditors\Attributes\PropertyEditorAttribute.cs" />
<Compile Include="PropertyEditors\Attributes\ShowLabelAttribute.cs" />
<Compile Include="PropertyEditors\BlankPreValueModel.cs" />
<Compile Include="PropertyEditors\EditorModel.cs" />
<Compile Include="PropertyEditors\EditorModel`T.cs" />
<Compile Include="PropertyEditors\IValueModel.cs" />
<Compile Include="PropertyEditors\PreValueDefinition.cs" />
<Compile Include="PropertyEditors\PreValueModel.cs" />
<Compile Include="PropertyEditors\PropertyEditor.cs" />
<Compile Include="PropertyEditors\PropertyEditor`T.cs" />
<Compile Include="PublishedContentExtensions.cs" />
<Compile Include="Dictionary\ICultureDictionary.cs" />
<Compile Include="Dynamics\ClassFactory.cs" />
@@ -800,6 +790,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Packaging\" />
<Folder Include="PropertyEditors\Attributes\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />