Browse Source

Introduce ThemeSelector

pull/301/head
Halil İbrahim Kalkan 9 years ago
parent
commit
8ea9c31bee
  1. 43
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs
  2. 37
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeSelector.cs
  3. 7
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/IThemeSelector.cs

43
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs

@ -1,52 +1,41 @@
using System; using System;
using System.Linq; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming 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(); public ITheme CurrentTheme => GetCurrentTheme();
protected ThemingOptions Options { get; } protected IThemeSelector ThemeSelector { get; }
protected IHttpContextAccessor HttpContextAccessor { get; }
public DefaultThemeManager( public DefaultThemeManager(
IOptions<ThemingOptions> options, IServiceProvider serviceProvider,
IServiceProvider serviceProvider) IThemeSelector themeSelector,
IHttpContextAccessor httpContextAccessor)
{ {
HttpContextAccessor = httpContextAccessor;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
Options = options.Value; ThemeSelector = themeSelector;
} }
protected virtual ITheme GetCurrentTheme() protected virtual ITheme GetCurrentTheme()
{ {
var themeInfo = GetCurrentThemeInfo(); var preSelectedTheme = HttpContextAccessor.HttpContext.Items[CurrentThemeHttpContextKey] as ITheme;
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 themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName); if (preSelectedTheme == null)
if (themeInfo == 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;
} }
} }
} }

37
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeSelector.cs

@ -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;
}
}
}

7
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/IThemeSelector.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Theming
{
public interface IThemeSelector
{
ThemeInfo GetCurrentThemeInfo();
}
}
Loading…
Cancel
Save