mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-25 10:59:18 +08:00
committed by
Sébastien Ros
parent
38bf36075a
commit
a5605b7fbd
@@ -17,13 +17,14 @@ using Orchard.Layouts.Services;
|
||||
using Orchard.Layouts.Shapes;
|
||||
using Orchard.Layouts.ViewModels;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Services;
|
||||
using Orchard.Themes.Services;
|
||||
using Orchard.Tokens;
|
||||
using Orchard.Utility.Extensions;
|
||||
|
||||
namespace Orchard.Layouts.Providers {
|
||||
[OrchardFeature("Orchard.Layouts.Snippets")]
|
||||
public class SnippetElementHarvester : Component, IElementHarvester {
|
||||
public class SnippetElementHarvester : IElementHarvester {
|
||||
private const string SnippetShapeSuffix = "Snippet";
|
||||
private readonly Work<IShapeFactory> _shapeFactory;
|
||||
private readonly Work<ISiteThemeService> _siteThemeService;
|
||||
@@ -33,6 +34,7 @@ namespace Orchard.Layouts.Providers {
|
||||
private readonly Work<ICurrentThemeShapeBindingResolver> _currentThemeShapeBindingResolver;
|
||||
private readonly Work<ITokenizer> _tokenizer;
|
||||
private readonly IWorkContextAccessor _wca;
|
||||
private readonly Work<IYamlParser> _yamlParser;
|
||||
|
||||
public SnippetElementHarvester(
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
@@ -42,7 +44,8 @@ namespace Orchard.Layouts.Providers {
|
||||
Work<IElementFactory> elementFactory,
|
||||
Work<IShapeDisplay> shapeDisplay,
|
||||
Work<ITokenizer> tokenizer,
|
||||
Work<ICurrentThemeShapeBindingResolver> currentThemeShapeBindingResolver) {
|
||||
Work<ICurrentThemeShapeBindingResolver> currentThemeShapeBindingResolver,
|
||||
Work<IYamlParser> yamlParser) {
|
||||
|
||||
_shapeFactory = shapeFactory;
|
||||
_siteThemeService = siteThemeService;
|
||||
@@ -51,6 +54,7 @@ namespace Orchard.Layouts.Providers {
|
||||
_shapeDisplay = shapeDisplay;
|
||||
_tokenizer = tokenizer;
|
||||
_currentThemeShapeBindingResolver = currentThemeShapeBindingResolver;
|
||||
_yamlParser = yamlParser;
|
||||
_wca = workContextAccessor;
|
||||
}
|
||||
|
||||
@@ -65,12 +69,13 @@ namespace Orchard.Layouts.Providers {
|
||||
var shapeType = shapeDescriptor.Value.ShapeType;
|
||||
var elementName = GetDisplayName(shapeDescriptor.Value.BindingSource);
|
||||
var closureDescriptor = shapeDescriptor;
|
||||
yield return new ElementDescriptor(elementType, shapeType, T(elementName), T("An element that renders the {0} shape.", shapeType), snippetElement.Category) {
|
||||
Displaying = displayContext => Displaying(displayContext, closureDescriptor.Value),
|
||||
var snippetDescriptor = ParseSnippetDescriptor(shapeDescriptor.Value.BindingSource);
|
||||
yield return new ElementDescriptor(elementType, shapeType, new LocalizedString(elementName), new LocalizedString(String.Format("An element that renders the {0} shape.", shapeType)), snippetElement.Category) {
|
||||
Displaying = displayContext => Displaying(displayContext, closureDescriptor.Value, snippetDescriptor),
|
||||
ToolboxIcon = "\uf10c",
|
||||
EnableEditorDialog = HasSnippetFields(shapeDescriptor.Value),
|
||||
Editor = ctx => Editor(DescribeSnippet(shapeType, snippetElement), ctx),
|
||||
UpdateEditor = ctx => UpdateEditor(DescribeSnippet(shapeType, snippetElement), ctx)
|
||||
EnableEditorDialog = snippetDescriptor != null || HasSnippetFields(shapeDescriptor.Value),
|
||||
Editor = ctx => Editor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx),
|
||||
UpdateEditor = ctx => UpdateEditor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -113,22 +118,53 @@ namespace Orchard.Layouts.Providers {
|
||||
context.EditorResult.Add(snippetEditorShape);
|
||||
}
|
||||
|
||||
private void Displaying(ElementDisplayingContext context, ShapeDescriptor shapeDescriptor) {
|
||||
private void Displaying(ElementDisplayingContext context, ShapeDescriptor shapeDescriptor, SnippetDescriptor snippetDescriptor) {
|
||||
var shapeType = shapeDescriptor.ShapeType;
|
||||
var shape = (dynamic)_shapeFactory.Value.Create(shapeType);
|
||||
|
||||
shape.Element = context.Element;
|
||||
|
||||
if (snippetDescriptor != null) {
|
||||
foreach (var fieldDescriptor in snippetDescriptor.Fields) {
|
||||
var value = context.Element.Data.Get(fieldDescriptor.Name);
|
||||
shape.Properties[fieldDescriptor.Name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
ElementShapes.AddTokenizers(shape, _tokenizer.Value);
|
||||
context.ElementShape.Snippet = shape;
|
||||
}
|
||||
|
||||
private string GetDisplayName(string bindingSource) {
|
||||
var fileName = Path.GetFileNameWithoutExtension(bindingSource);
|
||||
var fileName = Path.GetFileNameWithoutExtension(bindingSource) ?? "";
|
||||
var lastIndex = fileName.IndexOf(SnippetShapeSuffix, StringComparison.OrdinalIgnoreCase);
|
||||
return fileName.Substring(0, lastIndex).CamelFriendly();
|
||||
}
|
||||
|
||||
private SnippetDescriptor ParseSnippetDescriptor(string bindingSource) {
|
||||
var physicalSourcePath = _wca.GetContext().HttpContext.Server.MapPath(bindingSource);
|
||||
var paramsFileName = Path.Combine(Path.GetDirectoryName(physicalSourcePath) ?? "", Path.GetFileNameWithoutExtension(physicalSourcePath) + ".txt");
|
||||
|
||||
if (!File.Exists(paramsFileName))
|
||||
return null;
|
||||
|
||||
var yaml = File.ReadAllText(paramsFileName);
|
||||
var snippetConfig = _yamlParser.Value.Deserialize(yaml);
|
||||
var fieldsConfig = snippetConfig.Fields != null ? snippetConfig.Fields.Children : new dynamic[0];
|
||||
var descriptor = new SnippetDescriptor();
|
||||
|
||||
foreach (var fieldConfig in fieldsConfig) {
|
||||
descriptor.Fields.Add(new SnippetFieldDescriptor {
|
||||
Name = (string)fieldConfig.Name,
|
||||
Type = (string)fieldConfig.Type,
|
||||
DisplayName = new LocalizedString((string)fieldConfig.DisplayName),
|
||||
Description = new LocalizedString((string)fieldConfig.Description)
|
||||
});
|
||||
}
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
private SnippetDescriptor DescribeSnippet(string shapeType, Snippet element) {
|
||||
var shape = (dynamic)_shapeFactory.Value.Create(shapeType);
|
||||
shape.Element = element;
|
||||
|
||||
Reference in New Issue
Block a user