From 789b00a0f3f0317ab70f5fa13dadcd96490d79c7 Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Tue, 12 Oct 2010 15:57:22 -0700 Subject: [PATCH] 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 --- src/Orchard.Web/Core/Shapes/CoreShapes.cs | 55 +----------------- .../Orchard.Widgets/Views/Admin/Index.cshtml | 2 +- src/Orchard/Orchard.Framework.csproj | 1 + src/Orchard/UI/FlatPositionComparer.cs | 56 +++++++++++++++++++ 4 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 src/Orchard/UI/FlatPositionComparer.cs diff --git a/src/Orchard.Web/Core/Shapes/CoreShapes.cs b/src/Orchard.Web/Core/Shapes/CoreShapes.cs index 620905093..fefbaec10 100644 --- a/src/Orchard.Web/Core/Shapes/CoreShapes.cs +++ b/src/Orchard.Web/Core/Shapes/CoreShapes.cs @@ -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 { - 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] diff --git a/src/Orchard.Web/Modules/Orchard.Widgets/Views/Admin/Index.cshtml b/src/Orchard.Web/Modules/Orchard.Widgets/Views/Admin/Index.cshtml index 717f6bf25..cd53940cf 100644 --- a/src/Orchard.Web/Modules/Orchard.Widgets/Views/Admin/Index.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Widgets/Views/Admin/Index.cshtml @@ -48,7 +48,7 @@
  • @zone
      - @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())) {
    • @if (widget.Position != "1") { diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index d5f2f1843..7be525f4a 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -181,6 +181,7 @@ + diff --git a/src/Orchard/UI/FlatPositionComparer.cs b/src/Orchard/UI/FlatPositionComparer.cs new file mode 100644 index 000000000..e5b5f7929 --- /dev/null +++ b/src/Orchard/UI/FlatPositionComparer.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Orchard.UI { + public class FlatPositionComparer : IComparer { + 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; + } + } +} \ No newline at end of file