refactor: 移动组件 NavigationManager 与 JSRuntime 到基类

This commit is contained in:
Argo Window10 2019-11-26 01:42:39 +08:00
parent 08ff785719
commit 14ac39c61d
14 changed files with 50 additions and 85 deletions

View File

@ -27,7 +27,7 @@ namespace Bootstrap.Admin.Components
/// ///
/// </summary> /// </summary>
[CascadingParameter(Name = "Default")] [CascadingParameter(Name = "Default")]
public DefaultLayout Layout { get; protected set; } = new DefaultLayout(); public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout();
/// <summary> /// <summary>
/// ///
@ -60,7 +60,7 @@ namespace Bootstrap.Admin.Components
{ {
if (TabSet != null) if (TabSet != null)
{ {
if (TabSet.TabCount == 1) Layout.NavigationManager?.NavigateTo(Layout.HomeUrl); if (TabSet.TabCount == 1) RootLayout.NavigationManager?.NavigateTo(RootLayout.HomeUrl);
else else
{ {
var pageId = await TabSet.CloseTab(tabId); var pageId = await TabSet.CloseTab(tabId);
@ -74,16 +74,15 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
/// <param name="firstRender"></param> /// <param name="firstRender"></param>
/// <returns></returns> /// <returns></returns>
protected override Task OnAfterRenderAsync(bool firstRender) protected override void OnAfterRender(bool firstRender)
{ {
if (firstRender) Layout.JSRuntime.EnableAnimation(); if (firstRender) RootLayout.JSRuntime.EnableAnimation();
var requestUrl = Layout.NavigationManager?.Uri ?? ""; var requestUrl = RootLayout.NavigationManager?.Uri ?? "";
var path = new Uri(requestUrl).PathAndQuery; var path = new Uri(requestUrl).PathAndQuery;
var menus = DataAccess.MenuHelper.RetrieveAllMenus(Layout.UserName); var menus = DataAccess.MenuHelper.RetrieveAllMenus(RootLayout.UserName);
var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl())); var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl()));
AddTab(menu); AddTab(menu);
return base.OnAfterRenderAsync(firstRender);
} }
} }
} }

View File

@ -1,5 +1,6 @@
using Bootstrap.Admin.Shared; using Bootstrap.Admin.Shared;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Bootstrap.Admin.Components namespace Bootstrap.Admin.Components
{ {
@ -17,13 +18,13 @@ namespace Bootstrap.Admin.Components
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
[CascadingParameter(Name = "Default")] [Inject]
protected DefaultLayout RootLayout { get; set; } = new DefaultLayout(); public NavigationManager? NavigationManager { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
[Inject] [Inject]
public NavigationManager? NavigationManager { get; set; } protected IJSRuntime? JSRuntime { get; set; }
} }
} }

View File

@ -2,6 +2,7 @@
using Bootstrap.Admin.Shared; using Bootstrap.Admin.Shared;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using System; using System;
using System.Net; using System.Net;
@ -18,7 +19,7 @@ namespace Bootstrap.Admin.Components
/// ///
/// </summary> /// </summary>
[Inject] [Inject]
public AuthenticationStateProvider? AuthenticationStateProvider { get; set; } public AuthenticationStateProvider AuthenticationStateProvider { get; set; } = new ServerAuthenticationStateProvider();
/// <summary> /// <summary>
/// ///
@ -35,7 +36,7 @@ namespace Bootstrap.Admin.Components
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public NavigatorBarModel? Model { get; set; } public NavigatorBarModel Model { get; set; } = new NavigatorBarModel("");
/// <summary> /// <summary>
/// ///
@ -72,8 +73,6 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{
if (AuthenticationStateProvider != null)
{ {
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (!state.User.Identity.IsAuthenticated) if (!state.User.Identity.IsAuthenticated)
@ -89,5 +88,4 @@ namespace Bootstrap.Admin.Components
} }
} }
} }
}
} }

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Bootstrap.Admin.Components namespace Bootstrap.Admin.Components
{ {
@ -44,12 +43,6 @@ namespace Bootstrap.Admin.Components
[Parameter] [Parameter]
public string Icon { get; set; } = ""; public string Icon { get; set; } = "";
/// <summary>
///
/// </summary>
[Inject]
protected IJSRuntime? JSRuntime { get; set; }
/// <summary> /// <summary>
/// 更新登录用户显示名称方法 /// 更新登录用户显示名称方法
/// </summary> /// </summary>

View File

@ -1,7 +1,5 @@
using Bootstrap.Admin.Extensions; using Bootstrap.Admin.Extensions;
using Bootstrap.Security; using Bootstrap.Security;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -18,12 +16,6 @@ namespace Bootstrap.Admin.Components
protected List<PageContentAttributes> Pages { get; set; } = new List<PageContentAttributes>(); protected List<PageContentAttributes> Pages { get; set; } = new List<PageContentAttributes>();
private string? curId = ""; private string? curId = "";
/// <summary>
///
/// </summary>
[Inject]
protected IJSRuntime? JSRuntime { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -30,6 +30,5 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
[Parameter] [Parameter]
public RenderFragment? ChildContent { get; set; } public RenderFragment? ChildContent { get; set; }
} }
} }

View File

@ -1,6 +1,5 @@
using Bootstrap.Admin.Models; using Bootstrap.Admin.Models;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Bootstrap.Admin.Components namespace Bootstrap.Admin.Components
{ {
@ -9,16 +8,10 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
public class SideBarBase : BootstrapComponentBase public class SideBarBase : BootstrapComponentBase
{ {
/// <summary>
///
/// </summary>
[Inject]
protected IJSRuntime? JSRuntime { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
[Parameter] [Parameter]
public NavigatorBarModel? Model { get; set; } public NavigatorBarModel Model { get; set; } = new NavigatorBarModel("");
} }
} }

View File

@ -1,8 +1,6 @@
using Bootstrap.Admin.Extensions; using Bootstrap.Admin.Extensions;
using Bootstrap.Admin.Shared; using Bootstrap.Admin.Shared;
using Bootstrap.Security; using Bootstrap.Security;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -14,12 +12,6 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
public class TabSetBase : BootstrapComponentBase public class TabSetBase : BootstrapComponentBase
{ {
/// <summary>
///
/// </summary>
[Inject]
protected IJSRuntime? JSRuntime { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -91,7 +83,7 @@ namespace Bootstrap.Admin.Components
/// </summary> /// </summary>
protected void CloseAllTab() protected void CloseAllTab()
{ {
NavigationManager?.NavigateTo(RootLayout?.HomeUrl); NavigationManager?.NavigateTo(Layout.RootLayout.HomeUrl);
} }
/// <summary> /// <summary>

View File

@ -3,22 +3,23 @@
@page "/Pages/Admin/Index" @page "/Pages/Admin/Index"
@layout DefaultLayout @layout DefaultLayout
@attribute [Authorize] @attribute [Authorize]
@inject IJSRuntime JSRuntime
<Section ShowCardTitle="@ShowCardTitle" LockScreenPeriod="@LockScreenPeriod" ShowBackground="true"> <Section ShowCardTitle="@Layout.Model.ShowCardTitle" LockScreenPeriod="@Layout.Model.LockScreenPeriod" ShowBackground="true">
<h4 style="color: #fff">欢迎使用后台管理</h4> <h4 style="color: #fff">欢迎使用后台管理</h4>
</Section> </Section>
@code { @code {
protected string? ShowCardTitle { get; set; } = ""; [CascadingParameter(Name = "Default")]
protected DefaultLayout Layout { get; set; } = new DefaultLayout();
protected int LockScreenPeriod { get; set; } = 30; /// <summary>
///
[CascadingParameter] /// </summary>
protected DefaultLayout? Layout { get; set; } protected override void OnAfterRender(bool firstRender)
protected override void OnInitialized()
{ {
ShowCardTitle = Layout?.Model?.ShowCardTitle; var menus = DataAccess.MenuHelper.RetrieveAllMenus(Layout.UserName);
LockScreenPeriod = Layout?.Model?.LockScreenPeriod ?? 30; var menu = menus.FirstOrDefault(menu => "/Pages/Admin/Index".Contains(menu.Url.ToBlazorMenuUrl(), StringComparison.OrdinalIgnoreCase));
JSRuntime.ActiveMenu(menu.Id);
} }
} }

View File

@ -2,7 +2,7 @@
@layout DefaultLayout @layout DefaultLayout
<CascadingValue Value=this Name="Admin"> <CascadingValue Value=this Name="Admin">
<Section ShowCardTitle="@Layout.Model?.ShowCardTitle" LockScreenPeriod=@(Layout.Model?.LockScreenPeriod ?? 0)> <Section ShowCardTitle="@RootLayout.Model.ShowCardTitle" LockScreenPeriod=@RootLayout.Model.LockScreenPeriod>
<TabSet @ref="TabSet"></TabSet> <TabSet @ref="TabSet"></TabSet>
<PageSet @ref="PageSet"></PageSet> <PageSet @ref="PageSet"></PageSet>
</Section> </Section>

View File

@ -1,8 +1,8 @@
@inherits DefaultLayoutComponentBase @inherits DefaultLayoutComponentBase
<CascadingValue Value=this Name="Default"> <CascadingValue Value=this Name="Default">
<Header @ref="Header" Icon="@Model?.Icon" Title="@Model?.Title" IsAdmin=@IsAdmin UserName="@Model?.UserName" @bind-DisplayName="@DisplayName"></Header> <Header @ref="Header" Icon="@Model.Icon" Title="@Model.Title" IsAdmin=@IsAdmin UserName="@Model.UserName" @bind-DisplayName="@DisplayName"></Header>
<SideBar @ref="SideBar" Model=@Model /> <SideBar @ref="SideBar" Model=@Model />
@Body @Body
<Footer Text="@Model?.Footer" IsDemo=@Model?.IsDemo></Footer> <Footer Text="@Model.Footer" IsDemo=@Model.IsDemo></Footer>
</CascadingValue> </CascadingValue>

View File

@ -1,7 +1,7 @@
<footer class="position-fixed"> <footer class="position-fixed">
<div> <div>
<span id="websiteFooter">@Text</span> <span id="websiteFooter">@Text</span>
@if (IsDemo ?? false) @if (IsDemo)
{ {
<span>(演示系统)</span> <span>(演示系统)</span>
} }
@ -16,5 +16,5 @@
public string Text { get; set; } = "未设置"; public string Text { get; set; } = "未设置";
[Parameter] [Parameter]
public bool? IsDemo { get; set; } public bool IsDemo { get; set; }
} }

View File

@ -3,7 +3,7 @@
<header class="header"> <header class="header">
<div class="bg"></div> <div class="bg"></div>
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
<a id="navbar" href="@RootLayout?.HomeUrl" class="sidebar-toggle-box"> <a id="navbar" href="@Layout.RootLayout.HomeUrl" class="sidebar-toggle-box">
<i class="fa fa-bars"></i> <i class="fa fa-bars"></i>
<span id="websiteTitle">@Title</span> <span id="websiteTitle">@Title</span>
</a> </a>

View File

@ -1,28 +1,25 @@
@inherits SideBarBase @inherits SideBarBase
<aside class="@Model?.ShowSideBar"> <aside class="@Model.ShowSideBar">
<div class="bg"></div> <div class="bg"></div>
<div class="nav-brand justify-content-center"> <div class="nav-brand justify-content-center">
<a href="@RootLayout?.HomeUrl"> <a href="@Layout.RootLayout.HomeUrl">
<img src="@Model?.WebSiteLogo.ToBlazorLink()" /> <img src="@Model.WebSiteLogo.ToBlazorLink()" />
<span>@Model?.Title</span> <span>@Model.Title</span>
</a> </a>
</div> </div>
<div class="nav-header flex-fill align-items-center"> <div class="nav-header flex-fill align-items-center">
<a href="/Pages/Admin/Profiles"> <a href="/Pages/Admin/Profiles">
<img src="@Model?.Icon.ToBlazorLink()" /> <img src="@Model.Icon.ToBlazorLink()" />
<span class="username d-inline-block text-truncate flex-fill">@Model?.DisplayName</span> <span class="username d-inline-block text-truncate flex-fill">@Model.DisplayName</span>
</a> </a>
</div> </div>
<div class="sidebar"> <div class="sidebar">
<ul class="nav-sidebar nav nav-pills flex-column flex-nowrap"> <ul class="nav-sidebar nav nav-pills flex-column flex-nowrap">
@if (Model != null) @foreach (var menu in Model.Navigations)
{
foreach (var menu in Model.Navigations)
{ {
<NavItem Menu="@menu"></NavItem> <NavItem Menu="@menu"></NavItem>
} }
}
</ul> </ul>
</div> </div>
</aside> </aside>