2014-10-15 07:07:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
using Orchard.ContentManagement.Handlers;
|
2014-11-15 12:43:06 +08:00
|
|
|
|
using Orchard.DisplayManagement;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
using Orchard.Layouts.Framework.Display;
|
2014-11-14 10:25:05 +08:00
|
|
|
|
using Orchard.Layouts.Framework.Drivers;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
using Orchard.Layouts.Framework.Elements;
|
2015-03-01 06:50:40 +08:00
|
|
|
|
using Orchard.Layouts.Helpers;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
using Orchard.Layouts.Models;
|
|
|
|
|
using Orchard.Layouts.Services;
|
|
|
|
|
using Orchard.Layouts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Layouts.Drivers {
|
|
|
|
|
public class LayoutPartDriver : ContentPartDriver<LayoutPart> {
|
|
|
|
|
private readonly ILayoutSerializer _serializer;
|
|
|
|
|
private readonly IElementDisplay _elementDisplay;
|
|
|
|
|
private readonly IElementManager _elementManager;
|
2014-11-14 08:37:21 +08:00
|
|
|
|
private readonly ILayoutManager _layoutManager;
|
2014-11-15 12:43:06 +08:00
|
|
|
|
private readonly Lazy<IContentPartDisplay> _contentPartDisplay;
|
|
|
|
|
private readonly IShapeDisplay _shapeDisplay;
|
2015-02-20 05:14:55 +08:00
|
|
|
|
private readonly ILayoutModelMapper _mapper;
|
2015-02-22 19:28:22 +08:00
|
|
|
|
private readonly ILayoutEditorFactory _layoutEditorFactory;
|
2014-11-14 08:37:21 +08:00
|
|
|
|
|
|
|
|
|
public LayoutPartDriver(
|
2015-02-22 16:45:49 +08:00
|
|
|
|
ILayoutSerializer serializer,
|
|
|
|
|
IElementDisplay elementDisplay,
|
|
|
|
|
IElementManager elementManager,
|
2014-11-15 12:43:06 +08:00
|
|
|
|
ILayoutManager layoutManager,
|
2015-02-22 16:45:49 +08:00
|
|
|
|
Lazy<IContentPartDisplay> contentPartDisplay,
|
|
|
|
|
IShapeDisplay shapeDisplay,
|
2015-02-22 19:28:22 +08:00
|
|
|
|
ILayoutModelMapper mapper,
|
|
|
|
|
ILayoutEditorFactory layoutEditorFactory) {
|
2014-10-15 07:07:00 +08:00
|
|
|
|
|
|
|
|
|
_serializer = serializer;
|
|
|
|
|
_elementDisplay = elementDisplay;
|
|
|
|
|
_elementManager = elementManager;
|
2014-11-14 08:37:21 +08:00
|
|
|
|
_layoutManager = layoutManager;
|
2014-11-15 12:43:06 +08:00
|
|
|
|
_contentPartDisplay = contentPartDisplay;
|
|
|
|
|
_shapeDisplay = shapeDisplay;
|
2015-02-20 05:14:55 +08:00
|
|
|
|
_mapper = mapper;
|
2015-02-22 19:28:22 +08:00
|
|
|
|
_layoutEditorFactory = layoutEditorFactory;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Display(LayoutPart part, string displayType, dynamic shapeHelper) {
|
2014-11-14 08:37:21 +08:00
|
|
|
|
return Combined(
|
|
|
|
|
ContentShape("Parts_Layout", () => {
|
|
|
|
|
var elements = _layoutManager.LoadElements(part);
|
|
|
|
|
var layoutRoot = _elementDisplay.DisplayElements(elements, part, displayType: displayType);
|
|
|
|
|
return shapeHelper.Parts_Layout(LayoutRoot: layoutRoot);
|
|
|
|
|
}),
|
|
|
|
|
ContentShape("Parts_Layout_Summary", () => {
|
2014-11-15 12:43:06 +08:00
|
|
|
|
var layoutShape = _contentPartDisplay.Value.BuildDisplay(part);
|
|
|
|
|
var layoutHtml = _shapeDisplay.Display(layoutShape);
|
|
|
|
|
return shapeHelper.Parts_Layout_Summary(LayoutHtml: layoutHtml);
|
2014-11-14 08:37:21 +08:00
|
|
|
|
}));
|
2014-10-15 07:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(LayoutPart part, dynamic shapeHelper) {
|
|
|
|
|
return Editor(part, null, shapeHelper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(LayoutPart part, IUpdateModel updater, dynamic shapeHelper) {
|
|
|
|
|
return ContentShape("Parts_Layout_Edit", () => {
|
|
|
|
|
var viewModel = new LayoutPartViewModel {
|
2015-02-22 19:28:22 +08:00
|
|
|
|
LayoutEditor = _layoutEditorFactory.Create(part)
|
2014-10-15 07:07:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (updater != null) {
|
|
|
|
|
updater.TryUpdateModel(viewModel, Prefix, null, new[] { "Part", "Templates" });
|
|
|
|
|
var describeContext = new DescribeElementsContext { Content = part };
|
2015-02-22 16:45:49 +08:00
|
|
|
|
var elementInstances = _mapper.ToLayoutModel(viewModel.LayoutEditor.Data, describeContext).ToArray();
|
|
|
|
|
var removedElementInstances = _serializer.Deserialize(viewModel.LayoutEditor.Trash, describeContext).ToArray();
|
2014-10-15 07:07:00 +08:00
|
|
|
|
var context = new LayoutSavingContext {
|
|
|
|
|
Content = part,
|
|
|
|
|
Updater = updater,
|
|
|
|
|
Elements = elementInstances,
|
|
|
|
|
RemovedElements = removedElementInstances
|
|
|
|
|
};
|
2015-02-22 16:45:49 +08:00
|
|
|
|
|
2014-10-15 07:07:00 +08:00
|
|
|
|
_elementManager.Saving(context);
|
|
|
|
|
_elementManager.Removing(context);
|
|
|
|
|
|
2015-02-20 05:14:55 +08:00
|
|
|
|
part.LayoutData = _serializer.Serialize(elementInstances);
|
2015-02-22 16:45:49 +08:00
|
|
|
|
part.TemplateId = viewModel.LayoutEditor.TemplateId;
|
|
|
|
|
part.SessionKey = viewModel.LayoutEditor.SessionKey;
|
2015-03-01 06:50:40 +08:00
|
|
|
|
viewModel.LayoutEditor.Data = _mapper.ToEditorModel(part.LayoutData, new DescribeElementsContext {Content = part}).ToJson();
|
2014-10-15 07:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shapeHelper.EditorTemplate(TemplateName: "Parts.Layout", Model: viewModel, Prefix: Prefix);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Exporting(LayoutPart part, ExportContentContext context) {
|
2014-11-14 10:25:05 +08:00
|
|
|
|
_layoutManager.Exporting(new ExportLayoutContext { Layout = part });
|
|
|
|
|
|
2015-02-20 05:14:55 +08:00
|
|
|
|
context.Element(part.PartDefinition.Name).SetElementValue("LayoutData", part.LayoutData);
|
2014-10-15 07:07:00 +08:00
|
|
|
|
|
|
|
|
|
if (part.TemplateId != null) {
|
|
|
|
|
var template = part.ContentItem.ContentManager.Get(part.TemplateId.Value);
|
|
|
|
|
|
|
|
|
|
if (template != null) {
|
|
|
|
|
var templateIdentity = part.ContentItem.ContentManager.GetItemMetadata(template).Identity;
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("TemplateId", templateIdentity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Importing(LayoutPart part, ImportContentContext context) {
|
2015-02-20 22:03:21 +08:00
|
|
|
|
context.ImportChildEl(part.PartDefinition.Name, "LayoutData", s => {
|
2015-02-20 05:14:55 +08:00
|
|
|
|
part.LayoutData = s;
|
2015-02-15 00:19:14 +08:00
|
|
|
|
_layoutManager.Importing(new ImportLayoutContext {
|
|
|
|
|
Layout = part,
|
|
|
|
|
Session = new ImportContentContextWrapper(context)
|
|
|
|
|
});
|
2014-11-14 10:25:05 +08:00
|
|
|
|
});
|
2015-02-22 16:45:49 +08:00
|
|
|
|
|
2014-10-15 07:07:00 +08:00
|
|
|
|
context.ImportAttribute(part.PartDefinition.Name, "TemplateId", s => part.TemplateId = GetTemplateId(context, s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int? GetTemplateId(ImportContentContext context, string templateIdentity) {
|
|
|
|
|
if (String.IsNullOrWhiteSpace(templateIdentity))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var template = context.GetItemFromSession(templateIdentity);
|
|
|
|
|
return template != null ? template.Id : default(int?);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|