Files
Umbraco-CMS/src/Umbraco.Web/Search/SearchableTreeCollection.cs

54 lines
2.1 KiB
C#
Raw Normal View History

2018-12-19 16:00:29 +01:00
using System;
using System.Collections.Generic;
2017-09-08 19:39:13 +02:00
using System.Collections.ObjectModel;
using System.Linq;
2018-12-19 16:00:29 +01:00
using Umbraco.Core;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Services;
using Umbraco.Web.Services;
2017-09-08 19:39:13 +02:00
using Umbraco.Web.Trees;
namespace Umbraco.Web.Search
{
2017-09-13 17:35:20 +02:00
public class SearchableTreeCollection : BuilderCollectionBase<ISearchableTree>
2017-09-08 19:39:13 +02:00
{
private readonly Dictionary<string, SearchableApplicationTree> _dictionary;
public SearchableTreeCollection(IEnumerable<ISearchableTree> items, ITreeService treeService)
2017-09-08 19:39:13 +02:00
: base(items)
{
_dictionary = CreateDictionary(treeService);
}
private Dictionary<string, SearchableApplicationTree> CreateDictionary(ITreeService treeService)
2017-09-08 19:39:13 +02:00
{
var appTrees = treeService.GetAll()
.OrderBy(x => x.SortOrder)
.ToArray();
2018-12-19 16:00:29 +01:00
var dictionary = new Dictionary<string, SearchableApplicationTree>(StringComparer.OrdinalIgnoreCase);
2017-09-08 19:39:13 +02:00
var searchableTrees = this.ToArray();
foreach (var appTree in appTrees)
2017-09-08 19:39:13 +02:00
{
var found = searchableTrees.FirstOrDefault(x => x.TreeAlias.InvariantEquals(appTree.TreeAlias));
2017-09-08 19:39:13 +02:00
if (found != null)
{
var searchableTreeAttribute = found.GetType().GetCustomAttribute<SearchableTreeAttribute>(false);
dictionary[found.TreeAlias] = new SearchableApplicationTree(
appTree.SectionAlias,
appTree.TreeAlias,
searchableTreeAttribute?.SortOrder ?? SearchableTreeAttribute.DefaultSortOrder,
searchableTreeAttribute?.ServiceName ?? string.Empty,
searchableTreeAttribute?.MethodName ?? string.Empty,
found
);
2017-09-08 19:39:13 +02:00
}
}
return dictionary;
}
2018-11-30 12:22:23 +11:00
public IReadOnlyDictionary<string, SearchableApplicationTree> SearchableApplicationTrees => _dictionary;
2017-09-08 19:39:13 +02:00
public SearchableApplicationTree this[string key] => _dictionary[key];
}
2017-09-23 10:08:18 +02:00
}