Extend snippet manifest format

This commit is contained in:
Matthew Harris
2016-05-27 17:08:03 +01:00
committed by Sébastien Ros
parent 0f155827fd
commit 897687e4b3
2 changed files with 59 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
using Orchard.Environment.Extensions; using Orchard.Environment.Extensions;
using Orchard.Layouts.Framework.Elements; using Orchard.Layouts.Framework.Elements;
using Orchard.Localization;
namespace Orchard.Layouts.Elements { namespace Orchard.Layouts.Elements {
[OrchardFeature("Orchard.Layouts.Snippets")] [OrchardFeature("Orchard.Layouts.Snippets")]
@@ -15,5 +16,9 @@ namespace Orchard.Layouts.Elements {
public override bool HasEditor { public override bool HasEditor {
get { return false; } get { return false; }
} }
public override string ToolboxIcon {
get { return "\uf10c"; }
}
} }
} }

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using Orchard.DisplayManagement; using Orchard.DisplayManagement;
using Orchard.DisplayManagement.Descriptors; using Orchard.DisplayManagement.Descriptors;
using Orchard.Environment; using Orchard.Environment;
@@ -63,13 +64,17 @@ namespace Orchard.Layouts.Providers {
var snippetElement = (Snippet)_elementFactory.Value.Activate(elementType); var snippetElement = (Snippet)_elementFactory.Value.Activate(elementType);
foreach (var shapeDescriptor in shapeDescriptors) { foreach (var shapeDescriptor in shapeDescriptors) {
var snippetManifest = ParseSnippetManifest(shapeDescriptor.Value.BindingSource);
var shapeType = shapeDescriptor.Value.ShapeType; var shapeType = shapeDescriptor.Value.ShapeType;
var elementName = GetDisplayName(shapeDescriptor.Value.BindingSource); var elementName = GetDisplayName(snippetManifest, shapeDescriptor.Value.BindingSource);
var toolboxIcon = GetToolboxIcon(snippetManifest, snippetElement);
var description = GetDescription(snippetManifest, shapeType);
var category = GetCategory(snippetManifest, snippetElement);
var closureDescriptor = shapeDescriptor; var closureDescriptor = shapeDescriptor;
var snippetDescriptor = ParseSnippetDescriptor(shapeDescriptor.Value.BindingSource); var snippetDescriptor = ParseSnippetDescriptor(snippetManifest);
yield return new ElementDescriptor(elementType, shapeType, new LocalizedString(elementName), new LocalizedString(String.Format("An element that renders the {0} shape.", shapeType)), snippetElement.Category) { yield return new ElementDescriptor(elementType, shapeType, new LocalizedString(elementName), description, category) {
Displaying = displayContext => Displaying(displayContext, closureDescriptor.Value, snippetDescriptor), Displaying = displayContext => Displaying(displayContext, closureDescriptor.Value, snippetDescriptor),
ToolboxIcon = "\uf10c", ToolboxIcon = toolboxIcon,
EnableEditorDialog = snippetDescriptor != null || HasSnippetFields(shapeDescriptor.Value), EnableEditorDialog = snippetDescriptor != null || HasSnippetFields(shapeDescriptor.Value),
Editor = ctx => Editor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx), Editor = ctx => Editor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx),
UpdateEditor = ctx => UpdateEditor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx) UpdateEditor = ctx => UpdateEditor(snippetDescriptor ?? DescribeSnippet(shapeType, snippetElement), ctx)
@@ -132,13 +137,7 @@ namespace Orchard.Layouts.Providers {
context.ElementShape.Snippet = shape; context.ElementShape.Snippet = shape;
} }
private string GetDisplayName(string bindingSource) { private dynamic ParseSnippetManifest(string 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 physicalSourcePath = _wca.GetContext().HttpContext.Server.MapPath(bindingSource);
var paramsFileName = Path.Combine(Path.GetDirectoryName(physicalSourcePath) ?? "", Path.GetFileNameWithoutExtension(physicalSourcePath) + ".txt"); var paramsFileName = Path.Combine(Path.GetDirectoryName(physicalSourcePath) ?? "", Path.GetFileNameWithoutExtension(physicalSourcePath) + ".txt");
@@ -147,7 +146,50 @@ namespace Orchard.Layouts.Providers {
var yaml = File.ReadAllText(paramsFileName); var yaml = File.ReadAllText(paramsFileName);
var snippetConfig = Deserialize(yaml); var snippetConfig = Deserialize(yaml);
var fieldsConfig = snippetConfig.Fields != null ? snippetConfig.Fields.Children : new dynamic[0];
return snippetConfig;
}
private string GetDisplayName(dynamic snippetManifest, string bindingSource) {
if (snippetManifest != null && (string)snippetManifest.DisplayName != null) {
return (string)snippetManifest.DisplayName;
}
var fileName = Path.GetFileNameWithoutExtension(bindingSource) ?? "";
var lastIndex = fileName.IndexOf(SnippetShapeSuffix, StringComparison.OrdinalIgnoreCase);
return fileName.Substring(0, lastIndex).CamelFriendly();
}
private string GetToolboxIcon(dynamic snippetManifest, Snippet snippetElement) {
if (snippetManifest != null && (string)snippetManifest.ToolboxIcon != null) {
return Regex.Unescape((string)snippetManifest.ToolboxIcon);
}
return snippetElement.ToolboxIcon;
}
private LocalizedString GetDescription(dynamic snippetManifest, string shapeType) {
if (snippetManifest != null && (string)snippetManifest.Description != null) {
return new LocalizedString((string)snippetManifest.Description);
}
return new LocalizedString(String.Format("An element that renders the {0} shape.", shapeType));
}
private string GetCategory(dynamic snippetManifest, Snippet snippetElement) {
if (snippetManifest != null && (string)snippetManifest.Category != null) {
return (string)snippetManifest.Category;
}
return snippetElement.Category;
}
private SnippetDescriptor ParseSnippetDescriptor(dynamic snippetManifest) {
if(snippetManifest == null || snippetManifest.Fields.Count == 0) {
return null;
}
var fieldsConfig = snippetManifest.Fields.Children;
var descriptor = new SnippetDescriptor(); var descriptor = new SnippetDescriptor();
foreach (var fieldConfig in fieldsConfig) { foreach (var fieldConfig in fieldsConfig) {