Adding support for Tabs and Groups in placement

It provides a way to define groups in placement files,
which is necessary for fields (there is no current solution).

Tab is not yet used.
This commit is contained in:
Sebastien Ros
2013-10-21 15:28:37 -07:00
parent 2d3145c754
commit 74fe95ddd9
4 changed files with 136 additions and 16 deletions

View File

@@ -0,0 +1,60 @@
using NUnit.Framework;
using Orchard.DisplayManagement.Descriptors;
namespace Orchard.Tests.DisplayManagement.Descriptors {
[TestFixture]
public class LocationParserTests : ContainerTestBase {
[Test]
public void ZoneShouldBeParsed() {
Assert.That(new PlacementInfo { Location = "Content" }.GetZone(), Is.EqualTo("Content"));
Assert.That(new PlacementInfo { Location = "Content:5" }.GetZone(), Is.EqualTo("Content"));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1" }.GetZone(), Is.EqualTo("Content"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1" }.GetZone(), Is.EqualTo("Content"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1#Tab1" }.GetZone(), Is.EqualTo("Content"));
}
[Test]
public void PositionShouldBeParsed() {
Assert.That(new PlacementInfo { Location = "Content" }.GetPosition(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5" }.GetPosition(), Is.EqualTo("5"));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1" }.GetPosition(), Is.EqualTo("5"));
Assert.That(new PlacementInfo { Location = "Content:5.1#Tab1" }.GetPosition(), Is.EqualTo("5.1"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1" }.GetPosition(), Is.EqualTo("5"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1#Tab1" }.GetPosition(), Is.EqualTo("5"));
}
[Test]
public void LayoutZoneShouldBeParsed() {
Assert.That(new PlacementInfo { Location = "/Content" }.IsLayoutZone(), Is.EqualTo(true));
Assert.That(new PlacementInfo { Location = "/Content:5" }.IsLayoutZone(), Is.EqualTo(true));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1" }.IsLayoutZone(), Is.EqualTo(false));
Assert.That(new PlacementInfo { Location = "Content:5.1#Tab1" }.IsLayoutZone(), Is.EqualTo(false));
Assert.That(new PlacementInfo { Location = "Content:5@Group1" }.IsLayoutZone(), Is.EqualTo(false));
Assert.That(new PlacementInfo { Location = "Content:5@Group1#Tab1" }.IsLayoutZone(), Is.EqualTo(false));
}
[Test]
public void TabShouldBeParsed() {
Assert.That(new PlacementInfo { Location = "Content" }.GetTab(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5" }.GetTab(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1" }.GetTab(), Is.EqualTo("Tab1"));
Assert.That(new PlacementInfo { Location = "Content:5.1#Tab1" }.GetTab(), Is.EqualTo("Tab1"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1" }.GetTab(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5@Group1#Tab1" }.GetTab(), Is.EqualTo("Tab1"));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1@Group1" }.GetTab(), Is.EqualTo("Tab1"));
}
[Test]
public void GroupShouldBeParsed() {
Assert.That(new PlacementInfo { Location = "Content" }.GetGroup(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5" }.GetGroup(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1" }.GetGroup(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5.1#Tab1" }.GetGroup(), Is.EqualTo(""));
Assert.That(new PlacementInfo { Location = "Content:5@Group1" }.GetGroup(), Is.EqualTo("Group1"));
Assert.That(new PlacementInfo { Location = "Content:5@Group1#Tab1" }.GetGroup(), Is.EqualTo("Group1"));
Assert.That(new PlacementInfo { Location = "Content:5#Tab1@Group1" }.GetGroup(), Is.EqualTo("Group1"));
}
}
}

View File

@@ -218,6 +218,7 @@
<Compile Include="Data\StubLocator.cs" />
<Compile Include="DisplayManagement\ArgsUtility.cs" />
<Compile Include="DisplayManagement\CompositeTests.cs" />
<Compile Include="DisplayManagement\Descriptors\LocationParserTests.cs" />
<Compile Include="DisplayManagement\NilTests.cs" />
<Compile Include="DisplayManagement\ZoneHoldingTests.cs" />
<Compile Include="DisplayManagement\DefaultDisplayManagerTests.cs" />

View File

@@ -26,9 +26,6 @@ namespace Orchard.ContentManagement.Drivers {
}
private void ApplyImplementation(BuildShapeContext context, string displayType) {
if (!string.Equals(context.GroupId ?? "", _groupId ?? "", StringComparison.OrdinalIgnoreCase))
return;
var placement = context.FindPlacement(_shapeType, _differentiator, _defaultLocation);
if (string.IsNullOrEmpty(placement.Location) || placement.Location == "-")
return;
@@ -65,6 +62,15 @@ namespace Orchard.ContentManagement.Drivers {
newShapeMetadata.Wrappers.Clear();
}
// parse group placement
var group = placement.GetGroup();
if (!String.IsNullOrEmpty(group)) {
_groupId = group;
}
if (!string.Equals(context.GroupId ?? "", _groupId ?? "", StringComparison.OrdinalIgnoreCase))
return;
foreach (var alternate in placement.Alternates) {
newShapeMetadata.Alternates.Add(alternate);
}
@@ -73,23 +79,19 @@ namespace Orchard.ContentManagement.Drivers {
newShapeMetadata.Wrappers.Add(wrapper);
}
// copy the current location for further processing
var localPlacement = placement.Location;
// the zone name is in reference of Layout, e.g. /AsideSecond
if (placement.Location.StartsWith("/")) {
localPlacement = placement.Location.Substring(1);
if (placement.IsLayoutZone()) {
parentShape = context.Layout;
}
var delimiterIndex = localPlacement.IndexOf(':');
if (delimiterIndex < 0) {
parentShape.Zones[localPlacement].Add(newShape);
var position = placement.GetPosition();
var zone = placement.GetZone();
if (String.IsNullOrEmpty(position)) {
parentShape.Zones[zone].Add(newShape);
}
else {
var zoneName = localPlacement.Substring(0, delimiterIndex);
var position = localPlacement.Substring(delimiterIndex + 1);
parentShape.Zones[zoneName].Add(newShape, position);
parentShape.Zones[zone].Add(newShape, position);
}
}

View File

@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Orchard.DisplayManagement.Descriptors {
public class PlacementInfo {
private static readonly char[] Delimiters = {':', '#', '@'};
public PlacementInfo() {
Alternates = Enumerable.Empty<string>();
Wrappers = Enumerable.Empty<string>();
@@ -10,9 +13,63 @@ namespace Orchard.DisplayManagement.Descriptors {
public string Location { get; set; }
public string Source { get; set; }
public string ShapeType { get; set; }
public IEnumerable<string> Alternates { get; set; }
public IEnumerable<string> Wrappers { get; set; }
public string GetZone() {
var firstDelimiter = Location.IndexOfAny(Delimiters);
if (firstDelimiter == -1) {
return Location.TrimStart('/');
}
return Location.Substring(0, firstDelimiter).TrimStart('/');
}
public string GetPosition() {
var contentDelimiter = Location.IndexOf(':');
if (contentDelimiter == -1) {
return "";
}
var secondDelimiter = Location.IndexOfAny(Delimiters, contentDelimiter + 1);
if (secondDelimiter == -1) {
return Location.Substring(contentDelimiter + 1);
}
return Location.Substring(contentDelimiter + 1, secondDelimiter - contentDelimiter - 1);
}
public bool IsLayoutZone() {
return GetZone().StartsWith("/");
}
public string GetTab() {
var tabDelimiter = Location.IndexOf('#');
if (tabDelimiter == -1) {
return "";
}
var nextDelimiter = Location.IndexOfAny(Delimiters, tabDelimiter + 1);
if (nextDelimiter == -1) {
return Location.Substring(tabDelimiter + 1);
}
return Location.Substring(tabDelimiter + 1, nextDelimiter - tabDelimiter - 1);
}
public string GetGroup() {
var groupDelimiter = Location.IndexOf('@');
if (groupDelimiter == -1) {
return "";
}
var nextDelimiter = Location.IndexOfAny(Delimiters, groupDelimiter + 1);
if (nextDelimiter == -1) {
return Location.Substring(groupDelimiter + 1);
}
return Location.Substring(groupDelimiter + 1, nextDelimiter - groupDelimiter - 1);
}
}
}