mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +08:00
Adding html helper for content item templates. Streamlining.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4043017
This commit is contained in:
@@ -32,6 +32,7 @@ namespace Orchard.Sandbox.Controllers {
|
|||||||
Pages = _contentManager.Query<SandboxPage, SandboxPageRecord>()
|
Pages = _contentManager.Query<SandboxPage, SandboxPageRecord>()
|
||||||
.OrderBy(x => x.Name)
|
.OrderBy(x => x.Name)
|
||||||
.List()
|
.List()
|
||||||
|
.Select(x => _contentManager.GetDisplays(x, null, "SummaryList"))
|
||||||
};
|
};
|
||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
@@ -39,8 +40,7 @@ namespace Orchard.Sandbox.Controllers {
|
|||||||
public ActionResult Show(int id) {
|
public ActionResult Show(int id) {
|
||||||
var page = _contentManager.Get<SandboxPage>(id);
|
var page = _contentManager.Get<SandboxPage>(id);
|
||||||
var model = new PageShowViewModel {
|
var model = new PageShowViewModel {
|
||||||
Page = page,
|
Page = _contentManager.GetDisplays(page, null, "Detail")
|
||||||
ItemView = _contentManager.GetDisplays(page, null, "Detail")
|
|
||||||
};
|
};
|
||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
@@ -78,8 +78,10 @@ namespace Orchard.Sandbox.Controllers {
|
|||||||
return RedirectToAction("show", new { id });
|
return RedirectToAction("show", new { id });
|
||||||
}
|
}
|
||||||
|
|
||||||
var model = new PageEditViewModel { Page = _contentManager.Get<SandboxPage>(id) };
|
var page = _contentManager.Get<SandboxPage>(id);
|
||||||
model.ItemView = _contentManager.GetEditors(model.Page, null);
|
var model = new PageEditViewModel {
|
||||||
|
Page = _contentManager.GetEditors(page, null)
|
||||||
|
};
|
||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +93,11 @@ namespace Orchard.Sandbox.Controllers {
|
|||||||
return RedirectToAction("show", new { id });
|
return RedirectToAction("show", new { id });
|
||||||
}
|
}
|
||||||
|
|
||||||
var model = new PageEditViewModel { Page = _contentManager.Get<SandboxPage>(id) };
|
var page = _contentManager.Get<SandboxPage>(id);
|
||||||
model.ItemView = _contentManager.UpdateEditors(model.Page, null, this);
|
var model = new PageEditViewModel {
|
||||||
if (!TryUpdateModel(model, input.ToValueProvider()))
|
Page = _contentManager.UpdateEditors(page, null, this)
|
||||||
|
};
|
||||||
|
if (!ModelState.IsValid)
|
||||||
return View(model);
|
return View(model);
|
||||||
|
|
||||||
return RedirectToAction("show", new { id });
|
return RedirectToAction("show", new { id });
|
||||||
|
@@ -4,7 +4,6 @@ using Orchard.Core.Common.Models;
|
|||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.Models;
|
using Orchard.Models;
|
||||||
using Orchard.Models.Driver;
|
using Orchard.Models.Driver;
|
||||||
using Orchard.Models.ViewModels;
|
|
||||||
|
|
||||||
namespace Orchard.Sandbox.Models {
|
namespace Orchard.Sandbox.Models {
|
||||||
public class SandboxContentProvider : ContentProvider {
|
public class SandboxContentProvider : ContentProvider {
|
||||||
@@ -23,6 +22,7 @@ namespace Orchard.Sandbox.Models {
|
|||||||
Filters.Add(new ActivatingFilter<RoutableAspect>(SandboxPage.ContentType.Name));
|
Filters.Add(new ActivatingFilter<RoutableAspect>(SandboxPage.ContentType.Name));
|
||||||
Filters.Add(new ActivatingFilter<BodyAspect>(SandboxPage.ContentType.Name));
|
Filters.Add(new ActivatingFilter<BodyAspect>(SandboxPage.ContentType.Name));
|
||||||
Filters.Add(new StorageFilter<SandboxPageRecord>(pageRepository) { AutomaticallyCreateMissingRecord = true });
|
Filters.Add(new StorageFilter<SandboxPageRecord>(pageRepository) { AutomaticallyCreateMissingRecord = true });
|
||||||
|
Filters.Add(new ContentItemTemplates<SandboxPage>("SandboxPage", "Summary"));
|
||||||
|
|
||||||
OnGetItemMetadata<SandboxPage>((context, page) => {
|
OnGetItemMetadata<SandboxPage>((context, page) => {
|
||||||
context.Metadata.DisplayText = page.Record.Name;
|
context.Metadata.DisplayText = page.Record.Name;
|
||||||
@@ -44,14 +44,8 @@ namespace Orchard.Sandbox.Models {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO: helper that glues this for free - include list of known-displaytype prefixes
|
|
||||||
|
|
||||||
OnGetDisplays<SandboxPage>((context, page) => context.ItemView.TemplateName = "SandboxPage" + context.DisplayType);
|
|
||||||
OnGetEditors<SandboxPage>((context, page) => context.ItemView.TemplateName = "SandboxPage");
|
|
||||||
OnUpdateEditors<SandboxPage>((context, page) => {
|
|
||||||
context.Updater.TryUpdateModel((ItemEditorViewModel<SandboxPage>)context.ItemView, "", null, null);
|
|
||||||
context.ItemView.TemplateName = "SandboxPage";
|
|
||||||
});
|
|
||||||
|
|
||||||
// add settings to site, and simple record-template gui
|
// add settings to site, and simple record-template gui
|
||||||
Filters.Add(new ActivatingFilter<ContentPart<SandboxSettingsRecord>>("site"));
|
Filters.Add(new ActivatingFilter<ContentPart<SandboxSettingsRecord>>("site"));
|
||||||
|
@@ -75,8 +75,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Package.txt" />
|
<Content Include="Package.txt" />
|
||||||
<Content Include="Views\Models\DisplayTemplates\SandboxPageList.ascx" />
|
<Content Include="Views\Models\DisplayTemplates\SandboxPageSummary.ascx" />
|
||||||
<Content Include="Views\Models\DisplayTemplates\SandboxPageDetail.ascx" />
|
<Content Include="Views\Models\DisplayTemplates\SandboxPage.ascx" />
|
||||||
<Content Include="Views\Models\EditorTemplates\SandboxPage.ascx" />
|
<Content Include="Views\Models\EditorTemplates\SandboxPage.ascx" />
|
||||||
<Content Include="Views\Models\EditorTemplates\SandboxSettingsRecord.ascx" />
|
<Content Include="Views\Models\EditorTemplates\SandboxSettingsRecord.ascx" />
|
||||||
<Content Include="Views\Page\Edit.aspx" />
|
<Content Include="Views\Page\Edit.aspx" />
|
||||||
|
@@ -6,7 +6,6 @@ using Orchard.Sandbox.Models;
|
|||||||
|
|
||||||
namespace Orchard.Sandbox.ViewModels {
|
namespace Orchard.Sandbox.ViewModels {
|
||||||
public class PageEditViewModel : BaseViewModel {
|
public class PageEditViewModel : BaseViewModel {
|
||||||
public SandboxPage Page { get; set; }
|
public ItemEditorViewModel<SandboxPage> Page { get; set; }
|
||||||
public ItemEditorViewModel ItemView { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Orchard.Models.ViewModels;
|
||||||
using Orchard.Mvc.ViewModels;
|
using Orchard.Mvc.ViewModels;
|
||||||
using Orchard.Sandbox.Models;
|
using Orchard.Sandbox.Models;
|
||||||
|
|
||||||
namespace Orchard.Sandbox.ViewModels {
|
namespace Orchard.Sandbox.ViewModels {
|
||||||
public class PageIndexViewModel : BaseViewModel {
|
public class PageIndexViewModel : BaseViewModel {
|
||||||
public IEnumerable<SandboxPage> Pages { get; set; }
|
public IEnumerable<ItemDisplayViewModel<SandboxPage>> Pages { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,6 @@ using Orchard.Sandbox.Models;
|
|||||||
|
|
||||||
namespace Orchard.Sandbox.ViewModels {
|
namespace Orchard.Sandbox.ViewModels {
|
||||||
public class PageShowViewModel : BaseViewModel {
|
public class PageShowViewModel : BaseViewModel {
|
||||||
public SandboxPage Page { get; set; }
|
public ItemDisplayViewModel<SandboxPage> Page { get; set; }
|
||||||
public ItemDisplayViewModel ItemView { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ItemDisplayViewModel<SandboxPage>>" %>
|
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ItemDisplayViewModel<SandboxPage>>" %>
|
||||||
|
<%@ Import Namespace="Orchard.Mvc.Html"%>
|
||||||
<%@ Import Namespace="Orchard.Sandbox.Models" %>
|
<%@ Import Namespace="Orchard.Sandbox.Models" %>
|
||||||
<%@ Import Namespace="Orchard.Models.ViewModels" %>
|
<%@ Import Namespace="Orchard.Models.ViewModels" %>
|
||||||
<%@ Import Namespace="Orchard.Models" %>
|
<%@ Import Namespace="Orchard.Models" %>
|
||||||
<h1><%=Html.Encode(Model.Item.Record.Name) %></h1>
|
<h2><%=Html.ItemDisplayLink(Model.Item) %></h2>
|
||||||
<%foreach (var display in Model.Displays) { %>
|
<%--<%foreach (var display in Model.Displays) { %>
|
||||||
<%=Html.DisplayFor(m=>display.Model, display.TemplateName, display.Prefix??"") %>
|
<%=Html.DisplayFor(m=>display.Model, display.TemplateName, display.Prefix??"") %>
|
||||||
<%} %>
|
<%} %>
|
||||||
|
--%>
|
@@ -23,19 +23,8 @@
|
|||||||
<h3>
|
<h3>
|
||||||
Edit Page</h3>
|
Edit Page</h3>
|
||||||
<%using (Html.BeginForm()) { %>
|
<%using (Html.BeginForm()) { %>
|
||||||
<ul>
|
<%=Html.EditorForItem(m=>m.Page) %>
|
||||||
<%=Html.EditorFor(m => m.ItemView, Model.ItemView.TemplateName, Model.ItemView.Prefix ?? "")%>
|
<input type="submit" name="submit" value="Save" />
|
||||||
|
|
||||||
<li><input type="submit" name="submit" value="Save" /></li>
|
|
||||||
|
|
||||||
<%-- <li>
|
|
||||||
<%=Html.LabelFor(x => x.Page.Record.Name)%><%=Html.EditorFor(x => x.Page.Record.Name)%></li>
|
|
||||||
<%foreach (var x in Model.ItemView.Editors) { %>
|
|
||||||
<%=Html.EditorFor(m=>x.Model, x.TemplateName, x.Prefix) %>
|
|
||||||
<%} %>
|
|
||||||
<li>
|
|
||||||
<input type="submit" name="submit" value="Save" /></li>--%>
|
|
||||||
</ul>
|
|
||||||
<%} %>
|
<%} %>
|
||||||
</div>
|
</div>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
|
@@ -22,12 +22,12 @@
|
|||||||
<% Html.RenderPartial("Messages", Model.Messages); %>
|
<% Html.RenderPartial("Messages", Model.Messages); %>
|
||||||
<h3>
|
<h3>
|
||||||
Sandbox Pages</h3>
|
Sandbox Pages</h3>
|
||||||
<ul>
|
|
||||||
<li>
|
<p><%=Html.ActionLink("Create new page", "create") %></p>
|
||||||
<%=Html.ActionLink("Create new page", "create") %></li>
|
|
||||||
<%foreach (var item in Model.Pages.OrderBy(x => x.Record.Name)) {%>
|
|
||||||
<li>
|
<%foreach (var item in Model.Pages) {%>
|
||||||
<%=Html.ActionLink(item.Record.Name??"(no name)", "show", new { item.ContentItem.Id }, new{})%></li>
|
<%=Html.DisplayForItem(x=>item) %>
|
||||||
<%}%>
|
<%}%>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -20,14 +20,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="main">
|
<div id="main">
|
||||||
<% Html.RenderPartial("Messages", Model.Messages); %>
|
<% Html.RenderPartial("Messages", Model.Messages); %>
|
||||||
<%=Html.DisplayFor(m => m.ItemView, Model.ItemView.TemplateName, "")%>
|
|
||||||
<%-- <h1><%=Html.Encode(Model.Page.Record.Name) %></h1>
|
<%= Html.DisplayForItem(m => m.Page) %>
|
||||||
<%foreach (var display in Model.ItemView.Displays) { %>
|
</div>
|
||||||
<%=Html.DisplayFor(m=>display.Model, display.TemplateName, display.Prefix) %>
|
|
||||||
<%} %>
|
|
||||||
<p>
|
|
||||||
<%=Html.ActionLink("Edit this page", "edit", new{Model.Page.ContentItem.Id}, new{}) %>, <%=Html.ActionLink("Return to list", "index") %></p>
|
|
||||||
--%> </div>
|
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<% Html.Include("footer"); %>
|
<% Html.Include("footer"); %>
|
||||||
</div>
|
</div>
|
||||||
|
46
src/Orchard/Models/Driver/ContentItemTemplates.cs
Normal file
46
src/Orchard/Models/Driver/ContentItemTemplates.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Orchard.Models.ViewModels;
|
||||||
|
|
||||||
|
namespace Orchard.Models.Driver {
|
||||||
|
public class ContentItemTemplates<TContent> : TemplateFilterBase<TContent> where TContent : class, IContent {
|
||||||
|
private readonly string _templateName;
|
||||||
|
private readonly string _prefix;
|
||||||
|
private readonly string[] _displayTypes;
|
||||||
|
private Action<UpdateContentContext, ItemEditorViewModel<TContent>> _updater;
|
||||||
|
|
||||||
|
public ContentItemTemplates(string templateName, params string[] displayTypes) {
|
||||||
|
_templateName = templateName;
|
||||||
|
_displayTypes = displayTypes;
|
||||||
|
_updater = (context, viewModel) => context.Updater.TryUpdateModel(viewModel, "", null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GetDisplays(GetDisplaysContext context, TContent instance) {
|
||||||
|
var longestMatch = LongestMatch(context.DisplayType);
|
||||||
|
context.ItemView.TemplateName = _templateName + longestMatch;
|
||||||
|
context.ItemView.Prefix = _prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string LongestMatch(string displayType) {
|
||||||
|
return _displayTypes.Aggregate("", (best, x) => {
|
||||||
|
if (displayType.StartsWith(x) && x.Length > best.Length) return x;
|
||||||
|
return best;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GetEditors(GetEditorsContext context, TContent instance) {
|
||||||
|
context.ItemView.TemplateName = _templateName;
|
||||||
|
context.ItemView.Prefix = _prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void UpdateEditors(UpdateContentContext context, TContent instance) {
|
||||||
|
_updater(context, (ItemEditorViewModel<TContent>)context.ItemView);
|
||||||
|
context.ItemView.TemplateName = _templateName;
|
||||||
|
context.ItemView.Prefix = _prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Updater(Action<UpdateContentContext, ItemEditorViewModel<TContent>> updater) {
|
||||||
|
_updater = updater;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -20,7 +20,7 @@ namespace Orchard.Mvc.Html {
|
|||||||
|
|
||||||
return html.ActionLink(
|
return html.ActionLink(
|
||||||
linkText ?? metadata.DisplayText,
|
linkText ?? metadata.DisplayText,
|
||||||
Convert.ToString(metadata.DisplayRouteValues["action"]),
|
Convert.ToString(metadata.DisplayRouteValues["action"]),
|
||||||
metadata.DisplayRouteValues);
|
metadata.DisplayRouteValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src/Orchard/Mvc/Html/ItemDisplayExtensions.cs
Normal file
17
src/Orchard/Mvc/Html/ItemDisplayExtensions.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Mvc.Html;
|
||||||
|
using Orchard.Models.ViewModels;
|
||||||
|
|
||||||
|
namespace Orchard.Mvc.Html {
|
||||||
|
public static class ItemDisplayExtensions {
|
||||||
|
public static MvcHtmlString DisplayForItem<TModel, TItemViewModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, TItemViewModel>> expression) where TItemViewModel : ItemDisplayViewModel {
|
||||||
|
|
||||||
|
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
|
||||||
|
var model = (TItemViewModel)metadata.Model;
|
||||||
|
|
||||||
|
return html.DisplayFor(expression, model.TemplateName, model.Prefix ?? "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
src/Orchard/Mvc/Html/ItemEditorExtensions.cs
Normal file
17
src/Orchard/Mvc/Html/ItemEditorExtensions.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Mvc.Html;
|
||||||
|
using Orchard.Models.ViewModels;
|
||||||
|
|
||||||
|
namespace Orchard.Mvc.Html {
|
||||||
|
public static class ItemEditorExtensions {
|
||||||
|
public static MvcHtmlString EditorForItem<TModel, TItemViewModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, TItemViewModel>> expression) where TItemViewModel : ItemEditorViewModel {
|
||||||
|
|
||||||
|
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
|
||||||
|
var model = (TItemViewModel)metadata.Model;
|
||||||
|
|
||||||
|
return html.EditorFor(expression, model.TemplateName, model.Prefix ?? "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -134,6 +134,7 @@
|
|||||||
<Compile Include="Models\DefaultContentQuery.cs" />
|
<Compile Include="Models\DefaultContentQuery.cs" />
|
||||||
<Compile Include="Models\Driver\ActivatedContentContext.cs" />
|
<Compile Include="Models\Driver\ActivatedContentContext.cs" />
|
||||||
<Compile Include="Models\Driver\ActivatingFilter.cs" />
|
<Compile Include="Models\Driver\ActivatingFilter.cs" />
|
||||||
|
<Compile Include="Models\Driver\ContentItemTemplates.cs" />
|
||||||
<Compile Include="Models\Driver\GetDisplaysContext.cs" />
|
<Compile Include="Models\Driver\GetDisplaysContext.cs" />
|
||||||
<Compile Include="Models\Driver\GetItemMetadataContext.cs" />
|
<Compile Include="Models\Driver\GetItemMetadataContext.cs" />
|
||||||
<Compile Include="Models\Driver\IContentActivatingFilter.cs" />
|
<Compile Include="Models\Driver\IContentActivatingFilter.cs" />
|
||||||
@@ -178,6 +179,8 @@
|
|||||||
<Compile Include="Models\ViewModels\ItemDisplayViewModel.cs" />
|
<Compile Include="Models\ViewModels\ItemDisplayViewModel.cs" />
|
||||||
<Compile Include="Models\ViewModels\ItemEditorViewModel.cs" />
|
<Compile Include="Models\ViewModels\ItemEditorViewModel.cs" />
|
||||||
<Compile Include="Mvc\Html\ContentItemExtensions.cs" />
|
<Compile Include="Mvc\Html\ContentItemExtensions.cs" />
|
||||||
|
<Compile Include="Mvc\Html\ItemDisplayExtensions.cs" />
|
||||||
|
<Compile Include="Mvc\Html\ItemEditorExtensions.cs" />
|
||||||
<Compile Include="Mvc\MvcModule.cs" />
|
<Compile Include="Mvc\MvcModule.cs" />
|
||||||
<Compile Include="Mvc\Html\HtmlHelperExtensions.cs" />
|
<Compile Include="Mvc\Html\HtmlHelperExtensions.cs" />
|
||||||
<Compile Include="Mvc\Filters\FilterProvider.cs" />
|
<Compile Include="Mvc\Filters\FilterProvider.cs" />
|
||||||
|
Reference in New Issue
Block a user