Got further with pre-value editors and strongly typed stuff, got the pre-value editor for drop down list working and saving array objects. Now need to wire that up to the editor but think i need to change how the drop down list displays it's values.

This commit is contained in:
Shannon
2013-08-23 18:10:32 +10:00
parent fff8eac43f
commit 59a8e03c79
12 changed files with 109 additions and 20 deletions

View File

@@ -22,6 +22,8 @@ namespace Umbraco.Core.Models.Editors
AdditionalData = new ReadOnlyDictionary<string, object>(additionalData);
}
//TODO: Change this to an object so we can post JSON or json converted clr types if we want!
/// <summary>
/// The string value submitted for the property
/// </summary>

View File

@@ -102,9 +102,10 @@ namespace Umbraco.Core.PropertyEditors
/// This can be overridden if perhaps you have a comma delimited string posted value but want to convert those to individual rows, or to convert
/// a json structure to multiple rows.
/// </remarks>
public virtual IDictionary<string, string> FormatDataForPersistence(IDictionary<string, string> editorValue, PreValueCollection currentValue)
public virtual IDictionary<string, string> FormatDataForPersistence(IDictionary<string, object> editorValue, PreValueCollection currentValue)
{
return editorValue;
//convert to a string based value to be saved in the db
return editorValue.ToDictionary(x => x.Key, x => x.Value == null ? null : x.Value.ToString());
}
/// <summary>

View File

@@ -171,6 +171,8 @@ namespace Umbraco.Core.PropertyEditors
return result.Result;
}
//TODO: Change the result to object so we can pass back JSON or json converted clr types if we want!
/// <summary>
/// A method used to serialize the databse value to a string value which is then used to be sent
/// to the editor in JSON format.

View File

@@ -0,0 +1,30 @@
/**
* @ngdoc directive
* @name umbraco.directives.directive:valHighlight
* @restrict A
* @description Used on input fields when you want to signal that they are in error, this will highlight the item for 1 second
**/
function valHighlight($timeout) {
return {
restrict: "A",
link: function (scope, element, attrs, ctrl) {
scope.$watch(function() {
return scope.$eval(attrs.valHighlight);
}, function(newVal, oldVal) {
if (newVal === true) {
element.addClass("highlight-error");
$timeout(function () {
//set the bound scope property to false
scope[attrs.valHighlight] = false;
}, 1000);
}
else {
element.removeClass("highlight-error");
}
});
}
};
}
angular.module('umbraco.directives').directive("valHighlight", valHighlight);

View File

@@ -399,6 +399,16 @@ input[type="checkbox"][readonly] {
}
input.highlight-error,
select.highlight-error,
textarea.highlight-error {
border-color: #953b39 !important;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #d59392;
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #d59392;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #d59392;
}
// FORM ACTIONS
// ------------

View File

@@ -198,9 +198,6 @@
}
}
// CSS3 PROPERTIES
// --------------------------------------------------

View File

@@ -0,0 +1,33 @@
angular.module("umbraco").controller("Umbraco.Editors.DropdownPreValueController",
function ($scope, $timeout) {
$scope.newItem = "";
$scope.hasError = false;
$scope.remove = function(item, evt) {
evt.preventDefault();
$scope.model.value = _.reject($scope.model.value, function(i) {
return i === item;
});
};
$scope.add = function (evt) {
evt.preventDefault();
if (!_.contains($scope.model.value, $scope.newItem)) {
if ($scope.newItem) {
$scope.model.value.push($scope.newItem);
$scope.newItem = "";
$scope.hasError = false;
return;
}
}
//there was an error, do the highlight (will be set back by the directive)
$scope.hasError = true;
};
});

View File

@@ -1,5 +1,13 @@
<div>
<input type="text" ng-model="model.value" />
<div ng-controller="Umbraco.Editors.DropdownPreValueController">
<ul class="unstyled">
<li>
<input name="newItem" type="text" ng-model="newItem" val-highlight="hasError" />
<button class="btn" ng-click="add($event)">Add</button>
</li>
<li ng-repeat="item in model.value">
<input type="text" ng-model="item" />
<button class="btn" ng-click="remove(item, $event)">Remove</button>
</li>
</ul>
</div>

View File

@@ -88,7 +88,7 @@ namespace Umbraco.Web.Editors
foreach (var v in propertyEditor.PreValueEditor.Fields.SelectMany(x => x.Validators))
{
foreach (var result in v.Validate(postedValue, preVal.Key, propertyEditor))
foreach (var result in v.Validate(postedValue != null ? postedValue.ToString() : null, preVal.Key, propertyEditor))
{
//if there are no member names supplied then we assume that the validation message is for the overall property
// not a sub field on the property editor

View File

@@ -18,6 +18,6 @@ namespace Umbraco.Web.Models.ContentEditing
/// The value stored for the pre-value field
/// </summary>
[DataMember(Name = "value", IsRequired = true)]
public string Value { get; set; }
public object Value { get; set; }
}
}

View File

@@ -70,7 +70,7 @@ namespace Umbraco.Web.Models.Mapping
LogHelper.Warn<PreValueDisplayResolver>("Could not find persisted pre-value for field " + field.Key);
continue;
}
field.Value = dictionaryVals.Single(x => x.Key.InvariantEquals(field.Key)).Value.ToString();
field.Value = dictionaryVals.Single(x => x.Key.InvariantEquals(field.Key)).Value;
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
@@ -23,9 +24,9 @@ namespace Umbraco.Web.PropertyEditors
{
var dictionary = PreValueCollection.AsDictionary(persistedPreVals);
var arrayOfVals = dictionary.Select(item => item.Value).ToList();
var json = JsonConvert.SerializeObject(arrayOfVals);
//var json = JsonConvert.SerializeObject(arrayOfVals);
return new Dictionary<string, object> {{"temp", json}};
return new Dictionary<string, object> { { "temp", arrayOfVals } };
}
/// <summary>
@@ -33,23 +34,28 @@ namespace Umbraco.Web.PropertyEditors
/// </summary>
/// <param name="editorValue"></param>
/// <param name="currentValue"></param>
/// <returns></returns>
/// <returns>
/// A string/string dictionary since all values that need to be persisted in the database are strings.
/// </returns>
/// <remarks>
/// This is mostly because we want to maintain compatibility with v6 drop down property editors that store their prevalues in different db rows.
/// </remarks>
public override IDictionary<string, string> FormatDataForPersistence(IDictionary<string, string> editorValue, Core.Models.PreValueCollection currentValue)
public override IDictionary<string, string> FormatDataForPersistence(IDictionary<string, object> editorValue, PreValueCollection currentValue)
{
var val = editorValue["temp"];
var val = editorValue["temp"] as JArray;
var result = new Dictionary<string, string>();
if (val.IsNullOrWhiteSpace()) return result;
if (val == null)
{
return result;
}
try
{
var deserialized = JsonConvert.DeserializeObject<string[]>(val);
var index = 0;
foreach (var item in deserialized)
foreach (var item in val)
{
result.Add(index.ToInvariantString(), item);
result.Add(index.ToInvariantString(), item.ToString());
index++;
}
}