This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PropertyListPropertyValueEditor.cs
178 lines (142 loc) · 8.14 KB
/
PropertyListPropertyValueEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Our.Umbraco.PropertyList.Models;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.PropertyEditors;
using static Our.Umbraco.PropertyList.PropertyListConstants;
namespace Our.Umbraco.PropertyList.PropertyEditors
{
internal class PropertyListPropertyValueEditor : PropertyValueEditorWrapper
{
public PropertyListPropertyValueEditor(PropertyValueEditor wrapped)
: base(wrapped)
{
Validators.Add(new PropertyListValidator());
}
public override void ConfigureForDisplay(PreValueCollection preValues)
{
base.ConfigureForDisplay(preValues);
if (preValues.PreValuesAsDictionary.ContainsKey(PreValueKeys.HideLabel))
{
HideLabel = preValues.PreValuesAsDictionary[PreValueKeys.HideLabel].Value == "1";
}
}
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var propertyValue = property?.Value?.ToString();
if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue))
return base.ConvertDbToEditor(property, propertyType, dataTypeService);
// We'd like this to be hot-swappable with "Repeatable Textstrings" property-editor.
// We make the assumption that if the value isn't JSON, then it could be newline-delimited list
// let's split/join the values and get it into a proxy JSON string.
if (propertyValue.DetectIsJson() == false)
{
var items = string.Join("', '", propertyValue.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));
propertyValue = $"{{ dtd: '{DataTypeGuids.DefaultTextstring}', values: [ '{items}' ] }}";
}
var model = JsonConvert.DeserializeObject<PropertyListValue>(propertyValue);
if (model == null || model.DataTypeGuid.Equals(Guid.Empty) || model.Values == null)
return base.ConvertDbToEditor(property, propertyType, dataTypeService);
// Get the associated datatype definition
var dtd = dataTypeService.GetDataTypeDefinitionById(model.DataTypeGuid);
if (dtd == null)
return base.ConvertDbToEditor(property, propertyType, dataTypeService);
// Lookup the property editor
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
if (propEditor == null)
return base.ConvertDbToEditor(property, propertyType, dataTypeService);
var propType = new PropertyType(dtd, propertyType.Alias);
for (var i = 0; i < model.Values.Count; i++)
{
var prop = new Property(propType, model.Values[i]);
var newValue = propEditor.ValueEditor.ConvertDbToEditor(prop, propType, dataTypeService);
model.Values[i] = (newValue == null) ? null : JToken.FromObject(newValue);
}
// Return the strongly-typed object, Umbraco will handle the JSON serializing/parsing, then Angular can handle it directly
return model;
}
public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var propertyValue = property?.Value?.ToString();
if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue))
return base.ConvertDbToString(property, propertyType, dataTypeService);
var model = JsonConvert.DeserializeObject<PropertyListValue>(propertyValue);
if (model == null || model.DataTypeGuid.Equals(Guid.Empty) || model.Values == null)
return base.ConvertDbToString(property, propertyType, dataTypeService);
var dtd = dataTypeService.GetDataTypeDefinitionById(model.DataTypeGuid);
if (dtd == null)
return base.ConvertDbToString(property, propertyType, dataTypeService);
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
if (propEditor == null)
return base.ConvertDbToString(property, propertyType, dataTypeService);
var propType = new PropertyType(dtd, propertyType.Alias);
for (var i = 0; i < model.Values.Count; i++)
{
var prop = new Property(propType, model.Values[i]);
var newValue = propEditor.ValueEditor.ConvertDbToString(prop, propType, dataTypeService);
model.Values[i] = newValue;
}
return JsonConvert.SerializeObject(model);
}
public override XNode ConvertDbToXml(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var propertyValue = property?.Value?.ToString();
if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue))
return base.ConvertDbToXml(property, propertyType, dataTypeService);
var model = JsonConvert.DeserializeObject<PropertyListValue>(propertyValue);
if (model == null || model.DataTypeGuid.Equals(Guid.Empty) || model.Values == null)
return base.ConvertDbToXml(property, propertyType, dataTypeService);
var dtd = dataTypeService.GetDataTypeDefinitionById(model.DataTypeGuid);
if (dtd == null)
return base.ConvertDbToXml(property, propertyType, dataTypeService);
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
if (propEditor == null)
return base.ConvertDbToXml(property, propertyType, dataTypeService);
var propType = new PropertyType(dtd, propertyType.Alias);
for (var i = 0; i < model.Values.Count; i++)
{
var prop = new Property(propType, model.Values[i]);
var newValue = propEditor.ValueEditor.ConvertDbToXml(prop, propType, dataTypeService);
model.Values[i] = newValue;
}
return new XElement("values",
new XAttribute("dtd", model.DataTypeGuid),
model.Values.Select(x => new XElement("value", x)));
}
public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue)
{
var value = editorValue?.Value?.ToString();
if (value == null || string.IsNullOrWhiteSpace(value))
return base.ConvertEditorToDb(editorValue, currentValue);
var model = JsonConvert.DeserializeObject<PropertyListValue>(value);
if (model == null || model.DataTypeGuid.Equals(Guid.Empty) || model.Values == null)
return base.ConvertEditorToDb(editorValue, currentValue);
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
var dtd = dataTypeService.GetDataTypeDefinitionById(model.DataTypeGuid);
if (dtd == null)
return base.ConvertEditorToDb(editorValue, currentValue);
var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);
if (preValues == null)
return base.ConvertEditorToDb(editorValue, currentValue);
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
if (propEditor == null)
return base.ConvertEditorToDb(editorValue, currentValue);
for (var i = 0; i < model.Values.Count; i++)
{
var obj = model.Values[i];
var propData = new ContentPropertyData(obj, preValues, new Dictionary<string, object>());
var newValue = propEditor.ValueEditor.ConvertEditorToDb(propData, obj);
model.Values[i] = (newValue == null) ? null : JToken.FromObject(newValue);
}
return JsonConvert.SerializeObject(model);
}
}
}