From 11b37286a27d761827b2c0fee297d6a8fd45470e Mon Sep 17 00:00:00 2001 From: Dave Reed Date: Mon, 31 Jan 2011 20:49:30 -0800 Subject: [PATCH] #17286: ResourceManager should allow condition to be applied. Actually, it did, but this wasn't useful for stylesheets that wanted a different 'media' type. You can now set any attribute in addition to the condition, which would also be great for scripts if you need to set an ID or other script attribute. --HG-- branch : dev --- src/Orchard.Web/Core/Shapes/CoreShapes.cs | 13 +++--- .../Themes/TheAdmin/Views/Layout.cshtml | 4 +- .../StylesheetBindingStrategy.cs | 3 +- src/Orchard/UI/Resources/RequireSettings.cs | 40 ++++++++++++++++++- src/Orchard/UI/Resources/ResourceManager.cs | 9 ++++- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/Orchard.Web/Core/Shapes/CoreShapes.cs b/src/Orchard.Web/Core/Shapes/CoreShapes.cs index 7e4ee66ca..783eb4958 100644 --- a/src/Orchard.Web/Core/Shapes/CoreShapes.cs +++ b/src/Orchard.Web/Core/Shapes/CoreShapes.cs @@ -221,13 +221,13 @@ namespace Orchard.Core.Shapes { } [Shape] - public void Style(TextWriter Output, ResourceDefinition Resource, string Url, string Condition) { - UI.Resources.ResourceManager.WriteResource(Output, Resource, Url, Condition); + public void Style(TextWriter Output, ResourceDefinition Resource, string Url, string Condition, Dictionary TagAttributes) { + UI.Resources.ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes); } [Shape] - public void Resource(TextWriter Output, ResourceDefinition Resource, string Url, string Condition) { - UI.Resources.ResourceManager.WriteResource(Output, Resource, Url, Condition); + public void Resource(TextWriter Output, ResourceDefinition Resource, string Url, string Condition, Dictionary TagAttributes) { + UI.Resources.ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes); } private static void WriteLiteralScripts(TextWriter output, IEnumerable scripts) { @@ -266,12 +266,13 @@ namespace Orchard.Core.Shapes { var path = context.GetResourceUrl(defaultSettings, appPath); var condition = context.Settings.Condition; + var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null; IHtmlString result; if (resourceType == "stylesheet") { - result = Display.Style(Url: path, Condition: condition, Resource: context.Resource); + result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes); } else { - result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource); + result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes); } Output.Write(result); } diff --git a/src/Orchard.Web/Themes/TheAdmin/Views/Layout.cshtml b/src/Orchard.Web/Themes/TheAdmin/Views/Layout.cshtml index 7aae5a236..3502c2c80 100644 --- a/src/Orchard.Web/Themes/TheAdmin/Views/Layout.cshtml +++ b/src/Orchard.Web/Themes/TheAdmin/Views/Layout.cshtml @@ -7,11 +7,11 @@ @{ Style.Include("site.css"); + Style.Include("ie.css").UseCondition("lte IE 8").SetAttribute("media", "screen, projection"); + Style.Include("ie6.css").UseCondition("lte IE 6").SetAttribute("media", "screen, projection"); Script.Require("jQuery"); Script.Require("ShapesBase"); Script.Include("admin.js"); - RegisterLink(new LinkEntry { Condition = "lte IE 8", Rel = "stylesheet", Type = "text/css", Href = Url.Content("~/Themes/TheAdmin/Styles/ie.css") }.AddAttribute("media", "screen, projection")); - RegisterLink(new LinkEntry { Condition = "lte IE 6", Rel = "stylesheet", Type = "text/css", Href = Url.Content("~/Themes/TheAdmin/Styles/ie6.css") }.AddAttribute("media", "screen, projection")); // these are just hacked together to fire existing partials... can change Model.Header.Add(Display.Header()); diff --git a/src/Orchard/DisplayManagement/Descriptors/ResourceBindingStrategy/StylesheetBindingStrategy.cs b/src/Orchard/DisplayManagement/Descriptors/ResourceBindingStrategy/StylesheetBindingStrategy.cs index 7ed76f709..c8a113cdc 100644 --- a/src/Orchard/DisplayManagement/Descriptors/ResourceBindingStrategy/StylesheetBindingStrategy.cs +++ b/src/Orchard/DisplayManagement/Descriptors/ResourceBindingStrategy/StylesheetBindingStrategy.cs @@ -83,7 +83,8 @@ namespace Orchard.DisplayManagement.Descriptors.ResourceBindingStrategy { var output = displayContext.ViewContext.Writer; ResourceDefinition resource = shape.Resource; string condition = shape.Condition; - ResourceManager.WriteResource(output, resource, hit.fileVirtualPath, condition); + Dictionary attributes = shape.TagAttributes; + ResourceManager.WriteResource(output, resource, hit.fileVirtualPath, condition, attributes); return null; }); } diff --git a/src/Orchard/UI/Resources/RequireSettings.cs b/src/Orchard/UI/Resources/RequireSettings.cs index a945b4a84..cececfbf1 100644 --- a/src/Orchard/UI/Resources/RequireSettings.cs +++ b/src/Orchard/UI/Resources/RequireSettings.cs @@ -1,7 +1,12 @@ using System; +using System.Linq; +using System.Collections.Generic; +using System.Web.Mvc; namespace Orchard.UI.Resources { public class RequireSettings { + private Dictionary _attributes; + public string BasePath { get; set; } public string Type { get; set; } public string Name { get; set; } @@ -11,6 +16,13 @@ namespace Orchard.UI.Resources { public ResourceLocation Location { get; set; } public string Condition { get; set; } public Action InlineDefinition { get; set; } + public Dictionary Attributes { + get { return _attributes ?? (_attributes = new Dictionary()); } + set { _attributes = value; } + } + public bool HasAttributes { + get { return _attributes != null && _attributes.Any(a => a.Value != null); } + } public RequireSettings AtHead() { return AtLocation(ResourceLocation.Head); @@ -66,8 +78,32 @@ namespace Orchard.UI.Resources { return this; } + public RequireSettings SetAttribute(string name, string value) { + if (_attributes == null) { + _attributes = new Dictionary(); + } + _attributes[name] = value; + return this; + } + + private Dictionary MergeAttributes(RequireSettings other) { + // efficiently merge the two dictionaries, taking into account that one or both may not exist + // and that attributes in 'other' should overridde attributes in this, even if the value is null. + if (_attributes == null) { + return other._attributes == null ? null : new Dictionary(other._attributes); + } + if (other._attributes == null) { + return new Dictionary(_attributes); + } + var mergedAttributes = new Dictionary(_attributes); + foreach (var pair in other._attributes) { + mergedAttributes[pair.Key] = pair.Value; + } + return mergedAttributes; + } + public RequireSettings Combine(RequireSettings other) { - return (new RequireSettings { + var settings = (new RequireSettings { Name = Name, Type = Type }).AtLocation(Location).AtLocation(other.Location) @@ -77,6 +113,8 @@ namespace Orchard.UI.Resources { .UseCulture(Culture).UseCulture(other.Culture) .UseCondition(Condition).UseCondition(other.Condition) .Define(InlineDefinition).Define(other.InlineDefinition); + settings._attributes = MergeAttributes(other); + return settings; } } } diff --git a/src/Orchard/UI/Resources/ResourceManager.cs b/src/Orchard/UI/Resources/ResourceManager.cs index 429332006..7ec6cfc9b 100644 --- a/src/Orchard/UI/Resources/ResourceManager.cs +++ b/src/Orchard/UI/Resources/ResourceManager.cs @@ -56,11 +56,16 @@ namespace Orchard.UI.Resources { return tagBuilder; } - public static void WriteResource(TextWriter writer, ResourceDefinition resource, string url, string condition) { + public static void WriteResource(TextWriter writer, ResourceDefinition resource, string url, string condition, Dictionary attributes) { if (!string.IsNullOrEmpty(condition)) { writer.WriteLine(""); }