Continuing theme work...

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044191
This commit is contained in:
ErikPorter
2009-12-16 23:23:31 +00:00
parent 1c5daae737
commit 51bc29d464
6 changed files with 119 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
using System;
using System.Web.Mvc;
using Autofac.Integration.Web;
namespace Orchard.Mvc.Html {
public static class ContainerExtensions {
public static TService Resolve<TService>(this HtmlHelper html) {
var containerProvider = html.ViewContext.RouteData.DataTokens["IContainerProvider"] as IContainerProvider;
if (containerProvider == null)
throw new ApplicationException("Unable to resolve");
return containerProvider.RequestContainer.Resolve<TService>();
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Web.Mvc;
using Orchard.Mvc.ViewEngines;
using Orchard.Mvc.ViewModels;
using Orchard.UI.Resources;
using Orchard.UI.Zones;
namespace Orchard.Mvc.Html {
@@ -46,8 +47,12 @@ namespace Orchard.Mvc.Html {
html.Zone(zoneName, () => html.RenderBody());
}
public static void RegisterStyle(this HtmlHelper html, string styleName) {
//todo: register the style
public static void RegisterStyle(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterStyle(fileName);
}
public static void RegisterScript(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterHeadScript(fileName);
}
}
}

View File

@@ -197,6 +197,7 @@
<Compile Include="Models\ViewModels\ItemDisplayModel.cs" />
<Compile Include="Models\ViewModels\ItemEditorModel.cs" />
<Compile Include="Mvc\Filters\AntiForgeryAuthorizationFilter.cs" />
<Compile Include="Mvc\Html\ContainerExtensions.cs" />
<Compile Include="Mvc\Html\ContentItemExtensions.cs" />
<Compile Include="Mvc\Html\ItemDisplayExtensions.cs" />
<Compile Include="Mvc\Html\ItemEditorExtensions.cs" />
@@ -222,6 +223,9 @@
<Compile Include="Mvc\ViewEngines\WebFormsViewEngineProvider.cs" />
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
<Compile Include="UI\Resources\IResourceManager.cs" />
<Compile Include="UI\Resources\ResourceFilter.cs" />
<Compile Include="UI\Resources\ResourceManager.cs" />
<Compile Include="UI\Zones\DelegateZoneItem.cs" />
<Compile Include="UI\Zones\ItemDisplayZoneItem.cs" />
<Compile Include="UI\Zones\RenderPartialZoneItem.cs" />

View File

@@ -0,0 +1,12 @@
using System.Web.Mvc;
namespace Orchard.UI.Resources {
public interface IResourceManager : IDependency {
void RegisterStyle(string fileName);
void RegisterHeadScript(string fileName);
void RegisterFootScript(string fileName);
MvcHtmlString GetStyles();
MvcHtmlString GetHeadScripts();
MvcHtmlString GetFootScripts();
}
}

View File

@@ -0,0 +1,26 @@
using System.Web.Mvc;
using Orchard.Mvc.Filters;
using Orchard.Mvc.ViewModels;
namespace Orchard.UI.Resources {
public class ResourceFilter : FilterProvider, IResultFilter {
private readonly IResourceManager _resourceManager;
public ResourceFilter(IResourceManager resourceManager) {
_resourceManager = resourceManager;
}
public void OnResultExecuting(ResultExecutingContext filterContext) {
BaseViewModel model = filterContext.Controller.ViewData.Model as BaseViewModel;
if (model != null) {
model.Zones.AddAction("head:styles", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetStyles()));
model.Zones.AddAction("head:scripts", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetHeadScripts()));
model.Zones.AddAction("body:after", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetFootScripts()));
}
}
public void OnResultExecuted(ResultExecutedContext filterContext) {
}
}
}

View File

@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Orchard.UI.Resources {
public class ResourceManager : IResourceManager {
private const string StyleFormat = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
private const string ScriptFormat = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
private readonly List<string> _styles;
private readonly List<string> _headScripts;
private readonly List<string> _footScripts;
public ResourceManager() {
_styles = new List<string>();
_headScripts = new List<string>();
_footScripts = new List<string>();
}
public void RegisterStyle(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_styles.Contains(fileName))
_styles.Add(fileName);
}
public void RegisterHeadScript(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName))
_headScripts.Add(fileName);
}
public void RegisterFootScript(string fileName) {
//TODO: (erikpo) Figure out best storage here
if (!string.IsNullOrEmpty(fileName) && !_headScripts.Contains(fileName) && !_footScripts.Contains(fileName))
_footScripts.Add(fileName);
}
public MvcHtmlString GetStyles() {
return GetFiles(_styles, StyleFormat);
}
public MvcHtmlString GetHeadScripts() {
return GetFiles(_headScripts, ScriptFormat);
}
public MvcHtmlString GetFootScripts() {
return GetFiles(_footScripts, ScriptFormat);
}
private static MvcHtmlString GetFiles(IEnumerable<string> files, string fileFormat) {
return
MvcHtmlString.Create(string.Join("\r\n",
files.Select(s => string.Format(fileFormat, s)).ToArray()));
}
}
}