refactor: 移动组件 NavigationManager 与 JSRuntime 到基类
This commit is contained in:
parent
08ff785719
commit
14ac39c61d
|
@ -27,7 +27,7 @@ namespace Bootstrap.Admin.Components
|
|||
///
|
||||
/// </summary>
|
||||
[CascadingParameter(Name = "Default")]
|
||||
public DefaultLayout Layout { get; protected set; } = new DefaultLayout();
|
||||
public DefaultLayout RootLayout { get; protected set; } = new DefaultLayout();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -60,7 +60,7 @@ namespace Bootstrap.Admin.Components
|
|||
{
|
||||
if (TabSet != null)
|
||||
{
|
||||
if (TabSet.TabCount == 1) Layout.NavigationManager?.NavigateTo(Layout.HomeUrl);
|
||||
if (TabSet.TabCount == 1) RootLayout.NavigationManager?.NavigateTo(RootLayout.HomeUrl);
|
||||
else
|
||||
{
|
||||
var pageId = await TabSet.CloseTab(tabId);
|
||||
|
@ -74,16 +74,15 @@ namespace Bootstrap.Admin.Components
|
|||
/// </summary>
|
||||
/// <param name="firstRender"></param>
|
||||
/// <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 menus = DataAccess.MenuHelper.RetrieveAllMenus(Layout.UserName);
|
||||
var menus = DataAccess.MenuHelper.RetrieveAllMenus(RootLayout.UserName);
|
||||
var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl()));
|
||||
AddTab(menu);
|
||||
return base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using Bootstrap.Admin.Shared;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Bootstrap.Admin.Components
|
||||
{
|
||||
|
@ -17,13 +18,13 @@ namespace Bootstrap.Admin.Components
|
|||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[CascadingParameter(Name = "Default")]
|
||||
protected DefaultLayout RootLayout { get; set; } = new DefaultLayout();
|
||||
[Inject]
|
||||
public NavigationManager? NavigationManager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
public NavigationManager? NavigationManager { get; set; }
|
||||
protected IJSRuntime? JSRuntime { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using Bootstrap.Admin.Shared;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Server;
|
||||
using Microsoft.JSInterop;
|
||||
using System;
|
||||
using System.Net;
|
||||
|
@ -18,7 +19,7 @@ namespace Bootstrap.Admin.Components
|
|||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
public AuthenticationStateProvider? AuthenticationStateProvider { get; set; }
|
||||
public AuthenticationStateProvider AuthenticationStateProvider { get; set; } = new ServerAuthenticationStateProvider();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -35,7 +36,7 @@ namespace Bootstrap.Admin.Components
|
|||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public NavigatorBarModel? Model { get; set; }
|
||||
public NavigatorBarModel Model { get; set; } = new NavigatorBarModel("");
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -73,20 +74,17 @@ namespace Bootstrap.Admin.Components
|
|||
/// <returns></returns>
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (AuthenticationStateProvider != null)
|
||||
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (!state.User.Identity.IsAuthenticated)
|
||||
{
|
||||
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (!state.User.Identity.IsAuthenticated)
|
||||
{
|
||||
NavigationManager?.NavigateTo("/Account/Login?returnUrl=" + WebUtility.UrlEncode(new Uri(NavigationManager.Uri).PathAndQuery));
|
||||
}
|
||||
else
|
||||
{
|
||||
Model = new NavigatorBarModel(state.User.Identity.Name);
|
||||
IsAdmin = state.User.IsInRole("Administrators");
|
||||
UserName = state.User.Identity.Name ?? "";
|
||||
DisplayName = Model.DisplayName;
|
||||
}
|
||||
NavigationManager?.NavigateTo("/Account/Login?returnUrl=" + WebUtility.UrlEncode(new Uri(NavigationManager.Uri).PathAndQuery));
|
||||
}
|
||||
else
|
||||
{
|
||||
Model = new NavigatorBarModel(state.User.Identity.Name);
|
||||
IsAdmin = state.User.IsInRole("Administrators");
|
||||
UserName = state.User.Identity.Name ?? "";
|
||||
DisplayName = Model.DisplayName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Bootstrap.Admin.Components
|
||||
{
|
||||
|
@ -44,12 +43,6 @@ namespace Bootstrap.Admin.Components
|
|||
[Parameter]
|
||||
public string Icon { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
protected IJSRuntime? JSRuntime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新登录用户显示名称方法
|
||||
/// </summary>
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
using Bootstrap.Admin.Extensions;
|
||||
using Bootstrap.Security;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
|
@ -18,12 +16,6 @@ namespace Bootstrap.Admin.Components
|
|||
protected List<PageContentAttributes> Pages { get; set; } = new List<PageContentAttributes>();
|
||||
private string? curId = "";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
protected IJSRuntime? JSRuntime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
|
|
@ -30,6 +30,5 @@ namespace Bootstrap.Admin.Components
|
|||
/// </summary>
|
||||
[Parameter]
|
||||
public RenderFragment? ChildContent { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using Bootstrap.Admin.Models;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Bootstrap.Admin.Components
|
||||
{
|
||||
|
@ -9,16 +8,10 @@ namespace Bootstrap.Admin.Components
|
|||
/// </summary>
|
||||
public class SideBarBase : BootstrapComponentBase
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
protected IJSRuntime? JSRuntime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public NavigatorBarModel? Model { get; set; }
|
||||
public NavigatorBarModel Model { get; set; } = new NavigatorBarModel("");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
using Bootstrap.Admin.Extensions;
|
||||
using Bootstrap.Admin.Shared;
|
||||
using Bootstrap.Security;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -14,12 +12,6 @@ namespace Bootstrap.Admin.Components
|
|||
/// </summary>
|
||||
public class TabSetBase : BootstrapComponentBase
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Inject]
|
||||
protected IJSRuntime? JSRuntime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
@ -91,7 +83,7 @@ namespace Bootstrap.Admin.Components
|
|||
/// </summary>
|
||||
protected void CloseAllTab()
|
||||
{
|
||||
NavigationManager?.NavigateTo(RootLayout?.HomeUrl);
|
||||
NavigationManager?.NavigateTo(Layout.RootLayout.HomeUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -3,22 +3,23 @@
|
|||
@page "/Pages/Admin/Index"
|
||||
@layout DefaultLayout
|
||||
@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>
|
||||
</Section>
|
||||
|
||||
@code {
|
||||
protected string? ShowCardTitle { get; set; } = "";
|
||||
[CascadingParameter(Name = "Default")]
|
||||
protected DefaultLayout Layout { get; set; } = new DefaultLayout();
|
||||
|
||||
protected int LockScreenPeriod { get; set; } = 30;
|
||||
|
||||
[CascadingParameter]
|
||||
protected DefaultLayout? Layout { get; set; }
|
||||
|
||||
protected override void OnInitialized()
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
ShowCardTitle = Layout?.Model?.ShowCardTitle;
|
||||
LockScreenPeriod = Layout?.Model?.LockScreenPeriod ?? 30;
|
||||
var menus = DataAccess.MenuHelper.RetrieveAllMenus(Layout.UserName);
|
||||
var menu = menus.FirstOrDefault(menu => "/Pages/Admin/Index".Contains(menu.Url.ToBlazorMenuUrl(), StringComparison.OrdinalIgnoreCase));
|
||||
JSRuntime.ActiveMenu(menu.Id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
@layout DefaultLayout
|
||||
|
||||
<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>
|
||||
<PageSet @ref="PageSet"></PageSet>
|
||||
</Section>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
@inherits DefaultLayoutComponentBase
|
||||
|
||||
<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 />
|
||||
@Body
|
||||
<Footer Text="@Model?.Footer" IsDemo=@Model?.IsDemo></Footer>
|
||||
<Footer Text="@Model.Footer" IsDemo=@Model.IsDemo></Footer>
|
||||
</CascadingValue>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<footer class="position-fixed">
|
||||
<div>
|
||||
<span id="websiteFooter">@Text</span>
|
||||
@if (IsDemo ?? false)
|
||||
@if (IsDemo)
|
||||
{
|
||||
<span>(演示系统)</span>
|
||||
}
|
||||
|
@ -16,5 +16,5 @@
|
|||
public string Text { get; set; } = "未设置";
|
||||
|
||||
[Parameter]
|
||||
public bool? IsDemo { get; set; }
|
||||
public bool IsDemo { get; set; }
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
<header class="header">
|
||||
<div class="bg"></div>
|
||||
<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>
|
||||
<span id="websiteTitle">@Title</span>
|
||||
</a>
|
||||
|
|
|
@ -1,27 +1,24 @@
|
|||
@inherits SideBarBase
|
||||
|
||||
<aside class="@Model?.ShowSideBar">
|
||||
<aside class="@Model.ShowSideBar">
|
||||
<div class="bg"></div>
|
||||
<div class="nav-brand justify-content-center">
|
||||
<a href="@RootLayout?.HomeUrl">
|
||||
<img src="@Model?.WebSiteLogo.ToBlazorLink()" />
|
||||
<span>@Model?.Title</span>
|
||||
<a href="@Layout.RootLayout.HomeUrl">
|
||||
<img src="@Model.WebSiteLogo.ToBlazorLink()" />
|
||||
<span>@Model.Title</span>
|
||||
</a>
|
||||
</div>
|
||||
<div class="nav-header flex-fill align-items-center">
|
||||
<a href="/Pages/Admin/Profiles">
|
||||
<img src="@Model?.Icon.ToBlazorLink()" />
|
||||
<span class="username d-inline-block text-truncate flex-fill">@Model?.DisplayName</span>
|
||||
<img src="@Model.Icon.ToBlazorLink()" />
|
||||
<span class="username d-inline-block text-truncate flex-fill">@Model.DisplayName</span>
|
||||
</a>
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<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>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue