/
IconExplorer.cs
198 lines (172 loc) · 4.46 KB
/
IconExplorer.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using SmartStore.Collections;
using SmartStore.Core;
using SmartStore.Core.Logging;
using SmartStore.Utilities;
namespace SmartStore.Web.Framework.UI
{
public interface IIconExplorer
{
ICollection<IconDescription> All { get; }
Multimap<string, string> SearchMap { get; }
IconDescription GetIconByName(string name);
IEnumerable<IconDescription> FindIcons(string searchTerm, bool relaxed = false);
}
public class IconExplorer : IIconExplorer
{
private IDictionary<string, IconDescription> _icons;
private readonly Multimap<string, string> _searchMap = new Multimap<string, string>(StringComparer.OrdinalIgnoreCase, x => new HashSet<string>(x));
private readonly object _lock = new object();
private readonly IApplicationEnvironment _env;
public IconExplorer(IApplicationEnvironment env)
{
_env = env;
Logger = NullLogger.Instance;
}
public ILogger Logger
{
get;
set;
}
private void EnsureIsLoaded()
{
if (_icons == null)
{
lock (_lock)
{
if (_icons == null)
{
try
{
_icons = LoadIconsMetadata();
}
catch (Exception ex)
{
Logger.Error(ex);
_icons = new Dictionary<string, IconDescription>(StringComparer.OrdinalIgnoreCase);
}
foreach (var kvp in _icons)
{
var key = kvp.Key;
var value = kvp.Value;
value.Name = key;
// Styles
var styles = value.Styles;
if (styles != null)
{
if (styles.Length == 1 && styles[0] == "brands")
{
value.IsBrandIcon = true;
}
else if (styles.Contains("regular"))
{
value.HasRegularStyle = true;
}
}
if (value.SearchInfo?.Terms?.Length > 0)
{
foreach (var term in value.SearchInfo.Terms)
{
_searchMap.Add(term, key);
}
}
}
}
}
}
}
private Dictionary<string, IconDescription> LoadIconsMetadata()
{
var fi = new FileInfo(_env.AppDataFolder.MapPath("icons.json"));
if (!fi.Exists)
{
throw new FileNotFoundException("Icons metadata file does not exist.", fi.FullName);
}
// (Perf) look up minified version of metadata file
var hashCode = HashCodeCombiner.Start().Add(fi, false).CombinedHashString;
var fiMin = new FileInfo(Path.Combine(FileSystemHelper.TempDir(), "icons.{0}.json".FormatInvariant(hashCode)));
var path = fiMin.Exists ? fiMin.FullName : fi.FullName;
var json = File.ReadAllText(path, Encoding.GetEncoding(1252));
var icons = JsonConvert.DeserializeObject<Dictionary<string, IconDescription>>(json);
if (!fiMin.Exists)
{
// minified file did not exist: save it.
var settings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, Formatting = Formatting.None };
json = JsonConvert.SerializeObject(icons, settings);
File.WriteAllText(fiMin.FullName, json, Encoding.GetEncoding(1252));
}
return icons;
}
public ICollection<IconDescription> All
{
get
{
EnsureIsLoaded();
return _icons.Values;
}
}
public Multimap<string, string> SearchMap
{
get
{
EnsureIsLoaded();
return _searchMap;
}
}
public IconDescription GetIconByName(string name)
{
Guard.NotEmpty(name, nameof(name));
EnsureIsLoaded();
if (!_icons.TryGetValue(name, out var description))
{
description = new IconDescription
{
IsPro = true,
Name = name,
Label = name,
Styles = new[] { "solid", "regular", "light" },
HasRegularStyle = true,
};
}
return description;
}
public IEnumerable<IconDescription> FindIcons(string searchTerm, bool relaxed = false)
{
Guard.NotEmpty(searchTerm, nameof(searchTerm));
EnsureIsLoaded();
var hasExactMatch = false;
if (_icons.TryGetValue(searchTerm, out var description))
{
hasExactMatch = true;
yield return description;
}
if (_searchMap.ContainsKey(searchTerm))
{
var names = _searchMap[searchTerm];
foreach (var name in names)
{
if (_icons.TryGetValue(name, out var description2))
{
hasExactMatch = true;
yield return description2;
}
}
}
if (relaxed && !hasExactMatch)
{
foreach (var kvp in _icons)
{
if (kvp.Key.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) > -1)
{
yield return kvp.Value;
}
}
}
}
}
}