2014-10-15 07:07:00 +08:00
|
|
|
|
using System;
|
2015-02-20 05:14:55 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
using System.Linq;
|
2015-02-20 05:14:55 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-10-15 07:07:00 +08:00
|
|
|
|
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-02-20 05:14:55 +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;
|
2015-02-20 05:14:55 +08:00
|
|
|
|
using Orchard.Utility.Extensions;
|
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;
|
2014-11-14 08:37:21 +08:00
|
|
|
|
|
|
|
|
|
public LayoutPartDriver(
|
|
|
|
|
ILayoutSerializer serializer,
|
|
|
|
|
IElementDisplay elementDisplay,
|
|
|
|
|
IElementManager elementManager,
|
2014-11-15 12:43:06 +08:00
|
|
|
|
ILayoutManager layoutManager,
|
|
|
|
|
Lazy<IContentPartDisplay> contentPartDisplay,
|
2015-02-20 05:14:55 +08:00
|
|
|
|
IShapeDisplay shapeDisplay,
|
|
|
|
|
ILayoutModelMapper mapper) {
|
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;
|
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-20 05:14:55 +08:00
|
|
|
|
Data = _mapper.ToEditorModel(part.LayoutData, new DescribeElementsContext { Content = part}).ToJson(),
|
|
|
|
|
ConfigurationData = GetConfigurationData(part),
|
2014-10-15 07:07:00 +08:00
|
|
|
|
TemplateId = part.TemplateId,
|
2015-01-04 00:28:24 +08:00
|
|
|
|
Content = part,
|
2015-02-20 05:14:55 +08:00
|
|
|
|
SessionKey = part.SessionKey,
|
|
|
|
|
Templates = _layoutManager.GetTemplates().Where(x => x.Id != part.Id).ToArray()
|
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-20 05:14:55 +08:00
|
|
|
|
var elementInstances = _mapper.ToLayoutModel(viewModel.Data, describeContext).ToArray();
|
2014-10-15 07:07:00 +08:00
|
|
|
|
var removedElementInstances = _serializer.Deserialize(viewModel.Trash, describeContext).ToArray();
|
|
|
|
|
var context = new LayoutSavingContext {
|
|
|
|
|
Content = part,
|
|
|
|
|
Updater = updater,
|
|
|
|
|
Elements = elementInstances,
|
|
|
|
|
RemovedElements = removedElementInstances
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_elementManager.Saving(context);
|
|
|
|
|
_elementManager.Removing(context);
|
|
|
|
|
|
2015-02-20 05:14:55 +08:00
|
|
|
|
part.LayoutData = _serializer.Serialize(elementInstances);
|
2014-10-15 07:07:00 +08:00
|
|
|
|
part.TemplateId = viewModel.TemplateId;
|
2015-01-04 00:28:24 +08:00
|
|
|
|
part.SessionKey = viewModel.SessionKey;
|
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-15 00:19:14 +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?);
|
|
|
|
|
}
|
2015-02-20 05:14:55 +08:00
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2014-10-15 07:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|