Adding position specification to file resource registration

- so base scripts and styles can be made to appear in the source before the other scripts and styles

--HG--
branch : dev
This commit is contained in:
Nathan Heskew 2010-07-21 08:54:28 -07:00
parent 9d7efb58f9
commit 5723255248
15 changed files with 65 additions and 35 deletions

View File

@ -1,8 +1,8 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="Orchard.Mvc.Html"%><%
<%
// a CSS file for styling things (e.g. content item edit buttons) for users with elevated privileges (in this case, anyone who is authenticated)
if (Request.IsAuthenticated) { Html.RegisterStyle("special.css"); }
Html.RegisterScript("jquery-1.4.2.js"); // <- change to .min.js for use on a real site :)
Html.RegisterFootScript("base.js");
Html.RegisterScript("jquery-1.4.2.js", "1"); // <- change to .min.js for use on a real site :)
Html.RegisterFootScript("base.js", "1");
%>

View File

@ -99,6 +99,7 @@
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
<Content Include="Refresh.html" />
<Content Include="Themes\TheAdmin\Scripts\admin.js" />
<Content Include="Themes\TheAdmin\Styles\ie.css" />
<Content Include="Web.config">
<SubType>Designer</SubType>

View File

@ -1,8 +1,8 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BaseViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels" %>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("blog.css");
Html.RegisterStyle("site.css", "1");
Html.RegisterStyle("blog.css", "1");
%>
<%-- todo:(nheskew) this will need to be a generated menu --%>

View File

@ -1,8 +1,8 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BaseViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels" %>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("blog.css");
Html.RegisterStyle("site.css", "1");
Html.RegisterStyle("blog.css", "1");
%>
<%-- todo:(nheskew) this will need to be a generated menu --%>

View File

@ -2,7 +2,7 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("site.css", "1");
Model.Zones.AddRenderPartial("header", "Header", Model);
Model.Zones.AddRenderPartial("menu", "Menu", Model);

View File

@ -2,7 +2,7 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("site.css", "1");
Model.Zones.AddRenderPartial("header", "Header", Model);
Model.Zones.AddRenderPartial("menu", "Menu", Model);

View File

@ -2,7 +2,7 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("site.css", "1");
Model.Zones.AddRenderPartial("header", "Header", Model);
Model.Zones.AddRenderPartial("menu", "Menu", Model);

View File

@ -2,7 +2,7 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("site.css");
Html.RegisterStyle("site.css", "1");
Model.Zones.AddRenderPartial("header", "Header", Model);
Model.Zones.AddRenderPartial("menu", "Menu", Model);

View File

@ -2,9 +2,9 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("yui.css");
Html.RegisterStyle("site.css");
Html.RegisterStyle("blog.css");
Html.RegisterStyle("yui.css", "1");
Html.RegisterStyle("site.css", "1");
Html.RegisterStyle("blog.css", "1");
%>
<script type="text/javascript">

View File

@ -2,9 +2,9 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%
Html.RegisterStyle("yui.css");
Html.RegisterStyle("site.css");
Html.RegisterStyle("blog.css");
Html.RegisterStyle("yui.css", "1");
Html.RegisterStyle("site.css", "1");
Html.RegisterStyle("blog.css", "1");
%>
<script type="text/javascript">

View File

@ -1,14 +1,13 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<BaseViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Mvc.Html"%><%
Html.RegisterStyle("site.css");
Html.RegisterStyle("ie.css").WithCondition("if (lte IE 8)").ForMedia("screen, projection");
Html.RegisterStyle("ie6.css").WithCondition("if lte IE 6").ForMedia("screen, projection");
<%@ Import Namespace="Orchard.Mvc.ViewModels"%><%
Html.RegisterStyle("site.css", "1");
Html.RegisterStyle("ie.css", "1").WithCondition("if (lte IE 8)").ForMedia("screen, projection");
Html.RegisterStyle("ie6.css", "1").WithCondition("if lte IE 6").ForMedia("screen, projection");
Html.RegisterFootScript("admin.js", "1");
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);
%>
<script src="<%: Url.Content("~/Themes/TheAdmin/Scripts/admin.js") %>" type="text/javascript"></script>
<div id="header" role="banner"><% Html.Zone("header"); %></div>
<div id="content">
<div id="navshortcut"><a href="#menu"><%: T("Skip to navigation") %></a></div>

View File

@ -11,11 +11,11 @@ namespace Orchard.Mvc.Html {
private static readonly Dictionary<string, string> _filePathAttributes = new Dictionary<string, string> {{"script", "src"}, {"link", "href"}};
private static readonly Dictionary<string, TagRenderMode> _fileTagRenderModes = new Dictionary<string, TagRenderMode> {{"script", TagRenderMode.Normal}, {"link", TagRenderMode.SelfClosing}};
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string fileName)
: this(viewContext, viewDataContainer, tagName, fileName, _filePathAttributes[tagName], _fileTagRenderModes[tagName]) {
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string fileName, string position)
: this(viewContext, viewDataContainer, tagName, fileName, position, _filePathAttributes[tagName], _fileTagRenderModes[tagName]) {
}
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string fileName, string filePathAttributeName, TagRenderMode fileTagRenderMode)
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string fileName, string position, string filePathAttributeName, TagRenderMode fileTagRenderMode)
: base(viewContext.HttpContext, viewContext.RouteData) {
_fileTagRenderMode = fileTagRenderMode;
Container = viewDataContainer as TemplateControl;
@ -31,6 +31,7 @@ namespace Orchard.Mvc.Html {
}
FileName = fileName;
Position = position;
FilePathAttributeName = filePathAttributeName;
_tagBuilder = new TagBuilder(tagName);
}
@ -40,6 +41,7 @@ namespace Orchard.Mvc.Html {
public string FileName { get; set; }
public string FilePathAttributeName { get; private set; }
public string Condition { get; set; }
public string Position { get; set; }
public void AddAttribute(string name, string value) {
_tagBuilder.MergeAttribute(name, value);

View File

@ -102,14 +102,25 @@ namespace Orchard.Mvc.Html {
return html.Resolve<IResourceManager>().RegisterStyle(fileName, html);
}
public static FileRegistrationContext RegisterStyle(this HtmlHelper html, string fileName, string position) {
return html.Resolve<IResourceManager>().RegisterStyle(fileName, html, position);
}
public static FileRegistrationContext RegisterScript(this HtmlHelper html, string fileName) {
return html.Resolve<IResourceManager>().RegisterHeadScript(fileName, html);
}
public static FileRegistrationContext RegisterScript(this HtmlHelper html, string fileName, string position) {
return html.Resolve<IResourceManager>().RegisterHeadScript(fileName, html, position);
}
public static FileRegistrationContext RegisterFootScript(this HtmlHelper html, string fileName) {
return html.Resolve<IResourceManager>().RegisterFootScript(fileName, html);
}
public static FileRegistrationContext RegisterFootScript(this HtmlHelper html, string fileName, string position) {
return html.Resolve<IResourceManager>().RegisterFootScript(fileName, html, position);
}
public static IDisposable Capture(this ViewUserControl control, string name) {
var writer = LayoutViewContext.From(control.ViewContext).GetNamedContent(name);

View File

@ -5,9 +5,12 @@ namespace Orchard.UI.Resources {
public interface IResourceManager : IDependency {
void RegisterMeta(string name, string content);
FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html);
FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html, string position);
void RegisterLink(LinkEntry entry, HtmlHelper html);
FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html);
FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html, string position);
FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html);
FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html, string position);
MvcHtmlString GetMetas();
MvcHtmlString GetStyles();
MvcHtmlString GetLinks(HtmlHelper html);

View File

@ -35,15 +35,27 @@ namespace Orchard.UI.Resources {
}
public FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) {
return RegisterStyle(fileName, html, "5");
}
public FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html) {
return RegisterHeadScript(fileName, html, "5");
}
public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html) {
return RegisterFootScript(fileName, html, "5");
}
public FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html, string position) {
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Style fileName was not given.").ToString());
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "link", fileName);
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "link", fileName, position);
context.SetAttribute("type", "text/css");
context.SetAttribute("rel", "stylesheet");
if (!_styles.Contains(context))
_styles.Insert(0, context);
_styles.Add(context);
return context;
}
@ -52,28 +64,28 @@ namespace Orchard.UI.Resources {
_links.Add(entry);
}
public FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html) {
public FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html, string position) {
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Head script fileName was not given.").ToString());
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName);
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName, position);
context.SetAttribute("type", "text/javascript");
if (!_headScripts.Contains(context))
_headScripts.Insert(0, context);
_headScripts.Add(context);
return context;
}
public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html) { // type=\"text/javascript\" src=\"{0}\"
public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html, string position) {
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Foot script fileName was not given.").ToString());
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName);
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName, position);
context.SetAttribute("type", "text/javascript");
if (!_footScripts.Contains(context))
_footScripts.Insert(0, context);
_footScripts.Add(context);
return context;
}
@ -139,7 +151,9 @@ namespace Orchard.UI.Resources {
private static MvcHtmlString GetFiles(IEnumerable<FileRegistrationContext> fileRegistrationContexts, string containerRelativePath) {
return
MvcHtmlString.Create(string.Join("",
fileRegistrationContexts.Select(
fileRegistrationContexts
.OrderBy(c => c.Position)
.Select(
c =>
string.Format(
!string.IsNullOrEmpty(c.Condition)