Extracted layout editor creation for reuse purposes.

This simplifies reusing the layout editor in other areas outside the context of LayoutPart (which is now a user of the layout editor).
This commit is contained in:
Sipke Schoorstra
2015-02-22 12:28:22 +01:00
parent c0e46f2930
commit 102fb14d9a
4 changed files with 119 additions and 53 deletions

View File

@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
@@ -10,11 +7,9 @@ using Orchard.DisplayManagement;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Utility.Extensions;
namespace Orchard.Layouts.Drivers {
public class LayoutPartDriver : ContentPartDriver<LayoutPart> {
@@ -25,6 +20,7 @@ namespace Orchard.Layouts.Drivers {
private readonly Lazy<IContentPartDisplay> _contentPartDisplay;
private readonly IShapeDisplay _shapeDisplay;
private readonly ILayoutModelMapper _mapper;
private readonly ILayoutEditorFactory _layoutEditorFactory;
public LayoutPartDriver(
ILayoutSerializer serializer,
@@ -33,7 +29,8 @@ namespace Orchard.Layouts.Drivers {
ILayoutManager layoutManager,
Lazy<IContentPartDisplay> contentPartDisplay,
IShapeDisplay shapeDisplay,
ILayoutModelMapper mapper) {
ILayoutModelMapper mapper,
ILayoutEditorFactory layoutEditorFactory) {
_serializer = serializer;
_elementDisplay = elementDisplay;
@@ -42,6 +39,7 @@ namespace Orchard.Layouts.Drivers {
_contentPartDisplay = contentPartDisplay;
_shapeDisplay = shapeDisplay;
_mapper = mapper;
_layoutEditorFactory = layoutEditorFactory;
}
protected override DriverResult Display(LayoutPart part, string displayType, dynamic shapeHelper) {
@@ -65,14 +63,7 @@ namespace Orchard.Layouts.Drivers {
protected override DriverResult Editor(LayoutPart part, IUpdateModel updater, dynamic shapeHelper) {
return ContentShape("Parts_Layout_Edit", () => {
var viewModel = new LayoutPartViewModel {
LayoutEditor = new LayoutEditor {
Data = _mapper.ToEditorModel(part.LayoutData, new DescribeElementsContext { Content = part }).ToJson(),
ConfigurationData = GetConfigurationData(part),
TemplateId = part.TemplateId,
Content = part,
SessionKey = part.SessionKey,
Templates = _layoutManager.GetTemplates().Where(x => x.Id != part.Id).ToArray()
}
LayoutEditor = _layoutEditorFactory.Create(part)
};
if (updater != null) {
@@ -133,44 +124,5 @@ namespace Orchard.Layouts.Drivers {
var template = context.GetItemFromSession(templateIdentity);
return template != null ? template.Id : default(int?);
}
private string GetConfigurationData(LayoutPart part) {
var elementCategories = GetCategories(part).ToArray();
var config = new {
categories = elementCategories.Select(category => new {
name = category.DisplayName.Text,
contentTypes = category.Elements.Where(x => !x.IsSystemElement).Select(descriptor => {
var element = _elementManager.ActivateElement(descriptor);
var map = _mapper.GetMapFor(element);
return new {
label = descriptor.DisplayText.Text,
id = descriptor.TypeName,
type = map.LayoutElementType,
typeClass = descriptor.DisplayText.Text.HtmlClassify(),
description = descriptor.Description.Text,
icon = descriptor.ToolboxIcon,
hasEditor = descriptor.EnableEditorDialog,
// If the element has no editor then the toolbox will add the element straight to to designer when being dragged & dropped,
// so we'll want to present the user with a prerendered element.
html = descriptor.EnableEditorDialog ? "" : RenderElement(element, new DescribeElementsContext { Content = part })
};
})
})
};
return JToken.FromObject(config).ToString(Formatting.None);
}
private string RenderElement(Element element, DescribeElementsContext describeContext, string displayType = "Design") {
return _shapeDisplay.Display(_elementDisplay.DisplayElement(element, describeContext.Content, displayType));
}
private IEnumerable<CategoryDescriptor> GetCategories(LayoutPart part) {
var describeContext = new DescribeElementsContext { Content = part };
var elementCategories = _elementManager.GetCategories(describeContext).ToArray();
return elementCategories.Where(category => category.Elements.Any(x => !x.IsSystemElement));
}
}
}

View File

@@ -458,9 +458,11 @@
<Compile Include="Models\ElementSessionState.cs" />
<Compile Include="Services\ICurrentControllerAccessor.cs" />
<Compile Include="Services\IElementEventHandler.cs" />
<Compile Include="Services\ILayoutEditorFactory.cs" />
<Compile Include="Services\ILayoutModelMap.cs" />
<Compile Include="Services\ILayoutModelMapper.cs" />
<Compile Include="Services\IObjectStore.cs" />
<Compile Include="Services\LayoutEditorFactory.cs" />
<Compile Include="Services\LayoutModelMapper.cs" />
<Compile Include="Services\ObjectStore.cs" />
<Compile Include="Signals.cs" />

View File

@@ -0,0 +1,10 @@
using Orchard.ContentManagement;
using Orchard.Layouts.Models;
using Orchard.Layouts.ViewModels;
namespace Orchard.Layouts.Services {
public interface ILayoutEditorFactory : IDependency {
LayoutEditor Create(LayoutPart layoutPart);
LayoutEditor Create(string layoutData, string sessionKey, int? templateId = null, IContent content = null);
}
}

View File

@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Layouts.ViewModels;
using Orchard.Utility.Extensions;
namespace Orchard.Layouts.Services {
public class LayoutEditorFactory : ILayoutEditorFactory {
private readonly ILayoutModelMapper _mapper;
private readonly ILayoutManager _layoutManager;
private readonly IElementManager _elementManager;
private readonly IElementDisplay _elementDisplay;
private readonly IShapeDisplay _shapeDisplay;
public LayoutEditorFactory(
ILayoutModelMapper mapper,
ILayoutManager layoutManager,
IElementManager elementManager,
IElementDisplay elementDisplay,
IShapeDisplay shapeDisplay) {
_mapper = mapper;
_layoutManager = layoutManager;
_elementManager = elementManager;
_elementDisplay = elementDisplay;
_shapeDisplay = shapeDisplay;
}
public LayoutEditor Create(LayoutPart layoutPart) {
return Create(layoutPart.LayoutData, layoutPart.SessionKey, layoutPart.TemplateId, layoutPart);
}
public LayoutEditor Create(string layoutData, string sessionKey, int? templateId = null, IContent content = null) {
return new LayoutEditor {
Data = _mapper.ToEditorModel(layoutData, new DescribeElementsContext {Content = content}).ToJson(),
ConfigurationData = GetConfigurationData(content),
TemplateId = templateId,
Content = content,
SessionKey = sessionKey,
Templates = GetTemplates(content)
};
}
private IList<LayoutPart> GetTemplates(IContent content = null) {
var query = _layoutManager.GetTemplates();
var layoutPart = content != null ? content.As<LayoutPart>() : null;
if (layoutPart != null) {
query = query.Where(x => x.Id != layoutPart.Id);
}
return query.ToList();
}
private string GetConfigurationData(IContent content) {
var elementCategories = GetCategories(content).ToArray();
var config = new {
categories = elementCategories.Select(category => new {
name = category.DisplayName.Text,
contentTypes = category.Elements.Where(x => !x.IsSystemElement).Select(descriptor => {
var element = _elementManager.ActivateElement(descriptor);
var map = _mapper.GetMapFor(element);
return new {
label = descriptor.DisplayText.Text,
id = descriptor.TypeName,
type = map.LayoutElementType,
typeClass = descriptor.DisplayText.Text.HtmlClassify(),
description = descriptor.Description.Text,
icon = descriptor.ToolboxIcon,
hasEditor = descriptor.EnableEditorDialog,
// If the element has no editor then the toolbox will add the element straight to to designer when being dragged & dropped,
// so we'll want to present the user with a prerendered element.
html = descriptor.EnableEditorDialog ? "" : RenderElement(element, new DescribeElementsContext { Content = content })
};
})
})
};
return JToken.FromObject(config).ToString(Formatting.None);
}
private string RenderElement(Element element, DescribeElementsContext describeContext, string displayType = "Design") {
return _shapeDisplay.Display(_elementDisplay.DisplayElement(element, describeContext.Content, displayType));
}
private IEnumerable<CategoryDescriptor> GetCategories(IContent content) {
var describeContext = new DescribeElementsContext { Content = content };
var elementCategories = _elementManager.GetCategories(describeContext).ToArray();
return elementCategories.Where(category => category.Elements.Any(x => !x.IsSystemElement));
}
}
}