mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-23 13:22:08 +08:00
Refactoring content capture into a InnerWriter substitution. Adding content dictionary to layout context.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044310
This commit is contained in:
@@ -128,6 +128,12 @@
|
||||
<Content Include="Views\Shared\EditorTemplates\TinyMceTextEditor.ascx" />
|
||||
<Content Include="Views\Web.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Orchard\Orchard.csproj">
|
||||
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
|
||||
<Name>Orchard</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
|
||||
<%@ Import Namespace="System.Web.Mvc.Html" %>
|
||||
<%@ Import Namespace="Orchard.Mvc.Html" %>
|
||||
<%@ Import Namespace="System.Web.Mvc.Html" %>
|
||||
<% Html.RegisterScript("tiny_mce.js"); %>
|
||||
<%=Html.TextArea("", Model, 25, 80, new { @class = "html" }) %>
|
||||
|
||||
<%using (this.Capture("end-of-page-scripts")) {%>
|
||||
<script type="text/javascript">
|
||||
tinyMCE.init({
|
||||
theme: "advanced",
|
||||
@@ -16,3 +18,4 @@
|
||||
theme_advanced_buttons3: ""
|
||||
});
|
||||
</script>
|
||||
<%}%>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
using Orchard.UI.ContentCapture;
|
||||
|
||||
namespace Orchard.Mvc.Html {
|
||||
public class ContentCaptureBlock : MvcForm {
|
||||
private readonly ViewContext _viewContext;
|
||||
private readonly IContentCapture _contentCapture;
|
||||
|
||||
public ContentCaptureBlock(ViewContext viewContext, IContentCapture contentCapture)
|
||||
: base(viewContext) {
|
||||
_viewContext = viewContext;
|
||||
_contentCapture = contentCapture;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing) {
|
||||
//_viewContext.HttpContext.Response.Filter.Flush();
|
||||
_contentCapture.EndContentCapture();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.UI.ContentCapture;
|
||||
|
||||
namespace Orchard.Mvc.Html {
|
||||
public static class ContentCaptureExtensions {
|
||||
public static ContentCaptureBlock CaptureContent(this HtmlHelper htmlHelper, string captureName) {
|
||||
IContentCapture contentCapture = htmlHelper.Resolve<IContentCapture>();
|
||||
|
||||
//htmlHelper.ViewContext.HttpContext.Response.Filter.Flush();
|
||||
contentCapture.BeginContentCapture(captureName);
|
||||
|
||||
return new ContentCaptureBlock(htmlHelper.ViewContext, contentCapture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.UI;
|
||||
using Orchard.Mvc.ViewEngines;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.UI.PageTitle;
|
||||
@@ -9,14 +12,14 @@ using Orchard.UI.Zones;
|
||||
namespace Orchard.Mvc.Html {
|
||||
public static class LayoutExtensions {
|
||||
public static void RenderBody(this HtmlHelper html) {
|
||||
OrchardLayoutContext layoutContext = OrchardLayoutContext.From(html.ViewContext);
|
||||
html.ViewContext.HttpContext.Response.Output.Write(layoutContext.BodyContent);
|
||||
LayoutViewContext layoutViewContext = LayoutViewContext.From(html.ViewContext);
|
||||
html.ViewContext.HttpContext.Response.Output.Write(layoutViewContext.BodyContent);
|
||||
}
|
||||
|
||||
public static MvcHtmlString Body(this HtmlHelper html) {
|
||||
OrchardLayoutContext layoutContext = OrchardLayoutContext.From(html.ViewContext);
|
||||
LayoutViewContext layoutViewContext = LayoutViewContext.From(html.ViewContext);
|
||||
|
||||
return MvcHtmlString.Create(layoutContext.BodyContent);
|
||||
return MvcHtmlString.Create(layoutViewContext.BodyContent);
|
||||
}
|
||||
|
||||
public static void AddTitleParts(this HtmlHelper html, params string[] titleParts) {
|
||||
@@ -67,8 +70,26 @@ namespace Orchard.Mvc.Html {
|
||||
html.Resolve<IResourceManager>().RegisterFootScript(fileName, html);
|
||||
}
|
||||
|
||||
public static ContentCaptureBlock RegisterInlineScript(this HtmlHelper html, string name) {
|
||||
return html.CaptureContent("inlinescript:" + name);
|
||||
|
||||
public static IDisposable Capture(this ViewUserControl control, string name) {
|
||||
var writer = LayoutViewContext.From(control.ViewContext).GetNamedContent(name);
|
||||
return new HtmlTextWriterScope(control.Writer, writer);
|
||||
}
|
||||
|
||||
class HtmlTextWriterScope : IDisposable {
|
||||
private readonly HtmlTextWriter _context;
|
||||
private readonly TextWriter _writer;
|
||||
|
||||
public HtmlTextWriterScope(HtmlTextWriter context, TextWriter writer) {
|
||||
_context = context;
|
||||
_writer = _context.InnerWriter;
|
||||
_context.InnerWriter = writer;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_context.InnerWriter = _writer;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,29 +7,34 @@ using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.Mvc.ViewEngines {
|
||||
public class LayoutView : IView {
|
||||
private IView[] _views;
|
||||
private ViewEngineResult[] _viewEngineResults;
|
||||
|
||||
public LayoutView(IEnumerable<IView> views) {
|
||||
_views = views.ToArray();
|
||||
public LayoutView(ViewEngineResult[] views) {
|
||||
_viewEngineResults = views;
|
||||
}
|
||||
|
||||
public void Render(ViewContext viewContext, TextWriter writer) {
|
||||
|
||||
var orchardViewContext = OrchardLayoutContext.From(viewContext);
|
||||
var layoutViewContext = LayoutViewContext.From(viewContext);
|
||||
|
||||
for (var index = 0; index != _views.Length; ++index)
|
||||
{
|
||||
var view = _views[index];
|
||||
if (index == _views.Length - 1) {
|
||||
view.Render(viewContext, writer);
|
||||
for (var index = 0; index != _viewEngineResults.Length; ++index) {
|
||||
var viewEngineResult = _viewEngineResults[index];
|
||||
if (index == _viewEngineResults.Length - 1) {
|
||||
viewEngineResult.View.Render(viewContext, writer);
|
||||
}
|
||||
else {
|
||||
//TEMP: to be replaced with an efficient spooling writer
|
||||
var childWriter = new StringWriter();
|
||||
view.Render(viewContext, childWriter);
|
||||
orchardViewContext.BodyContent = childWriter.ToString();
|
||||
viewEngineResult.View.Render(viewContext, childWriter);
|
||||
layoutViewContext.BodyContent = childWriter.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReleaseViews(ControllerContext context) {
|
||||
foreach (var viewEngineResult in _viewEngineResults) {
|
||||
viewEngineResult.ViewEngine.ReleaseView(context, viewEngineResult.View);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
src/Orchard/Mvc/ViewEngines/LayoutViewContext.cs
Normal file
45
src/Orchard/Mvc/ViewEngines/LayoutViewContext.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.UI;
|
||||
|
||||
namespace Orchard.Mvc.ViewEngines {
|
||||
public class LayoutViewContext {
|
||||
private static readonly object _key = typeof(LayoutViewContext);
|
||||
|
||||
private LayoutViewContext() {
|
||||
Contents = new Dictionary<string, TextWriter>();
|
||||
}
|
||||
|
||||
public static LayoutViewContext From(ControllerContext context) {
|
||||
return From(context.HttpContext);
|
||||
}
|
||||
|
||||
public static LayoutViewContext From(HttpContextBase context) {
|
||||
if (!context.Items.Contains(_key)) {
|
||||
context.Items.Add(_key, new LayoutViewContext());
|
||||
}
|
||||
return (LayoutViewContext)context.Items[_key];
|
||||
}
|
||||
|
||||
|
||||
public string BodyContent { get; set; }
|
||||
|
||||
public IDictionary<string, TextWriter> Contents { get; set; }
|
||||
|
||||
public TextWriter GetNamedContent(string name) {
|
||||
if (!Contents.ContainsKey(name)) {
|
||||
Contents.Add(name, new StringWriter());
|
||||
}
|
||||
return Contents[name];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -47,16 +47,17 @@ namespace Orchard.Mvc.ViewEngines {
|
||||
}
|
||||
|
||||
var view = new LayoutView(new[] {
|
||||
bodyView.View,
|
||||
layoutView.View,
|
||||
documentView.View,
|
||||
bodyView,
|
||||
layoutView,
|
||||
documentView,
|
||||
});
|
||||
|
||||
return new ViewEngineResult(view, this);
|
||||
}
|
||||
|
||||
public void ReleaseView(ControllerContext controllerContext, IView view) {
|
||||
|
||||
var layoutView = (LayoutView) view;
|
||||
layoutView.ReleaseViews(controllerContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Orchard.Mvc.ViewEngines {
|
||||
public class OrchardLayoutContext {
|
||||
private static readonly object _key = typeof(OrchardLayoutContext);
|
||||
|
||||
public string BodyContent { get; set; }
|
||||
|
||||
public static OrchardLayoutContext From(ControllerContext context) {
|
||||
return From(context.HttpContext);
|
||||
}
|
||||
|
||||
public static OrchardLayoutContext From(HttpContextBase context) {
|
||||
if (!context.Items.Contains(_key)) {
|
||||
context.Items.Add(_key, new OrchardLayoutContext());
|
||||
}
|
||||
return (OrchardLayoutContext)context.Items[_key];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,8 +125,6 @@
|
||||
<Compile Include="Environment\IOrchardShellEvents.cs" />
|
||||
<Compile Include="Extensions\ExtensionDescriptor.cs" />
|
||||
<Compile Include="Extensions\ExtensionEntry.cs" />
|
||||
<Compile Include="Mvc\Html\ContentCaptureBlock.cs" />
|
||||
<Compile Include="Mvc\Html\ContentCaptureExtensions.cs" />
|
||||
<Compile Include="Mvc\Html\FileRegistrationContext.cs" />
|
||||
<Compile Include="Themes\ExtensionManagerExtensions.cs" />
|
||||
<Compile Include="Extensions\Helpers\PathHelpers.cs" />
|
||||
@@ -222,19 +220,16 @@
|
||||
<Compile Include="Mvc\ViewEngines\IViewEngineProvider.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\LayoutView.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\LayoutViewEngine.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\OrchardLayoutContext.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\LayoutViewContext.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\ViewEngineFilter.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\WebFormsViewEngineProvider.cs" />
|
||||
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
|
||||
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
|
||||
<Compile Include="UI\ContentCapture\ContentCaptureFilter.cs" />
|
||||
<Compile Include="UI\ContentCapture\IContentCapture.cs" />
|
||||
<Compile Include="UI\PageTitle\IPageTitleBuilder.cs" />
|
||||
<Compile Include="UI\PageTitle\PageTitleBuilder.cs" />
|
||||
<Compile Include="UI\Resources\IResourceManager.cs" />
|
||||
<Compile Include="UI\Resources\ResourceFilter.cs" />
|
||||
<Compile Include="UI\Resources\ResourceManager.cs" />
|
||||
<Compile Include="UI\ContentCapture\ContentCaptureStream.cs" />
|
||||
<Compile Include="UI\Zones\DelegateZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\ItemDisplayZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\IZoneManager.cs" />
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.IO;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Mvc.ViewEngines;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.UI.Resources {
|
||||
@@ -11,14 +13,21 @@ namespace Orchard.UI.Resources {
|
||||
}
|
||||
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
BaseViewModel model = filterContext.Controller.ViewData.Model as BaseViewModel;
|
||||
|
||||
if (model != null) {
|
||||
model.Zones.AddAction("head:metas", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetMetas()));
|
||||
model.Zones.AddAction("head:styles", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetStyles()));
|
||||
model.Zones.AddAction("head:scripts", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetHeadScripts()));
|
||||
model.Zones.AddAction("body:after", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetFootScripts()));
|
||||
var model = filterContext.Controller.ViewData.Model as BaseViewModel;
|
||||
if (model == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
model.Zones.AddAction("head:metas", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetMetas()));
|
||||
model.Zones.AddAction("head:styles", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetStyles()));
|
||||
model.Zones.AddAction("head:scripts", html => html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetHeadScripts()));
|
||||
model.Zones.AddAction("body:after", html => {
|
||||
html.ViewContext.HttpContext.Response.Output.Write(_resourceManager.GetFootScripts());
|
||||
TextWriter captured;
|
||||
if (LayoutViewContext.From(html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured)) {
|
||||
html.ViewContext.HttpContext.Response.Output.Write(captured);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {
|
||||
|
||||
@@ -11,49 +11,44 @@ namespace Orchard.UI.Zones {
|
||||
if (!zones.TryGetValue(zoneName, out zone))
|
||||
return;
|
||||
|
||||
var partitionItems = LocatePartitionItems(partitions, zone);
|
||||
|
||||
foreach (var partitionItem in partitionItems) {
|
||||
foreach (var item in zone.Items) {
|
||||
if (item.WasExecuted)
|
||||
continue;
|
||||
|
||||
item.WasExecuted = true;
|
||||
item.Execute(html);
|
||||
}
|
||||
var groups = BuildGroups(partitions, zone);
|
||||
|
||||
foreach (var item in groups.SelectMany(x => x.Items)) {
|
||||
item.WasExecuted = true;
|
||||
item.Execute(html);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private IEnumerable<PartitionItem> LocatePartitionItems(string partitions, ZoneEntry zone) {
|
||||
private IEnumerable<Group> BuildGroups(string partitions, ZoneEntry zone) {
|
||||
|
||||
var partitionCodes = (":before " + partitions + " :* :after").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var itemsRemaining = zone.Items.Where(x => x.WasExecuted == false);
|
||||
|
||||
PartitionItem catchAllItem = null;
|
||||
Group catchAllItem = null;
|
||||
|
||||
var results = new List<PartitionItem>();
|
||||
var results = new List<Group>();
|
||||
foreach (var code in partitionCodes) {
|
||||
if (code == ":*") {
|
||||
catchAllItem=new PartitionItem();
|
||||
catchAllItem = new Group();
|
||||
results.Add(catchAllItem);
|
||||
}
|
||||
else {
|
||||
var value = code;
|
||||
var items = itemsRemaining.Where(x => (":" + x.Position).StartsWith(value));
|
||||
results.Add(new PartitionItem { ZoneItems = items.ToArray() });
|
||||
results.Add(new Group { Items = items.ToArray() });
|
||||
itemsRemaining = itemsRemaining.Except(items).ToArray();
|
||||
}
|
||||
}
|
||||
if (catchAllItem != null) {
|
||||
catchAllItem.ZoneItems = itemsRemaining;
|
||||
catchAllItem.Items = itemsRemaining;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
class PartitionItem {
|
||||
public IEnumerable<ZoneItem> ZoneItems { get; set; }
|
||||
class Group {
|
||||
public IEnumerable<ZoneItem> Items { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user