Skip to content

Commit

Permalink
AJAXifying FontAwesomeIconChooser (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcakir committed Apr 19, 2019
1 parent 693e8e9 commit f42b147
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Presentation/SmartStore.Web.Framework/UI/Icons/IconExplorer.cs
Expand Up @@ -16,7 +16,7 @@ public interface IIconExplorer
ICollection<IconDescription> All { get; }
Multimap<string, string> SearchMap { get; }
IconDescription GetIconByName(string name);
IEnumerable<IconDescription> FindIcons(string searchTerm);
IEnumerable<IconDescription> FindIcons(string searchTerm, bool relaxed = false);
}

public class IconExplorer : IIconExplorer
Expand Down Expand Up @@ -157,13 +157,16 @@ public IconDescription GetIconByName(string name)
return description;
}

public IEnumerable<IconDescription> FindIcons(string searchTerm)
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;
}

Expand All @@ -174,10 +177,22 @@ public IEnumerable<IconDescription> FindIcons(string searchTerm)
{
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;
}
}
}
}
}
}
Expand Up @@ -68,6 +68,7 @@ public class CommonController : AdminControllerBase
private readonly ILocalizationService _localizationService;
private readonly Lazy<IImageCache> _imageCache;
private readonly Lazy<IImportProfileService> _importProfileService;
private readonly Lazy<IIconExplorer> _iconExplorer;
private readonly IGenericAttributeService _genericAttributeService;
private readonly IDbCache _dbCache;
private readonly ITaskScheduler _taskScheduler;
Expand All @@ -87,6 +88,7 @@ public class CommonController : AdminControllerBase
ILocalizationService localizationService,
Lazy<IImageCache> imageCache,
Lazy<IImportProfileService> importProfileService,
Lazy<IIconExplorer> iconExplorer,
IGenericAttributeService genericAttributeService,
IDbCache dbCache,
ITaskScheduler taskScheduler,
Expand All @@ -105,6 +107,7 @@ public class CommonController : AdminControllerBase
_localizationService = localizationService;
_imageCache = imageCache;
_importProfileService = importProfileService;
_iconExplorer = iconExplorer;
_genericAttributeService = genericAttributeService;
_dbCache = dbCache;
_taskScheduler = taskScheduler;
Expand Down Expand Up @@ -267,6 +270,35 @@ private bool InstallablePackageExists()

#region UI Helpers

[HttpPost]
public JsonResult SearchIcons(string term, int page = 1)
{
var icons = _iconExplorer.Value.All.AsEnumerable();

if (term.HasValue())
{
icons = _iconExplorer.Value.FindIcons(term, true);
}

var result = new PagedList<IconDescription>(icons, page - 1, 250);

return Json(new
{
results = result.Select(x => new
{
id = x.Name,
text = x.Name,
name = x.Name,
hasRegularStyle = x.HasRegularStyle,
isBrandIcon = x.IsBrandIcon,
isPro = x.IsPro,
label = x.Label,
styles = x.Styles
}),
pagination = new { more = result.HasNextPage }
});
}

[HttpPost]
public JsonResult SetSelectedTab(string navId, string tabId, string path)
{
Expand Down

0 comments on commit f42b147

Please sign in to comment.