Refactored ShapePart and moved Language setting to ShapePart/Type settings.

"Language" has been renamed to "Processor" to better match the term "ITemplateProcessor".
This commit is contained in:
Sipke Schoorstra
2014-01-11 23:43:19 +01:00
parent 372e00db43
commit 625d7cbd5a
17 changed files with 136 additions and 51 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using Orchard.Compilation.Razor;
using Orchard.ContentManagement;
@@ -40,20 +39,17 @@ namespace Orchard.Templates.Drivers {
protected override DriverResult Editor(ShapePart part, IUpdateModel updater, dynamic shapeHelper) {
var viewModel = new ShapePartViewModel {
Name = part.Name,
Template = part.Template,
Language = part.Language,
AvailableLanguages = _processors.Select(x => x.Type).Distinct().ToArray()
Template = part.Template
};
if (updater != null
&& updater.TryUpdateModel(viewModel, Prefix, null, new[] { "AvailableLanguages" })
&& ValidateShapeName(viewModel.Name, updater)) {
part.Name = viewModel.Name.TrimSafe();
part.Language = viewModel.Language;
part.Template = viewModel.Template;
try {
var processor = _processors.FirstOrDefault(x => String.Equals(x.Type, part.Language, StringComparison.OrdinalIgnoreCase));
var processor = _processors.FirstOrDefault(x => String.Equals(x.Type, part.ProcessorName, StringComparison.OrdinalIgnoreCase)) ?? _processors.First();
processor.Verify(part.Template);
_templateHolder.Set(part.Name, part.Template);
}
@@ -67,13 +63,11 @@ namespace Orchard.Templates.Drivers {
protected override void Exporting(ShapePart part, ExportContentContext context) {
context.Element(part.PartDefinition.Name).SetAttributeValue("Name", part.Name);
context.Element(part.PartDefinition.Name).SetAttributeValue("Language", part.Language);
context.Element(part.PartDefinition.Name).Add(new XCData(part.Template));
}
protected override void Importing(ShapePart part, ImportContentContext context) {
context.ImportAttribute(part.PartDefinition.Name, "Name", x => part.Name = x);
context.ImportAttribute(part.PartDefinition.Name, "Language", x => part.Language = x);
var shapeElement = context.Data.Element(part.PartDefinition.Name);
if(shapeElement != null)

View File

@@ -8,7 +8,6 @@ namespace Orchard.Templates {
SchemaBuilder.CreateTable("ShapePartRecord", table => table
.ContentPartRecord()
.Column<string>("Name", c => c.WithLength(100))
.Column<string>("Language", c => c.WithLength(50))
.Column<string>("Template", c => c.Unlimited()));
ContentDefinitionManager.AlterPartDefinition("ShapePart", part => part
@@ -18,7 +17,8 @@ namespace Orchard.Templates {
ContentDefinitionManager.AlterTypeDefinition("Template", type => type
.WithPart("CommonPart")
.WithPart("IdentityPart")
.WithPart("ShapePart")
.WithPart("ShapePart", p => p
.WithSetting("ShapePartSettings.Processor", "Razor"))
.Draftable());
return 1;
}

View File

@@ -1,6 +1,5 @@
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using Orchard.Data.Conventions;
using Orchard.Templates.Settings;
namespace Orchard.Templates.Models {
public class ShapePart : ContentPart<ShapePartRecord> {
@@ -9,9 +8,8 @@ namespace Orchard.Templates.Models {
set { Store(x => x.Name, value); }
}
public string Language {
get { return Retrieve(x => x.Language); }
set { Store(x => x.Language, value); }
public string ProcessorName {
get { return TypePartDefinition.Settings.GetModel<ShapePartSettings>().Processor; }
}
public string Template {
@@ -19,12 +17,4 @@ namespace Orchard.Templates.Models {
set { Store(x => x.Template, value); }
}
}
public class ShapePartRecord : ContentPartRecord {
public virtual string Name { get; set; }
public virtual string Language { get; set; }
[StringLengthMax]
public virtual string Template { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using Orchard.ContentManagement.Records;
using Orchard.Data.Conventions;
namespace Orchard.Templates.Models {
public class ShapePartRecord : ContentPartRecord {
public virtual string Name { get; set; }
[StringLengthMax]
public virtual string Template { get; set; }
}
}

View File

@@ -167,12 +167,16 @@
<Compile Include="Drivers\ShapePartDriver.cs" />
<Compile Include="Models\ShapePart.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Models\ShapePartRecord.cs" />
<Compile Include="Services\ITemplateProcessor.cs" />
<Compile Include="Services\ITemplateService.cs" />
<Compile Include="Services\TemplateProcessorImpl.cs" />
<Compile Include="Services\RazorTemplateProcessor.cs" />
<Compile Include="Services\TemplateBindingStrategy.cs" />
<Compile Include="Services\DefaultTemplateService.cs" />
<Compile Include="Settings\ShapePartSettings.cs" />
<Compile Include="Settings\ShapePartSettingsEvents.cs" />
<Compile Include="ViewModels\ShapePartSettingsViewModel.cs" />
<Compile Include="ViewModels\ShapePartViewModel.cs" />
</ItemGroup>
<ItemGroup>
@@ -188,11 +192,17 @@
<Content Include="Views\Admin\List.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\EditorTemplates\TemplateLanguagePicker.cshtml" />
<Content Include="Views\EditorTemplates\TemplateProcessorPicker.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Tokens\" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\DefinitionTemplates\Parts.Shape.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\DefinitionTemplates\ShapePartSettingsViewModel.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -20,13 +20,13 @@ namespace Orchard.Templates.Services {
_processors = processors;
}
public string Execute<TModel>(string template, string name, string language, TModel model = default(TModel)) {
return Execute(template, name, language, null, model);
public string Execute<TModel>(string template, string name, string processorName, TModel model = default(TModel)) {
return Execute(template, name, processorName, null, model);
}
public string Execute<TModel>(string template, string name, string language, DisplayContext context, TModel model = default(TModel)) {
var processor = _processors.FirstOrDefault(x => String.Equals(x.Type, language, StringComparison.OrdinalIgnoreCase));
return processor != null ? processor.Process(template, name, context, model) : string.Empty;
public string Execute<TModel>(string template, string name, string processorName, DisplayContext context, TModel model = default(TModel)) {
var processor = _processors.FirstOrDefault(x => String.Equals(x.Type, processorName, StringComparison.OrdinalIgnoreCase)) ?? _processors.First();
return processor.Process(template, name, context, model);
}
public IEnumerable<ShapePart> GetTemplates(VersionOptions versionOptions = null) {

View File

@@ -5,8 +5,8 @@ using Orchard.Templates.Models;
namespace Orchard.Templates.Services {
public interface ITemplateService : IDependency {
string Execute<TModel>(string template, string name, string language, TModel model = default(TModel));
string Execute<TModel>(string template, string name, string language, DisplayContext context, TModel model = default(TModel));
string Execute<TModel>(string template, string name, string processorName, TModel model = default(TModel));
string Execute<TModel>(string template, string name, string processorName, DisplayContext context, TModel model = default(TModel));
IEnumerable<ShapePart> GetTemplates(VersionOptions versionOptions = null);
}
}

View File

@@ -34,7 +34,7 @@ namespace Orchard.Templates.Services {
var shapes = _templateService.GetTemplates().Select(r =>
new {
r.Name,
r.Language,
r.ProcessorName,
r.Template
})
.ToList();
@@ -60,7 +60,7 @@ namespace Orchard.Templates.Services {
.BoundAs("Template::" + shapeType,
descriptor => context => {
var template = _templateProvider.Get(record.Name);
return template != null ? PerformInvoke(context, record.Name, record.Language, template) : new HtmlString("");
return template != null ? PerformInvoke(context, record.Name, record.ProcessorName, template) : new HtmlString("");
});
}
}

View File

@@ -0,0 +1,5 @@
namespace Orchard.Templates.Settings {
public class ShapePartSettings {
public string Processor { get; set; }
}
}

View File

@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;
using Orchard.Templates.Services;
using Orchard.Templates.ViewModels;
namespace Orchard.Templates.Settings {
public class ShapePartSettingsEvents : ContentDefinitionEditorEventsBase {
private readonly IEnumerable<ITemplateProcessor> _processors;
public ShapePartSettingsEvents(IEnumerable<ITemplateProcessor> processors) {
_processors = processors;
}
public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition) {
if (definition.PartDefinition.Name != "ShapePart")
yield break;
var settings = definition.Settings.GetModel<ShapePartSettings>();
var model = new ShapePartSettingsViewModel {
Processor = settings.Processor,
AvailableProcessors = _processors.ToArray()
};
yield return DefinitionTemplate(model);
}
public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel) {
if (builder.Name != "ShapePart")
yield break;
var model = new ShapePartSettingsViewModel {
AvailableProcessors = _processors.ToArray()
};
updateModel.TryUpdateModel(model, "ShapePartSettingsViewModel", new[] {"Processor"}, null);
builder.WithSetting("ShapePartSettings.Processor", model.Processor);
yield return DefinitionTemplate(model);
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Orchard.Templates.Services;
namespace Orchard.Templates.ViewModels {
public class ShapePartSettingsViewModel {
[UIHint("TemplateProcessorPicker")]
public string Processor { get; set; }
public IList<ITemplateProcessor> AvailableProcessors { get; set; }
}
}

View File

@@ -1,13 +1,9 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
namespace Orchard.Templates.ViewModels {
public class ShapePartViewModel {
[StringLength(100)]
public string Name { get; set; }
[UIHint("TemplateLanguagePicker")]
public string Language { get; set; }
public string Template { get; set; }
public IEnumerable<string> AvailableLanguages { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
@model Orchard.Templates.ViewModels.ShapePartViewModel
@{
Style.Include("~/modules/orchard.templates/scripts/codemirror/lib/codemirror.css");
Style.Include("template-editor.css");
Script.Require("jQuery");
Script.Include("codemirror/lib/codemirror.js");
Script.Include("codemirror/mode/xml/xml.js");
Script.Include("codemirror/mode/javascript/javascript.js");
Script.Include("codemirror/mode/css/css.js");
Script.Include("codemirror/mode/htmlmixed/htmlmixed.js");
Script.Include("codemirror/mode/htmlembedded/htmlembedded.js");
Script.Include("template-editor.js", "template-editor.min.js");
}
<fieldset>
<div>
@Html.LabelFor(m => m.Name, T("Name"))
@Html.TextBoxFor(m => m.Name, new { @class = "text medium" })
<span class="hint">@T("The name of this template, which can be used to reference from code (as when using shape type names) and tokens.")</span>
</div>
</fieldset>
<fieldset>
<div>
@Html.TextAreaFor(m => m.Template, new { @class = "text large code-editor" })
</div>
</fieldset>

View File

@@ -0,0 +1,5 @@
@model Orchard.Templates.ViewModels.ShapePartSettingsViewModel
<fieldset>
<label for="@Html.FieldIdFor(m => m.Processor)">@T("Template Processor")</label>
@Html.EditorFor(m => m.Processor, new { AvailableProcessors = Model.AvailableProcessors })
</fieldset>

View File

@@ -22,11 +22,4 @@
<div>
@Html.TextAreaFor(m => m.Template, new { @class = "text large code-editor" })
</div>
</fieldset>
<fieldset>
<div>
@Html.LabelFor(m => m.Language, T("Language"))
@Html.EditorFor(m => m.Language, new { Model.AvailableLanguages })
<span class="hint">@T("The templating language to use.")</span>
</div>
</fieldset>

View File

@@ -1,6 +0,0 @@
@{
var languages = (IList<string>)ViewBag.AvailableLanguages;
var currentValue = ViewData.TemplateInfo.FormattedModelValue as string;
var options = languages.Select(x => new SelectListItem() {Text = x, Value = x, Selected = x == currentValue});
}
@Html.DropDownList("", options)

View File

@@ -0,0 +1,7 @@
@using Orchard.Templates.Services
@{
var processors = (IList<ITemplateProcessor>)ViewBag.AvailableProcessors;
var currentValue = ViewData.TemplateInfo.FormattedModelValue as string;
var options = processors.Select(x => new SelectListItem() {Text = x.Type, Value = x.Type, Selected = x.Type == currentValue});
}
@Html.DropDownList("", options)