Implementing and applying reusable flavor picker.

This commit is contained in:
Sipke Schoorstra
2014-07-13 15:54:27 -07:00
parent 3a0a040cf0
commit 3ee53e1565
9 changed files with 67 additions and 39 deletions

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Orchard.Caching;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Utility.Extensions;
namespace Orchard.Core.Common.Services {
public class FlavorService : IFlavorService {
private readonly Func<IShapeTableLocator> _shapeTableLocator;
private readonly IWorkContextAccessor _wca;
private readonly ICacheManager _cacheManager;
public FlavorService(Func<IShapeTableLocator> shapeTableLocator, IWorkContextAccessor wca, ICacheManager cacheManager) {
_shapeTableLocator = shapeTableLocator;
_wca = wca;
_cacheManager = cacheManager;
}
public IList<string> GetFlavors() {
return _cacheManager.Get("Flavors", context => {
var shapeTable = _shapeTableLocator().Lookup(_wca.GetContext().CurrentTheme.Id);
var flavors = shapeTable.Bindings.Keys
.Where(x => x.StartsWith("Body_Editor__", StringComparison.OrdinalIgnoreCase))
.Select(x => x.Substring("Body_Editor__".Length))
.Where(x => !String.IsNullOrWhiteSpace(x))
.Select(x => x[0].ToString(CultureInfo.InvariantCulture).ToUpper() + x.Substring(1))
.Select(x => x.CamelFriendly());
return flavors.ToList();
});
}
}
}

View File

@@ -0,0 +1,7 @@
using System.Collections.Generic;
namespace Orchard.Core.Common.Services {
public interface IFlavorService : IDependency {
IList<string> GetFlavors();
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
@@ -9,6 +10,8 @@ namespace Orchard.Core.Common.Settings {
public class BodyPartSettings {
public const string FlavorDefaultDefault = "html";
private string _flavorDefault;
[DataType("Flavor")]
public string FlavorDefault {
get { return !string.IsNullOrWhiteSpace(_flavorDefault)
? _flavorDefault
@@ -18,6 +21,7 @@ namespace Orchard.Core.Common.Settings {
}
public class BodyTypePartSettings {
[DataType("Flavor")]
public string Flavor { get; set; }
}

View File

@@ -1,6 +1,9 @@
namespace Orchard.Core.Common.Settings {
using System.ComponentModel.DataAnnotations;
namespace Orchard.Core.Common.Settings {
public class TextFieldSettings {
[DataType("Flavor")]
public string Flavor { get; set; }
public bool Required { get; set; }
public string Hint { get; set; }

View File

@@ -1,41 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization;
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.Core.Common.ViewModels;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Utility.Extensions;
namespace Orchard.Core.Common.Settings {
public class TextFieldSettingsEvents : ContentDefinitionEditorEventsBase {
private readonly IOrchardServices _orchardServices;
private readonly Func<IShapeTableLocator> _shapeTableLocator;
public TextFieldSettingsEvents(IOrchardServices orchardServices, Func<IShapeTableLocator> shapeTableLocator) {
_orchardServices = orchardServices;
_shapeTableLocator = shapeTableLocator;
}
public override IEnumerable<TemplateViewModel> PartFieldEditor(ContentPartFieldDefinition definition) {
if (definition.FieldDefinition.Name == "TextField") {
var shapeTable = _shapeTableLocator().Lookup(_orchardServices.WorkContext.CurrentTheme.Id);
var flavors = shapeTable.Bindings.Keys
.Where(x => x.StartsWith("Body_Editor__", StringComparison.OrdinalIgnoreCase))
.Select(x => x.Substring("Body_Editor__".Length))
.Where(x => !String.IsNullOrWhiteSpace(x))
.Select(x => x[0].ToString(CultureInfo.InvariantCulture).ToUpper() + x.Substring(1) )
.Select(x => x.CamelFriendly())
;
var model = new TextFieldSettingsEventsViewModel {
Settings = definition.Settings.GetModel<TextFieldSettings>(),
Flavors = flavors.ToArray()
};
yield return DefinitionTemplate(model);

View File

@@ -2,11 +2,6 @@
namespace Orchard.Core.Common.ViewModels {
public class TextFieldSettingsEventsViewModel {
public TextFieldSettingsEventsViewModel() {
Flavors = new string[0];
}
public TextFieldSettings Settings { get; set; }
public string[] Flavors { get; set; }
}
}

View File

@@ -1,16 +1,9 @@
@using Orchard.Utility.Extensions
@model Orchard.Core.Common.ViewModels.TextFieldSettingsEventsViewModel
@model Orchard.Core.Common.ViewModels.TextFieldSettingsEventsViewModel
<fieldset>
<div>
<label for="@Html.FieldIdFor(m => m.Settings.Flavor)" class="forcheckbox">@T("Display options")</label>
<select id="@Html.FieldIdFor(m => m.Settings.Flavor)" name="@Html.FieldNameFor(m => m.Settings.Flavor)">
@Html.SelectOption("", String.IsNullOrWhiteSpace(Model.Settings.Flavor), T("Default flavor").ToString())
@foreach(string flavor in Model.Flavors) {
@Html.SelectOption(flavor, flavor.Equals(Model.Settings.Flavor, StringComparison.OrdinalIgnoreCase), flavor.CamelFriendly())
}
</select>
@Html.EditorFor(m => m.Settings.Flavor)
@Html.ValidationMessageFor(m => m.Settings.Flavor)
</div>
</fieldset>
@@ -23,6 +16,6 @@
<fieldset>
<label for="@Html.FieldIdFor(m => m.Settings.Hint)">@T("Help text")</label>
@Html.TextAreaFor(m => m.Settings.Hint, new { @class = "text medium", rows = "5" })
<span class="hint">@T("The help text is written under the field when authors are editing the content item.")</span>
<span class="hint">@T("The help text is written under the field when authors are editing the content item.")</span>
@Html.ValidationMessageFor(m => m.Settings.Hint)
</fieldset>

View File

@@ -0,0 +1,8 @@
@using Orchard.Core.Common.Services
@using Orchard.Utility.Extensions
@{
var flavors = WorkContext.Resolve<IFlavorService>().GetFlavors();
var selectedFlavor = ViewData.TemplateInfo.FormattedModelValue as string;
var options = flavors.Select(x => new SelectListItem {Text = x.CamelFriendly(), Value = x, Selected = String.Equals(x, selectedFlavor, StringComparison.OrdinalIgnoreCase)}).ToArray();
}
@Html.DropDownList("", options, T("Default Flavor").Text)

View File

@@ -86,7 +86,9 @@
<Compile Include="Common\Models\IdentityPart.cs" />
<Compile Include="Common\ResourceManifest.cs" />
<Compile Include="Common\Routes.cs" />
<Compile Include="Common\Services\FlavorService.cs" />
<Compile Include="Common\Services\IdentifierResolverSelector.cs" />
<Compile Include="Common\Services\IFlavorService.cs" />
<Compile Include="Common\Services\TextFieldFilter.cs" />
<Compile Include="Common\Services\XmlRpcHandler.cs" />
<Compile Include="Common\DateEditor\DateEditorViewModel.cs" />
@@ -559,6 +561,9 @@
<Content Include="Containers\Views\EditorTemplates\ContainerWidget.cshtml" />
<Content Include="Containers\Views\Parts.ContainerWidget.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Common\Views\EditorTemplates\Flavor.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>