Adding page classification (aka class names for the body tag).

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045161
This commit is contained in:
skewed
2010-01-08 22:07:54 +00:00
parent b98f7a9607
commit d3b1eb4dc5
7 changed files with 55 additions and 1 deletions

View File

@@ -11,7 +11,7 @@
Html.Zone("head", ":metas :styles :scripts"); %> Html.Zone("head", ":metas :styles :scripts"); %>
<script type="text/javascript">window.document.documentElement.className="dyn"</script> <script type="text/javascript">window.document.documentElement.className="dyn"</script>
</head> </head>
<body><% <body class="<%=Html.ClassForPage() %>"><%
Html.ZoneBody("body"); %> Html.ZoneBody("body"); %>
</body> </body>
</html> </html>

View File

@@ -1,4 +1,5 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% Html.AddPageClassNames("home"); %>
<h1><%=Html.Encode(Html.SiteName()) %></h1> <h1><%=Html.Encode(Html.SiteName()) %></h1>
<h2><%=Html.Encode(ViewData["Message"]) %></h2> <h2><%=Html.Encode(ViewData["Message"]) %></h2>
<p>To learn more about Orchard visit <a href="http://orchardproject.net" title="Orchard Project">http://orchardproject.net</a>.</p> <p>To learn more about Orchard visit <a href="http://orchardproject.net" title="Orchard Project">http://orchardproject.net</a>.</p>

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc; using System.Web.Mvc;
using System.Web.Mvc.Html; using System.Web.Mvc.Html;
using System.Web.Routing; using System.Web.Routing;

View File

@@ -4,6 +4,7 @@ using System.Web.Mvc;
using System.Web.UI; using System.Web.UI;
using Orchard.Mvc.ViewEngines; using Orchard.Mvc.ViewEngines;
using Orchard.Mvc.ViewModels; using Orchard.Mvc.ViewModels;
using Orchard.UI.PageClass;
using Orchard.UI.PageTitle; using Orchard.UI.PageTitle;
using Orchard.UI.Resources; using Orchard.UI.Resources;
using Orchard.UI.Zones; using Orchard.UI.Zones;
@@ -46,6 +47,20 @@ namespace Orchard.Mvc.Html {
return MvcHtmlString.Create(html.Encode(titleParts[0])); return MvcHtmlString.Create(html.Encode(titleParts[0]));
} }
public static void AddPageClassNames(this HtmlHelper html, params object[] classNames) {
html.Resolve<IPageClassBuilder>().AddClassNames(classNames);
}
public static MvcHtmlString ClassForPage(this HtmlHelper html, params object[] classNames) {
IPageClassBuilder pageClassBuilder = html.Resolve<IPageClassBuilder>();
html.AddPageClassNames(classNames);
//todo: (heskew) need ContentItem.ContentType
html.AddPageClassNames(html.ViewContext.RouteData.Values["area"]);
return MvcHtmlString.Create(html.Encode(pageClassBuilder.ToString()));
}
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : IZoneContainer { public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : IZoneContainer {
var manager = html.Resolve<IZoneManager>(); var manager = html.Resolve<IZoneManager>();

View File

@@ -248,6 +248,8 @@
<Compile Include="Mvc\ViewEngines\WebFormsViewEngineProvider.cs" /> <Compile Include="Mvc\ViewEngines\WebFormsViewEngineProvider.cs" />
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" /> <Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" /> <Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
<Compile Include="UI\PageClass\IPageClassBuilder.cs" />
<Compile Include="UI\PageClass\PageClassBuilder.cs" />
<Compile Include="UI\PageTitle\IPageTitleBuilder.cs" /> <Compile Include="UI\PageTitle\IPageTitleBuilder.cs" />
<Compile Include="UI\PageTitle\PageTitleBuilder.cs" /> <Compile Include="UI\PageTitle\PageTitleBuilder.cs" />
<Compile Include="UI\Resources\IResourceManager.cs" /> <Compile Include="UI\Resources\IResourceManager.cs" />

View File

@@ -0,0 +1,5 @@
namespace Orchard.UI.PageClass {
public interface IPageClassBuilder : IDependency {
void AddClassNames(params object[] titleParts);
}
}

View File

@@ -0,0 +1,30 @@
using System.Text;
namespace Orchard.UI.PageClass {
public class PageClassBuilder : IPageClassBuilder {
private readonly StringBuilder _classNameBuilder;
public PageClassBuilder() {
_classNameBuilder = new StringBuilder();
}
public void AddClassNames(params object[] classNames) {
if (classNames == null)
return;
foreach (var className in classNames) {
if (className == null)
continue;
if (_classNameBuilder.Length > 0)
_classNameBuilder.AppendFormat(" {0}", className);
else
_classNameBuilder.Append(className);
}
}
public override string ToString() {
return _classNameBuilder.ToString().ToLower().Replace('.', '-'); // <- just keeping it simple for now, assuming what was passed is is pretty good with '.'s to be the only invalid class name chars in module/area names
}
}
}