Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Global Navigation Layer #708

Merged
merged 13 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions src/Masa.Stack.Components/Configs/GlobalConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

public class GlobalConfig : IScopedDependency
{
private const string DarkCookieKey = "GlobalConfig_IsDark";
private const string MiniCookieKey = "GlobalConfig_NavigationMini";
private const string FavoriteCookieKey = "GlobalConfig_Favorite";
private const string LangCookieKey = "GlobalConfig_Lang";
private const string DarkStoreKey = "GlobalConfig_IsDark";
private const string MiniStoreKey = "GlobalConfig_NavigationMini";
private const string FavoriteStoreKey = "GlobalConfig_Favorite";
private const string LangStoreKey = "GlobalConfig_Lang";
private const string NavLayerStoreKey = "GlobalConfig_NavLayer";

private readonly CookieStorage _cookieStorage;
private readonly LocalStorage _localStore;
private readonly I18n _i18N;
private bool _dark;
private bool _mini;
private string _favorite;
private Guid _currentTeamId;
private int _navLayer = 2;

public List<int> NavLayerItems = new List<int> { 1, 2, 3 };

public delegate void GlobalConfigChanged();

Expand All @@ -22,10 +26,14 @@ public class GlobalConfig : IScopedDependency

public event CurrentTeamChanged? OnCurrentTeamChanged;

public GlobalConfig(CookieStorage cookieStorage, I18n i18n)
public delegate Task NavLayerChanged();

public event NavLayerChanged? OnNavLayerChanged;
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

推荐用 EventHandler


public GlobalConfig(I18n i18n, LocalStorage localStore)
{
_cookieStorage = cookieStorage;
_i18N = i18n;
_localStore = localStore;
}

public CultureInfo Culture
Expand All @@ -34,7 +42,7 @@ public CultureInfo Culture
set
{
_i18N.SetCulture(new CultureInfo("en-US"), value);
_cookieStorage.SetAsync(LangCookieKey, value.Name);
_localStore.SetItemAsync(LangStoreKey, value.Name);
OnLanguageChanged?.Invoke();
}
}
Expand All @@ -61,7 +69,7 @@ public bool Dark
set
{
_dark = value;
_cookieStorage.SetAsync(DarkCookieKey, value);
_localStore.SetItemAsync(DarkStoreKey, value);
}
}

Expand All @@ -73,7 +81,7 @@ public bool Mini
set
{
_mini = value;
_cookieStorage?.SetAsync(MiniCookieKey, value);
_localStore?.SetItemAsync(MiniStoreKey, value);
}
}

Expand All @@ -83,20 +91,40 @@ public string Favorite
set
{
_favorite = value;
_cookieStorage?.SetAsync(FavoriteCookieKey, value);
_localStore?.SetItemAsync(FavoriteStoreKey, value);
}
}

public int NavLayer
{
get => _navLayer;
set
{
if (_navLayer != value)
{
_navLayer = value;
OnNavLayerChanged?.Invoke();
_localStore.SetItemAsync(NavLayerStoreKey, value);
}
}
}

public async void Initialization()
{
_dark = Convert.ToBoolean(await _cookieStorage.GetAsync(DarkCookieKey));
bool.TryParse(await _cookieStorage.GetAsync(MiniCookieKey), out _mini);
_favorite = await _cookieStorage.GetAsync(FavoriteCookieKey);
_dark = Convert.ToBoolean(await _localStore.GetItemAsync(DarkStoreKey));
bool.TryParse(await _localStore.GetItemAsync(MiniStoreKey), out _mini);
_favorite = await _localStore.GetItemAsync(FavoriteStoreKey) ?? string.Empty;

var lang = await _cookieStorage.GetAsync(LangCookieKey);
var lang = await _localStore.GetItemAsync(LangStoreKey);
if (!string.IsNullOrWhiteSpace(lang))
{
_i18N.SetCulture(new CultureInfo("en-US"), new CultureInfo(lang));
}

var navLayer = await _localStore.GetItemAsync<int>(NavLayerStoreKey);
if (navLayer > 0)
{
_navLayer = navLayer;
}
}
}
4 changes: 3 additions & 1 deletion src/Masa.Stack.Components/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,7 @@
},
"403Tips": "You are not authorized! ",
"404Tips": "The requested URL was not found.",
"BackToHome": "Back to home"
"BackToHome": "Back to home",
"NavigationLayerLabel": "Menu display",
"Layer": "layer"
}
4 changes: 3 additions & 1 deletion src/Masa.Stack.Components/Locales/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,7 @@
"Create": "Создать",
"403Tips": "У вас нет разрешения!",
"404Tips": "Не найден запрашиваемый URL.",
"BackToHome": "Вернуться на главную"
"BackToHome": "Вернуться на главную",
"NavigationLayerLabel": "Меню Показать",
"Layer": "Слой"
}
4 changes: 3 additions & 1 deletion src/Masa.Stack.Components/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,7 @@
"Create": "新建",
"403Tips": "您未经授权!",
"404Tips": "找不到请求的URL。",
"BackToHome": "回到主页"
"BackToHome": "回到主页",
"NavigationLayerLabel": "菜单显示",
"Layer": "层"
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@

@code {

private RenderFragment RenderNestNav(ExpansionMenu menu) => __builder =>
private RenderFragment RenderNestNav(ExpansionMenu menu, int layer = 1) => __builder =>
{
@RenderNav(menu)

if (GlobalConfig.NavLayer <= layer)
{
return;
}

if (menu.Children.Count != 0)
{
foreach (var child in menu.Children)
{
@RenderNestNav(child)
layer++;
@RenderNestNav(child, layer)
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ public partial class ExpansionAppWrapper
[Parameter]
public EventCallback<ExpansionMenu> OnItemOperClick { get; set; }

[Inject]
public GlobalConfig GlobalConfig { get; set; } = null!;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

GlobalConfig.OnNavLayerChanged += Changed;
}

private async Task ItemOperClick()
{
await OnItemOperClick.InvokeAsync(Value);
Expand Down Expand Up @@ -43,4 +53,15 @@ private static string GetClass(ExpansionMenu menu)

return string.Join(" ", css);
}

async Task Changed()
{
await InvokeAsync(StateHasChanged);
}

protected override ValueTask DisposeAsyncCore()
{
GlobalConfig.OnNavLayerChanged -= Changed;
return base.DisposeAsyncCore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<div class="d-flex">
<span class="global-nav-header__title">@T("GlobalNavigation")</span>
<MSpacer />
<div style="width: 140px;" Class="mr-6">
<SSelect @bind-Value="GlobalConfig.NavLayer" Items="GlobalConfig.NavLayerItems" ItemText="@(v => $"{v.ToWords()} {T("Layer")}")" ItemValue="v=> v" Label="@T("NavigationLayerLabel")" Small />
</div>

<SSearch Dense Class="mr-6" Value="@_search" ValueChanged="SearchChanged" />
<MButton Icon OnClick="() => _visible = false">
<MIcon>mdi-close</MIcon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public partial class GlobalNavigation : MasaComponentBase
[Parameter]
public Func<string, Task>? OnFavoriteRemove { get; set; }

[Inject]
public GlobalConfig GlobalConfig { get; set; } = null!;

private bool _visible;
private List<(string name, string url)>? _recentVisits;
private List<KeyValuePair<string, string>>? _recommendApps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ async Task Changed()
await InvokeAsync(StateHasChanged);
}

public async void Dispose()
protected override async ValueTask DisposeAsyncCore()
{
NoticeState.OnNoticeChanged -= Changed;
await HubConnection.DisposeAsync();
if (HubConnection != null)
{
await HubConnection.DisposeAsync();
}
await base.DisposeAsyncCore();
}
}