Pulling the FlatPositionComparer* out of Orchard.Core's CoreShapes and changing the widget managemen index to use that

* need to reconcile this and the Orchard.UI.Navigation.PositionComparer and make the comparer work with negative numbers w/ null/""/0 as the default

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-10-12 15:57:22 -07:00
parent 7540e437b8
commit 789b00a0f3
4 changed files with 59 additions and 55 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
@@ -10,7 +9,6 @@ using System.Web.Mvc.Html;
using Orchard.DisplayManagement;
using Orchard.DisplayManagement.Descriptors;
using Orchard.DisplayManagement.Implementation;
using Orchard.DisplayManagement.Shapes;
using Orchard.Settings;
using Orchard.UI;
using Orchard.UI.Resources;
@@ -158,57 +156,6 @@ namespace Orchard.Core.Shapes {
return ordering.Select(ordered => ordered.item).ToList();
}
private class FlatPositionComparer : IComparer<string> {
public int Compare(string x, string y) {
if (x == y)
return 0;
// "" == "5"
x = string.IsNullOrWhiteSpace(x) ? "5" : x.TrimStart(':'); // ':' is _sometimes_ used as a partition identifier
y = string.IsNullOrWhiteSpace(y) ? "5" : y.TrimStart(':');
var xParts = x.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
var yParts = y.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < xParts.Count(); i++) {
if (yParts.Length < i - 1) // x is further defined meaning it comes after y (e.g. x == 1.2.3 and y == 1.2)
return 1;
int xPos;
int yPos;
xParts[i] = normalizeKnownPartitions(xParts[i]);
yParts[i] = normalizeKnownPartitions(yParts[i]);
var xIsInt = int.TryParse(xParts[i], out xPos);
var yIsInt = int.TryParse(yParts[i], out yPos);
if (!xIsInt && !yIsInt)
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
if (!xIsInt || (yIsInt && xPos > yPos)) // non-int after int or greater x pos than y pos (which is an int)
return 1;
if (!yIsInt || xPos < yPos)
return -1;
}
if (xParts.Length < yParts.Length) // all things being equal y might be further defined than x (e.g. x == 1.2 and y == 1.2.3)
return -1;
return 0;
}
private static string normalizeKnownPartitions(string partition) {
if (partition.Length < 5) // known partitions are long
return partition;
if (string.Compare(partition, "before", StringComparison.OrdinalIgnoreCase) == 0)
return "-9999";
if (string.Compare(partition, "after", StringComparison.OrdinalIgnoreCase) == 0)
return "9999";
return partition;
}
}
#endregion
[Shape]

View File

@@ -48,7 +48,7 @@
<li>
<div class="widgets-zone">@zone</div>
<ul>
@foreach (WidgetPart widget in Model.CurrentLayerWidgets.Where(widgetPart => widgetPart.Zone == zone).OrderBy(widgetPart => widgetPart.Position)) {
@foreach (WidgetPart widget in Model.CurrentLayerWidgets.Where(widgetPart => widgetPart.Zone == zone).OrderBy(widgetPart => widgetPart.Position, new Orchard.UI.FlatPositionComparer())) {
<li class="widgets-zoneWidget">
@if (widget.Position != "1") {
<input type="image" name="submit.MoveUp.@widget.Id" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/moveup.gif")" alt="Move up" value="@widget.Id" />

View File

@@ -181,6 +181,7 @@
<Compile Include="Security\CurrentUserWorkContext.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="UI\FlatPositionComparer.cs" />
<Compile Include="UI\Resources\IResourceManifestProvider.cs" />
<Compile Include="UI\Resources\ResourceManifestBuilder.cs" />
<Compile Include="UI\Widgets\IRuleManager.cs" />

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Orchard.UI {
public class FlatPositionComparer : IComparer<string> {
public int Compare(string x, string y) {
if (x == y)
return 0;
// "" == "5"
x = string.IsNullOrWhiteSpace(x) ? "5" : x.TrimStart(':'); // ':' is _sometimes_ used as a partition identifier
y = string.IsNullOrWhiteSpace(y) ? "5" : y.TrimStart(':');
var xParts = x.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
var yParts = y.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < xParts.Count(); i++) {
if (yParts.Length < i - 1) // x is further defined meaning it comes after y (e.g. x == 1.2.3 and y == 1.2)
return 1;
int xPos;
int yPos;
xParts[i] = NormalizeKnownPartitions(xParts[i]);
yParts[i] = NormalizeKnownPartitions(yParts[i]);
var xIsInt = int.TryParse(xParts[i], out xPos);
var yIsInt = int.TryParse(yParts[i], out yPos);
if (!xIsInt && !yIsInt)
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
if (!xIsInt || (yIsInt && xPos > yPos)) // non-int after int or greater x pos than y pos (which is an int)
return 1;
if (!yIsInt || xPos < yPos)
return -1;
}
if (xParts.Length < yParts.Length) // all things being equal y might be further defined than x (e.g. x == 1.2 and y == 1.2.3)
return -1;
return 0;
}
private static string NormalizeKnownPartitions(string partition) {
if (partition.Length < 5) // known partitions are long
return partition;
if (string.Compare(partition, "before", StringComparison.OrdinalIgnoreCase) == 0)
return "-9999";
if (string.Compare(partition, "after", StringComparison.OrdinalIgnoreCase) == 0)
return "9999";
return partition;
}
}
}