zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
commit | author | age
921de2 1 using Bootstrap.Admin.Pages.Extensions;
Z 2 using Bootstrap.Admin.Pages.Shared;
3 using Bootstrap.Security;
4 using Microsoft.AspNetCore.Components;
5 using Microsoft.JSInterop;
6 using System;
7 using System.Collections.Generic;
8 using System.Linq;
9 using System.Threading.Tasks;
10
11 namespace Bootstrap.Admin.Pages.Components
12 {
13     /// <summary>
14     /// TabSet 组件类
15     /// </summary>
16     public class TabSetBase : ComponentBase
17     {
18         /// <summary>
19         /// 获得 NavigationManager 实例
20         /// </summary>
21         [Inject]
22         protected NavigationManager? NavigationManager { get; set; }
23
24         /// <summary>
25         /// 获得 IJSRuntime 实例
26         /// </summary>
27         [Inject]
28         protected IJSRuntime? JSRuntime { get; set; }
29
30         /// <summary>
31         /// 获得 DefaultLayout 实例
32         /// </summary>
33         [CascadingParameter(Name = "Default")]
34         protected DefaultLayout? RootLayout { get; set; }
35
36         /// <summary>
37         /// 获得 页面集合
38         /// </summary>
39         protected List<PageContentAttributes> Pages { get; set; } = new List<PageContentAttributes>();
40
41         /// <summary>
42         /// T获得 ab 集合
43         /// </summary>
44         protected List<Tab> Tabs { get; set; } = new List<Tab>();
45         private string? curId;
46
47         /// <summary>
48         /// OnParametersSet 方法
49         /// </summary>
50         protected override void OnParametersSet()
51         {
52             var requestUrl = NavigationManager?.Uri ?? "";
53             var path = new Uri(requestUrl).PathAndQuery;
54             var menus = DataAccess.MenuHelper.RetrieveAllMenus(RootLayout?.UserName);
55             var menu = menus.FirstOrDefault(menu => path.Contains(menu.Url.ToBlazorMenuUrl()));
56             if (menu != null) AddTab(menu);
57         }
58
59         /// <summary>
60         /// 页面呈现后回调方法
61         /// </summary>
62         /// <param name="firstRender"></param>
63         protected override void OnAfterRender(bool firstRender)
64         {
65             if (!string.IsNullOrEmpty(curId))
66             {
67                 // Add Tab 后设置 active 状态
68                 JSRuntime.ActiveMenu(curId);
69                 curId = "";
70             }
71         }
72
73         /// <summary>
74         /// 添加 Tab 由侧边栏菜单调用
75         /// </summary>
76         /// <param name="menu"></param>
77         public void AddTab(BootstrapMenu menu)
78         {
79             var tab = Tabs.FirstOrDefault(tb => tb.Id == menu.Id);
80             if (tab == null)
81             {
82                 tab = new Tab();
83                 tab.Init(menu);
84                 Tabs.ForEach(t => t.SetActive(false));
85                 Tabs.Add(tab);
86                 Pages.ForEach(p => p.Active = false);
87                 Pages.Add(new PageContentAttributes() { Id = menu.Id, Name = menu.Url.Replace("~/", ""), Active = true });
88             }
89             else
90             {
91                 Tabs.ForEach(t => t.SetActive(t.Id == tab.Id));
92                 Pages.ForEach(p => p.Active = p.Id == tab.Id);
93             }
94             curId = tab.Id;
95             StateHasChanged();
96         }
97
98         /// <summary>
99         /// 关闭指定标签页方法
100         /// </summary>
101         /// <param name="tabId"></param>
102         /// <returns></returns>
103         public async Task CloseTab(string? tabId)
104         {
105             if (Tabs.Count == 1)
106             {
107                 CloseAllTab();
108                 return;
109             }
110             if (!string.IsNullOrEmpty(tabId))
111             {
112                 var tab = Tabs.FirstOrDefault(tb => tb.Id == tabId);
113                 if (tab != null)
114                 {
115                     // 移除 PageContent
116                     var page = Pages.FirstOrDefault(p => p.Id == tab.Id);
117                     if (page != null) Pages.Remove(page);
118
119                     // 移除 Tab 返回下一个 TabId
120                     tabId = await JSRuntime.RemoveTabAsync(tab.Id);
121                     Tabs.Remove(tab);
122                     tab = Tabs.FirstOrDefault(t => t.Id == tabId);
123                     tab.SetActive(true);
124
125                     page = Pages.FirstOrDefault(p => p.Id == tabId);
126                     if (page != null) page.Active = true;
127                     StateHasChanged();
128                 }
129             }
130         }
131
132         /// <summary>
133         /// 关闭所有标签页方法
134         /// </summary>
135         protected void CloseAllTab()
136         {
137             if (RootLayout != null) NavigationManager?.NavigateTo(RootLayout.HomeUrl);
138         }
139
140         /// <summary>
141         /// 关闭当前标签页方法
142         /// </summary>
143         protected async Task CloseCurrentTab()
144         {
145             var tabId = Tabs.FirstOrDefault(t => t.Active)?.Id;
146             await CloseTab(tabId);
147         }
148
149         /// <summary>
150         /// 关闭其他标签页方法
151         /// </summary>
152         protected void CloseOtherTab()
153         {
154             var tabId = Tabs.FirstOrDefault(t => t.Active)?.Id;
155             if (!string.IsNullOrEmpty(tabId))
156             {
157                 Tabs.RemoveAll(t => t.Id != tabId);
158                 Pages.RemoveAll(page => page.Id != tabId);
159                 curId = tabId;
160                 StateHasChanged();
161             }
162         }
163
164         /// <summary>
165         /// 前移标签页方法
166         /// </summary>
167         protected async Task MovePrev()
168         {
169             var url = await JSRuntime.MovePrevTabAsync();
170             if (!string.IsNullOrEmpty(url)) NavigationManager?.NavigateTo(url);
171         }
172
173         /// <summary>
174         /// 后移标签页方法
175         /// </summary>
176         /// <returns></returns>
177         protected async Task MoveNext()
178         {
179             var url = await JSRuntime.MoveNextTabAsync();
180             if (!string.IsNullOrEmpty(url)) NavigationManager?.NavigateTo(url);
181         }
182     }
183 }