From 99d570dc97fa5edbfeb3fd2305807dbe0cbbc375 Mon Sep 17 00:00:00 2001 From: kjac Date: Thu, 25 Jun 2015 07:59:16 +0200 Subject: [PATCH] Handle upload and image cropper clientside --- .../Archetype.Umbraco.csproj | 1 + .../Extensions/PropertyEditorExtensions.cs | 58 +++++++++++++++++++ app/directives/archetypeproperty.js | 2 + 3 files changed, 61 insertions(+) create mode 100644 app/Umbraco/Umbraco.Archetype/Extensions/PropertyEditorExtensions.cs diff --git a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj index 3a97867..d96c351 100644 --- a/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj +++ b/app/Umbraco/Umbraco.Archetype/Archetype.Umbraco.csproj @@ -234,6 +234,7 @@ + diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/PropertyEditorExtensions.cs b/app/Umbraco/Umbraco.Archetype/Extensions/PropertyEditorExtensions.cs new file mode 100644 index 0000000..6ec2946 --- /dev/null +++ b/app/Umbraco/Umbraco.Archetype/Extensions/PropertyEditorExtensions.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using Umbraco.Core.PropertyEditors; + +namespace Archetype.Extensions +{ + public static class PropertyEditorExtensions + { + /// + /// Formats the default prevalues for a property editor for use within an Archetype clientside context + /// + /// The property editor + /// The formatted default prevalues + public static IDictionary DefaultPreValuesForArchetype(this PropertyEditor propertyEditor) + { + if (propertyEditor.DefaultPreValues == null || propertyEditor.DefaultPreValues.Any() == false) + { + return propertyEditor.DefaultPreValues; + } + var view = propertyEditor.ValueEditor.View.ToLowerInvariant(); + + // This is the extension point for default prevalues formatting, in case we need to handle any other + // property editors later on. It should be replaced with a switch statement by then, or maybe some fancy + // auto discovery of formatters :) + if (view == "imagecropper") + { + propertyEditor.FormatImageCropperDefaultPreValuesForArchetype(); + } + return propertyEditor.DefaultPreValues; + } + + /// + /// Format the default prevalues of the image cropper property editor + /// + /// + /// + /// In order for the image cropper to work clientside, we need to make sure it's default prevalue "focalpoint" is returned + /// as a JSON object and not as the string it's defined as on the image cropper property editor. + /// + private static void FormatImageCropperDefaultPreValuesForArchetype(this PropertyEditor propertyEditor) + { + const string focalPointKey = "focalPoint"; + + if (propertyEditor.DefaultPreValues.ContainsKey(focalPointKey) == false || propertyEditor.DefaultPreValues[focalPointKey] == null) + { + return; + } + var focalPoint = propertyEditor.DefaultPreValues[focalPointKey].ToString(); + if (string.IsNullOrEmpty(focalPoint)) + { + return; + } + // translate the JSON string to a JSON object + propertyEditor.DefaultPreValues[focalPointKey] = JsonConvert.DeserializeObject(focalPoint); + } + } +} \ No newline at end of file diff --git a/app/directives/archetypeproperty.js b/app/directives/archetypeproperty.js index 7f275e6..2f73789 100644 --- a/app/directives/archetypeproperty.js +++ b/app/directives/archetypeproperty.js @@ -143,6 +143,8 @@ angular.module("umbraco.directives").directive('archetypeProperty', function ($c //some items need an alias scope.model.alias = "archetype-property-" + propertyAlias; + //some items also need an id (file upload for example) + scope.model.id = propertyAlias; //watch for changes since there is no two-way binding with the local model.value scope.$watch('model.value', function (newValue, oldValue) {