Skip to content

Commit c3381e1

Browse files
committed
2 parents 857a21b + 61ef8f7 commit c3381e1

File tree

17 files changed

+453
-58
lines changed

17 files changed

+453
-58
lines changed

src/BootstrapBlazor.Server/Components/Layout/HomeLayout.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<div class="ms-1">BB @VersionService.Version</div>
6565
<FooterCounter></FooterCounter>
6666
<CacheCounter></CacheCounter>
67+
<div class="ms-2">
68+
<NetworkMonitorIndicator></NetworkMonitorIndicator>
69+
</div>
6770
</div>
6871
<div class="d-flex flex-fill align-items-center justify-content-center">
6972
<a class="d-none d-md-block me-3" href="@WebsiteOption.CurrentValue.GiteeRepositoryUrl" target="_blank">@Localizer["Footer"]</a>

src/BootstrapBlazor.Server/Components/Samples/Client.razor

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
<h4>@Localizer["SubTitle"]</h4>
77

88
<DemoBlock Title="@Localizer["BasicUsageTitle"]" Introduction="@Localizer["BasicUsageIntro"]" Name="Normal">
9-
<p>@Localizer["BasicUsageP1"]</p>
10-
<div class="mb-3">
9+
<section ignore>
10+
<p>@Localizer["BasicUsageP1"]</p>
1111
<p class="code-label">@((MarkupString)Localizer["BasicUsageP2"].Value)</p>
1212
<Pre>public void Configure(IApplicationBuilder app)
1313
{
1414
// ...
1515
// 增加下面这一行
16+
// add this line
1617
app.UseBootstrapBlazor();
1718
app.UseEndpoints(endpoints =>
1819
{
@@ -21,13 +22,11 @@
2122
endpoints.MapFallbackToPage("/_Host");
2223
});
2324
}</Pre>
24-
</div>
2525

26-
<Tips>
27-
<p>@((MarkupString)Localizer["BasicUsageTips"].Value)</p>
28-
</Tips>
26+
<Tips>
27+
<p>@((MarkupString)Localizer["BasicUsageTips"].Value)</p>
28+
</Tips>
2929

30-
<div class="mb-3">
3130
<p class="code-label">@((MarkupString)Localizer["BasicUsageP3"].Value)</p>
3231
<Pre>[Inject]
3332
[NotNull]
@@ -44,9 +43,29 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4443
ClientInfo = await ClientService.GetClientInfo();
4544
StateHasChanged();
4645
}
47-
}
48-
</Pre>
49-
</div>
46+
}</Pre>
47+
48+
<p class="code-label">@((MarkupString)Localizer["BasicUsageP4"].Value)</p>
49+
<p>@((MarkupString)Localizer["LocatorsProviderOptions"].Value)</p>
50+
<p>@((MarkupString)Localizer["LocatorsProviderDesc1"].Value)</p>
51+
<Pre>{
52+
"BootstrapBlazorOptions": {
53+
"WebClientOptions": {
54+
"EnableIpLocator": true
55+
}
56+
}</Pre>
57+
<p>@((MarkupString)Localizer["LocatorsProviderDesc2"].Value)</p>
58+
<Pre>services.AddBootstrapBlazor(op =>
59+
{
60+
op.WebClientOptions.EnableIpLocator = true;
61+
});</Pre>
62+
<p>@((MarkupString)Localizer["LocatorsProviderDesc3"].Value)</p>
63+
<Pre>services.Configure&lt;BootstrapBlazorOptions&gt;(op =>
64+
{
65+
op.WebClientOptions.EnableIpLocator = true;
66+
});</Pre>
67+
68+
</section>
5069
<GroupBox Title="@Localizer["GroupBoxTitle"]">
5170
<p class="code-label">@Localizer["IpLocatorFactoryDesc"] <a href="locator" target="_blank">IpLocatorFactory</a></p>
5271

src/BootstrapBlazor.Server/Components/Samples/Locators.razor

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
1717
</Tips>
1818
<Pre>Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)</Pre>
1919
<p><b>@Localizer["LocatorsNormalExtendDescription"]</b></p>
20+
2021
<p><b>@Localizer["LocatorsNormalExtend1"]</b></p>
2122
<Pre>private class CustomerLocatorProvider : DefaultIpLocatorProvider
2223
{
@@ -25,34 +26,22 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
2526
throw new NotImplementedException();
2627
}
2728
}</Pre>
29+
2830
<p><b>@Localizer["LocatorsNormalExtend2"]</b></p>
2931
<Pre>services.AddSingleton&lt;IIpLocatorProvider, CustomerLocatorProvider&gt;();</Pre>
3032
<p>@((MarkupString)Localizer["LocatorsNormalCustomerLocator"].Value)</p>
33+
34+
<p><b>@Localizer["LocatorsNormalExtend3"]</b></p>
35+
<Pre>var provider = IpLocatorFactory.Create(ProviderName);
36+
Location = await provider.Locate(Ip);</Pre>
37+
3138
<p>@Localizer["LocatorsNormalIpTitle"]</p>
3239
<p><code>112.224.74.239</code> @Localizer["LocatorsNormalTips3"]</p>
3340
<p><code>183.160.236.53</code> @Localizer["LocatorsNormalTips4"]</p>
3441

3542
<DemoBlock Title="@Localizer["LocatorsNormalTitle"]" Introduction="@Localizer["LocatorsNormalIntro"]" Name="Normal">
3643
<section ignore>
3744
@((MarkupString)Localizer["LocatorsProviderDesc"].Value)
38-
<Tips>
39-
<p>@((MarkupString)Localizer["LocatorsProviderOptions"].Value)</p>
40-
</Tips>
41-
<Pre>{
42-
"BootstrapBlazorOptions": {
43-
"WebClientOptions": {
44-
"EnableIpLocator": true
45-
}
46-
}
47-
}</Pre>
48-
<Pre>services.AddBootstrapBlazor(op =>
49-
{
50-
op.WebClientOptions.EnableIpLocator = true;
51-
});</Pre>
52-
<Pre>services.Configure&lt;BootstrapBlazorOptions&gt;(op =>
53-
{
54-
op.WebClientOptions.EnableIpLocator = true;
55-
});</Pre>
5645
</section>
5746
<div class="row g-3 form-inline">
5847
<div class="col-12 col-sm-6">

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,11 @@
38093809
"BasicUsageP2": "1. The <code>UseBootstrapBlazor</code> middleware in the <b>Startup.cs</b> file that client information collection is performed.",
38103810
"BasicUsageTips": "<code>app.UseBootstrapBlazor</code> Middleware is located assembly <code>BootstrapBlazor.Middleware</code>, please refer to this package yourself for proper use",
38113811
"BasicUsageP3": "2. The component uses the injection service <code>WebClientService</code> to call the <code>GetClientInfo</code> method.",
3812+
"BasicUsageP4": "3. Turn on IP geolocation",
3813+
"LocatorsProviderOptions": "<code>BootstrapBlazorOptions</code> section <code>WebClientOptions</code> By default it is <code>false</code>, which means the IP address location function is not enabled. Please change it to <code>true</code> in the configuration file or code.",
3814+
"LocatorsProviderDesc1": "Update the <code>appsetting.json</code> project configuration file",
3815+
"LocatorsProviderDesc2": "Or use the code to open",
3816+
"LocatorsProviderDesc3": "Or enable this function through configuration",
38123817
"GroupBoxTitle": "Connection information",
38133818
"IpLocatorFactoryDesc": "This service has built-in IP geolocation function. For detailed configuration and documentation, please refer to",
38143819
"Id": "Connection ID",
@@ -4095,15 +4100,15 @@
40954100
"LocatorsNormalExtendDescription": "Extend the custom geo-location query interface",
40964101
"LocatorsNormalExtend1": "1. Implement a custom locator",
40974102
"LocatorsNormalExtend2": "2. Configure a custom locator",
4103+
"LocatorsNormalExtend3": "3. Use locator",
40984104
"LocatorsNormalCustomerLocator": "Add <code>CustomerLocatorProvider</code> to the service container using the <code>AddSingleton</code> method",
40994105
"LocatorsNormalIpTitle": "IP test data",
41004106
"LocatorsNormalTips3": "Shandong, China Unicom",
41014107
"LocatorsNormalTips4": "Shushan District Hefei, Anhui China Telecom",
41024108
"LocatorsNormalInputText": "IpAddress",
41034109
"LocatorsNormalDisplayText": "Geographical location",
41044110
"LocatorsNormalButtonText": "Locating",
4105-
"LocatorsProviderDesc": "<p>The component library has two built-in free online geolocation locators, <code>BaiduIpLocatorProvider</code> <code>BaiduIpLocatorProviderV2</code><p><p>The component library has a built-in paid online geolocation locator <code>BootstrapBlazor.JuHeIpLocatorProvider</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.JuHeIpLocatorProvider\" target=\"_blank\">Nuget package</a> <a href=\"https://juhe.cn\" target=\"_blank\">Official website address</a></p><p>The component library has a built-in free offline geolocation locator <code>BootstrapBlazor.IP2Region</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.IP2Region\" target=\"_blank\">Nuget package</a></p>",
4106-
"LocatorsProviderOptions": "<code>BootstrapBlazorOptions</code> section <code>WebClientOptions</code> By default it is <code>false</code>, which means the IP address location function is not enabled. Please change it to <code>true</code> in the configuration file or code."
4111+
"LocatorsProviderDesc": "<p>The component library has two built-in free online geolocation locators, <code>BaiduIpLocatorProvider</code> <code>BaiduIpLocatorProviderV2</code><p><p>The component library has a built-in paid online geolocation locator <code>BootstrapBlazor.JuHeIpLocatorProvider</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.JuHeIpLocatorProvider\" target=\"_blank\">Nuget package</a> <a href=\"https://juhe.cn\" target=\"_blank\">Official website address</a></p><p>The component library has a built-in free offline geolocation locator <code>BootstrapBlazor.IP2Region</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.IP2Region\" target=\"_blank\">Nuget package</a></p>"
41074112
},
41084113
"BootstrapBlazor.Server.Components.Samples.Print": {
41094114
"PrintsTitle": "Print",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,11 @@
38093809
"BasicUsageP2": "1. <b>Startup.cs</b> 文件中使用 <code>UseBootstrapBlazor</code> 中间件进行客户端信息收集",
38103810
"BasicUsageTips": "<code>app.UseBootstrapBlazor</code> 中间件位于程序集 <code>BootstrapBlazor.Middleware</code>,请自行引用此包才能正常使用",
38113811
"BasicUsageP3": "2. 组件中使用注入服务 <code>WebClientService</code> 调用 <code>GetClientInfo</code> 方法",
3812+
"BasicUsageP4": "3. 开启 IP 地理位置定位功能",
3813+
"LocatorsProviderOptions": "全局配置定位器选项 <code>WebClientOptions</code> 默认 <code>false</code> 没有启用 IP 地址定位功能,请在配置文件中或者代码中更改为 <code>true</code>",
3814+
"LocatorsProviderDesc1": "更新 <code>appsetting.json</code> 项目配置文件",
3815+
"LocatorsProviderDesc2": "或者使用代码开启",
3816+
"LocatorsProviderDesc3": "或者通过配置开启本功能",
38123817
"GroupBoxTitle": "您的连接信息",
38133818
"IpLocatorFactoryDesc": "本服务已内置 IP 地理位置定位功能,详细配置与文档请参考",
38143819
"Id": "连接 ID",
@@ -4095,15 +4100,15 @@
40954100
"LocatorsNormalExtendDescription": "扩展自定义地理位置查询接口",
40964101
"LocatorsNormalExtend1": "1. 实现自定义定位器",
40974102
"LocatorsNormalExtend2": "2. 配置自定义定位器",
4103+
"LocatorsNormalExtend3": "3. 通过定位器定位",
40984104
"LocatorsNormalCustomerLocator": "通过 <code>AddSingleton</code> 方法将自定义定位器 <code>CustomerLocatorProvider</code> 添加到服务容器中",
40994105
"LocatorsNormalIpTitle": "IP 测试数据",
41004106
"LocatorsNormalTips3": "山东省 中国联通",
41014107
"LocatorsNormalTips4": "安徽省合肥市蜀山区 中国电信",
41024108
"LocatorsNormalInputText": "Ip 地址",
41034109
"LocatorsNormalDisplayText": "地理位置",
41044110
"LocatorsNormalButtonText": "定位",
4105-
"LocatorsProviderDesc": "<p>组件库内置两个免费在线地理位置定位器分别为 <code>BaiduIpLocatorProvider</code> <code>BaiduIpLocatorProviderV2</code><p><p>组件库内置一个收费在线地理位置定位器 <code>BootstrapBlazor.JuHeIpLocatorProvider</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.JuHeIpLocatorProvider\" target=\"_blank\">Nuget 包</a> <a href=\"https://juhe.cn\" target=\"_blank\">官网地址</a></p><p>组件库内置一个免费离线地理位置定位器 <code>BootstrapBlazor.IP2Region</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.IP2Region\" target=\"_blank\">Nuget 包</a></p>",
4106-
"LocatorsProviderOptions": "全局配置定位器选项 <code>WebClientOptions</code> 默认 <code>false</code> 没有启用 IP 地址定位功能,请在配置文件中或者代码中更改为 <code>true</code>"
4111+
"LocatorsProviderDesc": "<p>组件库内置两个免费在线地理位置定位器分别为 <code>BaiduIpLocatorProvider</code> <code>BaiduIpLocatorProviderV2</code><p><p>组件库内置一个收费在线地理位置定位器 <code>BootstrapBlazor.JuHeIpLocatorProvider</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.JuHeIpLocatorProvider\" target=\"_blank\">Nuget 包</a> <a href=\"https://juhe.cn\" target=\"_blank\">官网地址</a></p><p>组件库内置一个免费离线地理位置定位器 <code>BootstrapBlazor.IP2Region</code> <a href=\"https://www.nuget.org/packages/BootstrapBlazor.IP2Region\" target=\"_blank\">Nuget 包</a></p>"
41074112
},
41084113
"BootstrapBlazor.Server.Components.Samples.Print": {
41094114
"PrintsTitle": "Print 打印按钮",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// 客户端链接组件
10+
/// </summary>
11+
[BootstrapModuleAutoLoader(ModuleName = "net", JSObjectReference = true)]
12+
public class NetworkMonitor : BootstrapModuleComponentBase
13+
{
14+
/// <summary>
15+
/// Gets or sets the callback function that is invoked when the network state changes.
16+
/// </summary>
17+
[Parameter]
18+
public Func<NetworkMonitorState, Task>? OnNetworkStateChanged { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the list of indicators used for display info.
22+
/// </summary>
23+
[Parameter]
24+
public List<string>? Indicators { get; set; }
25+
26+
private NetworkMonitorState _state = new();
27+
28+
/// <summary>
29+
/// <inheritdoc/>
30+
/// </summary>
31+
/// <returns></returns>
32+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, new
33+
{
34+
Invoke = Interop,
35+
OnlineStateChangedCallback = nameof(TriggerOnlineStateChanged),
36+
OnNetworkStateChangedCallback = nameof(TriggerNetworkStateChanged),
37+
Indicators
38+
});
39+
40+
/// <summary>
41+
/// JSInvoke 回调方法
42+
/// </summary>
43+
/// <returns></returns>
44+
[JSInvokable]
45+
public async Task TriggerOnlineStateChanged(bool online)
46+
{
47+
_state.IsOnline = online;
48+
if (OnNetworkStateChanged != null)
49+
{
50+
await OnNetworkStateChanged(_state);
51+
}
52+
}
53+
54+
/// <summary>
55+
/// JSInvoke 回调方法
56+
/// </summary>
57+
/// <returns></returns>
58+
[JSInvokable]
59+
public async Task TriggerNetworkStateChanged(NetworkMonitorState state)
60+
{
61+
// 网络状态变化回调方法
62+
_state = state;
63+
_state.IsOnline = true;
64+
if (OnNetworkStateChanged != null)
65+
{
66+
await OnNetworkStateChanged(_state);
67+
}
68+
}
69+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits IdComponentBase
3+
4+
<Popover Title="@Title" Trigger="@Trigger" Placement="@PopoverPlacement">
5+
<ChildContent>
6+
<span @attributes="@AdditionalAttributes" id="@Id" tabindex="0" class="@ClassString"></span>
7+
</ChildContent>
8+
<Template>
9+
<div class="bb-nt-main">
10+
<div class="bb-nt-item">
11+
<span>NetworkType:</span>
12+
<div>@_state.NetworkType</div>
13+
</div>
14+
<div class="bb-nt-item">
15+
<span>Downlink:</span>
16+
<div>@_state.Downlink Mbps</div>
17+
</div>
18+
<div class="bb-nt-item">
19+
<span>RTT:</span>
20+
<div>@_state.RTT ms</div>
21+
</div>
22+
</div>
23+
</Template>
24+
</Popover>
25+
26+
<NetworkMonitor OnNetworkStateChanged="OnNetworkStateChanged" Indicators="@_indicators"></NetworkMonitor>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.Extensions.Localization;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// Represents a network monitor indicator with customizable tooltip settings.
12+
/// </summary>
13+
/// <remarks>This component allows you to configure the text, placement, and trigger behavior of a tooltip that
14+
/// appears when interacting with the network monitor indicator. The tooltip can be customized to provide additional
15+
/// information to users.</remarks>
16+
public partial class NetworkMonitorIndicator
17+
{
18+
/// <summary>
19+
/// 获得/设置 Popover 弹窗标题 默认为 null
20+
/// </summary>
21+
[Parameter]
22+
public string? Title { get; set; }
23+
24+
/// <summary>
25+
/// 获得/设置 Popover 显示位置 默认为 Top
26+
/// </summary>
27+
[Parameter]
28+
public Placement PopoverPlacement { get; set; } = Placement.Top;
29+
30+
/// <summary>
31+
/// 获得/设置 Popover 触发方式 默认为 hover focus
32+
/// </summary>
33+
[Parameter]
34+
[NotNull]
35+
public string? Trigger { get; set; }
36+
37+
[Inject, NotNull]
38+
private IStringLocalizer<NetworkMonitorIndicator>? Localizer { get; set; }
39+
40+
private NetworkMonitorState _state = new();
41+
private readonly List<string> _indicators = [];
42+
private string _networkTypeString = "";
43+
private string _downlinkString = "";
44+
private string _rttString = "";
45+
46+
private string? ClassString => CssBuilder.Default("bb-nt-indicator")
47+
.AddClass("bb-nt-indicator-4g", _state.NetworkType == "4g")
48+
.AddClass("bb-nt-indicator-3g", _state.NetworkType == "3g")
49+
.AddClass("bb-nt-indicator-2g", _state.NetworkType == "2g")
50+
.AddClassFromAttributes(AdditionalAttributes)
51+
.Build();
52+
53+
/// <summary>
54+
/// <inheritdoc/>
55+
/// </summary>
56+
protected override void OnInitialized()
57+
{
58+
base.OnInitialized();
59+
60+
_indicators.Add(Id);
61+
}
62+
63+
/// <summary>
64+
/// <inheritdoc/>
65+
/// </summary>
66+
protected override void OnParametersSet()
67+
{
68+
base.OnParametersSet();
69+
70+
Trigger ??= "hover focus";
71+
Title ??= Localizer["Title"];
72+
_networkTypeString = Localizer["NetworkType"];
73+
_downlinkString = Localizer["Downlink"];
74+
_rttString = Localizer["RTT"];
75+
}
76+
77+
private Task OnNetworkStateChanged(NetworkMonitorState state)
78+
{
79+
_state = state;
80+
StateHasChanged();
81+
return Task.CompletedTask;
82+
}
83+
}

0 commit comments

Comments
 (0)