Enhancing the Text field

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-01-17 17:07:17 -08:00
parent bc162c5478
commit 6f53afffa4
11 changed files with 189 additions and 14 deletions

View File

@@ -1,14 +1,23 @@
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Common.Fields;
using Orchard.Core.Common.Settings;
using Orchard.Core.Common.ViewModels;
using Orchard.Localization;
using Orchard.Services;
namespace Orchard.Core.Common.Drivers {
[UsedImplicitly]
public class TextFieldDriver : ContentFieldDriver<TextField> {
public TextFieldDriver(IOrchardServices services) {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
public TextFieldDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
Services = services;
T = NullLocalizer.Instance;
}
@@ -26,16 +35,48 @@ namespace Orchard.Core.Common.Drivers {
protected override DriverResult Display(ContentPart part, TextField field, string displayType, dynamic shapeHelper) {
return ContentShape("Fields_Common_Text", GetDifferentiator(field, part),
() => shapeHelper.Fields_Common_Text(Name: field.Name, Value: field.Value));
() => {
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
object fieldValue = field.Value;
if (!string.IsNullOrWhiteSpace(settings.Flavor)) {
fieldValue = new HtmlString(_htmlFilters.Aggregate(field.Value, (text, filter) => filter.ProcessContent(text, settings.Flavor)));
}
return shapeHelper.Fields_Common_Text(Name: field.Name, Value: fieldValue);
});
}
protected override DriverResult Editor(ContentPart part, TextField field, dynamic shapeHelper) {
return ContentShape("Fields_Common_Text_Edit", GetDifferentiator(field, part),
() => shapeHelper.EditorTemplate(TemplateName: "Fields.Common.Text.Edit", Model: field, Prefix: GetPrefix(field, part)));
() => {
var viewModel = new TextFieldDriverViewModel {
Field = field,
Text = field.Value,
Settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>()
};
return shapeHelper.EditorTemplate(TemplateName: "Fields.Common.Text.Edit", Model: viewModel, Prefix: GetPrefix(field, part));
});
}
protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) {
updater.TryUpdateModel(field, GetPrefix(field, part), null, null);
var viewModel = new TextFieldDriverViewModel {
Field = field,
Text = field.Value,
Settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>()
};
if(updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null)) {
if (viewModel.Settings.Required && string.IsNullOrWhiteSpace(viewModel.Text)) {
updater.AddModelError("Text", T("The fields {0} is mandatory", field.DisplayName));
return ContentShape("Fields_Common_Text_Edit", GetDifferentiator(field, part),
() => shapeHelper.EditorTemplate(TemplateName: "Fields.Common.Text.Edit", Model: viewModel, Prefix: GetPrefix(field, part)));
}
field.Value = viewModel.Text;
}
return Editor(part, field, shapeHelper);
}

View File

@@ -31,6 +31,7 @@
</Match>
<Match DisplayType="SummaryAdmin">
<Place Parts_Common_Body_Summary="-"
Parts_Common_Metadata_SummaryAdmin="Meta:5"/>
Parts_Common_Metadata_SummaryAdmin="Meta:5"
Fields_Common_Text="-"/>
</Match>
</Placement>

View File

@@ -0,0 +1,8 @@
namespace Orchard.Core.Common.Settings {
public class TextFieldSettings {
public string Flavor { get; set; }
public bool Required { get; set; }
public string Hint { get; set; }
}
}

View File

@@ -0,0 +1,63 @@
using System;
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);
}
}
public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPartFieldDefinitionBuilder builder, IUpdateModel updateModel) {
if (builder.FieldType != "TextField") {
yield break;
}
var model = new TextFieldSettingsEventsViewModel {
Settings = new TextFieldSettings()
};
if (updateModel.TryUpdateModel(model, "TextFieldSettingsEventsViewModel", null, null)) {
builder.WithSetting("TextFieldSettings.Flavor", model.Settings.Flavor);
builder.WithSetting("TextFieldSettings.Hint", model.Settings.Hint);
builder.WithSetting("TextFieldSettings.Required", model.Settings.Required.ToString(CultureInfo.InvariantCulture));
yield return DefinitionTemplate(model);
}
}
}
}

View File

@@ -0,0 +1,10 @@
using Orchard.Core.Common.Fields;
using Orchard.Core.Common.Settings;
namespace Orchard.Core.Common.ViewModels {
public class TextFieldDriverViewModel {
public TextField Field { get; set; }
public string Text { get; set; }
public TextFieldSettings Settings { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
using Orchard.Core.Common.Settings;
namespace Orchard.Core.Common.ViewModels {
public class TextFieldSettingsEventsViewModel {
public TextFieldSettings Settings { get; set; }
public string[] Flavors { get; set; }
}
}

View File

@@ -0,0 +1 @@
@Html.TextArea("Text", (string)Model.Text, 10, 80, new {})

View File

@@ -0,0 +1,28 @@
@using Orchard.Utility.Extensions
@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.ValidationMessageFor(m => m.Settings.Flavor)
</div>
</fieldset>
<fieldset>
<div>
@Html.CheckBoxFor(m => m.Settings.Required) <label for="@Html.FieldIdFor(m => m.Settings.Required)" class="forcheckbox">@T("Required")</label>
<span class="hint">@T("Check to ensure the user enters a value in this field.")</span>
</div>
</fieldset>
<fieldset>
<label for="@Html.FieldIdFor(m => m.Settings.Hint)">@T("Help text")</label>
@Html.TextAreaFor(m => m.Settings.Hint, new { @class = "textMedium", rows = "5" })
<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

@@ -1,5 +1,13 @@
@model Orchard.Core.Common.Fields.TextField
<fieldset>
<label for="@Html.FieldIdFor(m=>m.Value)">@Model.Name</label>
@Html.EditorFor(m=>m.Value)@Html.ValidationMessageFor(m=>m.Value)
</fieldset>
@model Orchard.Core.Common.ViewModels.TextFieldDriverViewModel
<label for="@Html.FieldIdFor(m => m.Text)">@Model.Field.DisplayName</label>
<fieldset>
@if (String.IsNullOrWhiteSpace(Model.Settings.Flavor)) {
@Html.TextBoxFor(m => m.Text, new { @class = "text" })
@Html.ValidationMessageFor(m => m.Text)
}
else {
@Display.Body_Editor(Text: Model.Text, EditorFlavor: Model.Settings.Flavor)
}
<span class="hint">@Model.Settings.Hint</span>
</fieldset>

View File

@@ -1,7 +1,7 @@
@{
string name = Model.ContentField.DisplayName;
string value = Model.Value;
}
@if (HasText(name) && HasText(value)) {
<p class="text-field"><span class="name">@name:</span> <span class="value">@value</span></p>
@if (HasText(name) && HasText(Model.Value)) {
<p class="text-field"><span class="name">@name:</span> <span class="value">@Model.Value</span></p>
}

View File

@@ -78,6 +78,10 @@
<Compile Include="Common\ResourceManifest.cs" />
<Compile Include="Common\Services\XmlRpcHandler.cs" />
<Compile Include="Common\DateEditor\DateEditorViewModel.cs" />
<Compile Include="Common\Settings\TextFieldSettingsEvents.cs" />
<Compile Include="Common\Settings\TextFieldSettings.cs" />
<Compile Include="Common\ViewModels\TextFieldDriverViewModel.cs" />
<Compile Include="Common\ViewModels\TextFieldSettingsEventsViewModel.cs" />
<Compile Include="Containers\Controllers\ItemController.cs" />
<Compile Include="Containers\Drivers\ContainablePartDriver.cs" />
<Compile Include="Containers\Drivers\ContainerPartDriver.cs" />
@@ -494,6 +498,9 @@
<ItemGroup>
<Content Include="Containers\Views\Parts.Container.Contained.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Common\Views\DefinitionTemplates\TextFieldSettingsEventsViewModel.cshtml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.