#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
This commit is contained in:
Dave Reed 2011-01-31 20:49:30 -08:00
parent 9adfa13a56
commit 11b37286a2
5 changed files with 57 additions and 12 deletions

View File

@ -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<string, string> 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<string, string> TagAttributes) {
UI.Resources.ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes);
}
private static void WriteLiteralScripts(TextWriter output, IEnumerable<string> 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);
}

View File

@ -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());

View File

@ -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<string, string> attributes = shape.TagAttributes;
ResourceManager.WriteResource(output, resource, hit.fileVirtualPath, condition, attributes);
return null;
});
}

View File

@ -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<string, string> _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<ResourceDefinition> InlineDefinition { get; set; }
public Dictionary<string, string> Attributes {
get { return _attributes ?? (_attributes = new Dictionary<string, string>()); }
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<string, string>();
}
_attributes[name] = value;
return this;
}
private Dictionary<string, string> 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<string, string>(other._attributes);
}
if (other._attributes == null) {
return new Dictionary<string, string>(_attributes);
}
var mergedAttributes = new Dictionary<string, string>(_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;
}
}
}

View File

@ -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<string,string> attributes) {
if (!string.IsNullOrEmpty(condition)) {
writer.WriteLine("<!--[if " + condition + "]>");
}
writer.WriteLine(GetTagBuilder(resource, url).ToString(resource.TagRenderMode));
var tagBuilder = GetTagBuilder(resource, url);
if (attributes != null) {
// todo: try null value
tagBuilder.MergeAttributes(attributes, true);
}
writer.WriteLine(tagBuilder.ToString(resource.TagRenderMode));
if (!string.IsNullOrEmpty(condition)) {
writer.WriteLine("<![endif]-->");
}