From 8651e33326812585139c18e4660929e0b985caae Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Sun, 10 Feb 2013 15:32:38 -0800 Subject: [PATCH] Adding ShapeMenuItem --HG-- branch : 1.x --- .../Drivers/ShapeMenuItemPartDriver.cs | 53 +++++++++++++++++++ .../Handlers/ShapeMenuItemPartHandler.cs | 11 ++++ src/Orchard.Web/Core/Navigation/Migrations.cs | 36 ++++++++++++- .../Navigation/Models/ShapeMenuItemPart.cs | 13 +++++ .../Models/ShapeMenuItemPartRecord.cs | 10 ++++ .../Core/Navigation/Placement.info | 1 + .../Parts.ShapeMenuItemPart.Edit.cshtml | 8 +++ .../Views/MenuItemLink-ShapeMenuItem.cshtml | 4 ++ src/Orchard.Web/Core/Orchard.Core.csproj | 10 ++++ .../Core/Shapes/Views/MenuItem.cshtml | 9 ++-- 10 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 src/Orchard.Web/Core/Navigation/Drivers/ShapeMenuItemPartDriver.cs create mode 100644 src/Orchard.Web/Core/Navigation/Handlers/ShapeMenuItemPartHandler.cs create mode 100644 src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPart.cs create mode 100644 src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPartRecord.cs create mode 100644 src/Orchard.Web/Core/Navigation/Views/EditorTemplates/Parts.ShapeMenuItemPart.Edit.cshtml create mode 100644 src/Orchard.Web/Core/Navigation/Views/MenuItemLink-ShapeMenuItem.cshtml diff --git a/src/Orchard.Web/Core/Navigation/Drivers/ShapeMenuItemPartDriver.cs b/src/Orchard.Web/Core/Navigation/Drivers/ShapeMenuItemPartDriver.cs new file mode 100644 index 000000000..e697c8e66 --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Drivers/ShapeMenuItemPartDriver.cs @@ -0,0 +1,53 @@ +using System; +using Orchard.ContentManagement; +using Orchard.ContentManagement.Drivers; +using Orchard.ContentManagement.Handlers; +using Orchard.Core.Navigation.Models; +using Orchard.Localization; + +namespace Orchard.Core.Navigation.Drivers { + public class ShapeMenuItemPartDriver : ContentPartDriver { + private const string TemplateName = "Parts.ShapeMenuItemPart.Edit"; + + public ShapeMenuItemPartDriver(IOrchardServices services) { + T = NullLocalizer.Instance; + Services = services; + } + + public Localizer T { get; set; } + public IOrchardServices Services { get; set; } + + protected override string Prefix { get { return "ShapeMenuItemPart"; } } + + protected override DriverResult Editor(ShapeMenuItemPart part, dynamic shapeHelper) { + return ContentShape("Parts_ShapeMenuItemPart_Edit", () => { + + return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix); + }); + } + + protected override DriverResult Editor(ShapeMenuItemPart part, IUpdateModel updater, dynamic shapeHelper) { + if (updater.TryUpdateModel(part, Prefix, null, null)) { + if (String.IsNullOrWhiteSpace(part.ShapeType)) { + updater.AddModelError("ShapeType", T("The Shape Type is mandatory.")); + } + } + + return Editor(part, shapeHelper); + } + + protected override void Importing(ShapeMenuItemPart part, ImportContentContext context) { + IfNotNull(context.Attribute(part.PartDefinition.Name, "ShapeType"), x => part.Record.ShapeType = x); + } + + private static void IfNotNull(T value, Action then) where T : class { + if(value != null) { + then(value); + } + } + + protected override void Exporting(ShapeMenuItemPart part, ExportContentContext context) { + context.Element(part.PartDefinition.Name).SetAttributeValue("ShapeType", part.Record.ShapeType); + } + } +} diff --git a/src/Orchard.Web/Core/Navigation/Handlers/ShapeMenuItemPartHandler.cs b/src/Orchard.Web/Core/Navigation/Handlers/ShapeMenuItemPartHandler.cs new file mode 100644 index 000000000..64edfc040 --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Handlers/ShapeMenuItemPartHandler.cs @@ -0,0 +1,11 @@ +using Orchard.Core.Navigation.Models; +using Orchard.Data; +using Orchard.ContentManagement.Handlers; + +namespace Orchard.Core.Navigation.Handlers { + public class ShapeMenuItemPartHandler : ContentHandler { + public ShapeMenuItemPartHandler(IRepository repository) { + Filters.Add(StorageFilter.For(repository)); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Navigation/Migrations.cs b/src/Orchard.Web/Core/Navigation/Migrations.cs index e02620072..43c8c6caa 100644 --- a/src/Orchard.Web/Core/Navigation/Migrations.cs +++ b/src/Orchard.Web/Core/Navigation/Migrations.cs @@ -93,7 +93,22 @@ namespace Orchard.Core.Navigation { .WithSetting("Stereotype", "MenuItem") ); - return 3; + SchemaBuilder.CreateTable("ShapeMenuItemPartRecord", + table => table.ContentPartRecord() + .Column("ShapeType") + ); + + ContentDefinitionManager.AlterTypeDefinition("ShapeMenuItem", + cfg => cfg + .WithPart("ShapeMenuItemPart") + .WithPart("MenuPart") + .WithPart("CommonPart") + .DisplayedAs("Shape Link") + .WithSetting("Description", "Injects menu items from a Shape") + .WithSetting("Stereotype", "MenuItem") + ); + + return 4; } public int UpdateFrom1() { @@ -177,5 +192,24 @@ namespace Orchard.Core.Navigation { return 3; } + + public int UpdateFrom3() { + SchemaBuilder.CreateTable("ShapeMenuItemPartRecord", + table => table.ContentPartRecord() + .Column("ShapeType") + ); + + ContentDefinitionManager.AlterTypeDefinition("ShapeMenuItem", + cfg => cfg + .WithPart("ShapeMenuItemPart") + .WithPart("MenuPart") + .WithPart("CommonPart") + .DisplayedAs("Shape Link") + .WithSetting("Description", "Injects menu items from a Shape") + .WithSetting("Stereotype", "MenuItem") + ); + + return 4; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPart.cs b/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPart.cs new file mode 100644 index 000000000..83bedbce5 --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPart.cs @@ -0,0 +1,13 @@ +using Orchard.ContentManagement; + +namespace Orchard.Core.Navigation.Models { + public class ShapeMenuItemPart : ContentPart { + /// + /// Maximum number of items to retrieve from db + /// + public virtual string ShapeType { + get { return Record.ShapeType; } + set { Record.ShapeType = value; } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPartRecord.cs b/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPartRecord.cs new file mode 100644 index 000000000..adf8edb12 --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Models/ShapeMenuItemPartRecord.cs @@ -0,0 +1,10 @@ +using Orchard.ContentManagement.Records; + +namespace Orchard.Core.Navigation.Models { + public class ShapeMenuItemPartRecord : ContentPartRecord { + /// + /// The shape to display + /// + public virtual string ShapeType { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Navigation/Placement.info b/src/Orchard.Web/Core/Navigation/Placement.info index ee35a0b4b..d01ad0880 100644 --- a/src/Orchard.Web/Core/Navigation/Placement.info +++ b/src/Orchard.Web/Core/Navigation/Placement.info @@ -6,4 +6,5 @@ + \ No newline at end of file diff --git a/src/Orchard.Web/Core/Navigation/Views/EditorTemplates/Parts.ShapeMenuItemPart.Edit.cshtml b/src/Orchard.Web/Core/Navigation/Views/EditorTemplates/Parts.ShapeMenuItemPart.Edit.cshtml new file mode 100644 index 000000000..0a8b5382d --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Views/EditorTemplates/Parts.ShapeMenuItemPart.Edit.cshtml @@ -0,0 +1,8 @@ +@model Orchard.Core.Navigation.Models.ShapeMenuItemPart + +
+
+ @Html.LabelFor(m => m.ShapeType, T("Type of the shape to display")) + @Html.TextBoxFor(m => m.ShapeType, new { @class = "text textMedium" }) +
+
diff --git a/src/Orchard.Web/Core/Navigation/Views/MenuItemLink-ShapeMenuItem.cshtml b/src/Orchard.Web/Core/Navigation/Views/MenuItemLink-ShapeMenuItem.cshtml new file mode 100644 index 000000000..48c1fb53e --- /dev/null +++ b/src/Orchard.Web/Core/Navigation/Views/MenuItemLink-ShapeMenuItem.cshtml @@ -0,0 +1,4 @@ +@{ + string type = Model.Content.ShapeMenuItemPart.ShapeType; +} +@Display(New.Create(type).MenuItem(Model)) \ 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 a4b76040a..d81456c77 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -135,11 +135,13 @@ + + @@ -147,6 +149,8 @@ + + @@ -556,6 +560,12 @@ + + + + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/src/Orchard.Web/Core/Shapes/Views/MenuItem.cshtml b/src/Orchard.Web/Core/Shapes/Views/MenuItem.cshtml index 952fa4365..e4f147950 100644 --- a/src/Orchard.Web/Core/Shapes/Views/MenuItem.cshtml +++ b/src/Orchard.Web/Core/Shapes/Views/MenuItem.cshtml @@ -6,11 +6,10 @@ if (!HasText(Model.Text)) { @DisplayChildren(Model) } else { - string requestUrl = Request.Path.Replace(Request.ApplicationPath, string.Empty).TrimEnd('/').ToUpperInvariant(); - string modelUrl = Model.Href.Replace(Request.ApplicationPath, string.Empty).TrimEnd('/').ToUpperInvariant(); - if (requestUrl == modelUrl || (!string.IsNullOrEmpty(modelUrl) && requestUrl.StartsWith(modelUrl + "/"))) { - Model.Classes.Add("current"); - } + if((bool)Model.Selected) { + Model.Classes.Add("current"); + } + if(items.Any()) { Model.Classes.Add("dropdown"); }