Shape alterations can provide displaying/displayed delegates

Enables components to add shapes binding alternates
Also provides a hook for the just-before-display preprocessing

--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-10-07 16:10:36 -07:00
parent dc61c92f31
commit d1fb5fe86a
2 changed files with 47 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Autofac;
using NUnit.Framework;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Implementation;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Tests.DisplayManagement.Descriptors {
@@ -12,7 +13,9 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
builder.RegisterType<DefaultShapeTableManager>().As<IShapeTableManager>();
builder.RegisterType<TestShapeProvider>().As<IShapeTableProvider>()
.WithMetadata("Feature", TestFeature());
.WithMetadata("Feature", TestFeature())
.As<TestShapeProvider>()
.InstancePerLifetimeScope();
}
static Feature TestFeature() {
@@ -29,8 +32,12 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
}
public class TestShapeProvider : IShapeTableProvider {
public void Discover(ShapeTableBuilder builder) {
public Action<ShapeTableBuilder> Discover = x => { };
void IShapeTableProvider.Discover(ShapeTableBuilder builder) {
builder.Describe("Hello");
Discover(builder);
}
}
@@ -48,5 +55,29 @@ namespace Orchard.Tests.DisplayManagement.Descriptors {
Assert.That(shapeTable1.Descriptors["Hello"], Is.Not.Null);
Assert.That(shapeTable2.Descriptors["Hello"], Is.Not.Null);
}
[Test]
public void CallbackAlterationsContributeToDescriptor() {
Action<ShapeCreatingContext> cb1 = x => { };
Action<ShapeCreatedContext> cb2 = x => { };
Action<ShapeDisplayingContext> cb3 = x => { };
Action<ShapeDisplayedContext> cb4 = x => { };
_container.Resolve<TestShapeProvider>().Discover =
builder => builder.Describe("Foo")
.OnCreating(cb1)
.OnCreated(cb2)
.OnDisplaying(cb3)
.OnDisplayed(cb4);
var manager = _container.Resolve<IShapeTableManager>();
var foo = manager.GetShapeTable(null).Descriptors["Foo"];
Assert.That(foo.Creating.Single(), Is.SameAs(cb1));
Assert.That(foo.Created.Single(), Is.SameAs(cb2));
Assert.That(foo.Displaying.Single(), Is.SameAs(cb3));
Assert.That(foo.Displayed.Single(), Is.SameAs(cb4));
}
}
}

View File

@@ -73,6 +73,20 @@ namespace Orchard.DisplayManagement.Descriptors {
descriptor.Created = existing.Concat(new[] { action });
});
}
public ShapeAlterationBuilder OnDisplaying(Action<ShapeDisplayingContext> action) {
return Configure(descriptor => {
var existing = descriptor.Displaying ?? Enumerable.Empty<Action<ShapeDisplayingContext>>();
descriptor.Displaying = existing.Concat(new[] { action });
});
}
public ShapeAlterationBuilder OnDisplayed(Action<ShapeDisplayedContext> action) {
return Configure(descriptor => {
var existing = descriptor.Displayed ?? Enumerable.Empty<Action<ShapeDisplayedContext>>();
descriptor.Displayed = existing.Concat(new[] { action });
});
}
public ShapeAlteration Build() {
return new ShapeAlteration(_shapeType, _feature, _configurations.ToArray());