2017-07-20 11:21:28 +02:00
|
|
|
using System;
|
2013-10-27 15:22:04 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Core.Models.Editors;
|
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2013-11-19 11:51:01 +11:00
|
|
|
using Umbraco.Core.Services;
|
2013-10-27 15:22:04 +01:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.PropertyEditors
|
|
|
|
|
{
|
2018-02-15 14:49:32 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a multiple text string property editor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataEditor(Constants.PropertyEditors.Aliases.MultipleTextstring, "Repeatable textstrings", "multipletextbox", ValueType = ValueTypes.Text, Icon="icon-ordered-list", Group="lists")]
|
2018-02-25 10:43:16 +01:00
|
|
|
public class MultipleTextStringPropertyEditor : DataEditor
|
2013-10-27 15:22:04 +01:00
|
|
|
{
|
2015-01-23 20:00:44 +11:00
|
|
|
/// <summary>
|
2018-02-15 14:49:32 +01:00
|
|
|
/// Initializes a new instance of the <see cref="MultipleTextStringPropertyEditor"/> class.
|
2015-01-23 20:00:44 +11:00
|
|
|
/// </summary>
|
2018-02-15 14:49:32 +01:00
|
|
|
public MultipleTextStringPropertyEditor(ILogger logger)
|
|
|
|
|
: base(logger)
|
2016-09-11 19:57:33 +02:00
|
|
|
{ }
|
2015-01-23 20:00:44 +11:00
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override IDataValueEditor CreateValueEditor() => new MultipleTextStringPropertyValueEditor(Attribute);
|
2013-10-27 15:22:04 +01:00
|
|
|
|
2018-02-15 14:49:32 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override IConfigurationEditor CreateConfigurationEditor() => new MultipleTextStringConfigurationEditor();
|
2013-10-27 15:22:04 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Custom value editor so we can format the value for the editor and the database
|
|
|
|
|
/// </summary>
|
2018-02-15 14:49:32 +01:00
|
|
|
internal class MultipleTextStringPropertyValueEditor : DataValueEditor
|
2013-10-27 15:22:04 +01:00
|
|
|
{
|
2018-02-15 14:49:32 +01:00
|
|
|
public MultipleTextStringPropertyValueEditor(DataEditorAttribute attribute)
|
2018-01-24 13:37:14 +01:00
|
|
|
: base(attribute)
|
|
|
|
|
{ }
|
2017-07-20 11:21:28 +02:00
|
|
|
|
2013-10-27 15:22:04 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// The value passed in from the editor will be an array of simple objects so we'll need to parse them to get the string
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="editorValue"></param>
|
|
|
|
|
/// <param name="currentValue"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// We will also check the pre-values here, if there are more items than what is allowed we'll just trim the end
|
|
|
|
|
/// </remarks>
|
2018-03-16 09:06:44 +01:00
|
|
|
public override object FromEditor(ContentPropertyData editorValue, object currentValue)
|
2013-10-27 15:22:04 +01:00
|
|
|
{
|
|
|
|
|
var asArray = editorValue.Value as JArray;
|
|
|
|
|
if (asArray == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:55:20 +01:00
|
|
|
if (!(editorValue.DataTypeConfiguration is MultipleTestStringConfiguration config))
|
|
|
|
|
throw new Exception("panic");
|
|
|
|
|
var max = config.Maximum;
|
2013-10-27 15:22:04 +01:00
|
|
|
|
|
|
|
|
//The legacy property editor saved this data as new line delimited! strange but we have to maintain that.
|
|
|
|
|
var array = asArray.OfType<JObject>()
|
|
|
|
|
.Where(x => x["value"] != null)
|
|
|
|
|
.Select(x => x["value"].Value<string>());
|
2017-07-20 11:21:28 +02:00
|
|
|
|
2013-11-25 17:36:56 +11:00
|
|
|
//only allow the max if over 0
|
|
|
|
|
if (max > 0)
|
|
|
|
|
{
|
2017-07-20 11:21:28 +02:00
|
|
|
return string.Join(Environment.NewLine, array.Take(max));
|
2013-11-25 17:36:56 +11:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
2013-11-25 17:36:56 +11:00
|
|
|
return string.Join(Environment.NewLine, array);
|
2013-10-27 15:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We are actually passing back an array of simple objects instead of an array of strings because in angular a primitive (string) value
|
|
|
|
|
/// cannot have 2 way binding, so to get around that each item in the array needs to be an object with a string.
|
|
|
|
|
/// </summary>
|
2013-11-19 11:51:01 +11:00
|
|
|
/// <param name="property"></param>
|
|
|
|
|
/// <param name="propertyType"></param>
|
|
|
|
|
/// <param name="dataTypeService"></param>
|
2013-10-27 15:22:04 +01:00
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The legacy property editor saved this data as new line delimited! strange but we have to maintain that.
|
|
|
|
|
/// </remarks>
|
2018-03-16 09:06:44 +01:00
|
|
|
public override object ToEditor(Property property, IDataTypeService dataTypeService)
|
2013-10-27 15:22:04 +01:00
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
return property.GetValue() == null
|
2013-10-27 15:22:04 +01:00
|
|
|
? new JObject[] {}
|
2017-11-07 19:49:14 +01:00
|
|
|
: property.GetValue().ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
|
2013-10-27 15:22:04 +01:00
|
|
|
.Select(x => JObject.FromObject(new {value = x}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|