2014-10-15 07:07:00 +08:00
|
|
|
|
using System;
|
2015-06-12 18:28:53 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
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;
|
2015-05-01 00:09:26 +08:00
|
|
|
|
using Orchard.Layouts.Settings;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
using Orchard.Layouts.ViewModels;
|
2015-06-12 18:28:53 +08:00
|
|
|
|
using Orchard.Logging;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
|
|
|
|
|
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;
|
2015-06-12 18:28:53 +08:00
|
|
|
|
private readonly HashSet<string> _stack;
|
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-06-12 18:28:53 +08:00
|
|
|
|
ILayoutModelMapper mapper,
|
2015-02-22 19:28:22 +08:00
|
|
|
|
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;
|
2015-06-12 18:28:53 +08:00
|
|
|
|
_stack = new HashSet<string>();
|
|
|
|
|
|
|
|
|
|
Logger = NullLogger.Instance;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-12 18:28:53 +08:00
|
|
|
|
public ILogger Logger { get; set; }
|
|
|
|
|
|
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", () => {
|
2015-06-12 18:28:53 +08:00
|
|
|
|
if (DetectRecursion(part, "Parts_Layout"))
|
|
|
|
|
return shapeHelper.Parts_Layout_Recursive();
|
|
|
|
|
|
2014-11-14 08:37:21 +08:00
|
|
|
|
var elements = _layoutManager.LoadElements(part);
|
|
|
|
|
var layoutRoot = _elementDisplay.DisplayElements(elements, part, displayType: displayType);
|
|
|
|
|
return shapeHelper.Parts_Layout(LayoutRoot: layoutRoot);
|
|
|
|
|
}),
|
|
|
|
|
ContentShape("Parts_Layout_Summary", () => {
|
2015-06-12 18:28:53 +08:00
|
|
|
|
if (DetectRecursion(part, "Parts_Layout_Summary"))
|
|
|
|
|
return shapeHelper.Parts_Layout_Summary_Recursive();
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-06-12 18:28:53 +08:00
|
|
|
|
private bool DetectRecursion(LayoutPart part, string shapeName) {
|
|
|
|
|
var key = String.Format("{0}:{1}", shapeName, part.Id);
|
|
|
|
|
|
|
|
|
|
if (_stack.Contains(key)) {
|
|
|
|
|
Logger.Debug(String.Format("Detected recursive layout rendering of layout with ID = {0} and shape = {1}", part.Id, shapeName));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_stack.Add(key);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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", () => {
|
2015-05-01 00:09:26 +08:00
|
|
|
|
|
|
|
|
|
if (part.Id == 0 && String.IsNullOrWhiteSpace(part.LayoutData)) {
|
|
|
|
|
part.LayoutData = part.TypePartDefinition.Settings.GetModel<LayoutTypePartSettings>().DefaultLayoutData;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 07:07:00 +08:00
|
|
|
|
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-06-12 18:28:53 +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?);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|