diff --git a/src/Orchard/Mvc/Html/FileRegistrationContext.cs b/src/Orchard/Mvc/Html/FileRegistrationContext.cs index a30467fd4..6cc6c757c 100644 --- a/src/Orchard/Mvc/Html/FileRegistrationContext.cs +++ b/src/Orchard/Mvc/Html/FileRegistrationContext.cs @@ -1,11 +1,19 @@ using System; +using System.Collections.Generic; using System.Web.Mvc; using System.Web.Routing; using System.Web.UI; namespace Orchard.Mvc.Html { public class FileRegistrationContext : RequestContext { - public FileRegistrationContext(ControllerContext viewContext, IViewDataContainer viewDataContainer, string fileName) + private readonly TagBuilder _tagBuilder; + private static readonly Dictionary _filePathAttributes = new Dictionary {{"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) { Container = viewDataContainer as TemplateControl; @@ -20,24 +28,22 @@ namespace Orchard.Mvc.Html { } FileName = fileName; + FilePathAttributeName = filePathAttributeName; + _tagBuilder = new TagBuilder(tagName); } public TemplateControl Container { get; set; } public string ContainerVirtualPath { get; set; } public string FileName { get; set; } public string Condition { get; set; } + public string FilePathAttributeName { get; set; } - 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); + public void AddAttribute(string name, string value) { + _tagBuilder.MergeAttribute(name, value); + } + + public void SetAttribute(string name, string value) { + _tagBuilder.MergeAttribute(name, value, true); } internal string GetFilePath(string containerRelativePath) { @@ -47,7 +53,21 @@ namespace Orchard.Mvc.Html { : (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)) { return false; } diff --git a/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs b/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs index 9063bf55d..ac76a8dfe 100644 --- a/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs +++ b/src/Orchard/Mvc/Html/FileRegistrationContextExtensions.cs @@ -8,12 +8,12 @@ return fileRegistrationContext; } - public static T ForMedia(this T styleFileRegistrationContext, string media) where T : StyleFileRegistrationContext { - if (styleFileRegistrationContext == null) + public static T ForMedia(this T fileRegistrationContext, string media) where T : FileRegistrationContext { + if (fileRegistrationContext == null) return null; - styleFileRegistrationContext.Media = media; - return styleFileRegistrationContext; + fileRegistrationContext.SetAttribute("media", media); + return fileRegistrationContext; } } } \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/LayoutExtensions.cs b/src/Orchard/Mvc/Html/LayoutExtensions.cs index c9dc48b80..953aaf249 100644 --- a/src/Orchard/Mvc/Html/LayoutExtensions.cs +++ b/src/Orchard/Mvc/Html/LayoutExtensions.cs @@ -98,7 +98,7 @@ namespace Orchard.Mvc.Html { html.Resolve().RegisterLink(entry, html); } - public static StyleFileRegistrationContext RegisterStyle(this HtmlHelper html, string fileName) { + public static FileRegistrationContext RegisterStyle(this HtmlHelper html, string fileName) { return html.Resolve().RegisterStyle(fileName, html); } diff --git a/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs b/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs deleted file mode 100644 index 9524ed82f..000000000 --- a/src/Orchard/Mvc/Html/StyleFileRegistrationContext.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/src/Orchard/Orchard.csproj b/src/Orchard/Orchard.csproj index 523312f47..f133ba627 100644 --- a/src/Orchard/Orchard.csproj +++ b/src/Orchard/Orchard.csproj @@ -157,7 +157,6 @@ - ASPXCodeBehind diff --git a/src/Orchard/UI/Resources/IResourceManager.cs b/src/Orchard/UI/Resources/IResourceManager.cs index 2a67678a2..0da6d033a 100644 --- a/src/Orchard/UI/Resources/IResourceManager.cs +++ b/src/Orchard/UI/Resources/IResourceManager.cs @@ -4,7 +4,7 @@ using Orchard.Mvc.Html; namespace Orchard.UI.Resources { public interface IResourceManager : IDependency { void RegisterMeta(string name, string content); - StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html); + FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html); void RegisterLink(LinkEntry entry, HtmlHelper html); FileRegistrationContext RegisterHeadScript(string fileName, HtmlHelper html); FileRegistrationContext RegisterFootScript(string fileName, HtmlHelper html); diff --git a/src/Orchard/UI/Resources/ResourceManager.cs b/src/Orchard/UI/Resources/ResourceManager.cs index 469fdd7f9..c603d636f 100644 --- a/src/Orchard/UI/Resources/ResourceManager.cs +++ b/src/Orchard/UI/Resources/ResourceManager.cs @@ -10,19 +10,17 @@ using Orchard.Mvc.Html; namespace Orchard.UI.Resources { [UsedImplicitly] public class ResourceManager : IResourceManager { - private const string ConditionFormat = "\r\n"; + private const string ConditionFormat = "\r\n"; private const string MetaFormat = "\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); @@ -36,11 +34,13 @@ namespace Orchard.UI.Resources { _metas.Add(name, content); } - public StyleFileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) { + public FileRegistrationContext RegisterStyle(string fileName, HtmlHelper html) { if (string.IsNullOrEmpty(fileName)) 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)) _styles.Add(context); @@ -56,7 +56,8 @@ namespace Orchard.UI.Resources { if (string.IsNullOrEmpty(fileName)) 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)) _headScripts.Add(context); @@ -64,11 +65,12 @@ namespace Orchard.UI.Resources { 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)) 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)) _footScripts.Add(context); @@ -83,7 +85,7 @@ namespace Orchard.UI.Resources { } public MvcHtmlString GetStyles() { - return GetStyleFiles(_styles, StyleFormat, "/styles/"); + return GetFiles(_styles, "/styles/"); } public MvcHtmlString GetLinks(HtmlHelper html) { @@ -128,36 +130,29 @@ namespace Orchard.UI.Resources { } public MvcHtmlString GetHeadScripts() { - return GetFiles(_headScripts, ScriptFormat, "/scripts/"); + return GetFiles(_headScripts, "/scripts/"); } public MvcHtmlString GetFootScripts() { - return GetFiles(_footScripts, ScriptFormat, "/scripts/"); + return GetFiles(_footScripts, "/scripts/"); } - private static MvcHtmlString GetFiles(IEnumerable fileRegistrationContexts, string fileFormat, string containerRelativePath) { + private static MvcHtmlString GetFiles(IEnumerable fileRegistrationContexts, string containerRelativePath) { return MvcHtmlString.Create(string.Join("", fileRegistrationContexts.Select( 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())); } - 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())); + private static string GetTag(FileRegistrationContext fileRegistrationContext, string filePath) { + fileRegistrationContext.SetAttribute(fileRegistrationContext.FilePathAttributeName, filePath); + return fileRegistrationContext.GetTag(); } } } \ No newline at end of file