Skip to content

Commit

Permalink
Revert "Layers enhancements" (#17125)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Dec 5, 2024
1 parent 2fbdd36 commit a45d29d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display.ContentDisplay;
Expand All @@ -25,83 +24,70 @@ public LayerMetadataWelder(
S = stringLocalizer;
}

protected override void BuildPrefix(ContentItem model, string htmlFieldPrefix)
{
base.BuildPrefix(model, htmlFieldPrefix);
if (string.IsNullOrWhiteSpace(htmlFieldPrefix))
{
Prefix = "LayerMetadata";
}
}

public override async Task<IDisplayResult> EditAsync(ContentItem model, BuildEditorContext context)
{
if (!model.TryGet<LayerMetadata>(out var layerMetadata))
var layerMetadata = model.As<LayerMetadata>();

if (layerMetadata == null)
{
layerMetadata = new LayerMetadata();
await context.Updater.TryUpdateModelAsync(layerMetadata, Prefix, m => m.Zone, m => m.Position);

// In the initial request, the 'Zone' and 'Position' are passed as route values.
// We attempt to populate the new 'LayerMetadata' and ensure the 'Zone' is correctly set.
await context.Updater.TryUpdateModelAsync(layerMetadata, string.Empty, m => m.Zone, m => m.Position);

if (string.IsNullOrEmpty(layerMetadata.Zone))
// Are we loading an editor that requires layer metadata?
if (!string.IsNullOrEmpty(layerMetadata.Zone))
{
model.Weld(layerMetadata);
}
else
{
// At this point, the 'Zone' value was not provided in the route values, so the widget is not visible.
return null;
}

model.Weld(layerMetadata);
}

return Initialize<LayerMetadataEditViewModel>("LayerMetadata_Edit", async m =>
return Initialize<LayerMetadataEditViewModel>("LayerMetadata_Edit", async shape =>
{
m.Title = model.DisplayText;
m.RenderTitle = layerMetadata.RenderTitle;
m.Position = layerMetadata.Position;
m.Zone = layerMetadata.Zone;
m.Layer = layerMetadata.Layer;
m.Layers = (await _layerService.GetLayersAsync()).Layers
.Select(x => new SelectListItem(x.Name, x.Name));
shape.Title = model.DisplayText;
shape.LayerMetadata = layerMetadata;
shape.Layers = (await _layerService.GetLayersAsync()).Layers;
}).Location("Content:before");
}

protected override void BuildPrefix(ContentItem model, string htmlFieldPrefix)
{
base.BuildPrefix(model, htmlFieldPrefix);
if (string.IsNullOrWhiteSpace(htmlFieldPrefix))
{
Prefix = nameof(LayerMetadata);
}
}

public override async Task<IDisplayResult> UpdateAsync(ContentItem model, UpdateEditorContext context)
{
var viewModel = new LayerMetadataEditViewModel();

await context.Updater.TryUpdateModelAsync(viewModel, Prefix);

if (string.IsNullOrEmpty(viewModel.Title))
if (viewModel.LayerMetadata == null)
{
context.Updater.ModelState.AddModelError(Prefix, nameof(viewModel.Title), S["The Title field is required"]);
return null;
}

if (string.IsNullOrEmpty(viewModel.Zone))
if (string.IsNullOrEmpty(viewModel.Title))
{
context.Updater.ModelState.AddModelError(Prefix, nameof(viewModel.Zone), S["The Zone field is required"]);
context.Updater.ModelState.AddModelError(Prefix, nameof(viewModel.Title), S["Title is required field."]);
}

if (string.IsNullOrEmpty(viewModel.Layer))
if (string.IsNullOrEmpty(viewModel.LayerMetadata.Zone))
{
context.Updater.ModelState.AddModelError(Prefix, nameof(viewModel.Layer), S["The Layer field is required"]);
context.Updater.ModelState.AddModelError(Prefix, "LayerMetadata.Zone", S["Zone is missing"]);
}
else
{
var document = await _layerService.GetLayersAsync();

if (!document.Layers.Any(x => x.Name == viewModel.Layer))
{
context.Updater.ModelState.AddModelError(Prefix, nameof(viewModel.Layer), S["Invalid Layer value provided"]);
}
if (string.IsNullOrEmpty(viewModel.LayerMetadata.Layer))
{
context.Updater.ModelState.AddModelError(Prefix, "LayerMetadata.Layer", S["Layer is missing field."]);
}

model.Apply(new LayerMetadata
{
RenderTitle = viewModel.RenderTitle,
Zone = viewModel.Zone,
Position = viewModel.Position,
Layer = viewModel.Layer,
});
model.Apply(viewModel.LayerMetadata);

model.DisplayText = viewModel.Title;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace OrchardCore.Layers.Models;
public class LayerMetadata : ContentPart
{
public bool RenderTitle { get; set; }

public double Position { get; set; }

public string Zone { get; set; }

public string Layer { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using OrchardCore.Layers.Models;

namespace OrchardCore.Layers.ViewModels;

public class LayerMetadataEditViewModel
{
public string Title { get; set; }

public bool RenderTitle { get; set; }

public double Position { get; set; }

public string Zone { get; set; }

public string Layer { get; set; }

[BindNever]
public IEnumerable<SelectListItem> Layers { get; set; }
public LayerMetadata LayerMetadata { get; set; }
public List<Layer> Layers { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@using OrchardCore.Layers.Drivers
@using OrchardCore.ContentManagement.Metadata.Models
@using OrchardCore.ContentManagement.Metadata

@model OrchardCore.Layers.ViewModels.LayersIndexViewModel

@inject IContentManager ContentManager
Expand Down Expand Up @@ -83,8 +84,8 @@
var contentItem = await ContentManager.NewAsync(type.Name);
var createRoute = (await ContentManager.PopulateAspectAsync<ContentItemMetadata>(contentItem)).CreateRouteValues;
createRoute["returnUrl"] = Context.Request.PathBase + Context.Request.Path;
createRoute["Zone"] = zone;
createRoute["Position"] = maxPosition + 1;
createRoute["LayerMetadata.Zone"] = zone;
createRoute["LayerMetadata.Position"] = maxPosition + 1;
<li>
<a class="dropdown-item add-list-widget"
data-widget-type="@type.Name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@
<div class="@Orchard.GetWrapperClasses()">
<div class="@Orchard.GetEndClasses(true)">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="RenderTitle">
<label class="form-check-label" asp-for="RenderTitle">@T["Render title"]</label>
<input type="checkbox" class="form-check-input" asp-for="LayerMetadata.RenderTitle" checked="@Model.LayerMetadata.RenderTitle">
<label class="form-check-label" asp-for="LayerMetadata.RenderTitle">@T["Render title"]</label>
<span class="hint dashed">@T["If checked, the title will be rendered with the widget."]</span>
</div>
</div>
</div>

<div class="@Orchard.GetWrapperClasses()" asp-validation-class-for="Layer">
<label asp-for="Layer" class="@Orchard.GetLabelClasses()">@T["The layer"]</label>
<div class="@Orchard.GetWrapperClasses()" asp-validation-class-for="LayerMetadata.Layer">
<label asp-for="LayerMetadata.Layer" class="@Orchard.GetLabelClasses()">@T["The layer the widget should be associated with."]</label>
<div class="@Orchard.GetEndClasses()">
<select asp-for="Layer" asp-items="Model.Layers" class="form-select">
<select asp-for="LayerMetadata.Layer" class="form-select">
<option value="">@T["Select a layer"]</option>
@foreach (var layer in Model.Layers)
{
<option value="@layer.Name" selected="@(Model.LayerMetadata.Layer == layer.Name)">@layer.Name</option>
}
</select>
<span asp-validation-for="Layer" class="text-danger"></span>
<span class="hint">@T["The layer controls the visibility of this widget."]</span>
<span asp-validation-for="LayerMetadata.Layer" class="text-danger"></span>
</div>
</div>

<input asp-for="Zone" type="hidden" />
<input asp-for="Position" type="hidden" />
<input asp-for="LayerMetadata.Zone" type="hidden" />
<input asp-for="LayerMetadata.Position" type="hidden" />

0 comments on commit a45d29d

Please sign in to comment.