/
JsonDataSource.cs
117 lines (97 loc) · 4.38 KB
/
JsonDataSource.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
namespace nuPickers.Shared.JsonDataSource
{
using DataSource;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using nuPickers.Shared.Editor;
using Paging;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Logging;
public class JsonDataSource : IDataSource
{
public string Url { get; set; }
public string JsonPath { get; set; }
public string KeyJsonPath { get; set; }
public string LabelJsonPath { get; set; }
bool IDataSource.HandledTypeahead { get { return false; } }
IEnumerable<EditorDataItem> IDataSource.GetEditorDataItems(int currentId, int parentId, string typeahead)
{
return this.GetEditorDataItems(currentId);
}
IEnumerable<EditorDataItem> IDataSource.GetEditorDataItems(int currentId, int parentId, string[] keys)
{
return this.GetEditorDataItems(currentId).Where(x => keys.Contains(x.Key));
}
IEnumerable<EditorDataItem> IDataSource.GetEditorDataItems(int currentId, int parentId, PageMarker pageMarker, out int total)
{
var editorDataItems = this.GetEditorDataItems(currentId);
total = editorDataItems.Count();
return editorDataItems.Skip(pageMarker.Skip).Take(pageMarker.Take);
}
/// <summary>
/// Main method for retrieving nuPicker data items.
/// </summary>
/// <param name="contextId">Current context node Id</param>
/// <returns>List of items for displaying inside a nuPicker JSON data type.</returns>
private IEnumerable<EditorDataItem> GetEditorDataItems(int contextId)
{
List<EditorDataItem> editorDataItems = new List<EditorDataItem>(); // prepare return value
var url = this.Url.Replace("@contextId", contextId.ToString());
JToken jToken = null; // object representation of all json source data
try
{
jToken = JToken.Parse(Helper.GetDataFromUrl(url));
}
catch (JsonException jsonException)
{
LogHelper.Warn(typeof(JsonDataSource), jsonException.Message + " (Check JSON at Url: " + url + ")");
}
if (jToken != null)
{
// check for: [ 'a', 'b', 'c' ] and create editor items without using any JsonPath
if (jToken is JArray && jToken.ToObject<object[]>().All(x => x is string))
{
editorDataItems = jToken.ToObject<string[]>()
.Select(x => new EditorDataItem()
{
Key = x,
Label = x
})
.ToList();
}
else // use JsonPath
{
IEnumerable<JToken> jTokens = null;
try
{
jTokens = jToken.SelectTokens(this.JsonPath);
}
catch (JsonException jsonException)
{
LogHelper.Warn(typeof(JsonDataSource), jsonException.Message + " (Check JSONPath: " + this.JsonPath + ")");
}
if (jTokens != null)
{
foreach(JToken jsonPathToken in jTokens.Where(x => x is JObject && x.HasValues))
{
JToken keyToken = jsonPathToken.SelectToken(this.KeyJsonPath);
JToken labelToken = jsonPathToken.SelectToken(this.LabelJsonPath);
if (keyToken != null && labelToken != null)
{
editorDataItems.Add(
new EditorDataItem()
{
Key = keyToken.ToString(),
Label =labelToken.ToString()
});
}
}
}
}
}
// TODO: distinct on editor data item keys
return editorDataItems;
}
}
}