diff --git a/src/Umbraco.Core/Manifest/ManifestParser.cs b/src/Umbraco.Core/Manifest/ManifestParser.cs index e772cd3df7..be3fcb238e 100644 --- a/src/Umbraco.Core/Manifest/ManifestParser.cs +++ b/src/Umbraco.Core/Manifest/ManifestParser.cs @@ -33,7 +33,10 @@ namespace Umbraco.Core.Manifest /// internal static IEnumerable GetPropertyEditors(JArray jsonEditors) { - return JsonConvert.DeserializeObject>(jsonEditors.ToString(), new PropertyEditorConverter()); + return JsonConvert.DeserializeObject>( + jsonEditors.ToString(), + new PropertyEditorConverter(), + new PreValueFieldConverter()); } /// diff --git a/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs b/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs index 9b502315e7..a5199af379 100644 --- a/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs +++ b/src/Umbraco.Core/Manifest/PropertyEditorConverter.cs @@ -27,6 +27,28 @@ namespace Umbraco.Core.Manifest target.StaticallyDefinedValueEditor.Validators = new List(); } + if (jObject["preValueEditor"] != null) + { + target.StaticallyDefinedPreValueEditor.Fields = new List(); + } + + base.Deserialize(jObject, target, serializer); + } + } + + internal class PreValueFieldConverter : JsonCreationConverter + { + protected override PreValueField Create(Type objectType, JObject jObject) + { + return new PreValueField() + { + //assign manifest validators so the serialization process can continue + Validators = new List() + }; + } + + protected override void Deserialize(JObject jObject, PreValueField target, JsonSerializer serializer) + { base.Deserialize(jObject, target, serializer); } } diff --git a/src/Umbraco.Core/PropertyEditors/PreValueEditor.cs b/src/Umbraco.Core/PropertyEditors/PreValueEditor.cs index 3bc978177a..ffbae6b824 100644 --- a/src/Umbraco.Core/PropertyEditors/PreValueEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/PreValueEditor.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; namespace Umbraco.Core.PropertyEditors @@ -11,14 +12,61 @@ namespace Umbraco.Core.PropertyEditors /// public class PreValueEditor { + public PreValueEditor() + { + Fields = Enumerable.Empty(); + //Validators = Enumerable.Empty(); + } + + ///// + ///// The full virtual path or the relative path to the current Umbraco folder for the angular view + ///// + //[JsonProperty("view")] + //public string View { get; set; } + + ///// + ///// A collection of validators for the pre value editor + ///// + //[JsonProperty("validation")] + //public IEnumerable Validators { get; set; } + /// - /// The full virtual path or the relative path to the current Umbraco folder for the angular view + /// A collection of pre-value fields to be edited /// - [JsonProperty("view")] + /// + /// If fields are specified then the master View and Validators will be ignored + /// + [JsonProperty("fields")] + public IEnumerable Fields { get; set; } + + ///// + ///// Returns true if this pre value editor is defined by individual fields + ///// + //public bool IsFieldBased + //{ + // get { return Fields.Any(); } + //} + } + + /// + /// Defines a pre value editable field + /// + public class PreValueField + { + /// + /// The key to store the pre-value against + /// + [JsonProperty("key", Required = Required.Always)] + public string Key { get; set; } + + /// + /// The view to render for the file + /// + [JsonProperty("view", Required = Required.Always)] public string View { get; set; } /// - /// A collection of validators for the pre value editor + /// A collection of validators for the pre value field /// [JsonProperty("validation")] public IEnumerable Validators { get; set; } diff --git a/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs b/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs index bb69b0a445..646b6d7583 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyEditor.cs @@ -32,7 +32,7 @@ namespace Umbraco.Core.PropertyEditors StaticallyDefinedValueEditor.ValueType = att.ValueType; StaticallyDefinedValueEditor.View = att.EditorView; - StaticallyDefinedPreValueEditor.View = att.PreValueEditorView; + //StaticallyDefinedPreValueEditor.View = att.PreValueEditorView; } } @@ -60,7 +60,7 @@ namespace Umbraco.Core.PropertyEditors [JsonProperty("name", Required = Required.Always)] public string Name { get; internal set; } - [JsonProperty("editor")] + [JsonProperty("editor", Required = Required.Always)] public ValueEditor ValueEditor { get { return CreateValueEditor(); } @@ -105,7 +105,18 @@ namespace Umbraco.Core.PropertyEditors /// /// protected virtual PreValueEditor CreatePreValueEditor() - { + { + if (StaticallyDefinedPreValueEditor != null) + { + foreach (var f in StaticallyDefinedPreValueEditor.Fields) + { + //detect if the view is a virtual path (in most cases, yes) then convert it + if (f.View.StartsWith("~/")) + { + f.View = IOHelper.ResolveUrl(f.View); + } + } + } return StaticallyDefinedPreValueEditor; } diff --git a/src/Umbraco.Core/PropertyEditors/ValidatorBase.cs b/src/Umbraco.Core/PropertyEditors/ValidatorBase.cs index 1ce79bf130..c75d1eb2d6 100644 --- a/src/Umbraco.Core/PropertyEditors/ValidatorBase.cs +++ b/src/Umbraco.Core/PropertyEditors/ValidatorBase.cs @@ -11,7 +11,11 @@ namespace Umbraco.Core.PropertyEditors /// /// Validates the object with the resolved ValueValidator found for this type /// - /// + /// + /// Depending on what is being validated, this value can be a json structure representing an editor's model, it could be a single + /// string representing an editor's model, this class structure is also used to validate pre-values and in that case this value + /// could be a json structure representing the entire pre-value model or it could ba a single value representing a pre-value field. + /// /// The current pre-values stored for the data type /// The property editor instance that we are validating for /// diff --git a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs index f5da535b70..8c4b859c4b 100644 --- a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs +++ b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs @@ -14,6 +14,59 @@ namespace Umbraco.Belle.Tests public class ManifestParserTests { + [Test] + public void Parse_Property_Editors_With_Pre_Vals() + { + + var a = JsonConvert.DeserializeObject(@"[ + { + id: '0EEBB7CE-51BA-4F6B-9D9C-78BB3314366C', + name: 'Test 1', + editor: { + view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html', + valueType: 'int', + validation: [ + { + type: 'Required' + }, + { + type: 'Regex', + value: '\\d*' + }, + ] + }, + preValueEditor: { + fields: [ + { + key: 'key1', + view: '~/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html', + validation: [ + { + type: 'Required' + } + ] + }, + { + key: 'key2', + view: '~/App_Plugins/MyPackage/PropertyEditors/Views/pre-val2.html' + } + ] + } + } +]"); + var parser = ManifestParser.GetPropertyEditors(a); + + Assert.AreEqual(1, parser.Count()); + Assert.AreEqual(2, parser.ElementAt(0).PreValueEditor.Fields.Count()); + Assert.AreEqual("key1", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).Key); + Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).View); + Assert.AreEqual(1, parser.ElementAt(0).PreValueEditor.Fields.ElementAt(0).Validators.Count()); + + Assert.AreEqual("key2", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).Key); + Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val2.html", parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).View); + Assert.AreEqual(0, parser.ElementAt(0).PreValueEditor.Fields.ElementAt(1).Validators.Count()); + } + [Test] public void Parse_Property_Editors() { @@ -39,22 +92,25 @@ namespace Umbraco.Belle.Tests { id: '1FCF5C39-5FC7-4BCE-AFBE-6500D9EBA261', name: 'Test 2', + defaultConfig: { key1: 'some default pre val' }, editor: { view: '~/App_Plugins/MyPackage/PropertyEditors/CsvEditor.html' } - }, + } ]"); var parser = ManifestParser.GetPropertyEditors(a); Assert.AreEqual(2, parser.Count()); Assert.AreEqual(new Guid("0EEBB7CE-51BA-4F6B-9D9C-78BB3314366C"), parser.ElementAt(0).Id); Assert.AreEqual("Test 1", parser.ElementAt(0).Name); - Assert.AreEqual("~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html", parser.ElementAt(0).ValueEditor.View); + Assert.AreEqual("/App_Plugins/MyPackage/PropertyEditors/MyEditor.html", parser.ElementAt(0).ValueEditor.View); Assert.AreEqual("int", parser.ElementAt(0).ValueEditor.ValueType); Assert.AreEqual(2, parser.ElementAt(0).ValueEditor.Validators.Count()); Assert.AreEqual(new Guid("1FCF5C39-5FC7-4BCE-AFBE-6500D9EBA261"), parser.ElementAt(1).Id); Assert.AreEqual("Test 2", parser.ElementAt(1).Name); + Assert.IsTrue(parser.ElementAt(1).DefaultPreValues.ContainsKey("key1")); + Assert.AreEqual("some default pre val", parser.ElementAt(1).DefaultPreValues["key1"]); } [Test] diff --git a/src/Umbraco.Web.UI.Client/src/common/mocks/resources/datatype.mocks.js b/src/Umbraco.Web.UI.Client/src/common/mocks/resources/datatype.mocks.js index abe12053bb..ae6de9eaa5 100644 --- a/src/Umbraco.Web.UI.Client/src/common/mocks/resources/datatype.mocks.js +++ b/src/Umbraco.Web.UI.Client/src/common/mocks/resources/datatype.mocks.js @@ -9,15 +9,17 @@ angular.module('umbraco.mocks'). } var id = mocksUtils.getParameterByName(data, "id") || 1234; - + + var selectedId = String.CreateGuid(); + var dataType = { id: id, name: "Data type " + id, - selectedEditor: String.CreateGuid(), + selectedEditor: selectedId, availableEditors: [ { name: "Some editor 1", editorId: String.CreateGuid() }, { name: "Some editor 2", editorId: String.CreateGuid() }, - { name: "Some editor 3", editorId: String.CreateGuid() }, + { name: "Some editor 3", editorId: selectedId }, { name: "Some editor 4", editorId: String.CreateGuid() }, { name: "Some editor 5", editorId: String.CreateGuid() }, { name: "Some editor 6", editorId: String.CreateGuid() } diff --git a/src/Umbraco.Web.UI.Client/src/views/datatype/edit.html b/src/Umbraco.Web.UI.Client/src/views/datatype/edit.html index 01f46d470a..4396fa7591 100644 --- a/src/Umbraco.Web.UI.Client/src/views/datatype/edit.html +++ b/src/Umbraco.Web.UI.Client/src/views/datatype/edit.html @@ -26,10 +26,10 @@
- + ng-options="e.editorId as e.name for e in content.availableEditors"> Required
diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest index 5893b50449..9f455c2989 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest @@ -3,6 +3,9 @@ { id: "0BA0F832-D759-4526-9B3E-94BBFC98F92E", name: "Regex", + defaultConfig: { + regexStatement: "^\\d*$" + }, editor: { view: "~/App_Plugins/MyPackage/PropertyEditors/Views/RegexEditor.html", validation: [ @@ -15,8 +18,18 @@ } ] }, - preValues: { - view: "myPreValues1" + preValueEditor: { + fields: [ + { + key: "regex", + view: "~/App_Plugins/MyPackage/PropertyEditors/Views/regexStatement.html", + validation: [ + { + type: "Required" + } + ] + } + ] } }, {