--HG--
branch : dev
This commit is contained in:
Suha Can
2010-03-08 16:19:54 -08:00
7 changed files with 63 additions and 60 deletions

View File

@@ -1,11 +1,19 @@
using System; using System;
using System.Collections.Generic;
using System.Web.Mvc; using System.Web.Mvc;
using System.Web.Routing; using System.Web.Routing;
using System.Web.UI; using System.Web.UI;
namespace Orchard.Mvc.Html { namespace Orchard.Mvc.Html {
public class FileRegistrationContext : RequestContext { public class FileRegistrationContext : RequestContext {
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string fileName) private readonly TagBuilder _tagBuilder;
private static readonly Dictionary<string, string> _filePathAttributes = new Dictionary<string, string> {{"script", "src"}, {"link", "href"}};
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string fileName)
: this(viewContext, viewDataContainer, tagName, _filePathAttributes[tagName], fileName) {
}
public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string tagName, string filePathAttributeName, string fileName)
: base(viewContext.HttpContext, viewContext.RouteData) { : base(viewContext.HttpContext, viewContext.RouteData) {
Container = viewDataContainer as TemplateControl; Container = viewDataContainer as TemplateControl;
@@ -20,24 +28,22 @@ namespace Orchard.Mvc.Html {
} }
FileName = fileName; FileName = fileName;
FilePathAttributeName = filePathAttributeName;
_tagBuilder = new TagBuilder(tagName);
} }
public TemplateControl Container { get; set; } public TemplateControl Container { get; set; }
public string ContainerVirtualPath { get; set; } public string ContainerVirtualPath { get; set; }
public string FileName { get; set; } public string FileName { get; set; }
public string Condition { get; set; } public string Condition { get; set; }
public string FilePathAttributeName { get; set; }
public override bool Equals(object obj) { public void AddAttribute(string name, string value) {
if (ReferenceEquals(null, obj)) { _tagBuilder.MergeAttribute(name, value);
return false; }
}
if (ReferenceEquals(this, obj)) { public void SetAttribute(string name, string value) {
return true; _tagBuilder.MergeAttribute(name, value, true);
}
if (obj.GetType() != typeof (FileRegistrationContext)) {
return false;
}
return Equals((FileRegistrationContext) obj);
} }
internal string GetFilePath(string containerRelativePath) { internal string GetFilePath(string containerRelativePath) {
@@ -47,7 +53,21 @@ namespace Orchard.Mvc.Html {
: (ContainerVirtualPath + containerRelativePath + FileName); : (ContainerVirtualPath + containerRelativePath + FileName);
} }
public bool Equals(FileRegistrationContext other) { internal string GetTag() {
return _tagBuilder.ToString();
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
return obj.GetType() == typeof (FileRegistrationContext) && Equals((FileRegistrationContext) obj);
}
private bool Equals(FileRegistrationContext other) {
if (ReferenceEquals(null, other)) { if (ReferenceEquals(null, other)) {
return false; return false;
} }

View File

@@ -8,12 +8,12 @@
return fileRegistrationContext; return fileRegistrationContext;
} }
public static T ForMedia<T>(this T styleFileRegistrationContext, string media) where T : StyleFileRegistrationContext { public static T ForMedia<T>(this T fileRegistrationContext, string media) where T : FileRegistrationContext {
if (styleFileRegistrationContext == null) if (fileRegistrationContext == null)
return null; return null;
styleFileRegistrationContext.Media = media; fileRegistrationContext.SetAttribute("media", media);
return styleFileRegistrationContext; return fileRegistrationContext;
} }
} }
} }

View File

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

View File

@@ -1,11 +0,0 @@
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

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

View File

@@ -4,7 +4,7 @@ using Orchard.Mvc.Html;
namespace Orchard.UI.Resources { namespace Orchard.UI.Resources {
public interface IResourceManager : IDependency { public interface IResourceManager : IDependency {
void RegisterMeta(string name, string content); void RegisterMeta(string name, string content);
StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html); FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html);
void RegisterLink(LinkEntry entry, HtmlHelper html); void RegisterLink(LinkEntry entry, HtmlHelper html);
FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html); FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html);
FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html); FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html);

View File

@@ -10,19 +10,17 @@ using Orchard.Mvc.Html;
namespace Orchard.UI.Resources { namespace Orchard.UI.Resources {
[UsedImplicitly] [UsedImplicitly]
public class ResourceManager : IResourceManager { public class ResourceManager : IResourceManager {
private const string ConditionFormat = "\r\n<!--[{0}]>{{0}}\r\n<![endif]-->"; private const string ConditionFormat = "\r\n<!--[{0}]>{{0}}<![endif]-->";
private const string MetaFormat = "\r\n<meta name=\"{0}\" content=\"{1}\" />"; 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}\" {1} />";
private const string ScriptFormat = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
private readonly Dictionary<string, string> _metas; private readonly Dictionary<string, string> _metas;
private readonly List<StyleFileRegistrationContext> _styles; private readonly List<FileRegistrationContext> _styles;
private readonly List<LinkEntry> _links; private readonly List<LinkEntry> _links;
private readonly List<FileRegistrationContext> _headScripts; private readonly List<FileRegistrationContext> _headScripts;
private readonly List<FileRegistrationContext> _footScripts; private readonly List<FileRegistrationContext> _footScripts;
public ResourceManager() { public ResourceManager() {
_metas = new Dictionary<string, string>(20) {{"generator", "Orchard"}}; _metas = new Dictionary<string, string>(20) {{"generator", "Orchard"}};
_styles = new List<StyleFileRegistrationContext>(10); _styles = new List<FileRegistrationContext>(10);
_links = new List<LinkEntry>(); _links = new List<LinkEntry>();
_headScripts = new List<FileRegistrationContext>(10); _headScripts = new List<FileRegistrationContext>(10);
_footScripts = new List<FileRegistrationContext>(5); _footScripts = new List<FileRegistrationContext>(5);
@@ -36,11 +34,13 @@ namespace Orchard.UI.Resources {
_metas.Add(name, content); _metas.Add(name, content);
} }
public StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) { public FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) {
if (string.IsNullOrEmpty(fileName)) if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Style fileName was not given.").ToString()); throw new ArgumentException(T("Style fileName was not given.").ToString());
var context = new StyleFileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "link", fileName);
context.SetAttribute("type", "text/css");
context.SetAttribute("rel", "stylesheet");
if (!_styles.Contains(context)) if (!_styles.Contains(context))
_styles.Add(context); _styles.Add(context);
@@ -56,7 +56,8 @@ namespace Orchard.UI.Resources {
if (string.IsNullOrEmpty(fileName)) if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Head script fileName was not given.").ToString()); throw new ArgumentException(T("Head script fileName was not given.").ToString());
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName);
context.SetAttribute("type", "text/javascript");
if (!_headScripts.Contains(context)) if (!_headScripts.Contains(context))
_headScripts.Add(context); _headScripts.Add(context);
@@ -64,11 +65,12 @@ namespace Orchard.UI.Resources {
return context; return context;
} }
public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html) { public FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html) { // type=\"text/javascript\" src=\"{0}\"
if (string.IsNullOrEmpty(fileName)) if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(T("Foot script fileName was not given.").ToString()); throw new ArgumentException(T("Foot script fileName was not given.").ToString());
var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, fileName); var context = new FileRegistrationContext(html.ViewContext, html.ViewDataContainer, "script", fileName);
context.SetAttribute("type", "text/javascript");
if (!_footScripts.Contains(context)) if (!_footScripts.Contains(context))
_footScripts.Add(context); _footScripts.Add(context);
@@ -83,7 +85,7 @@ namespace Orchard.UI.Resources {
} }
public MvcHtmlString GetStyles() { public MvcHtmlString GetStyles() {
return GetStyleFiles(_styles, StyleFormat, "/styles/"); return GetFiles(_styles, "/styles/");
} }
public MvcHtmlString GetLinks(HtmlHelper html) { public MvcHtmlString GetLinks(HtmlHelper html) {
@@ -128,36 +130,29 @@ namespace Orchard.UI.Resources {
} }
public MvcHtmlString GetHeadScripts() { public MvcHtmlString GetHeadScripts() {
return GetFiles(_headScripts, ScriptFormat, "/scripts/"); return GetFiles(_headScripts, "/scripts/");
} }
public MvcHtmlString GetFootScripts() { public MvcHtmlString GetFootScripts() {
return GetFiles(_footScripts, ScriptFormat, "/scripts/"); return GetFiles(_footScripts, "/scripts/");
} }
private static MvcHtmlString GetFiles(IEnumerable<FileRegistrationContext> fileRegistrationContexts, string fileFormat, string containerRelativePath) { private static MvcHtmlString GetFiles(IEnumerable<FileRegistrationContext> fileRegistrationContexts, string containerRelativePath) {
return return
MvcHtmlString.Create(string.Join("", MvcHtmlString.Create(string.Join("",
fileRegistrationContexts.Select( fileRegistrationContexts.Select(
c => c =>
string.Format(fileFormat, c.GetFilePath(containerRelativePath))). string.Format(
!string.IsNullOrEmpty(c.Condition)
? string.Format(ConditionFormat, c.Condition)
: "{0}",
GetTag(c, c.GetFilePath(containerRelativePath)))).
ToArray())); ToArray()));
} }
private static MvcHtmlString GetStyleFiles(IEnumerable<StyleFileRegistrationContext> styleFileRegistrationContexts, string fileFormat, string containerRelativePath) { private static string GetTag(FileRegistrationContext fileRegistrationContext, string filePath) {
return MvcHtmlString.Create(string.Join("", fileRegistrationContext.SetAttribute(fileRegistrationContext.FilePathAttributeName, filePath);
styleFileRegistrationContexts.Select( return fileRegistrationContext.GetTag();
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()));
} }
} }
} }