mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 21:13:35 +08:00
Added default menu position option to AdminMenuPart. If the default position is taken it advances by a minor number (2, 2.1, 2.2, etc). Each type with the part can have a different default position.
--HG-- branch : dev
This commit is contained in:
@@ -3,6 +3,7 @@ using JetBrains.Annotations;
|
|||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Drivers;
|
using Orchard.ContentManagement.Drivers;
|
||||||
using Orchard.Core.Navigation.Models;
|
using Orchard.Core.Navigation.Models;
|
||||||
|
using Orchard.Core.Navigation.Settings;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
using Orchard.UI.Navigation;
|
using Orchard.UI.Navigation;
|
||||||
@@ -24,10 +25,26 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
|
|
||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
|
private string GetDefaultPosition(ContentPart part) {
|
||||||
|
var settings = part.Settings.GetModel<AdminMenuPartTypeSettings>();
|
||||||
|
var defaultPosition = settings == null ? "" : settings.DefaultPosition;
|
||||||
|
var adminMenu = _navigationManager.BuildMenu("admin");
|
||||||
|
if (!string.IsNullOrEmpty(defaultPosition)) {
|
||||||
|
int major;
|
||||||
|
return int.TryParse(defaultPosition, out major) ? Position.GetNextMinor(major, adminMenu) : defaultPosition;
|
||||||
|
}
|
||||||
|
return Position.GetNext(adminMenu);
|
||||||
|
}
|
||||||
|
|
||||||
protected override DriverResult Editor(AdminMenuPart part, dynamic shapeHelper) {
|
protected override DriverResult Editor(AdminMenuPart part, dynamic shapeHelper) {
|
||||||
// todo: we need a 'ManageAdminMenu' too?
|
// todo: we need a 'ManageAdminMenu' too?
|
||||||
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, _orchardServices.WorkContext.CurrentUser, part))
|
if (!_authorizationService.TryCheckAccess(Permissions.ManageMainMenu, _orchardServices.WorkContext.CurrentUser, part)) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(part.AdminMenuPosition)) {
|
||||||
|
part.AdminMenuPosition = GetDefaultPosition(part);
|
||||||
|
}
|
||||||
|
|
||||||
return ContentShape("Parts_Navigation_AdminMenu_Edit",
|
return ContentShape("Parts_Navigation_AdminMenu_Edit",
|
||||||
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Navigation.AdminMenu.Edit", Model: part, Prefix: Prefix));
|
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Navigation.AdminMenu.Edit", Model: part, Prefix: Prefix));
|
||||||
@@ -39,11 +56,18 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
|
|
||||||
updater.TryUpdateModel(part, Prefix, null, null);
|
updater.TryUpdateModel(part, Prefix, null, null);
|
||||||
|
|
||||||
if (part.OnAdminMenu && string.IsNullOrEmpty(part.AdminMenuText))
|
if (part.OnAdminMenu) {
|
||||||
|
if (string.IsNullOrEmpty(part.AdminMenuText)) {
|
||||||
updater.AddModelError("AdminMenuText", T("The AdminMenuText field is required"));
|
updater.AddModelError("AdminMenuText", T("The AdminMenuText field is required"));
|
||||||
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(part.AdminMenuPosition))
|
if (string.IsNullOrEmpty(part.AdminMenuPosition)) {
|
||||||
part.AdminMenuPosition = Position.GetNext(_navigationManager.BuildMenu("admin"));
|
part.AdminMenuPosition = GetDefaultPosition(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
part.AdminMenuPosition = "";
|
||||||
|
}
|
||||||
|
|
||||||
return Editor(part, shapeHelper);
|
return Editor(part, shapeHelper);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,35 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.ContentManagement.MetaData;
|
||||||
|
using Orchard.ContentManagement.MetaData.Builders;
|
||||||
|
using Orchard.ContentManagement.MetaData.Models;
|
||||||
|
using Orchard.ContentManagement.ViewModels;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Navigation.Settings {
|
||||||
|
public class AdminMenuPartTypeSettings {
|
||||||
|
public string DefaultPosition { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AdminMenuSettingsHooks : ContentDefinitionEditorEventsBase {
|
||||||
|
public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition) {
|
||||||
|
if (definition.PartDefinition.Name != "AdminMenuPart") {
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var model = definition.Settings.GetModel<AdminMenuPartTypeSettings>();
|
||||||
|
|
||||||
|
yield return DefinitionTemplate(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel) {
|
||||||
|
if (builder.Name != "AdminMenuPart") {
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var model = new AdminMenuPartTypeSettings();
|
||||||
|
updateModel.TryUpdateModel(model, "AdminMenuPartTypeSettings", null, null);
|
||||||
|
builder.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", model.DefaultPosition);
|
||||||
|
yield return DefinitionTemplate(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
@model Orchard.Core.Navigation.Settings.AdminMenuPartTypeSettings
|
||||||
|
<fieldset>
|
||||||
|
<label for="@Html.FieldIdFor(m => m.DefaultPosition)">@T("Default Position")</label>
|
||||||
|
@Html.EditorFor(m => m.DefaultPosition)
|
||||||
|
@Html.ValidationMessageFor(m => m.DefaultPosition)
|
||||||
|
</fieldset>
|
@@ -115,6 +115,7 @@
|
|||||||
<Compile Include="Navigation\Models\AdminMenuPartRecord.cs" />
|
<Compile Include="Navigation\Models\AdminMenuPartRecord.cs" />
|
||||||
<Compile Include="Navigation\Services\AdminMenuNavigationProvider.cs" />
|
<Compile Include="Navigation\Services\AdminMenuNavigationProvider.cs" />
|
||||||
<Compile Include="Navigation\Services\MainMenuNavigationProvider.cs" />
|
<Compile Include="Navigation\Services\MainMenuNavigationProvider.cs" />
|
||||||
|
<Compile Include="Navigation\Settings\AdminMenuPartTypeSettings.cs" />
|
||||||
<Compile Include="Routable\Events\ISlugEventHandler.cs" />
|
<Compile Include="Routable\Events\ISlugEventHandler.cs" />
|
||||||
<Compile Include="Routable\ResourceManifest.cs" />
|
<Compile Include="Routable\ResourceManifest.cs" />
|
||||||
<Compile Include="Routable\Services\RoutableHomePageProvider.cs" />
|
<Compile Include="Routable\Services\RoutableHomePageProvider.cs" />
|
||||||
@@ -418,6 +419,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Navigation\Views\EditorTemplates\Parts.Navigation.AdminMenu.Edit.cshtml" />
|
<Content Include="Navigation\Views\EditorTemplates\Parts.Navigation.AdminMenu.Edit.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Navigation\Views\DefinitionTemplates\AdminMenuPartTypeSettings.cshtml" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.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.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
@@ -40,7 +40,7 @@ namespace Orchard.Blogs {
|
|||||||
.WithPart("CommonPart")
|
.WithPart("CommonPart")
|
||||||
.WithPart("RoutePart")
|
.WithPart("RoutePart")
|
||||||
.WithPart("MenuPart")
|
.WithPart("MenuPart")
|
||||||
.WithPart("AdminMenuPart")
|
.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2"))
|
||||||
);
|
);
|
||||||
|
|
||||||
ContentDefinitionManager.AlterTypeDefinition("BlogPost",
|
ContentDefinitionManager.AlterTypeDefinition("BlogPost",
|
||||||
@@ -68,12 +68,17 @@ namespace Orchard.Blogs {
|
|||||||
.WithSetting("Stereotype", "Widget")
|
.WithSetting("Stereotype", "Widget")
|
||||||
);
|
);
|
||||||
|
|
||||||
return 2;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int UpdateFrom1() {
|
public int UpdateFrom1() {
|
||||||
ContentDefinitionManager.AlterTypeDefinition("Blog", cfg => cfg.WithPart("AdminMenuPart"));
|
ContentDefinitionManager.AlterTypeDefinition("Blog", cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
|
||||||
return 2;
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int UpdateFrom2() {
|
||||||
|
ContentDefinitionManager.AlterTypeDefinition("Blog", cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
|
||||||
|
return 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -11,15 +11,21 @@ namespace Orchard.Lists {
|
|||||||
.WithPart("RoutePart")
|
.WithPart("RoutePart")
|
||||||
.WithPart("ContainerPart")
|
.WithPart("ContainerPart")
|
||||||
.WithPart("MenuPart")
|
.WithPart("MenuPart")
|
||||||
.WithPart("AdminMenuPart")
|
.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2"))
|
||||||
.Creatable());
|
.Creatable());
|
||||||
|
|
||||||
return 2;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int UpdateFrom1() {
|
public int UpdateFrom1() {
|
||||||
ContentDefinitionManager.AlterTypeDefinition("List", cfg => cfg.WithPart("AdminMenuPart"));
|
ContentDefinitionManager.AlterTypeDefinition("List", cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
|
||||||
return 2;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int UpdateFrom2() {
|
||||||
|
ContentDefinitionManager.AlterTypeDefinition("List", cfg => cfg.WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "2")));
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Orchard.UI;
|
using Orchard.UI;
|
||||||
using Orchard.UI.Navigation;
|
using Orchard.UI.Navigation;
|
||||||
@@ -8,7 +9,7 @@ namespace Orchard.Utility {
|
|||||||
public static class Position {
|
public static class Position {
|
||||||
public static string GetNext(IEnumerable<MenuItem> menuItems) {
|
public static string GetNext(IEnumerable<MenuItem> menuItems) {
|
||||||
|
|
||||||
var maxMenuItem = menuItems.Where(PositionHasMojorNumber).OrderByDescending(mi => mi.Position, new FlatPositionComparer()).FirstOrDefault();
|
var maxMenuItem = menuItems.Where(mi => PositionHasMajorNumber(mi)).OrderByDescending(mi => mi.Position, new FlatPositionComparer()).FirstOrDefault();
|
||||||
var positionParts = maxMenuItem.Position.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim().Length > 0);
|
var positionParts = maxMenuItem.Position.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim().Length > 0);
|
||||||
if (positionParts.Count() > 0) {
|
if (positionParts.Count() > 0) {
|
||||||
int result;
|
int result;
|
||||||
@@ -21,10 +22,34 @@ namespace Orchard.Utility {
|
|||||||
return "1";
|
return "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool PositionHasMojorNumber(MenuItem mi) {
|
public static string GetNextMinor(int major, IEnumerable<MenuItem> menuItems) {
|
||||||
int foo;
|
var majorStr = major.ToString(CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
var allmajors = menuItems.Where(mi => PositionHasMajorNumber(mi, major)).ToArray();
|
||||||
|
|
||||||
|
var maxMenuItem = menuItems.Where(mi => PositionHasMajorNumber(mi, major)).OrderByDescending(mi => mi.Position, new FlatPositionComparer()).FirstOrDefault();
|
||||||
|
var positionParts = maxMenuItem.Position.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim().Length > 0);
|
||||||
|
if (positionParts.Count() > 1) {
|
||||||
|
int result;
|
||||||
|
if (int.TryParse(positionParts.ElementAt(1), out result)) {
|
||||||
|
return majorStr + "." + (result + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (positionParts.Count() == 1) {
|
||||||
|
return majorStr + ".1";
|
||||||
|
}
|
||||||
|
|
||||||
|
return majorStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool PositionHasMajorNumber(MenuItem mi, int? position = null) {
|
||||||
|
int menuPosition;
|
||||||
var major = mi.Position == null ? null : mi.Position.Split('.')[0];
|
var major = mi.Position == null ? null : mi.Position.Split('.')[0];
|
||||||
return !string.IsNullOrEmpty(major) && int.TryParse(major, out foo);
|
if (string.IsNullOrEmpty(major)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var parsed = int.TryParse(major, out menuPosition);
|
||||||
|
return parsed && (!position.HasValue || position.Value == menuPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user