From ef03c8f60827a00febcd63186b238ef0a1a97be3 Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Mon, 8 Mar 2010 01:49:53 -0800 Subject: [PATCH] Enabling Html.RegisterStyle("ie6.css").WithCondition("if lte IE 6").ForMedia("screen, projection"); - Added Condition property to FileRegistrationContext - Added StyleFileRegistrationContext + Media property - Updated Html helper methods for script and style reg to return registered context and added FileRegistrationContext extension methods for WithCondition and ForMedia --HG-- branch : dev --- .hgignore | 1 + .../Themes/SafeMode/Views/Layout.ascx | 9 ++- .../Themes/TheAdmin/Views/Layout.ascx | 4 +- .../Mvc/Html/FileRegistrationContext.cs | 48 +++++++++++---- .../Html/FileRegistrationContextExtensions.cs | 19 ++++++ src/Orchard/Mvc/Html/LayoutExtensions.cs | 12 ++-- .../Mvc/Html/StyleFileRegistrationContext.cs | 11 ++++ src/Orchard/Orchard.csproj | 2 + src/Orchard/UI/Resources/IResourceManager.cs | 7 ++- src/Orchard/UI/Resources/ResourceManager.cs | 59 ++++++++++++++----- 10 files changed, 130 insertions(+), 42 deletions(-) create mode 100644 src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs create mode 100644 src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs diff --git a/.hgignore b/.hgignore index a8351cd36..dc8bbfe68 100644 --- a/.hgignore +++ b/.hgignore @@ -10,3 +10,4 @@ glob:build glob:*.sln.cache glob:src/Orchard.Web/Modules/Orchard.DevTools/Module.txt glob:src/Orchard.Web/Modules/Orchard.Sandbox/Module.txt +glob:src/Orchard.Web/Media/* diff --git a/src/Orchard.Web/Themes/SafeMode/Views/Layout.ascx b/src/Orchard.Web/Themes/SafeMode/Views/Layout.ascx index 93dd3b619..7f587ed6d 100644 --- a/src/Orchard.Web/Themes/SafeMode/Views/Layout.ascx +++ b/src/Orchard.Web/Themes/SafeMode/Views/Layout.ascx @@ -10,11 +10,10 @@ html.ViewContext.Writer.Write(@"")); var siteCss = ResolveUrl("../Styles/site.css"); Model.Zones.AddAction("head:styles", html => - html.ViewContext.Writer.Write(@"")); %> - - + html.ViewContext.Writer.Write(@"")); + var ie6Css = ResolveUrl("../Styles/ie6.css"); + Model.Zones.AddAction("head:styles", html => + html.ViewContext.Writer.Write(@"")); %> diff --git a/src/Orchard.Web/Themes/TheAdmin/Views/Layout.ascx b/src/Orchard.Web/Themes/TheAdmin/Views/Layout.ascx index 915f3a860..64f0876db 100644 --- a/src/Orchard.Web/Themes/TheAdmin/Views/Layout.ascx +++ b/src/Orchard.Web/Themes/TheAdmin/Views/Layout.ascx @@ -2,13 +2,11 @@ <%@ Import Namespace="Orchard.Mvc.ViewModels"%> <%@ Import Namespace="Orchard.Mvc.Html"%><% Html.RegisterStyle("site.css"); +Html.RegisterStyle("ie6.css").WithCondition("if lte IE 6").ForMedia("screen, projection"); Model.Zones.AddRenderPartial("header", "Header", Model); Model.Zones.AddRenderPartial("header:after", "User", Model); // todo: (heskew) should be a user display or widget Model.Zones.AddRenderPartial("menu", "Menu", Model); %> -
diff --git a/src/Orchard/Mvc/Html/FileRegistrationContext.cs b/src/Orchard/Mvc/Html/FileRegistrationContext.cs index 45fa67a0e..a30467fd4 100644 --- a/src/Orchard/Mvc/Html/FileRegistrationContext.cs +++ b/src/Orchard/Mvc/Html/FileRegistrationContext.cs @@ -5,11 +5,11 @@ using System.Web.UI; namespace Orchard.Mvc.Html { public class FileRegistrationContext : RequestContext { - public FileRegistrationContext(ViewContext viewContext, IViewDataContainer viewDataContainer, string fileName) + public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string fileName) : base(viewContext.HttpContext, viewContext.RouteData) { Container = viewDataContainer as TemplateControl; - if (Container != null) + if (Container != null) { ContainerVirtualPath = Container.AppRelativeVirtualPath.Substring( 0, Container.AppRelativeVirtualPath.IndexOf( @@ -17,6 +17,7 @@ namespace Orchard.Mvc.Html { StringComparison.InvariantCultureIgnoreCase ) ); + } FileName = fileName; } @@ -24,21 +25,46 @@ namespace Orchard.Mvc.Html { public TemplateControl Container { get; set; } public string ContainerVirtualPath { get; set; } public string FileName { get; set; } + public string Condition { get; set; } - public override bool Equals(object obj) - { - FileRegistrationContext incoming = obj as FileRegistrationContext; - - return incoming != null && - string.Equals(ContainerVirtualPath, incoming.ContainerVirtualPath, StringComparison.InvariantCultureIgnoreCase) && - string.Equals(FileName, incoming.FileName, StringComparison.InvariantCultureIgnoreCase); + public override bool Equals(object obj) { + if (ReferenceEquals(null, obj)) { + return false; + } + if (ReferenceEquals(this, obj)) { + return true; + } + if (obj.GetType() != typeof (FileRegistrationContext)) { + return false; + } + return Equals((FileRegistrationContext) obj); } internal string GetFilePath(string containerRelativePath) { //todo: (heskew) maybe not here but file paths for non-app locations need to be taken into account return Container != null - ? Container.ResolveUrl(ContainerVirtualPath + containerRelativePath + FileName) - : (ContainerVirtualPath + containerRelativePath + FileName); + ? Container.ResolveUrl(ContainerVirtualPath + containerRelativePath + FileName) + : (ContainerVirtualPath + containerRelativePath + FileName); + } + + public bool Equals(FileRegistrationContext other) { + if (ReferenceEquals(null, other)) { + return false; + } + if (ReferenceEquals(this, other)) { + return true; + } + return Equals(other.Container, Container) && Equals(other.ContainerVirtualPath, ContainerVirtualPath) && Equals(other.FileName, FileName) && Equals(other.Condition, Condition); + } + + public override int GetHashCode() { + unchecked { + var result = (Container != null ? Container.GetHashCode() : 0); + result = (result*397) ^ (ContainerVirtualPath != null ? ContainerVirtualPath.GetHashCode() : 0); + result = (result*397) ^ (FileName != null ? FileName.GetHashCode() : 0); + result = (result*397) ^ (Condition != null ? Condition.GetHashCode() : 0); + return result; + } } } } \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs b/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs new file mode 100644 index 000000000..9063bf55d --- /dev/null +++ b/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs @@ -0,0 +1,19 @@ +namespace Orchard.Mvc.Html { + public static class FileRegistrationContextExtensions { + public static T WithCondition(this T fileRegistrationContext, string condition)where T : FileRegistrationContext { + if (fileRegistrationContext == null) + return null; + + fileRegistrationContext.Condition = condition; + return fileRegistrationContext; + } + + public static T ForMedia(this T styleFileRegistrationContext, string media) where T : StyleFileRegistrationContext { + if (styleFileRegistrationContext == null) + return null; + + styleFileRegistrationContext.Media = media; + return styleFileRegistrationContext; + } + } +} \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/LayoutExtensions.cs b/src/Orchard/Mvc/Html/LayoutExtensions.cs index b7b4bcc96..c9dc48b80 100644 --- a/src/Orchard/Mvc/Html/LayoutExtensions.cs +++ b/src/Orchard/Mvc/Html/LayoutExtensions.cs @@ -98,16 +98,16 @@ namespace Orchard.Mvc.Html { html.Resolve().RegisterLink(entry, html); } - public static void RegisterStyle(this HtmlHelper html, string fileName) { - html.Resolve().RegisterStyle(fileName, html); + public static StyleFileRegistrationContext RegisterStyle(this HtmlHelper html, string fileName) { + return html.Resolve().RegisterStyle(fileName, html); } - public static void RegisterScript(this HtmlHelper html, string fileName) { - html.Resolve().RegisterHeadScript(fileName, html); + public static FileRegistrationContext RegisterScript(this HtmlHelper html, string fileName) { + return html.Resolve().RegisterHeadScript(fileName, html); } - public static void RegisterFootScript(this HtmlHelper html, string fileName) { - html.Resolve().RegisterFootScript(fileName, html); + public static FileRegistrationContext RegisterFootScript(this HtmlHelper html, string fileName) { + return html.Resolve().RegisterFootScript(fileName, html); } diff --git a/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs b/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs new file mode 100644 index 000000000..9524ed82f --- /dev/null +++ b/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs @@ -0,0 +1,11 @@ +using System.Web.Mvc; + +namespace Orchard.Mvc.Html { + public class StyleFileRegistrationContext : FileRegistrationContext { + public StyleFileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string fileName) + : base(viewContext, viewDataContainer, fileName) { + } + + public string Media { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard/Orchard.csproj b/src/Orchard/Orchard.csproj index f45f503fa..00953a71c 100644 --- a/src/Orchard/Orchard.csproj +++ b/src/Orchard/Orchard.csproj @@ -154,7 +154,9 @@ + + ASPXCodeBehind diff --git a/src/Orchard/UI/Resources/IResourceManager.cs b/src/Orchard/UI/Resources/IResourceManager.cs index e33434e10..2a67678a2 100644 --- a/src/Orchard/UI/Resources/IResourceManager.cs +++ b/src/Orchard/UI/Resources/IResourceManager.cs @@ -1,12 +1,13 @@ using System.Web.Mvc; +using Orchard.Mvc.Html; namespace Orchard.UI.Resources { public interface IResourceManager : IDependency { void RegisterMeta(string name, string content); - void RegisterStyle(string fileName, HtmlHelper html); + StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html); void RegisterLink(LinkEntry entry, HtmlHelper html); - void RegisterHeadScript(string fileName, HtmlHelper html); - void RegisterFootScript(string fileName, HtmlHelper html); + FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html); + FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html); MvcHtmlString GetMetas(); MvcHtmlString GetStyles(); MvcHtmlString GetLinks(HtmlHelper html); diff --git a/src/Orchard/UI/Resources/ResourceManager.cs b/src/Orchard/UI/Resources/ResourceManager.cs index 2f03ec765..469fdd7f9 100644 --- a/src/Orchard/UI/Resources/ResourceManager.cs +++ b/src/Orchard/UI/Resources/ResourceManager.cs @@ -1,77 +1,89 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using JetBrains.Annotations; +using Orchard.Localization; using Orchard.Mvc.Html; namespace Orchard.UI.Resources { [UsedImplicitly] public class ResourceManager : IResourceManager { + private const string ConditionFormat = "\r\n"; private const string MetaFormat = "\r\n"; - private const string StyleFormat = "\r\n"; + private const string StyleFormat = "\r\n"; private const string ScriptFormat = "\r\n"; private readonly Dictionary _metas; - private readonly List _styles; + private readonly List _styles; private readonly List _links; private readonly List _headScripts; private readonly List _footScripts; public ResourceManager() { _metas = new Dictionary(20) {{"generator", "Orchard"}}; - _styles = new List(10); + _styles = new List(10); _links = new List(); _headScripts = new List(10); _footScripts = new List(5); + T = NullLocalizer.Instance; } + public Localizer T { get; set; } + public void RegisterMeta(string name, string content) { if (!string.IsNullOrEmpty(name) && !_metas.ContainsKey(name)) _metas.Add(name, content); } - public void RegisterStyle(string fileName, HtmlHelper html) { + public StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) { if (string.IsNullOrEmpty(fileName)) - return; + throw new ArgumentException(T("Style fileName was not given.").ToString()); - var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); + var context = new StyleFileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); if (!_styles.Contains(context)) _styles.Add(context); + + return context; } public void RegisterLink(LinkEntry entry, HtmlHelper html) { _links.Add(entry); } - public void RegisterHeadScript(string fileName, HtmlHelper html) { + public FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html) { if (string.IsNullOrEmpty(fileName)) - return; + throw new ArgumentException(T("Head script fileName was not given.").ToString()); var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); if (!_headScripts.Contains(context)) _headScripts.Add(context); + + return context; } - public void RegisterFootScript(string fileName, HtmlHelper html) { + public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html) { if (string.IsNullOrEmpty(fileName)) - return; + throw new ArgumentException(T("Foot script fileName was not given.").ToString()); var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); if (!_footScripts.Contains(context)) _footScripts.Add(context); + + return context; } public MvcHtmlString GetMetas() { return - MvcHtmlString.Create(string.Join("\r\n", + MvcHtmlString.Create(string.Join("", _metas.Select(m => string.Format(MetaFormat, m.Key, m.Value)).Reverse().ToArray())); } public MvcHtmlString GetStyles() { - return GetFiles(_styles, StyleFormat, "/styles/"); + return GetStyleFiles(_styles, StyleFormat, "/styles/"); } public MvcHtmlString GetLinks(HtmlHelper html) { @@ -125,8 +137,27 @@ namespace Orchard.UI.Resources { private static MvcHtmlString GetFiles(IEnumerable fileRegistrationContexts, string fileFormat, string containerRelativePath) { return - MvcHtmlString.Create(string.Join("\r\n", - fileRegistrationContexts.Select(c => string.Format(fileFormat, c.GetFilePath(containerRelativePath))).ToArray())); + MvcHtmlString.Create(string.Join("", + fileRegistrationContexts.Select( + c => + string.Format(fileFormat, c.GetFilePath(containerRelativePath))). + ToArray())); + } + + private static MvcHtmlString GetStyleFiles(IEnumerable styleFileRegistrationContexts, string fileFormat, string containerRelativePath) { + return MvcHtmlString.Create(string.Join("", + styleFileRegistrationContexts.Select( + c => + string.Format( + !string.IsNullOrEmpty(c.Condition) + ? string.Format(ConditionFormat, c.Condition) + : "{0}", + string.Format(fileFormat, + c.GetFilePath(containerRelativePath), + !string.IsNullOrEmpty(c.Media) + ? string.Format("media=\"{0}\"", c.Media) + : ""))) + .ToArray())); } } } \ No newline at end of file