Skip to content

Commit

Permalink
ZoneOnDemand.AddAsync(). (OrchardCMS#3864)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech authored and MatthijsKrempel committed Sep 25, 2019
1 parent 31ccd94 commit b124a04
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 55 deletions.
13 changes: 11 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Admin/AdminMenuFilter.cs
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc.Filters;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.DisplayManagement.Zones;
using OrchardCore.Navigation;

namespace OrchardCore.Admin
Expand Down Expand Up @@ -67,9 +68,17 @@ public async Task OnResultExecutionAsync(ResultExecutingContext filterContext, R
}));

dynamic layout = await _layoutAccessor.GetLayoutAsync();
layout.Navigation.Add(menuShape);

if (layout.Navigation is ZoneOnDemand zoneOnDemand)
{
await zoneOnDemand.AddAsync(menuShape);
}
else
{
layout.Navigation.Add(menuShape);
}

await next();
}
}
}
}
103 changes: 56 additions & 47 deletions src/OrchardCore.Modules/OrchardCore.Layers/Services/LayerFilter.cs
Expand Up @@ -11,6 +11,7 @@
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Theming;
using OrchardCore.DisplayManagement.Zones;
using OrchardCore.Environment.Cache;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Layers.Handlers;
Expand All @@ -32,8 +33,8 @@ public class LayerFilter : IAsyncResultFilter
private readonly IAdminThemeService _adminThemeService;
private readonly ILayerService _layerService;

public LayerFilter(
ILayerService layerService,
public LayerFilter(
ILayerService layerService,
ILayoutAccessor layoutAccessor,
IContentItemDisplayManager contentItemDisplayManager,
IUpdateModelAccessor modelUpdaterAccessor,
Expand All @@ -44,8 +45,8 @@ public class LayerFilter : IAsyncResultFilter
IThemeManager themeManager,
IAdminThemeService adminThemeService)
{
_layerService = layerService;
_layoutAccessor = layoutAccessor;
_layerService = layerService;
_layoutAccessor = layoutAccessor;
_contentItemDisplayManager = contentItemDisplayManager;
_modelUpdaterAccessor = modelUpdaterAccessor;
_scriptingManager = scriptingManager;
Expand All @@ -57,10 +58,10 @@ public class LayerFilter : IAsyncResultFilter

public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
// Should only run on the front-end for a full view
if ((context.Result is ViewResult || context.Result is PageResult) &&
// Should only run on the front-end for a full view
if ((context.Result is ViewResult || context.Result is PageResult) &&
!AdminAttribute.IsApplied(context.HttpContext))
{
{
// Even if the Admin attribute is not applied we might be using the admin theme, for instance in Login views.
// In this case don't render Layers.
var selectedTheme = (await _themeManager.GetThemeAsync())?.Id;
Expand All @@ -77,49 +78,49 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
return _layerService.GetLayerWidgetsMetadataAsync(x => x.Published);
});

var layers = (await _layerService.GetLayersAsync()).Layers.ToDictionary(x => x.Name);
var layers = (await _layerService.GetLayersAsync()).Layers.ToDictionary(x => x.Name);

dynamic layout = await _layoutAccessor.GetLayoutAsync();
var updater = _modelUpdaterAccessor.ModelUpdater;
dynamic layout = await _layoutAccessor.GetLayoutAsync();
var updater = _modelUpdaterAccessor.ModelUpdater;

var engine = _scriptingManager.GetScriptingEngine("js");
var scope = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), ShellScope.Services, null, null);
var engine = _scriptingManager.GetScriptingEngine("js");
var scope = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), ShellScope.Services, null, null);

var layersCache = new Dictionary<string, bool>();
var layersCache = new Dictionary<string, bool>();

foreach (var widget in widgets)
{
var layer = layers[widget.Layer];

if (layer == null)
{
continue;
}

bool display;
if (!layersCache.TryGetValue(layer.Name, out display))
{
if (String.IsNullOrEmpty(layer.Rule))
{
display = false;
}
else
{
display = Convert.ToBoolean(engine.Evaluate(scope, layer.Rule));
}
foreach (var widget in widgets)
{
var layer = layers[widget.Layer];

layersCache[layer.Rule] = display;
}
if (layer == null)
{
continue;
}

if (!display)
{
continue;
}
bool display;
if (!layersCache.TryGetValue(layer.Name, out display))
{
if (String.IsNullOrEmpty(layer.Rule))
{
display = false;
}
else
{
display = Convert.ToBoolean(engine.Evaluate(scope, layer.Rule));
}

layersCache[layer.Rule] = display;
}

if (!display)
{
continue;
}

var widgetContent = await _contentItemDisplayManager.BuildDisplayAsync(widget.ContentItem, updater);
var widgetContent = await _contentItemDisplayManager.BuildDisplayAsync(widget.ContentItem, updater);

widgetContent.Classes.Add("widget");
widgetContent.Classes.Add("widget-" + widget.ContentItem.ContentType.HtmlClassify());
widgetContent.Classes.Add("widget");
widgetContent.Classes.Add("widget-" + widget.ContentItem.ContentType.HtmlClassify());

var wrapper = new WidgetWrapper
{
Expand All @@ -130,12 +131,20 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
wrapper.Metadata.Alternates.Add("Widget_Wrapper__" + widget.ContentItem.ContentType);
wrapper.Metadata.Alternates.Add("Widget_Wrapper__Zone__" + widget.Zone);

var contentZone = layout.Zones[widget.Zone];
contentZone.Add(wrapper);
}
}
var contentZone = layout.Zones[widget.Zone];

if (contentZone is ZoneOnDemand zoneOnDemand)
{
await zoneOnDemand.AddAsync(wrapper);
}
else
{
contentZone.Add(wrapper);
}
}
}

await next.Invoke();
await next.Invoke();
}
}
}
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.DisplayManagement.Zones;

namespace OrchardCore.DisplayManagement.TagHelpers
{
Expand Down Expand Up @@ -35,7 +36,14 @@ public async override Task ProcessAsync(TagHelperContext context, TagHelperOutpu
dynamic layout = await _layoutAccessor.GetLayoutAsync();
var zone = layout.Zones[Name];

zone.Add(childContent, Position);
if (zone is ZoneOnDemand zoneOnDemand)
{
await zoneOnDemand.AddAsync(childContent, Position);
}
else
{
zone.Add(childContent, Position);
}

// Don't render the zone tag or the inner content
output.SuppressOutput();
Expand Down
Expand Up @@ -5,6 +5,7 @@
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Implementation;
using OrchardCore.DisplayManagement.Shapes;
using OrchardCore.DisplayManagement.Zones;
using OrchardCore.Environment.Cache;

namespace OrchardCore.DisplayManagement.Views
Expand Down Expand Up @@ -54,7 +55,7 @@ private async Task ApplyImplementationAsync(BuildShapeContext context, string di
if (String.IsNullOrEmpty(_defaultLocation))
{
_defaultLocation = context.DefaultZone;
}
}

// Look into specific implementations of placements (like placement.info files)
var placement = context.FindPlacement(_shapeType, _differentiator, displayType, context);
Expand Down Expand Up @@ -86,7 +87,7 @@ private async Task ApplyImplementationAsync(BuildShapeContext context, string di
{
placement.DefaultPosition = context.DefaultPosition;
}


// If there are no placement or it's explicitely noop then stop rendering execution
if (String.IsNullOrEmpty(placement.Location) || placement.Location == "-")
Expand Down Expand Up @@ -188,9 +189,11 @@ private async Task ApplyImplementationAsync(BuildShapeContext context, string di
}
}

if (String.IsNullOrEmpty(position))
position = !String.IsNullOrEmpty(position) ? position : null;

if (parentShape is ZoneOnDemand zoneOnDemand)
{
parentShape.Add(newShape);
await zoneOnDemand.AddAsync(newShape, position);
}
else
{
Expand Down
Expand Up @@ -253,5 +253,28 @@ public override Shape Add(object item, string position = null)

return zone.Add(item, position);
}

public async Task<Shape> AddAsync(object item, string position = null)
{
if (item == null)
{
return (Shape)_parent;
}

dynamic parent = _parent;

dynamic zone = await _zoneFactory();
zone.Parent = _parent;
zone.ZoneName = _potentialZoneName;
parent[_potentialZoneName] = zone;

if (position == null)
{
return zone.Add(item);
}

return zone.Add(item, position);
}
}
}
}

0 comments on commit b124a04

Please sign in to comment.