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