From 37f742cca20dd69479aff6307faaea6a203e9a19 Mon Sep 17 00:00:00 2001 From: Argo Window10 Date: Thu, 12 Dec 2019 21:54:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20BootstrapCompo?= =?UTF-8?q?nentBase=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AdminLayoutComponentBase.cs | 29 +-------------- .../Components/BootstrapComponentBase.cs | 30 --------------- .../Bootstrap.Admin/Components/HeaderBase.cs | 11 +++++- .../Bootstrap.Admin/Components/NavItemBase.cs | 2 +- .../Bootstrap.Admin/Components/SideBarBase.cs | 9 ++++- .../Bootstrap.Admin/Components/TabBase.cs | 15 +++++++- .../Bootstrap.Admin/Components/TabSetBase.cs | 37 ++++++++++++++++++- .../Pages/Admin/Profiles.razor | 18 +++++---- src/admin/Bootstrap.Admin/Shared/Header.razor | 2 +- .../Bootstrap.Admin/Shared/SideBar.razor | 2 +- 10 files changed, 81 insertions(+), 74 deletions(-) delete mode 100644 src/admin/Bootstrap.Admin/Components/BootstrapComponentBase.cs diff --git a/src/admin/Bootstrap.Admin/Components/AdminLayoutComponentBase.cs b/src/admin/Bootstrap.Admin/Components/AdminLayoutComponentBase.cs index f0570f3f..ee109656 100644 --- a/src/admin/Bootstrap.Admin/Components/AdminLayoutComponentBase.cs +++ b/src/admin/Bootstrap.Admin/Components/AdminLayoutComponentBase.cs @@ -1,8 +1,5 @@ -using Bootstrap.Admin.Extensions; -using Bootstrap.Admin.Shared; +using Bootstrap.Admin.Shared; using Microsoft.AspNetCore.Components; -using System; -using System.Linq; namespace Bootstrap.Admin.Components { @@ -21,29 +18,5 @@ namespace Bootstrap.Admin.Components /// [CascadingParameter(Name = "Default")] public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout(); - - /// - /// - /// - public string UserName { get; set; } = ""; - - /// - /// - /// - public string DisplayName { get; set; } = ""; - - /// - /// - /// - /// - /// - protected override void OnAfterRender(bool firstRender) - { - var requestUrl = RootLayout.NavigationManager?.Uri ?? ""; - var path = new Uri(requestUrl).PathAndQuery; - var menus = DataAccess.MenuHelper.RetrieveAllMenus(RootLayout.UserName); - var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl())); - if (menu != null) TabSet?.AddTab(menu); - } } } diff --git a/src/admin/Bootstrap.Admin/Components/BootstrapComponentBase.cs b/src/admin/Bootstrap.Admin/Components/BootstrapComponentBase.cs deleted file mode 100644 index cad1a8df..00000000 --- a/src/admin/Bootstrap.Admin/Components/BootstrapComponentBase.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Bootstrap.Admin.Shared; -using Microsoft.AspNetCore.Components; -using Microsoft.JSInterop; - -namespace Bootstrap.Admin.Components -{ - /// - /// - /// - public class BootstrapComponentBase : ComponentBase - { - - /// - /// - /// - [Inject] - public NavigationManager? NavigationManager { get; set; } - - /// - /// - /// - [Inject] - protected IJSRuntime? JSRuntime { get; set; } - /// - /// - /// - [CascadingParameter(Name = "Admin")] - protected AdminLayout Layout { get; set; } = new AdminLayout(); - } -} diff --git a/src/admin/Bootstrap.Admin/Components/HeaderBase.cs b/src/admin/Bootstrap.Admin/Components/HeaderBase.cs index 35862332..74c0b19b 100644 --- a/src/admin/Bootstrap.Admin/Components/HeaderBase.cs +++ b/src/admin/Bootstrap.Admin/Components/HeaderBase.cs @@ -1,11 +1,12 @@ -using Microsoft.AspNetCore.Components; +using Bootstrap.Admin.Shared; +using Microsoft.AspNetCore.Components; namespace Bootstrap.Admin.Components { /// /// /// - public class HeaderBase : BootstrapComponentBase + public class HeaderBase : ComponentBase { /// /// @@ -43,6 +44,12 @@ namespace Bootstrap.Admin.Components [Parameter] public string Icon { get; set; } = ""; + /// + /// + /// + [CascadingParameter(Name = "Default")] + public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout(); + /// /// 更新登录用户显示名称方法 /// diff --git a/src/admin/Bootstrap.Admin/Components/NavItemBase.cs b/src/admin/Bootstrap.Admin/Components/NavItemBase.cs index ea10c523..93e19348 100644 --- a/src/admin/Bootstrap.Admin/Components/NavItemBase.cs +++ b/src/admin/Bootstrap.Admin/Components/NavItemBase.cs @@ -6,7 +6,7 @@ namespace Bootstrap.Admin.Components /// /// /// - public class NavItemBase : BootstrapComponentBase + public class NavItemBase : ComponentBase { /// /// diff --git a/src/admin/Bootstrap.Admin/Components/SideBarBase.cs b/src/admin/Bootstrap.Admin/Components/SideBarBase.cs index 03921d0b..e80dc17a 100644 --- a/src/admin/Bootstrap.Admin/Components/SideBarBase.cs +++ b/src/admin/Bootstrap.Admin/Components/SideBarBase.cs @@ -1,4 +1,5 @@ using Bootstrap.Admin.Models; +using Bootstrap.Admin.Shared; using Microsoft.AspNetCore.Components; namespace Bootstrap.Admin.Components @@ -6,7 +7,7 @@ namespace Bootstrap.Admin.Components /// /// 侧边栏组件 /// - public class SideBarBase : BootstrapComponentBase + public class SideBarBase : ComponentBase { /// /// 获得/设置 侧边栏绑定 Model 实例 @@ -19,5 +20,11 @@ namespace Bootstrap.Admin.Components /// [Parameter] public EventCallback ModelChanged { get; set; } + + /// + /// + /// + [CascadingParameter(Name = "Default")] + public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout(); } } diff --git a/src/admin/Bootstrap.Admin/Components/TabBase.cs b/src/admin/Bootstrap.Admin/Components/TabBase.cs index cd734ad9..e7a87a2f 100644 --- a/src/admin/Bootstrap.Admin/Components/TabBase.cs +++ b/src/admin/Bootstrap.Admin/Components/TabBase.cs @@ -1,4 +1,5 @@ using Bootstrap.Admin.Extensions; +using Bootstrap.Admin.Shared; using Bootstrap.Security; using Microsoft.AspNetCore.Components; using System.Threading.Tasks; @@ -8,7 +9,7 @@ namespace Bootstrap.Admin.Components /// /// /// - public class TabBase : BootstrapComponentBase + public class TabBase : ComponentBase { /// /// @@ -40,6 +41,18 @@ namespace Bootstrap.Admin.Components [Parameter] public bool Active { get; set; } + /// + /// + /// + [CascadingParameter(Name = "Admin")] + public AdminLayout Layout { get; protected set; } = new AdminLayout(); + + /// + /// + /// + [Inject] + public NavigationManager? NavigationManager { get; set; } + private bool closeTab; /// /// diff --git a/src/admin/Bootstrap.Admin/Components/TabSetBase.cs b/src/admin/Bootstrap.Admin/Components/TabSetBase.cs index 6cf0e4c9..8262c476 100644 --- a/src/admin/Bootstrap.Admin/Components/TabSetBase.cs +++ b/src/admin/Bootstrap.Admin/Components/TabSetBase.cs @@ -1,6 +1,9 @@ using Bootstrap.Admin.Extensions; using Bootstrap.Admin.Shared; using Bootstrap.Security; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -10,8 +13,26 @@ namespace Bootstrap.Admin.Components /// /// TabSet 组件类 /// - public class TabSetBase : BootstrapComponentBase + public class TabSetBase : ComponentBase { + /// + /// + /// + [Inject] + public NavigationManager? NavigationManager { get; set; } + + /// + /// + /// + [Inject] + protected IJSRuntime? JSRuntime { get; set; } + + /// + /// + /// + [CascadingParameter(Name = "Default")] + public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout(); + /// /// 页面集合 /// @@ -23,6 +44,18 @@ namespace Bootstrap.Admin.Components protected List Tabs { get; set; } = new List(); private string? curId; + /// + /// + /// + protected override void OnParametersSet() + { + var requestUrl = NavigationManager?.Uri ?? ""; + var path = new Uri(requestUrl).PathAndQuery; + var menus = DataAccess.MenuHelper.RetrieveAllMenus(RootLayout.UserName); + var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl())); + if (menu != null) AddTab(menu); + } + /// /// 页面呈现后回调方法 /// @@ -101,7 +134,7 @@ namespace Bootstrap.Admin.Components /// protected void CloseAllTab() { - NavigationManager?.NavigateTo(Layout.RootLayout.HomeUrl); + NavigationManager?.NavigateTo(RootLayout.HomeUrl); } /// diff --git a/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor b/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor index 99df8650..0984d8de 100644 --- a/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor +++ b/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor @@ -1,6 +1,4 @@ -@inherits BootstrapComponentBase - -
+
基本资料
@if (IsDemo) @@ -17,7 +15,7 @@
- +
@@ -113,21 +111,27 @@
@code { + /// + /// + /// + [CascadingParameter(Name = "Default")] + public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout(); + private ProfilesModel? Model; private bool IsDemo; protected override void OnInitialized() { base.OnInitialized(); - Model = new ProfilesModel(Layout?.UserName); + Model = new ProfilesModel(RootLayout.UserName); IsDemo = Model?.IsDemo ?? false; } private void SaveDisplayName() { - if (!string.IsNullOrEmpty(Model?.UserName) && Layout != null && !string.IsNullOrEmpty(Layout.DisplayName)) + if (!string.IsNullOrEmpty(Model?.UserName) && !string.IsNullOrEmpty(RootLayout.DisplayName)) { - Bootstrap.DataAccess.UserHelper.SaveDisplayName(Model.UserName, Layout.DisplayName); + Bootstrap.DataAccess.UserHelper.SaveDisplayName(Model.UserName, RootLayout.DisplayName); } } } diff --git a/src/admin/Bootstrap.Admin/Shared/Header.razor b/src/admin/Bootstrap.Admin/Shared/Header.razor index c1b754ff..a6f58c69 100644 --- a/src/admin/Bootstrap.Admin/Shared/Header.razor +++ b/src/admin/Bootstrap.Admin/Shared/Header.razor @@ -3,7 +3,7 @@
- + @Title diff --git a/src/admin/Bootstrap.Admin/Shared/SideBar.razor b/src/admin/Bootstrap.Admin/Shared/SideBar.razor index 41f98afa..6c2e31f3 100644 --- a/src/admin/Bootstrap.Admin/Shared/SideBar.razor +++ b/src/admin/Bootstrap.Admin/Shared/SideBar.razor @@ -3,7 +3,7 @@