mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
Converting ItemDisplayModel's Displays collection into a Zones collection. Changing display item templates to use Zone helpers.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044547
This commit is contained in:
@@ -16,7 +16,7 @@ namespace Orchard.Core.Common.Providers {
|
||||
|
||||
OnGetDisplayViewModel<BodyAspect>((context, body) => {
|
||||
var model = new BodyDisplayViewModel { BodyAspect = body };
|
||||
context.AddDisplay(new TemplateViewModel(model, TemplatePrefix) { TemplateName = TemplateName });
|
||||
context.AddDisplay(new TemplateViewModel(model, TemplatePrefix) { TemplateName = TemplateName, ZoneName = "body" });
|
||||
});
|
||||
|
||||
OnGetEditorViewModel<BodyAspect>((context, body) => {
|
||||
|
@@ -10,7 +10,7 @@
|
||||
<p><%=Model.Item.Description%></p>
|
||||
<div class="actions"><a href="<%=Url.BlogPostCreate(Model.Item.Slug) %>" class="add button">New Post</a></div>
|
||||
<%--TODO: (erikpo) Need to figure out which zones should be displayed in this template--%>
|
||||
<%=Html.DisplayZonesAny() %>
|
||||
<% Html.ZonesAny(); %>
|
||||
|
||||
<%--<%
|
||||
if (Model.Posts.Count() > 0) { %>
|
||||
|
@@ -6,4 +6,4 @@
|
||||
<h1><%=Html.TitleForPage(Model.Item.Name) %></h1>
|
||||
<div><%=Html.Encode(Model.Item.Description) %></div>
|
||||
<%--TODO: (erikpo) Need to figure out which zones should be displayed in this template--%>
|
||||
<%=Html.DisplayZonesAny() %>
|
||||
<% Html.ZonesAny(); %>
|
@@ -10,4 +10,4 @@
|
||||
%><div class="posted">Posted by <%=Html.Encode(Model.Item.Creator.UserName)%> <%=Html.PublishedWhen(Model.Item)%></div><%
|
||||
} %>
|
||||
</div>
|
||||
<%=Html.DisplayZonesAny() %>
|
||||
<% Html.ZonesAny(); %>
|
@@ -15,7 +15,7 @@ namespace Orchard.Comments.Controllers {
|
||||
PartTemplate(part, "Parts/Comments.HasComments").Location("body", "below.5"));
|
||||
}
|
||||
|
||||
return PartTemplate(part, "Parts/Comments.HasComments").Location("body", "below.5");
|
||||
return PartTemplate(part, "Parts/Comments.Count").Location("body", "above.5");
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(HasComments part) {
|
||||
|
@@ -6,6 +6,7 @@ using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.ContentManagement.ViewModels;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.UI.Zones;
|
||||
|
||||
namespace Orchard.DevTools.ViewModels {
|
||||
public class ContentDetailsViewModel : BaseViewModel {
|
||||
@@ -17,7 +18,20 @@ namespace Orchard.DevTools.ViewModels {
|
||||
|
||||
public ItemEditorModel EditorModel { get; set; }
|
||||
|
||||
public IEnumerable<TemplateViewModel> Displays { get { return DisplayModel.Displays; } }
|
||||
public IEnumerable<TemplateViewModel> Displays {
|
||||
get {
|
||||
return DisplayModel.Zones
|
||||
.SelectMany(z => z.Value.Items
|
||||
.OfType<PartDisplayZoneItem>()
|
||||
.Select(x=>new{ZoneName=z.Key,Item=x}))
|
||||
.Select(x => new TemplateViewModel(x.Item.Model,x.Item.Prefix) {
|
||||
Model = x.Item.Model,
|
||||
TemplateName=x.Item.TemplateName,
|
||||
WasUsed=x.Item.WasExecuted,
|
||||
ZoneName=x.ZoneName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<TemplateViewModel> Editors { get { return EditorModel.Editors; } }
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
<%@ Import Namespace="Orchard.ContentManagement.ViewModels" %>
|
||||
|
||||
<div class="item">
|
||||
<%=Html.DisplayZone("title") %>
|
||||
<%=Html.DisplayZone("metatop")%>
|
||||
<%=Html.DisplayZone("body") %>
|
||||
<% Html.Zone("title"); %>
|
||||
<% Html.Zone("metatop"); %>
|
||||
<% Html.Zone("body"); %>
|
||||
</div>
|
||||
|
@@ -2,22 +2,22 @@
|
||||
<%@ Import Namespace="Orchard.Sandbox.Models" %>
|
||||
<%@ Import Namespace="Orchard.ContentManagement.ViewModels" %>
|
||||
<div class="item">
|
||||
<%=Html.DisplayZone("first")%>
|
||||
<% Html.Zone("first"); %>
|
||||
<div class="title">
|
||||
<%=Html.DisplayZone("title")%>
|
||||
<% Html.Zone("title"); %>
|
||||
</div>
|
||||
<%=Html.DisplayZone("metatop")%>
|
||||
<% Html.Zone("metatop"); %>
|
||||
<div class="actions">
|
||||
<%=Html.ItemEditLink("Edit this page", Model.Item) %>
|
||||
<%=Html.ActionLink("Return to list", "index") %>
|
||||
<%=Html.DisplayZone("actions") %>
|
||||
<% Html.Zone("actions"); %>
|
||||
</div>
|
||||
<div class="body">
|
||||
<%=Html.DisplayZone("body")%>
|
||||
<% Html.Zone("body"); %>
|
||||
</div>
|
||||
<%=Html.DisplayZone("metabottom")%>
|
||||
<% Html.Zone("metabottom"); %>
|
||||
<div class="footer">
|
||||
<%=Html.DisplayZonesExcept("last") %>
|
||||
<%=Html.DisplayZone("last")%>
|
||||
<% Html.ZonesExcept("last"); %>
|
||||
<% Html.Zone("last"); %>
|
||||
</div>
|
||||
</div>
|
||||
|
@@ -137,12 +137,11 @@ namespace Orchard.ContentManagement {
|
||||
}
|
||||
|
||||
public ItemDisplayModel<TContentPart> BuildDisplayModel<TContentPart>(TContentPart content, string displayType) where TContentPart : IContent {
|
||||
var itemView = new ItemDisplayModel<TContentPart> {Item = content, Displays = Enumerable.Empty<TemplateViewModel>()};
|
||||
var itemView = new ItemDisplayModel<TContentPart> {Item = content};
|
||||
var context = new BuildDisplayModelContext(itemView, displayType);
|
||||
foreach (var handler in Handlers) {
|
||||
handler.BuildDisplayModel(context);
|
||||
}
|
||||
context.DisplayModel.Displays = OrderTemplates(context.DisplayModel.Displays);
|
||||
return itemView;
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,12 @@ namespace Orchard.ContentManagement.Handlers {
|
||||
public ItemDisplayModel DisplayModel { get; private set; }
|
||||
|
||||
public void AddDisplay(TemplateViewModel display) {
|
||||
DisplayModel.Displays = DisplayModel.Displays.Concat(new[] { display });
|
||||
//TEMP: (loudej) transition code - from TemplateViewMode to ZoneItem
|
||||
DisplayModel.Zones.AddDisplayPart(
|
||||
display.ZoneName+":"+display.Position,
|
||||
display.Model,
|
||||
display.TemplateName,
|
||||
display.Prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,24 +2,26 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.UI.Zones;
|
||||
|
||||
namespace Orchard.ContentManagement.ViewModels {
|
||||
public class ItemDisplayModel {
|
||||
public class ItemDisplayModel : IZoneContainer {
|
||||
private ContentItem _item;
|
||||
|
||||
protected ItemDisplayModel() {
|
||||
Zones = new ZoneCollection();
|
||||
}
|
||||
|
||||
protected ItemDisplayModel(ItemDisplayModel displayModel) {
|
||||
TemplateName = displayModel.TemplateName;
|
||||
Prefix = displayModel.Prefix;
|
||||
Displays = displayModel.Displays.ToArray();
|
||||
Zones = displayModel.Zones;
|
||||
Item = displayModel.Item;
|
||||
}
|
||||
|
||||
public ItemDisplayModel(ContentItem item) {
|
||||
Item = item;
|
||||
Displays = Enumerable.Empty<TemplateViewModel>();
|
||||
Zones = new ZoneCollection();
|
||||
}
|
||||
|
||||
public ContentItem Item {
|
||||
@@ -34,7 +36,24 @@ namespace Orchard.ContentManagement.ViewModels {
|
||||
public Func<HtmlHelper, ItemDisplayModel, HtmlHelper> Adaptor { get; set; }
|
||||
public string TemplateName { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
public IEnumerable<TemplateViewModel> Displays { get; set; }
|
||||
|
||||
|
||||
public IEnumerable<TemplateViewModel> Displays {
|
||||
get {
|
||||
return Zones
|
||||
.SelectMany(z => z.Value.Items
|
||||
.OfType<PartDisplayZoneItem>()
|
||||
.Select(x=>new{ZoneName=z.Key,Item=x}))
|
||||
.Select(x => new TemplateViewModel(x.Item.Model,x.Item.Prefix) {
|
||||
Model = x.Item.Model,
|
||||
TemplateName=x.Item.TemplateName,
|
||||
WasUsed=x.Item.WasExecuted,
|
||||
ZoneName=x.ZoneName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ZoneCollection Zones { get; set; }
|
||||
}
|
||||
|
||||
public class ItemDisplayModel<TPart> : ItemDisplayModel where TPart : IContent {
|
||||
|
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
@@ -11,6 +9,7 @@ namespace Orchard.Mvc.Html {
|
||||
public static MvcHtmlString DisplayForItem<TModel, TItemModel>(this HtmlHelper<TModel> html, TItemModel item) where TItemModel : ItemDisplayModel {
|
||||
return html.DisplayForItem(x => item);
|
||||
}
|
||||
|
||||
public static MvcHtmlString DisplayForItem<TModel, TItemModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, TItemModel>> expression) where TItemModel : ItemDisplayModel {
|
||||
|
||||
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
|
||||
@@ -24,44 +23,8 @@ namespace Orchard.Mvc.Html {
|
||||
}
|
||||
|
||||
|
||||
public static MvcHtmlString DisplayZone<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel : ItemDisplayModel {
|
||||
var templates = html.ViewData.Model.Displays.Where(x => x.ZoneName == zoneName && x.WasUsed == false);
|
||||
return DisplayZoneImplementation(html, templates);
|
||||
}
|
||||
|
||||
public static MvcHtmlString DisplayZonesAny<TModel>(this HtmlHelper<TModel> html) where TModel : ItemDisplayModel {
|
||||
var templates = html.ViewData.Model.Displays.Where(x => x.WasUsed == false);
|
||||
return DisplayZoneImplementation(html, templates);
|
||||
}
|
||||
|
||||
public static MvcHtmlString DisplayZones<TModel>(this HtmlHelper<TModel> html, params string[] include) where TModel : ItemDisplayModel {
|
||||
var templates = html.ViewData.Model.Displays.Where(x => include.Contains(x.ZoneName) && x.WasUsed == false);
|
||||
return DisplayZoneImplementation(html, templates);
|
||||
}
|
||||
|
||||
public static MvcHtmlString DisplayZonesExcept<TModel>(this HtmlHelper<TModel> html, params string[] exclude) where TModel : ItemDisplayModel {
|
||||
var templates = html.ViewData.Model.Displays.Where(x => !exclude.Contains(x.ZoneName) && x.WasUsed == false);
|
||||
return DisplayZoneImplementation(html, templates);
|
||||
}
|
||||
|
||||
private static MvcHtmlString DisplayZoneImplementation<TModel>(HtmlHelper<TModel> html, IEnumerable<TemplateViewModel> templates) {
|
||||
var count = templates.Count();
|
||||
if (count == 0)
|
||||
return null;
|
||||
|
||||
if (count == 1) {
|
||||
var t = templates.Single();
|
||||
t.WasUsed = true;
|
||||
return html.DisplayFor(m => t.Model, t.TemplateName, t.Prefix ?? "");
|
||||
}
|
||||
|
||||
var strings = new List<MvcHtmlString>();
|
||||
foreach (var template in templates) {
|
||||
var t = template;
|
||||
t.WasUsed = true;
|
||||
strings.Add(html.DisplayFor(m => t.Model, t.TemplateName, t.Prefix ?? ""));
|
||||
}
|
||||
return MvcHtmlString.Create(string.Concat(strings.ToArray()));
|
||||
public static void DisplayZonesAny<TModel>(this HtmlHelper<TModel> html) where TModel : ItemDisplayModel {
|
||||
html.ZonesAny();
|
||||
}
|
||||
}
|
||||
}
|
@@ -46,22 +46,31 @@ namespace Orchard.Mvc.Html {
|
||||
return MvcHtmlString.Create(html.Encode(titleParts[0]));
|
||||
}
|
||||
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : BaseViewModel {
|
||||
IZoneManager manager = html.Resolve<IZoneManager>();
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, string partitions) where TModel : IZoneContainer {
|
||||
var manager = html.Resolve<IZoneManager>();
|
||||
|
||||
manager.Render(html, html.ViewData.Model.Zones, zoneName, partitions);
|
||||
manager.Render(html, html.ViewData.Model.Zones, zoneName, partitions, null);
|
||||
}
|
||||
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel : BaseViewModel {
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel : IZoneContainer {
|
||||
html.Zone(zoneName, string.Empty);
|
||||
}
|
||||
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, Action action) where TModel : BaseViewModel {
|
||||
public static void Zone<TModel>(this HtmlHelper<TModel> html, string zoneName, Action action) where TModel : IZoneContainer {
|
||||
//TODO: again, IoC could move this AddAction (or similar) method out of the data-bearing object
|
||||
html.ViewData.Model.Zones.AddAction(zoneName, x => action());
|
||||
html.Zone(zoneName, string.Empty);
|
||||
}
|
||||
|
||||
public static void ZonesAny<TModel>(this HtmlHelper<TModel> html) where TModel : IZoneContainer {
|
||||
html.ZonesExcept();
|
||||
}
|
||||
|
||||
public static void ZonesExcept<TModel>(this HtmlHelper<TModel> html, params string[] except) where TModel : IZoneContainer {
|
||||
var manager = html.Resolve<IZoneManager>();
|
||||
manager.Render(html, html.ViewData.Model.Zones, null, null, except);
|
||||
}
|
||||
|
||||
public static void ZoneBody<TModel>(this HtmlHelper<TModel> html, string zoneName) where TModel : BaseViewModel {
|
||||
html.Zone(zoneName, () => html.RenderBody());
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ using Orchard.UI.Notify;
|
||||
using Orchard.UI.Zones;
|
||||
|
||||
namespace Orchard.Mvc.ViewModels {
|
||||
public class BaseViewModel {
|
||||
public class BaseViewModel : IZoneContainer {
|
||||
public BaseViewModel() {
|
||||
Messages = new List<NotifyEntry>();
|
||||
Zones = new ZoneCollection();
|
||||
|
@@ -251,6 +251,7 @@
|
||||
<Compile Include="UI\Zones\DelegateZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\ItemDisplayZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\IZoneManager.cs" />
|
||||
<Compile Include="UI\Zones\PartDisplayZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\RenderPartialZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\ZoneCollection.cs" />
|
||||
<Compile Include="UI\Zones\ZoneEntry.cs" />
|
||||
|
@@ -2,6 +2,6 @@ using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.UI.Zones {
|
||||
public interface IZoneManager : IDependency {
|
||||
void Render<TModel>(HtmlHelper<TModel> html, ZoneCollection zones, string zoneName, string partitions);
|
||||
void Render<TModel>(HtmlHelper<TModel> html, ZoneCollection zones, string zoneName, string partitions, string[] except);
|
||||
}
|
||||
}
|
15
src/Orchard/UI/Zones/PartDisplayZoneItem.cs
Normal file
15
src/Orchard/UI/Zones/PartDisplayZoneItem.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
|
||||
namespace Orchard.UI.Zones {
|
||||
public class PartDisplayZoneItem : ZoneItem {
|
||||
public object Model { get; set; }
|
||||
public string TemplateName { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
|
||||
public override void Execute<TModel>(HtmlHelper<TModel> html) {
|
||||
html.ViewContext.Writer.Write(
|
||||
html.DisplayFor(m => Model, TemplateName, Prefix));
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,12 +4,19 @@ using System.Web.Mvc;
|
||||
using Orchard.ContentManagement.ViewModels;
|
||||
|
||||
namespace Orchard.UI.Zones {
|
||||
public interface IZoneContainer {
|
||||
ZoneCollection Zones { get; }
|
||||
}
|
||||
|
||||
public class ZoneCollection : Dictionary<string, ZoneEntry> {
|
||||
public void AddRenderPartial(string location, string templateName, object model) {
|
||||
AddZoneItem(location, new RenderPartialZoneItem() { Model = model, TemplateName = templateName });
|
||||
AddZoneItem(location, new RenderPartialZoneItem { Model = model, TemplateName = templateName });
|
||||
}
|
||||
public void AddDisplayItem(string location, ItemDisplayModel displayModel) {
|
||||
AddZoneItem(location, new ItemDisplayZoneItem { DisplayModel = displayModel });
|
||||
AddZoneItem(location, new ItemDisplayZoneItem() { DisplayModel = displayModel });
|
||||
}
|
||||
public void AddDisplayPart(string location, object model, string templateName, string prefix) {
|
||||
AddZoneItem(location, new PartDisplayZoneItem { Model = model, TemplateName = templateName, Prefix = prefix });
|
||||
}
|
||||
public void AddAction(string location, Action<HtmlHelper> action) {
|
||||
AddZoneItem(location, new DelegateZoneItem { Action = action });
|
||||
|
@@ -2,16 +2,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.UI.Navigation;
|
||||
|
||||
namespace Orchard.UI.Zones {
|
||||
public class ZoneManager : IZoneManager {
|
||||
public void Render<TModel>(HtmlHelper<TModel> html, ZoneCollection zones, string zoneName, string partitions) {
|
||||
|
||||
ZoneEntry zone;
|
||||
if (!zones.TryGetValue(zoneName, out zone))
|
||||
public void Render<TModel>(HtmlHelper<TModel> html, ZoneCollection zones, string zoneName, string partitions, string[] exclude) {
|
||||
IEnumerable<Group> groups;
|
||||
if (string.IsNullOrEmpty(zoneName)) {
|
||||
var entries = zones.Values.Where(z => !exclude.Contains(z.ZoneName));
|
||||
groups = BuildGroups(partitions, entries);
|
||||
}
|
||||
else {
|
||||
ZoneEntry entry;
|
||||
if (!zones.TryGetValue(zoneName, out entry))
|
||||
return;
|
||||
|
||||
var groups = BuildGroups(partitions, zone);
|
||||
groups = BuildGroups(partitions, new[] { entry });
|
||||
}
|
||||
|
||||
foreach (var item in groups.SelectMany(x => x.Items)) {
|
||||
item.WasExecuted = true;
|
||||
@@ -20,14 +26,15 @@ namespace Orchard.UI.Zones {
|
||||
|
||||
}
|
||||
|
||||
private IEnumerable<Group> BuildGroups(string partitions, ZoneEntry zone) {
|
||||
private IEnumerable<Group> BuildGroups(string partitions, IEnumerable<ZoneEntry> zones) {
|
||||
|
||||
var partitionCodes = (":before " + partitions + " :* :after").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var itemsRemaining = zone.Items.Where(x => x.WasExecuted == false);
|
||||
var itemsRemaining = zones.SelectMany(zone => zone.Items.Where(x => x.WasExecuted == false));
|
||||
|
||||
Group catchAllItem = null;
|
||||
|
||||
var positionComparer = new PositionComparer();
|
||||
var results = new List<Group>();
|
||||
foreach (var code in partitionCodes) {
|
||||
if (code == ":*") {
|
||||
@@ -36,13 +43,17 @@ namespace Orchard.UI.Zones {
|
||||
}
|
||||
else {
|
||||
var value = code;
|
||||
var items = itemsRemaining.Where(x => (":" + x.Position).StartsWith(value));
|
||||
var items = itemsRemaining
|
||||
.Where(x => (":" + x.Position).StartsWith(value))
|
||||
.OrderBy(x => x.Position, positionComparer);
|
||||
results.Add(new Group { Items = items.ToArray() });
|
||||
itemsRemaining = itemsRemaining.Except(items).ToArray();
|
||||
}
|
||||
}
|
||||
if (catchAllItem != null) {
|
||||
catchAllItem.Items = itemsRemaining;
|
||||
catchAllItem.Items = itemsRemaining
|
||||
.OrderBy(x => x.Position, positionComparer)
|
||||
.ToArray();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
Reference in New Issue
Block a user