Removed temp "ParseMacros" extension method

Removed package.manifest
Added C# property editor definition
Added initial code for handling data value conversions when accessing from / writing to datavabase / xml cache
This commit is contained in:
mattbrailsford
2014-04-21 20:23:07 +01:00
parent 5aeef0383c
commit 1ca8878592
5 changed files with 214 additions and 110 deletions
-5
View File
@@ -29,11 +29,6 @@ module.exports = function(grunt) {
tasks: ['copy:html']
},
config: {
files: ['config/package.manifest'],
tasks: ['copy:config']
},
dll: {
files: ['app/Umbraco/**/*.dll'],
tasks: ['copy:dll']
@@ -220,6 +220,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\VersionInfo.cs" />
<Compile Include="PropertyConverters\ArchetypeValueConverter.cs" />
<Compile Include="PropertyEditors\ArchetypePropertyEditor.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@@ -23,27 +23,5 @@ namespace Archetype.Umbraco.Extensions
{
return prop.PropertyEditorAlias.InvariantEquals(Constants.PropertyEditorAlias);
}
public static HtmlString ParseMacros(this string input, UmbracoHelper umbHelper)
{
var regex = new Regex("(<div class=\".*umb-macro-holder.*\".*macroAlias=\"(\\w*)\"(.*)\\/>.*\\/div>)");
var match = regex.Match(input);
while (match.Success)
{
var parms = match.Groups[3].ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
var dictionary = new Dictionary<string, object>();
foreach (var parm in parms)
{
var thisParm = parm.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
dictionary.Add(thisParm[0], thisParm[1].Substring(1, thisParm[1].Length - 2));
}
input = input.Replace(match.Groups[0].ToString(), umbHelper.RenderMacro(match.Groups[2].ToString(), dictionary).ToHtmlString());
match = regex.Match(input);
}
return new HtmlString(input);
}
}
}
@@ -1,95 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Archetype.Umbraco.Extensions;
using Archetype.Umbraco.Models;
using Newtonsoft.Json;
using System.Linq;
using Archetype.Umbraco.Extensions;
using Archetype.Umbraco.Models;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using umbraco.interfaces;
namespace Archetype.Umbraco.PropertyConverters
{
/* based on the Tim Geyssens sample at: https://github.com/TimGeyssens/MatrixPropEditor/blob/master/SamplePropertyValueConverter/SamplePropertyValueConverter/MatrixValueConverter.cs */
[PropertyValueType(typeof(Archetype.Umbraco.Models.Archetype))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class ArchetypeValueConverter : PropertyValueConverterBase
{
protected JsonSerializerSettings _jsonSettings;
public ArchetypeValueConverter()
{
var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
_jsonSettings = new JsonSerializerSettings { ContractResolver = dcr };
}
public ServiceContext Services
{
get { return ApplicationContext.Current.Services; }
}
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditorAlias);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var defaultValue = new Models.Archetype();
if (source == null)
return defaultValue;
var sourceString = source.ToString();
if (sourceString.DetectIsJson())
{
try
{
// Deserialize value to archetype model
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(sourceString, _jsonSettings);
try
{
// Get list of configured properties and their types
namespace Archetype.Umbraco.PropertyConverters
{
/* based on the Tim Geyssens sample at: https://github.com/TimGeyssens/MatrixPropEditor/blob/master/SamplePropertyValueConverter/SamplePropertyValueConverter/MatrixValueConverter.cs */
[PropertyValueType(typeof(Archetype.Umbraco.Models.Archetype))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class ArchetypeValueConverter : PropertyValueConverterBase
{
protected JsonSerializerSettings _jsonSettings;
public ArchetypeValueConverter()
{
var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
_jsonSettings = new JsonSerializerSettings { ContractResolver = dcr };
}
public ServiceContext Services
{
get { return ApplicationContext.Current.Services; }
}
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditorAlias);
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var defaultValue = new Models.Archetype();
if (source == null)
return defaultValue;
var sourceString = source.ToString();
if (sourceString.DetectIsJson())
{
try
{
// Deserialize value to archetype model
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(sourceString, _jsonSettings);
try
{
// Get list of configured properties and their types
// and map them to the deserialized archetype model
var dataTypeCache = new Dictionary<Guid, IDataTypeDefinition>();
var preValue = GetArchetypePreValueFromDataTypeId(propertyType.DataTypeId, dataTypeCache);
foreach (var fieldset in preValue.Fieldsets)
{
var fieldsetAlias = fieldset.Alias;
foreach (var fieldsetInst in archetype.Fieldsets.Where(x => x.Alias == fieldsetAlias))
{
foreach (var property in fieldset.Properties)
{
var propertyAlias = property.Alias;
var dataTypeCache = new Dictionary<Guid, IDataTypeDefinition>();
var preValue = GetArchetypePreValueFromDataTypeId(propertyType.DataTypeId, dataTypeCache);
foreach (var fieldset in preValue.Fieldsets)
{
var fieldsetAlias = fieldset.Alias;
foreach (var fieldsetInst in archetype.Fieldsets.Where(x => x.Alias == fieldsetAlias))
{
foreach (var property in fieldset.Properties)
{
var propertyAlias = property.Alias;
foreach (var propertyInst in fieldsetInst.Properties.Where(x => x.Alias == propertyAlias))
{
propertyInst.DataTypeId = GetDataTypeByGuid(property.DataTypeGuid).Id;
propertyInst.PropertyEditorAlias = property.PropertyEditorAlias;
}
}
}
}
}
catch (Exception ex)
{
}
return archetype;
}
catch (Exception ex)
{
return defaultValue;
}
}
return defaultValue;
propertyInst.PropertyEditorAlias = property.PropertyEditorAlias;
}
}
}
}
}
catch (Exception ex)
{
}
return archetype;
}
catch (Exception ex)
{
return defaultValue;
}
}
return defaultValue;
}
private ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId, IDictionary<Guid, IDataTypeDefinition> dataTypeCache)
@@ -125,5 +125,5 @@ namespace Archetype.Umbraco.PropertyConverters
Constants.CacheKey_DataTypeByGuid + guid,
() => Services.DataTypeService.GetDataTypeDefinitionById(guid));
}
}
}
}
}
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using ClientDependency.Core;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.PropertyEditors;
namespace Archetype.Umbraco.PropertyEditors
{
[PropertyEditorAsset(ClientDependencyType.Javascript, "/App_Plugins/Archetype/js/archetype.js")]
[PropertyEditor("Imulus.Archetype", "Archetype", "/App_Plugins/Archetype/views/archetype.html",
ValueType = "JSON")]
public class ArchetypePropertyEditor : PropertyEditor
{
#region Pre Value Editor
protected override PreValueEditor CreatePreValueEditor()
{
return new ArchetypePreValueEditor();
}
internal class ArchetypePreValueEditor : PreValueEditor
{
[PreValueField("archetypeConfig", "Config", "/App_Plugins/Archetype/views/archetype.config.html",
Description = "(Required) Describe your Archetype.")]
public string Config { get; set; }
[PreValueField("hideLabel", "Hide Label", "boolean",
Description = "Hide the Umbraco property title and description, making the Archetype span the entire page width")]
public bool HideLabel { get; set; }
}
#endregion
#region Value Editor
protected override PropertyValueEditor CreateValueEditor()
{
return new ArchetypePropertyValueEditor(base.CreateValueEditor());
}
internal class ArchetypePropertyValueEditor : PropertyValueEditorWrapper
{
protected JsonSerializerSettings _jsonSettings;
public ArchetypePropertyValueEditor(PropertyValueEditor wrapped)
: base(wrapped)
{
var dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
_jsonSettings = new JsonSerializerSettings { ContractResolver = dcr };
}
public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (property.Value == null)
return string.Empty;
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(property.Value.ToString(), _jsonSettings);
foreach (var fieldset in archetype.Fieldsets)
{
foreach (var propDef in fieldset.Properties)
{
var dtd = dataTypeService.GetDataTypeDefinitionById(Guid.Parse(propDef.DataTypeGuid));
var propType = new PropertyType(dtd) { Alias = propDef.Alias };
var prop = new Property(propType, propDef.Value);
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
propDef.Value = propEditor.ValueEditor.ConvertDbToString(prop, propType, dataTypeService);
}
}
property.Value = JsonConvert.SerializeObject(archetype);
return base.ConvertDbToString(property, propertyType, dataTypeService);
}
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
if (property.Value == null)
return string.Empty;
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(property.Value.ToString(), _jsonSettings);
foreach (var fieldset in archetype.Fieldsets)
{
foreach (var propDef in fieldset.Properties)
{
var dtd = dataTypeService.GetDataTypeDefinitionById(Guid.Parse(propDef.DataTypeGuid));
var propType = new PropertyType(dtd) { Alias = propDef.Alias };
var prop = new Property(propType, propDef.Value);
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
propDef.Value = propEditor.ValueEditor.ConvertDbToEditor(prop, propType, dataTypeService);
}
}
return archetype;
}
public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue)
{
if (editorValue.Value == null)
return string.Empty;
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(editorValue.Value.ToString(), _jsonSettings);
foreach (var fieldset in archetype.Fieldsets)
{
foreach (var propDef in fieldset.Properties)
{
var dtd = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(Guid.Parse(propDef.DataTypeGuid));
var preValues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);
var propData = new ContentPropertyData(propDef.Value, preValues, new Dictionary<string, object>());
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
propDef.Value = propEditor.ValueEditor.ConvertEditorToDb(propData, propDef.Value);
}
}
return JsonConvert.SerializeObject(archetype);
}
}
#endregion
}
}