Files
Umbraco-CMS/src/Umbraco.Web.BackOffice/Trees/TreeController.cs

65 lines
2.4 KiB
C#

using System;
using System.Collections.Concurrent;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Trees;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.BackOffice.Trees
{
/// <summary>
/// The base controller for all tree requests
/// </summary>
public abstract class TreeController : TreeControllerBase
{
private static readonly ConcurrentDictionary<Type, TreeAttribute> _treeAttributeCache = new ConcurrentDictionary<Type, TreeAttribute>();
private readonly TreeAttribute _treeAttribute;
protected ILocalizedTextService LocalizedTextService { get; }
protected TreeController(ILocalizedTextService localizedTextService, UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, IEventAggregator eventAggregator)
: base(umbracoApiControllerTypeCollection, eventAggregator)
{
LocalizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
_treeAttribute = GetTreeAttribute();
}
/// <inheritdoc />
public override string RootNodeDisplayName => Tree.GetRootNodeDisplayName(this, LocalizedTextService);
/// <inheritdoc />
public override string TreeGroup => _treeAttribute.TreeGroup;
/// <inheritdoc />
public override string TreeAlias => _treeAttribute.TreeAlias;
/// <inheritdoc />
public override string TreeTitle => _treeAttribute.TreeTitle;
/// <inheritdoc />
public override TreeUse TreeUse => _treeAttribute.TreeUse;
/// <inheritdoc />
public override string SectionAlias => _treeAttribute.SectionAlias;
/// <inheritdoc />
public override int SortOrder => _treeAttribute.SortOrder;
/// <inheritdoc />
public override bool IsSingleNodeTree => _treeAttribute.IsSingleNodeTree;
private TreeAttribute GetTreeAttribute()
{
return _treeAttributeCache.GetOrAdd(GetType(), type =>
{
var treeAttribute = type.GetCustomAttribute<TreeAttribute>(false);
if (treeAttribute == null)
throw new InvalidOperationException("The Tree controller is missing the " + typeof(TreeAttribute).FullName + " attribute");
return treeAttribute;
});
}
}
}