diff --git a/Gruntfile.js b/Gruntfile.js index f6631e7..cac485b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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'] diff --git a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj index 49eff37..a15642b 100644 --- a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj +++ b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj @@ -220,6 +220,7 @@ + diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs b/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs index b98baa9..c9f3953 100644 --- a/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs +++ b/app/Umbraco/Umbraco.Archetype/Extensions/Extensions.cs @@ -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>)"); - var match = regex.Match(input); - - while (match.Success) - { - var parms = match.Groups[3].ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); - var dictionary = new Dictionary(); - 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); - } } } diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index 207056d..4073225 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -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(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(sourceString, _jsonSettings); + + try + { + // Get list of configured properties and their types // and map them to the deserialized archetype model - var dataTypeCache = new Dictionary(); - 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(); + 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 dataTypeCache) @@ -125,5 +125,5 @@ namespace Archetype.Umbraco.PropertyConverters Constants.CacheKey_DataTypeByGuid + guid, () => Services.DataTypeService.GetDataTypeDefinitionById(guid)); } - } -} + } +} diff --git a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs new file mode 100644 index 0000000..eda1e23 --- /dev/null +++ b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs @@ -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(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(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(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()); + var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias); + propDef.Value = propEditor.ValueEditor.ConvertEditorToDb(propData, propDef.Value); + } + } + + return JsonConvert.SerializeObject(archetype); + } + } + + #endregion + } +}