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
This commit is contained in:
Nathan Heskew
2010-03-08 01:49:53 -08:00
parent 3dcc0e4261
commit ef03c8f608
10 changed files with 130 additions and 42 deletions

View File

@@ -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/*

View File

@@ -10,11 +10,10 @@
html.ViewContext.Writer.Write(@"<script type=""text/javascript"" src=""" + basejs + @"""></script>"));
var siteCss = ResolveUrl("../Styles/site.css");
Model.Zones.AddAction("head:styles", html =>
html.ViewContext.Writer.Write(@"<link rel=""stylesheet"" type=""text/css"" href=""" + siteCss + @"""/>")); %>
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" media="screen, projection" href="/Themes/SafeMode/Styles/ie6.css" />
<![endif]-->
html.ViewContext.Writer.Write(@"<link rel=""stylesheet"" type=""text/css"" href=""" + siteCss + @"""/>"));
var ie6Css = ResolveUrl("../Styles/ie6.css");
Model.Zones.AddAction("head:styles", html =>
html.ViewContext.Writer.Write(@"<!--[if lte IE 6]><link rel=""stylesheet"" type=""text/css"" media=""screen, projection"" href=""" + ie6Css + @"""/><![endif]-->")); %>
<div id="header">
<div id="branding"><h1>Welcome to Orchard</h1></div>
</div>

View File

@@ -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);
%>
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" media="screen, projection" href="/Themes/TheAdmin/Styles/ie6.css" />
<![endif]-->
<div id="header" role="banner"><% Html.Zone("header"); %></div>
<div id="content">
<div id="navshortcut"><a href="#menu"><%=_Encoded("Skip to navigation") %></a></div>

View File

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

View File

@@ -0,0 +1,19 @@
namespace Orchard.Mvc.Html {
public static class FileRegistrationContextExtensions {
public static T WithCondition<T>(this T fileRegistrationContext, string condition)where T : FileRegistrationContext {
if (fileRegistrationContext == null)
return null;
fileRegistrationContext.Condition = condition;
return fileRegistrationContext;
}
public static T ForMedia<T>(this T styleFileRegistrationContext, string media) where T : StyleFileRegistrationContext {
if (styleFileRegistrationContext == null)
return null;
styleFileRegistrationContext.Media = media;
return styleFileRegistrationContext;
}
}
}

View File

@@ -98,16 +98,16 @@ namespace Orchard.Mvc.Html {
html.Resolve<IResourceManager>().RegisterLink(entry, html);
}
public static void RegisterStyle(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterStyle(fileName, html);
public static StyleFileRegistrationContext RegisterStyle(this HtmlHelper html, string fileName) {
return html.Resolve<IResourceManager>().RegisterStyle(fileName, html);
}
public static void RegisterScript(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterHeadScript(fileName, html);
public static FileRegistrationContext RegisterScript(this HtmlHelper html, string fileName) {
return html.Resolve<IResourceManager>().RegisterHeadScript(fileName, html);
}
public static void RegisterFootScript(this HtmlHelper html, string fileName) {
html.Resolve<IResourceManager>().RegisterFootScript(fileName, html);
public static FileRegistrationContext RegisterFootScript(this HtmlHelper html, string fileName) {
return html.Resolve<IResourceManager>().RegisterFootScript(fileName, html);
}

View File

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

View File

@@ -154,7 +154,9 @@
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\UriExtensions.cs" />
<Compile Include="Mvc\AntiForgery\ValidateAntiForgeryTokenOrchardAttribute.cs" />
<Compile Include="Mvc\Html\FileRegistrationContextExtensions.cs" />
<Compile Include="Mvc\Extensions\UrlHelperExtensions.cs" />
<Compile Include="Mvc\Html\StyleFileRegistrationContext.cs" />
<Compile Include="Mvc\ViewModels\AdaptedViewModel.cs" />
<Compile Include="Mvc\ViewUserControl.cs">
<SubType>ASPXCodeBehind</SubType>

View File

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

View File

@@ -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<!--[{0}]>{{0}}\r\n<![endif]-->";
private const string MetaFormat = "\r\n<meta name=\"{0}\" content=\"{1}\" />";
private const string StyleFormat = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
private const string StyleFormat = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" {1} />";
private const string ScriptFormat = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
private readonly Dictionary<string, string> _metas;
private readonly List<FileRegistrationContext> _styles;
private readonly List<StyleFileRegistrationContext> _styles;
private readonly List<LinkEntry> _links;
private readonly List<FileRegistrationContext> _headScripts;
private readonly List<FileRegistrationContext> _footScripts;
public ResourceManager() {
_metas = new Dictionary<string, string>(20) {{"generator", "Orchard"}};
_styles = new List<FileRegistrationContext>(10);
_styles = new List<StyleFileRegistrationContext>(10);
_links = new List<LinkEntry>();
_headScripts = new List<FileRegistrationContext>(10);
_footScripts = new List<FileRegistrationContext>(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<FileRegistrationContext> 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<StyleFileRegistrationContext> 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()));
}
}
}