Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carlwoodhouse/409f9a5d7e909af0190590592f3b9208 to your computer and use it in GitHub Desktop.
Save carlwoodhouse/409f9a5d7e909af0190590592f3b9208 to your computer and use it in GitHub Desktop.
@Patient3.Helpers.ViewHelper.DisplayChildrenOrdered(Model, WorkContext, Display)
using System.Web;
using Orchard;
using Orchard.Mvc.Spooling;
namespace Patient3.Helpers {
public static class ViewHelper {
public static IHtmlString DisplayChildrenOrdered(dynamic model, WorkContext workContext, dynamic display) {
var writer = new HtmlStringWriter();
foreach (var item in Patient.Core.OrchardIntegration.ZoneWriter.ResolveShapeOrder(model)) {
writer.Write(display(item));
}
return writer;
}
}
}
namespace Patient.Core.OrchardIntegration
{
using System.Collections.Generic;
using System.Linq;
using Orchard.UI;
public static class ZoneWriter
{
public static IEnumerable<dynamic> ResolveShapeOrder(dynamic shape)
{
IEnumerable<dynamic> unordered = shape;
if (unordered == null || unordered.Count() < 2)
return shape;
var i = 1;
var progress = 1;
var flatPositionComparer = new FlatPositionComparer();
var ordering = unordered.Select(item =>
{
var position = (item == null || item.GetType().GetProperty("Metadata") == null || item.Metadata.GetType().GetProperty("Position") == null)
? null
: item.Metadata.Position;
return new { item, position };
}).ToList();
while (i < ordering.Count())
{
if (flatPositionComparer.Compare(ordering[i].position, ordering[i - 1].position) > -1)
{
if (i == progress)
progress = ++i;
else
i = progress;
}
else
{
var higherThanItShouldBe = ordering[i];
ordering[i] = ordering[i - 1];
ordering[i - 1] = higherThanItShouldBe;
if (i > 1)
--i;
}
}
return ordering.Select(ordered => ordered.item).ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment