mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +08:00
[Themed] attribute signals the use Orchard theming w/out BaseViewModel
ThemeFilter detects attribute at action time or TModel at result time LayoutViewEngine uses ThemeFilter.IsApplied the same way AdminFilter.IsApplied is used Themed(false) at an action will override [Themed(true)] or [Themed] at a controller level --HG-- branch : dev
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.DevTools.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.Themes;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.DevTools.Controllers {
|
||||
//[Themed]
|
||||
[Themed]
|
||||
public class HomeController : Controller {
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public HomeController(INotifier notifier) {
|
||||
_notifier = notifier;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult Index() {
|
||||
return View(new BaseViewModel());
|
||||
}
|
||||
@@ -28,5 +34,15 @@ namespace Orchard.DevTools.Controllers {
|
||||
public ActionResult _RenderableAction() {
|
||||
return PartialView("_RenderableAction", "This is render action");
|
||||
}
|
||||
|
||||
public ActionResult SimpleMessage() {
|
||||
_notifier.Information(T("Notifier works without BaseViewModel"));
|
||||
return RedirectToAction("Simple");
|
||||
}
|
||||
|
||||
[Themed(false)]
|
||||
public ActionResult SimpleNoTheme() {
|
||||
return View("Simple", new Simple { Title = "This is not themed", Quantity = 5 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,3 +8,5 @@
|
||||
<%= Model.Quantity %></p>
|
||||
<div style="border: solid 1px #ccc;">
|
||||
<% Html.RenderAction("_RenderableAction"); %></div>
|
||||
<p>
|
||||
<%=Html.ActionLink("Test Messages", "SimpleMessage")%></p>
|
||||
|
@@ -47,6 +47,7 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
builder.Register<MvcModule>().As<IModule>().ContainerScoped();
|
||||
builder.Register<WebFormsViewEngineProvider>().As<IViewEngineProvider>().ContainerScoped();
|
||||
builder.Register<ViewEngineFilter>().As<IFilterProvider>().ContainerScoped();
|
||||
builder.Register<ThemeFilter>().As<IFilterProvider>().ContainerScoped();
|
||||
builder.Register<PageTitleBuilder>().As<IPageTitleBuilder>().ContainerScoped();
|
||||
builder.Register<ZoneManager>().As<IZoneManager>().ContainerScoped();
|
||||
builder.Register<PageClassBuilder>().As<IPageClassBuilder>().ContainerScoped();
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.Themes;
|
||||
|
||||
namespace Orchard.Mvc.ViewEngines {
|
||||
public class LayoutViewEngine : IViewEngine {
|
||||
@@ -24,6 +25,8 @@ namespace Orchard.Mvc.ViewEngines {
|
||||
var skipLayoutViewEngine = false;
|
||||
if (string.IsNullOrEmpty(masterName) == false)
|
||||
skipLayoutViewEngine = true;
|
||||
if (!ThemeFilter.IsApplied(controllerContext.RequestContext))
|
||||
skipLayoutViewEngine = true;
|
||||
if (_viewEngines == null || _viewEngines.Count == 0)
|
||||
skipLayoutViewEngine = true;
|
||||
if (skipLayoutViewEngine)
|
||||
|
@@ -225,6 +225,7 @@
|
||||
<Compile Include="Extensions\ExtensionDescriptor.cs" />
|
||||
<Compile Include="Extensions\ExtensionEntry.cs" />
|
||||
<Compile Include="IOrchardServices.cs" />
|
||||
<Compile Include="Themes\ThemeFilter.cs" />
|
||||
<Compile Include="Themes\ThemedAttribute.cs" />
|
||||
<Compile Include="UI\Admin\AdminAttribute.cs" />
|
||||
<Compile Include="UI\Admin\AdminFilter.cs" />
|
||||
|
55
src/Orchard/Themes/ThemeFilter.cs
Normal file
55
src/Orchard/Themes/ThemeFilter.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Themes {
|
||||
public class ThemeFilter : FilterProvider, IActionFilter, IResultFilter {
|
||||
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext filterContext) {
|
||||
var attribute = GetThemedAttribute(filterContext.ActionDescriptor);
|
||||
if (attribute != null && attribute.Enabled) {
|
||||
Apply(filterContext.RequestContext);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext filterContext) {
|
||||
}
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
var viewResult = filterContext.Result as ViewResult;
|
||||
if (viewResult == null)
|
||||
return;
|
||||
|
||||
var model = viewResult.ViewData.Model as BaseViewModel;
|
||||
if (model == null)
|
||||
return;
|
||||
|
||||
Apply(filterContext.RequestContext);
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
}
|
||||
|
||||
public static void Apply(RequestContext context) {
|
||||
// the value isn't important
|
||||
context.HttpContext.Items[typeof(ThemeFilter)] = null;
|
||||
}
|
||||
|
||||
public static bool IsApplied(RequestContext context) {
|
||||
return context.HttpContext.Items.Contains(typeof(ThemeFilter));
|
||||
}
|
||||
|
||||
|
||||
private static ThemedAttribute GetThemedAttribute(ActionDescriptor descriptor) {
|
||||
return descriptor.GetCustomAttributes(typeof(ThemedAttribute), true)
|
||||
.Concat(descriptor.ControllerDescriptor.GetCustomAttributes(typeof(ThemedAttribute), true))
|
||||
.OfType<ThemedAttribute>()
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -30,11 +30,11 @@ namespace Orchard.UI.Admin {
|
||||
|
||||
public static void Apply(RequestContext context) {
|
||||
// the value isn't important
|
||||
context.HttpContext.Items[typeof(AdminThemeSelector)] = null;
|
||||
context.HttpContext.Items[typeof(AdminFilter)] = null;
|
||||
}
|
||||
|
||||
public static bool IsApplied(RequestContext context) {
|
||||
return context.HttpContext.Items.Contains(typeof(AdminThemeSelector));
|
||||
return context.HttpContext.Items.Contains(typeof(AdminFilter));
|
||||
}
|
||||
|
||||
private static bool IsAdmin(AuthorizationContext filterContext) {
|
||||
|
Reference in New Issue
Block a user