diff --git a/src/Orchard/Mvc/Html/ContainerExtensions.cs b/src/Orchard/Mvc/Html/ContainerExtensions.cs new file mode 100644 index 000000000..4fc0bff13 --- /dev/null +++ b/src/Orchard/Mvc/Html/ContainerExtensions.cs @@ -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(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(); + } + } +} \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/LayoutExtensions.cs b/src/Orchard/Mvc/Html/LayoutExtensions.cs index 937d960d8..adaa64208 100644 --- a/src/Orchard/Mvc/Html/LayoutExtensions.cs +++ b/src/Orchard/Mvc/Html/LayoutExtensions.cs @@ -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().RegisterStyle(fileName); + } + + public static void RegisterScript(this HtmlHelper html, string fileName) { + html.Resolve().RegisterHeadScript(fileName); } } } \ No newline at end of file diff --git a/src/Orchard/Orchard.csproj b/src/Orchard/Orchard.csproj index 3921636e1..7ffa64a75 100644 --- a/src/Orchard/Orchard.csproj +++ b/src/Orchard/Orchard.csproj @@ -197,6 +197,7 @@ + @@ -222,6 +223,9 @@ + + + diff --git a/src/Orchard/UI/Resources/IResourceManager.cs b/src/Orchard/UI/Resources/IResourceManager.cs new file mode 100644 index 000000000..16067cb01 --- /dev/null +++ b/src/Orchard/UI/Resources/IResourceManager.cs @@ -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(); + } +} \ No newline at end of file diff --git a/src/Orchard/UI/Resources/ResourceFilter.cs b/src/Orchard/UI/Resources/ResourceFilter.cs new file mode 100644 index 000000000..fb278495c --- /dev/null +++ b/src/Orchard/UI/Resources/ResourceFilter.cs @@ -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) { + } + } +} \ No newline at end of file diff --git a/src/Orchard/UI/Resources/ResourceManager.cs b/src/Orchard/UI/Resources/ResourceManager.cs new file mode 100644 index 000000000..0a0b1a8ce --- /dev/null +++ b/src/Orchard/UI/Resources/ResourceManager.cs @@ -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"; + private const string ScriptFormat = "\r\n"; + private readonly List _styles; + private readonly List _headScripts; + private readonly List _footScripts; + + public ResourceManager() { + _styles = new List(); + _headScripts = new List(); + _footScripts = new List(); + } + + 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 files, string fileFormat) { + return + MvcHtmlString.Create(string.Join("\r\n", + files.Select(s => string.Format(fileFormat, s)).ToArray())); + } + } +} \ No newline at end of file