Skip to content

Commit 6bb601f

Browse files
committed
refactor: Layout 组件增加 Tab 右键菜单
1 parent 7021101 commit 6bb601f

File tree

5 files changed

+147
-13
lines changed

5 files changed

+147
-13
lines changed

src/BootstrapBlazor/Components/Layout/Layout.razor

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,50 @@
116116
@<main class="layout-main">
117117
@if (UseTabSet)
118118
{
119-
<Tab ClickTabToNavigation="ClickTabToNavigation" AdditionalAssemblies="@AdditionalAssemblies"
120-
ShowExtendButtons="ShowTabExtendButtons" ShowClose="ShowTabItemClose" AllowDrag="AllowDragTab"
121-
DefaultUrl="@TabDefaultUrl" ExcludeUrls="@ExcludeUrls" IsOnlyRenderActiveTab="IsOnlyRenderActiveTab"
122-
TabStyle="TabStyle" ShowToolbar="@ShowToolbar" ToolbarTemplate="@ToolbarTemplate"
123-
ShowRefreshToolbarButton="ShowRefreshToolbarButton" ShowFullscreenToolbarButton="ShowFullscreenToolbarButton"
124-
RefreshToolbarButtonIcon="@RefreshToolbarButtonIcon" FullscreenToolbarButtonIcon="@FullscreenToolbarButtonIcon"
125-
RefreshToolbarTooltipText="@RefreshToolbarTooltipText" FullscreenToolbarTooltipText="@FullscreenToolbarTooltipText"
126-
OnToolbarRefreshCallback="OnToolbarRefreshCallback"
127-
Body="@Main" NotAuthorized="NotAuthorized!" NotFound="NotFound!" NotFoundTabText="@NotFoundTabText">
128-
</Tab>
119+
@if (ShowTabContextMenu)
120+
{
121+
<ContextMenuZone>
122+
@RenderTab
123+
<ContextMenu>
124+
@if (BeforeTabContextMenuTemplate != null)
125+
{
126+
@BeforeTabContextMenuTemplate(_tab)
127+
}
128+
<ContextMenuItem Icon="@TabContextMenuRefreshIcon" Text="@Localizer["ContextRefresh"]" OnClick="OnRefrsh"></ContextMenuItem>
129+
<ContextMenuDivider></ContextMenuDivider>
130+
<ContextMenuItem Icon="@TabContextMenuCloseIcon" Text="@Localizer["ContextClose"]" OnClick="OnClose"></ContextMenuItem>
131+
<ContextMenuItem Icon="@TabContextMenuCloseOtherIcon" Text="@Localizer["ContextCloseOther"]" OnClick="OnCloseOther"></ContextMenuItem>
132+
<ContextMenuItem Icon="@TabContextMenuCloseAllIcon" Text="@Localizer["ContextCloseAll"]" OnClick="OnCloseAll"></ContextMenuItem>
133+
@if (TabContextMenuTemplate != null)
134+
{
135+
@TabContextMenuTemplate(_tab)
136+
}
137+
</ContextMenu>
138+
</ContextMenuZone>
139+
}
140+
else
141+
{
142+
@RenderTab
143+
}
129144
}
130145
else
131146
{
132147
@HandlerMain()
133148
}
134149
</main>;
135150

151+
RenderFragment RenderTab =>
152+
@<Tab ClickTabToNavigation="ClickTabToNavigation" AdditionalAssemblies="@AdditionalAssemblies" @ref="_tab"
153+
ShowExtendButtons="ShowTabExtendButtons" ShowClose="ShowTabItemClose" AllowDrag="AllowDragTab"
154+
DefaultUrl="@TabDefaultUrl" ExcludeUrls="@ExcludeUrls" IsOnlyRenderActiveTab="IsOnlyRenderActiveTab"
155+
TabStyle="TabStyle" ShowToolbar="@ShowToolbar" ToolbarTemplate="@ToolbarTemplate"
156+
ShowRefreshToolbarButton="ShowRefreshToolbarButton" ShowFullscreenToolbarButton="ShowFullscreenToolbarButton"
157+
RefreshToolbarButtonIcon="@RefreshToolbarButtonIcon" FullscreenToolbarButtonIcon="@FullscreenToolbarButtonIcon"
158+
RefreshToolbarTooltipText="@RefreshToolbarTooltipText" FullscreenToolbarTooltipText="@FullscreenToolbarTooltipText"
159+
OnToolbarRefreshCallback="OnToolbarRefreshCallback"
160+
Body="@Main" NotAuthorized="NotAuthorized!" NotFound="NotFound!" NotFoundTabText="@NotFoundTabText">
161+
</Tab>;
162+
136163
RenderFragment RenderFooter =>
137164
@<footer class="@FooterClassString">
138165
@Footer

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,48 @@ public partial class Layout : IHandlerException
297297
[Parameter]
298298
public string NotAuthorizeUrl { get; set; } = "/Account/Login";
299299

300+
/// <summary>
301+
/// Gets or sets whether enable tab context menu. Default is false.
302+
/// </summary>
303+
[Parameter]
304+
public bool ShowTabContextMenu { get; set; }
305+
306+
/// <summary>
307+
/// Gets or sets the template of before tab context menu. Default is null.
308+
/// </summary>
309+
[Parameter]
310+
public RenderFragment<Tab>? BeforeTabContextMenuTemplate { get; set; }
311+
312+
/// <summary>
313+
/// Gets or sets the template of tab context menu. Default is null.
314+
/// </summary>
315+
[Parameter]
316+
public RenderFragment<Tab>? TabContextMenuTemplate { get; set; }
317+
318+
/// <summary>
319+
/// Gets or sets the icon of tab item context menu refresh button. Default is null.
320+
/// </summary>
321+
[Parameter]
322+
public string? TabContextMenuRefreshIcon { get; set; }
323+
324+
/// <summary>
325+
/// Gets or sets the icon of tab item context menu close button. Default is null.
326+
/// </summary>
327+
[Parameter]
328+
public string? TabContextMenuCloseIcon { get; set; }
329+
330+
/// <summary>
331+
/// Gets or sets the icon of tab item context menu close other button. Default is null.
332+
/// </summary>
333+
[Parameter]
334+
public string? TabContextMenuCloseOtherIcon { get; set; }
335+
336+
/// <summary>
337+
/// Gets or sets the icon of tab item context menu close all button. Default is null.
338+
/// </summary>
339+
[Parameter]
340+
public string? TabContextMenuCloseAllIcon { get; set; }
341+
300342
[Inject]
301343
[NotNull]
302344
private NavigationManager? Navigation { get; set; }
@@ -404,7 +446,8 @@ public partial class Layout : IHandlerException
404446
[NotNull]
405447
private IStringLocalizer<Layout>? Localizer { get; set; }
406448

407-
private bool _init { get; set; }
449+
private bool _init;
450+
private Tab _tab = default!;
408451

409452
/// <summary>
410453
/// <inheritdoc/>
@@ -466,6 +509,10 @@ protected override void OnParametersSet()
466509

467510
TooltipText ??= Localizer[nameof(TooltipText)];
468511
MenuBarIcon ??= IconTheme.GetIconByKey(ComponentIcons.LayoutMenuBarIcon);
512+
TabContextMenuRefreshIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuRefreshIcon);
513+
TabContextMenuCloseIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseIcon);
514+
TabContextMenuCloseOtherIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseOtherIcon);
515+
TabContextMenuCloseAllIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseAllIcon);
469516
}
470517

471518
/// <summary>
@@ -580,6 +627,38 @@ public virtual Task HandlerException(Exception ex, RenderFragment<Exception> err
580627

581628
private string? GetTargetString() => IsFixedTabHeader ? ".tabs-body" : null;
582629

630+
private async Task OnRefrsh(ContextMenuItem item, object? context)
631+
{
632+
if (context is TabItem tabItem)
633+
{
634+
await _tab.Refresh(tabItem);
635+
}
636+
}
637+
638+
private async Task OnClose(ContextMenuItem item, object? context)
639+
{
640+
if (context is TabItem tabItem)
641+
{
642+
await _tab.RemoveTab(tabItem);
643+
}
644+
}
645+
646+
private Task OnCloseOther(ContextMenuItem item, object? context)
647+
{
648+
if (context is TabItem tabItem)
649+
{
650+
_tab.ActiveTab(tabItem);
651+
}
652+
_tab.CloseOtherTabs();
653+
return Task.CompletedTask;
654+
}
655+
656+
private Task OnCloseAll(ContextMenuItem item, object? context)
657+
{
658+
_tab.CloseAllTabs();
659+
return Task.CompletedTask;
660+
}
661+
583662
/// <summary>
584663
/// <inheritdoc/>
585664
/// </summary>

src/BootstrapBlazor/Enums/ComponentIcons.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,26 @@ public enum ComponentIcons
710710
/// </summary>
711711
TabRefreshButtonIcon,
712712

713+
/// <summary>
714+
/// Tab 组件 TabContextMenuRefreshIcon 属性图标
715+
/// </summary>
716+
TabContextMenuRefreshIcon,
717+
718+
/// <summary>
719+
/// Tab 组件 TabContextMenuCloseIcon 属性图标
720+
/// </summary>
721+
TabContextMenuCloseIcon,
722+
723+
/// <summary>
724+
/// Tab 组件 TabContextMenuCloseOtherIcon 属性图标
725+
/// </summary>
726+
TabContextMenuCloseOtherIcon,
727+
728+
/// <summary>
729+
/// Tab 组件 TabContextMenuCloseAllIcon 属性图标
730+
/// </summary>
731+
TabContextMenuCloseAllIcon,
732+
713733
/// <summary>
714734
/// Timer 组件 Icon 属性图标
715735
/// </summary>

src/BootstrapBlazor/Locales/en.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@
105105
"TooltipText": "Go top"
106106
},
107107
"BootstrapBlazor.Components.Layout": {
108-
"TooltipText": "Click to Expand/Collapse sidebar"
108+
"TooltipText": "Click to Expand/Collapse sidebar",
109+
"ContextRefresh": "Refresh",
110+
"ContextClose": "Close",
111+
"ContextCloseOther": "Close Other Tabs",
112+
"ContextCloseAll": "Close All Tabs"
109113
},
110114
"BootstrapBlazor.Components.Logout": {
111115
"PrefixDisplayNameText": "Welcome",

src/BootstrapBlazor/Locales/zh.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@
105105
"TooltipText": "返回顶端"
106106
},
107107
"BootstrapBlazor.Components.Layout": {
108-
"TooltipText": "点击展开收缩左侧菜单"
108+
"TooltipText": "点击展开收缩左侧菜单",
109+
"ContextRefresh": "刷新",
110+
"ContextClose": "关闭",
111+
"ContextCloseOther": "关闭其他",
112+
"ContextCloseAll": "关闭全部"
109113
},
110114
"BootstrapBlazor.Components.Logout": {
111115
"PrefixDisplayNameText": "欢迎",

0 commit comments

Comments
 (0)