From 3ee53e1565981d4970e7b45318e17e644acf04b2 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sun, 13 Jul 2014 15:54:27 -0700 Subject: [PATCH] Implementing and applying reusable flavor picker. --- .../Core/Common/Services/FlavorService.cs | 35 +++++++++++++++++++ .../Core/Common/Services/IFlavorService.cs | 7 ++++ .../Core/Common/Settings/BodySettings.cs | 4 +++ .../Core/Common/Settings/TextFieldSettings.cs | 5 ++- .../Settings/TextFieldSettingsEvents.cs | 24 +------------ .../TextFieldSettingsEventsViewModel.cs | 5 --- .../TextFieldSettingsEventsViewModel.cshtml | 13 ++----- .../Views/EditorTemplates/Flavor.cshtml | 8 +++++ src/Orchard.Web/Core/Orchard.Core.csproj | 5 +++ 9 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 src/Orchard.Web/Core/Common/Services/FlavorService.cs create mode 100644 src/Orchard.Web/Core/Common/Services/IFlavorService.cs create mode 100644 src/Orchard.Web/Core/Common/Views/EditorTemplates/Flavor.cshtml diff --git a/src/Orchard.Web/Core/Common/Services/FlavorService.cs b/src/Orchard.Web/Core/Common/Services/FlavorService.cs new file mode 100644 index 000000000..6abcd58de --- /dev/null +++ b/src/Orchard.Web/Core/Common/Services/FlavorService.cs @@ -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 _shapeTableLocator; + private readonly IWorkContextAccessor _wca; + private readonly ICacheManager _cacheManager; + + public FlavorService(Func shapeTableLocator, IWorkContextAccessor wca, ICacheManager cacheManager) { + _shapeTableLocator = shapeTableLocator; + _wca = wca; + _cacheManager = cacheManager; + } + + public IList 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(); + }); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Services/IFlavorService.cs b/src/Orchard.Web/Core/Common/Services/IFlavorService.cs new file mode 100644 index 000000000..a0f94fe2f --- /dev/null +++ b/src/Orchard.Web/Core/Common/Services/IFlavorService.cs @@ -0,0 +1,7 @@ +using System.Collections.Generic; + +namespace Orchard.Core.Common.Services { + public interface IFlavorService : IDependency { + IList GetFlavors(); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Settings/BodySettings.cs b/src/Orchard.Web/Core/Common/Settings/BodySettings.cs index 156779570..44f47d89f 100644 --- a/src/Orchard.Web/Core/Common/Settings/BodySettings.cs +++ b/src/Orchard.Web/Core/Common/Settings/BodySettings.cs @@ -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; } } diff --git a/src/Orchard.Web/Core/Common/Settings/TextFieldSettings.cs b/src/Orchard.Web/Core/Common/Settings/TextFieldSettings.cs index 4674286c5..981f205c8 100644 --- a/src/Orchard.Web/Core/Common/Settings/TextFieldSettings.cs +++ b/src/Orchard.Web/Core/Common/Settings/TextFieldSettings.cs @@ -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; } diff --git a/src/Orchard.Web/Core/Common/Settings/TextFieldSettingsEvents.cs b/src/Orchard.Web/Core/Common/Settings/TextFieldSettingsEvents.cs index 74b589634..c7a97caa4 100644 --- a/src/Orchard.Web/Core/Common/Settings/TextFieldSettingsEvents.cs +++ b/src/Orchard.Web/Core/Common/Settings/TextFieldSettingsEvents.cs @@ -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 _shapeTableLocator; - - public TextFieldSettingsEvents(IOrchardServices orchardServices, Func shapeTableLocator) { - _orchardServices = orchardServices; - _shapeTableLocator = shapeTableLocator; - } public override IEnumerable 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(), - Flavors = flavors.ToArray() }; yield return DefinitionTemplate(model); diff --git a/src/Orchard.Web/Core/Common/ViewModels/TextFieldSettingsEventsViewModel.cs b/src/Orchard.Web/Core/Common/ViewModels/TextFieldSettingsEventsViewModel.cs index d7fe22de4..55636e0a9 100644 --- a/src/Orchard.Web/Core/Common/ViewModels/TextFieldSettingsEventsViewModel.cs +++ b/src/Orchard.Web/Core/Common/ViewModels/TextFieldSettingsEventsViewModel.cs @@ -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; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Views/DefinitionTemplates/TextFieldSettingsEventsViewModel.cshtml b/src/Orchard.Web/Core/Common/Views/DefinitionTemplates/TextFieldSettingsEventsViewModel.cshtml index e00c2a2a0..4078ccb50 100644 --- a/src/Orchard.Web/Core/Common/Views/DefinitionTemplates/TextFieldSettingsEventsViewModel.cshtml +++ b/src/Orchard.Web/Core/Common/Views/DefinitionTemplates/TextFieldSettingsEventsViewModel.cshtml @@ -1,16 +1,9 @@ -@using Orchard.Utility.Extensions -@model Orchard.Core.Common.ViewModels.TextFieldSettingsEventsViewModel +@model Orchard.Core.Common.ViewModels.TextFieldSettingsEventsViewModel
- - + @Html.EditorFor(m => m.Settings.Flavor) @Html.ValidationMessageFor(m => m.Settings.Flavor)
@@ -23,6 +16,6 @@
@Html.TextAreaFor(m => m.Settings.Hint, new { @class = "text medium", rows = "5" }) - @T("The help text is written under the field when authors are editing the content item.") + @T("The help text is written under the field when authors are editing the content item.") @Html.ValidationMessageFor(m => m.Settings.Hint)
\ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Views/EditorTemplates/Flavor.cshtml b/src/Orchard.Web/Core/Common/Views/EditorTemplates/Flavor.cshtml new file mode 100644 index 000000000..ffe72cda9 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Views/EditorTemplates/Flavor.cshtml @@ -0,0 +1,8 @@ +@using Orchard.Core.Common.Services +@using Orchard.Utility.Extensions +@{ + var flavors = WorkContext.Resolve().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) \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index ec0d287c4..a722d50ef 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -86,7 +86,9 @@ + + @@ -559,6 +561,9 @@ + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)