#18829: Making scripts overridable by other modules

Work Item: 18829

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-09-26 17:22:41 -07:00
parent ade44206de
commit 421afa9747
2 changed files with 68 additions and 9 deletions

View File

@@ -202,17 +202,27 @@ namespace Orchard.Core.Shapes {
.OnDisplaying(displaying => { .OnDisplaying(displaying => {
var resource = displaying.Shape; var resource = displaying.Shape;
string url = resource.Url; string url = resource.Url;
string fileName = StylesheetBindingStrategy.GetAlternateShapeNameFromFileName(url); string fileName = StaticFileBindingStrategy.GetAlternateShapeNameFromFileName(url);
if (!string.IsNullOrEmpty(fileName)) { if (!string.IsNullOrEmpty(fileName)) {
resource.Metadata.Alternates.Add("Style__" + fileName); resource.Metadata.Alternates.Add("Style__" + fileName);
} }
}); });
builder.Describe("Script")
.OnDisplaying(displaying => {
var resource = displaying.Shape;
string url = resource.Url;
string fileName = StaticFileBindingStrategy.GetAlternateShapeNameFromFileName(url);
if (!string.IsNullOrEmpty(fileName)) {
resource.Metadata.Alternates.Add("Script__" + fileName);
}
});
builder.Describe("Resource") builder.Describe("Resource")
.OnDisplaying(displaying => { .OnDisplaying(displaying => {
var resource = displaying.Shape; var resource = displaying.Shape;
string url = resource.Url; string url = resource.Url;
string fileName = StylesheetBindingStrategy.GetAlternateShapeNameFromFileName(url); string fileName = StaticFileBindingStrategy.GetAlternateShapeNameFromFileName(url);
if (!string.IsNullOrEmpty(fileName)) { if (!string.IsNullOrEmpty(fileName)) {
resource.Metadata.Alternates.Add("Resource__" + fileName); resource.Metadata.Alternates.Add("Resource__" + fileName);
} }
@@ -330,6 +340,11 @@ namespace Orchard.Core.Shapes {
ResourceManager.WriteResource(Html.ViewContext.Writer, Resource, Url, Condition, TagAttributes); ResourceManager.WriteResource(Html.ViewContext.Writer, Resource, Url, Condition, TagAttributes);
} }
[Shape]
public void Script(TextWriter Output, ResourceDefinition Resource, string Url, string Condition, Dictionary<string, string> TagAttributes) {
ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes);
}
[Shape] [Shape]
public void Resource(TextWriter Output, ResourceDefinition Resource, string Url, string Condition, Dictionary<string, string> TagAttributes) { public void Resource(TextWriter Output, ResourceDefinition Resource, string Url, string Condition, Dictionary<string, string> TagAttributes) {
ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes); ResourceManager.WriteResource(Output, Resource, Url, Condition, TagAttributes);
@@ -376,6 +391,9 @@ namespace Orchard.Core.Shapes {
if (resourceType == "stylesheet") { if (resourceType == "stylesheet") {
result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes); result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
} }
else if (resourceType == "script") {
result = Display.Script(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
}
else { else {
result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes); result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
} }

View File

@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using Orchard.Environment.Descriptor.Models; using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions; using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models; using Orchard.Environment.Extensions.Models;
@@ -12,19 +11,23 @@ using Orchard.UI.Resources;
using Orchard.Utility.Extensions; using Orchard.Utility.Extensions;
namespace Orchard.DisplayManagement.Descriptors.ResourceBindingStrategy { namespace Orchard.DisplayManagement.Descriptors.ResourceBindingStrategy {
// discovers .css files and turns them into Style__<filename> shapes. // discovers static files and turns them into shapes.
public class StylesheetBindingStrategy : IShapeTableProvider { public abstract class StaticFileBindingStrategy {
private readonly IExtensionManager _extensionManager; private readonly IExtensionManager _extensionManager;
private readonly ShellDescriptor _shellDescriptor; private readonly ShellDescriptor _shellDescriptor;
private readonly IVirtualPathProvider _virtualPathProvider; private readonly IVirtualPathProvider _virtualPathProvider;
private static readonly char[] unsafeCharList = "/:?#[]@!&'()*+,;=\r\n\t\f\" <>.-_".ToCharArray(); private static readonly char[] unsafeCharList = "/:?#[]@!&'()*+,;=\r\n\t\f\" <>.-_".ToCharArray();
public StylesheetBindingStrategy(IExtensionManager extensionManager, ShellDescriptor shellDescriptor, IVirtualPathProvider virtualPathProvider) { protected StaticFileBindingStrategy(IExtensionManager extensionManager, ShellDescriptor shellDescriptor, IVirtualPathProvider virtualPathProvider) {
_extensionManager = extensionManager; _extensionManager = extensionManager;
_shellDescriptor = shellDescriptor; _shellDescriptor = shellDescriptor;
_virtualPathProvider = virtualPathProvider; _virtualPathProvider = virtualPathProvider;
} }
public abstract string GetFileExtension();
public abstract string GetFolder();
public abstract string GetShapePrefix();
private static string SafeName(string name) { private static string SafeName(string name) {
if (string.IsNullOrWhiteSpace(name)) if (string.IsNullOrWhiteSpace(name))
return String.Empty; return String.Empty;
@@ -59,14 +62,14 @@ namespace Orchard.DisplayManagement.Descriptors.ResourceBindingStrategy {
var hits = activeExtensions.SelectMany(extensionDescriptor => { var hits = activeExtensions.SelectMany(extensionDescriptor => {
var basePath = Path.Combine(extensionDescriptor.Location, extensionDescriptor.Id).Replace(Path.DirectorySeparatorChar, '/'); var basePath = Path.Combine(extensionDescriptor.Location, extensionDescriptor.Id).Replace(Path.DirectorySeparatorChar, '/');
var virtualPath = Path.Combine(basePath, "Styles").Replace(Path.DirectorySeparatorChar, '/'); var virtualPath = Path.Combine(basePath, GetFolder()).Replace(Path.DirectorySeparatorChar, '/');
var shapes = _virtualPathProvider.ListFiles(virtualPath) var shapes = _virtualPathProvider.ListFiles(virtualPath)
.Select(Path.GetFileName) .Select(Path.GetFileName)
.Where(fileName => string.Equals(Path.GetExtension(fileName), ".css", System.StringComparison.OrdinalIgnoreCase)) .Where(fileName => string.Equals(Path.GetExtension(fileName), GetFileExtension(), StringComparison.OrdinalIgnoreCase))
.Select(cssFileName => new { .Select(cssFileName => new {
fileName = Path.GetFileNameWithoutExtension(cssFileName), fileName = Path.GetFileNameWithoutExtension(cssFileName),
fileVirtualPath = Path.Combine(virtualPath, cssFileName).Replace(Path.DirectorySeparatorChar, '/'), fileVirtualPath = Path.Combine(virtualPath, cssFileName).Replace(Path.DirectorySeparatorChar, '/'),
shapeType = "Style__" + GetAlternateShapeNameFromFileName(cssFileName), shapeType = GetShapePrefix() + GetAlternateShapeNameFromFileName(cssFileName),
extensionDescriptor extensionDescriptor
}); });
return shapes; return shapes;
@@ -98,4 +101,42 @@ namespace Orchard.DisplayManagement.Descriptors.ResourceBindingStrategy {
_shellDescriptor.Features.Any(sf => sf.Name == fd.Id); _shellDescriptor.Features.Any(sf => sf.Name == fd.Id);
} }
} }
// discovers .css files and turns them into Style__<filename> shapes.
public class StylesheetBindingStrategy : StaticFileBindingStrategy, IShapeTableProvider {
public StylesheetBindingStrategy(IExtensionManager extensionManager, ShellDescriptor shellDescriptor, IVirtualPathProvider virtualPathProvider) : base(extensionManager, shellDescriptor, virtualPathProvider) {
}
public override string GetFileExtension() {
return ".css";
}
public override string GetFolder() {
return "Styles";
}
public override string GetShapePrefix() {
return "Style__";
}
}
// discovers .css files and turns them into Style__<filename> shapes.
public class ScriptBindingStrategy : StaticFileBindingStrategy, IShapeTableProvider {
public ScriptBindingStrategy(IExtensionManager extensionManager, ShellDescriptor shellDescriptor, IVirtualPathProvider virtualPathProvider)
: base(extensionManager, shellDescriptor, virtualPathProvider) {
}
public override string GetFileExtension() {
return ".js";
}
public override string GetFolder() {
return "Scripts";
}
public override string GetShapePrefix() {
return "Script__";
}
}
} }