mirror of https://github.com/abpframework/abp.git
3 changed files with 60 additions and 27 deletions
@ -1,52 +1,41 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming |
|||
{ |
|||
public class DefaultThemeManager : IThemeManager, ITransientDependency, IServiceProviderAccessor |
|||
public class DefaultThemeManager : IThemeManager, IScopedDependency, IServiceProviderAccessor |
|||
{ |
|||
public IServiceProvider ServiceProvider { get; } |
|||
private const string CurrentThemeHttpContextKey = "__AbpCurrentTheme"; |
|||
|
|||
public IServiceProvider ServiceProvider { get; } |
|||
public ITheme CurrentTheme => GetCurrentTheme(); |
|||
|
|||
protected ThemingOptions Options { get; } |
|||
protected IThemeSelector ThemeSelector { get; } |
|||
protected IHttpContextAccessor HttpContextAccessor { get; } |
|||
|
|||
public DefaultThemeManager( |
|||
IOptions<ThemingOptions> options, |
|||
IServiceProvider serviceProvider) |
|||
IServiceProvider serviceProvider, |
|||
IThemeSelector themeSelector, |
|||
IHttpContextAccessor httpContextAccessor) |
|||
{ |
|||
HttpContextAccessor = httpContextAccessor; |
|||
ServiceProvider = serviceProvider; |
|||
Options = options.Value; |
|||
ThemeSelector = themeSelector; |
|||
} |
|||
|
|||
protected virtual ITheme GetCurrentTheme() |
|||
{ |
|||
var themeInfo = GetCurrentThemeInfo(); |
|||
return (ITheme) ServiceProvider.GetRequiredService(themeInfo.ThemeType); |
|||
} |
|||
|
|||
protected virtual ThemeInfo GetCurrentThemeInfo() |
|||
{ |
|||
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 preSelectedTheme = HttpContextAccessor.HttpContext.Items[CurrentThemeHttpContextKey] as ITheme; |
|||
|
|||
var themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName); |
|||
if (themeInfo == null) |
|||
if (preSelectedTheme == null) |
|||
{ |
|||
throw new AbpException("Default theme is configured but it's not found in the registered themes: " + Options.DefaultThemeName); |
|||
preSelectedTheme = (ITheme)ServiceProvider.GetRequiredService(ThemeSelector.GetCurrentThemeInfo().ThemeType); |
|||
HttpContextAccessor.HttpContext.Items[CurrentThemeHttpContextKey] = preSelectedTheme; |
|||
} |
|||
|
|||
return themeInfo; |
|||
return preSelectedTheme; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System.Linq; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming |
|||
{ |
|||
public class DefaultThemeSelector : IThemeSelector, ITransientDependency |
|||
{ |
|||
protected ThemingOptions Options { get; } |
|||
|
|||
public DefaultThemeSelector(IOptions<ThemingOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual ThemeInfo GetCurrentThemeInfo() |
|||
{ |
|||
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,7 @@ |
|||
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming |
|||
{ |
|||
public interface IThemeSelector |
|||
{ |
|||
ThemeInfo GetCurrentThemeInfo(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue