add ability to dynamically set module title and visible from components

This commit is contained in:
Shaun Walker 2022-08-12 13:05:48 -04:00
parent 469b436f10
commit 4cae3f02ed
5 changed files with 94 additions and 29 deletions

View File

@ -3,7 +3,6 @@
@inject NavigationManager NavigationManager
@inject IUserService UserService
@inject IServiceProvider ServiceProvider
@inject SiteState SiteState
@inject IStringLocalizer<Index> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

View File

@ -6,7 +6,6 @@
@inject ISiteService SiteService
@inject IStringLocalizer<Index> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer
@inject SiteState SiteState
@if (users == null)
{

View File

@ -24,6 +24,9 @@ namespace Oqtane.Modules
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[Inject]
protected SiteState SiteState { get; set; }
[CascadingParameter]
protected PageState PageState { get; set; }
@ -198,7 +201,7 @@ namespace Oqtane.Modules
return urlParameters;
}
// user feedback methods
// UI methods
public void AddModuleMessage(string message, MessageType type)
{
ModuleInstance.AddModuleMessage(message, type);
@ -219,6 +222,18 @@ namespace Oqtane.Modules
ModuleInstance.HideProgressIndicator();
}
public void SetModuleTitle(string title)
{
var obj = new { PageModuleId = ModuleState.PageModuleId, Title = title };
SiteState.Properties.ModuleTitle = obj;
}
public void SetModuleVisibility(bool visible)
{
var obj = new { PageModuleId = ModuleState.PageModuleId, Visible = visible };
SiteState.Properties.ModuleVisibility = obj;
}
// logging methods
public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args)
{

View File

@ -1,6 +1,8 @@
@using System.ComponentModel
@namespace Oqtane.Themes.Controls
@inherits ContainerBase
@attribute [OqtaneIgnore]
@inject SiteState SiteState
<span class="app-moduletitle">
<a id="@ModuleState.PageModuleId.ToString()">
@ -9,17 +11,39 @@
</span>
@code {
private string title = "";
private string title = "";
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(ModuleState.ControlTitle))
{
title = ModuleState.ControlTitle;
}
else
{
title = ModuleState.Title;
}
}
protected override void OnInitialized()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged;
}
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(ModuleState.ControlTitle))
{
title = ModuleState.ControlTitle;
}
else
{
title = ModuleState.Title;
}
}
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ModuleTitle")
{
if (SiteState.Properties.ModuleTitle.PageModuleId == ModuleState.PageModuleId)
{
title = SiteState.Properties.ModuleTitle.Title;
StateHasChanged();
}
}
}
public void Dispose()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;
}
}

View File

@ -1,20 +1,26 @@
@using System.ComponentModel
@namespace Oqtane.UI
@inject SiteState SiteState
<CascadingValue Value="@ModuleState">
@if (_useadminborder)
{
<div class="app-pane-admin-border">
@DynamicComponent
</div>
}
else
{
@DynamicComponent
}
</CascadingValue>
@if (_visible)
{
<CascadingValue Value="@ModuleState">
@if (_useadminborder)
{
<div class="app-pane-admin-border">
@DynamicComponent
</div>
}
else
{
@DynamicComponent
}
</CascadingValue>
}
@code {
private bool _useadminborder = false;
private bool _visible = true;
private bool _useadminborder = false;
[CascadingParameter]
protected PageState PageState { get; set; }
@ -24,7 +30,12 @@
RenderFragment DynamicComponent { get; set; }
protected override void OnParametersSet()
protected override void OnInitialized()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged;
}
protected override void OnParametersSet()
{
string container = ModuleState.ContainerType;
if (PageState.ModuleId != -1 && ModuleState.UseAdminContainer)
@ -53,4 +64,21 @@
builder.CloseComponent();
};
}
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ModuleVisibility")
{
if (SiteState.Properties.ModuleVisibility.PageModuleId == ModuleState.PageModuleId)
{
_visible = SiteState.Properties.ModuleVisibility.Visible;
StateHasChanged();
}
}
}
public void Dispose()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;
}
}