Added default theme option

pull/301/head
Halil İbrahim Kalkan 7 years ago
parent ad91d53cf5
commit 68e44f5387

@ -18,6 +18,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic
services.Configure<ThemingOptions>(options =>
{
options.Themes.Add<BasicTheme>();
if (options.DefaultThemeName == null)
{
options.DefaultThemeName = BasicTheme.Name;
}
});
services.Configure<VirtualFileSystemOptions>(options =>

@ -3,8 +3,11 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic
{
[ThemeName(Name)]
public class BasicTheme : ITheme, ITransientDependency
{
public const string Name = "Basic";
public string DefaultLayout => "~/Views/Shared/_AppLayout.cshtml";
public string GetLayoutOrNull(string name)

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
@ -16,39 +14,39 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
protected ThemingOptions Options { get; }
protected Lazy<List<ITheme>> Themes { get; }
private readonly IHttpContextAccessor _httpContextAccessor;
public DefaultThemeManager(
IOptions<ThemingOptions> options,
IServiceProvider serviceProvider,
IHttpContextAccessor httpContextAccessor)
IServiceProvider serviceProvider)
{
_httpContextAccessor = httpContextAccessor;
ServiceProvider = serviceProvider;
Options = options.Value;
Themes = new Lazy<List<ITheme>>(CreateThemeList, true);
}
protected virtual ITheme GetCurrentTheme()
{
return Themes.Value.First();
////TODO: Temporary code!
//var currentTheme = _httpContextAccessor.HttpContext.Items["_AbpCurrentTheme"] as ITheme;
//if (currentTheme != null)
//{
// return currentTheme;
//}
//_httpContextAccessor.HttpContext.Items["_AbpCurrentTheme"] = currentTheme = RandomHelper.GetRandomOf(Themes.Value.ToArray());
//return currentTheme;
var themeInfo = GetCurrentThemeInfo();
return (ITheme) ServiceProvider.GetRequiredService(themeInfo.ThemeType);
}
protected virtual List<ITheme> CreateThemeList()
protected virtual ThemeInfo GetCurrentThemeInfo()
{
return Options.Themes.Select(t => (ITheme) ServiceProvider.GetRequiredService(t)).ToList();
if (!Options.Themes.Any())
{
throw new AbpException($"No theme registered! Use {nameof(ThemingOptions)} to register themes.");
}
if (Options.DefaultThemeName == null)
{
return Options.Themes.Values.First();
}
var themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName);
if (themeInfo == null)
{
throw new AbpException("Default theme is configured but it's not found in the registered themes: " + Options.DefaultThemeName);
}
return themeInfo;
}
}
}

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
{
public class ThemeDictionary : Dictionary<Type, ThemeInfo>
{
public ThemeInfo Add<TTheme>()
where TTheme : ITheme
{
return Add(typeof(TTheme));
}
public ThemeInfo Add(Type themeType)
{
if (ContainsKey(themeType))
{
throw new AbpException("This theme is already added before: " + themeType.AssemblyQualifiedName);
}
return this[themeType] = new ThemeInfo(themeType);
}
}
}

@ -0,0 +1,25 @@
using System;
using JetBrains.Annotations;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
{
public class ThemeInfo
{
public Type ThemeType { get; }
public string Name { get; }
public ThemeInfo([NotNull] Type themeType)
{
Check.NotNull(themeType, nameof(themeType));
if (!typeof(ITheme).IsAssignableFrom(themeType))
{
throw new AbpException($"Given {nameof(themeType)} ({themeType.AssemblyQualifiedName}) should be type of {typeof(ITheme).AssemblyQualifiedName}");
}
ThemeType = themeType;
Name = ThemeNameAttribute.GetName(themeType);
}
}
}

@ -0,0 +1,24 @@
using System;
using System.Linq;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
{
[AttributeUsage(AttributeTargets.Class)]
public class ThemeNameAttribute : Attribute
{
public string Name { get; set; }
public ThemeNameAttribute(string name)
{
Name = name;
}
public static string GetName(Type themeType)
{
return themeType
.GetCustomAttributes(true)
.OfType<ThemeNameAttribute>()
.FirstOrDefault()?.Name ?? themeType.Name;
}
}
}

@ -1,14 +1,14 @@
using Volo.Abp.Collections;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
{
public class ThemingOptions
{
public TypeList<ITheme> Themes { get; }
public ThemeDictionary Themes { get; }
public string DefaultThemeName { get; set; }
public ThemingOptions()
{
Themes = new TypeList<ITheme>();
Themes = new ThemeDictionary();
}
}
}

Loading…
Cancel
Save