mirror of https://github.com/abpframework/abp.git
9 changed files with 208 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||
using System.Collections.ObjectModel; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Alerts |
|||
{ |
|||
public class AlertList : ObservableCollection<AlertMessage> |
|||
{ |
|||
public void Add(AlertType type, string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Add(new AlertMessage(type, text, title, dismissible)); |
|||
} |
|||
|
|||
public void Info(string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Add(new AlertMessage(AlertType.Info, text, title, dismissible)); |
|||
} |
|||
|
|||
public void Warning(string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Add(new AlertMessage(AlertType.Warning, text, title, dismissible)); |
|||
} |
|||
|
|||
public void Danger(string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Add(new AlertMessage(AlertType.Danger, text, title, dismissible)); |
|||
} |
|||
|
|||
public void Success(string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Add(new AlertMessage(AlertType.Success, text, title, dismissible)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Alerts |
|||
{ |
|||
public class AlertManager : IAlertManager, IScopedDependency |
|||
{ |
|||
public AlertList Alerts { get; } |
|||
|
|||
public AlertManager() |
|||
{ |
|||
Alerts = new AlertList(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Alerts |
|||
{ |
|||
public class AlertMessage |
|||
{ |
|||
[NotNull] |
|||
public string Text |
|||
{ |
|||
get => _text; |
|||
set => _text = Check.NotNullOrWhiteSpace(value, nameof(value)); |
|||
} |
|||
private string _text; |
|||
|
|||
public AlertType Type { get; set; } |
|||
|
|||
[CanBeNull] |
|||
public string Title { get; set; } |
|||
|
|||
public bool Dismissible { get; set; } |
|||
|
|||
public AlertMessage(AlertType type, [NotNull] string text, string title = null, bool dismissible = true) |
|||
{ |
|||
Type = type; |
|||
Text = Check.NotNullOrWhiteSpace(text, nameof(text)); |
|||
Title = title; |
|||
Dismissible = dismissible; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Alerts |
|||
{ |
|||
public enum AlertType |
|||
{ |
|||
Default, |
|||
Primary, |
|||
Secondary, |
|||
Success, |
|||
Danger, |
|||
Warning, |
|||
Info, |
|||
Light, |
|||
Dark |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Alerts |
|||
{ |
|||
public interface IAlertManager |
|||
{ |
|||
AlertList Alerts { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using Volo.Abp.AspNetCore.Components.WebAssembly.Alerts; |
|||
|
|||
namespace Volo.Abp.BlazoriseUI.Components |
|||
{ |
|||
internal class AlertWrapper |
|||
{ |
|||
public AlertMessage AlertMessage { get; set; } |
|||
public bool IsVisible { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
@foreach (var alert in Alerts) |
|||
{ |
|||
<Alert Visible="@alert.IsVisible" Dismisable="@alert.AlertMessage.Dismissible" Color="@GetAlertColor(alert.AlertMessage.Type)"> |
|||
@if (!string.IsNullOrEmpty(alert.AlertMessage.Title)) |
|||
{ |
|||
<Heading Size="HeadingSize.Is4" Class="alert-heading"> |
|||
@alert.AlertMessage.Title |
|||
@if (alert.AlertMessage.Dismissible) |
|||
{ |
|||
<CloseButton Clicked="() => alert.IsVisible = false" /> |
|||
} |
|||
</Heading> |
|||
} |
|||
<Paragraph>@alert.AlertMessage.Text</Paragraph> |
|||
</Alert> |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Specialized; |
|||
using System.Linq; |
|||
using Blazorise; |
|||
using Microsoft.AspNetCore.Components; |
|||
using Microsoft.AspNetCore.Components.Routing; |
|||
using Volo.Abp.AspNetCore.Components.WebAssembly.Alerts; |
|||
|
|||
namespace Volo.Abp.BlazoriseUI.Components |
|||
{ |
|||
public partial class PageAlert : ComponentBase, IDisposable |
|||
{ |
|||
private List<AlertWrapper> Alerts = new List<AlertWrapper>(); |
|||
|
|||
[Inject] |
|||
protected IAlertManager AlertManager { get; set; } |
|||
|
|||
[Inject] |
|||
protected NavigationManager NavigationManager { get; set; } |
|||
|
|||
protected override void OnInitialized() |
|||
{ |
|||
base.OnInitialized(); |
|||
NavigationManager.LocationChanged += NavigationManager_LocationChanged; |
|||
AlertManager.Alerts.CollectionChanged += Alerts_CollectionChanged; |
|||
|
|||
Alerts.AddRange(AlertManager.Alerts.Select(t => new AlertWrapper |
|||
{ |
|||
AlertMessage = t, |
|||
IsVisible = true |
|||
})); |
|||
} |
|||
|
|||
//Since Blazor WASM doesn't support scoped dependency, we need to clear alerts on each location changed event.
|
|||
private void NavigationManager_LocationChanged(object sender, LocationChangedEventArgs e) |
|||
{ |
|||
AlertManager.Alerts.Clear(); |
|||
Alerts.Clear(); |
|||
} |
|||
|
|||
private void Alerts_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
|||
{ |
|||
if (e.Action == NotifyCollectionChangedAction.Add) |
|||
{ |
|||
foreach (var item in e.NewItems) |
|||
{ |
|||
Alerts.Add(new AlertWrapper |
|||
{ |
|||
AlertMessage = (AspNetCore.Components.WebAssembly.Alerts.AlertMessage)item, |
|||
IsVisible = true |
|||
}); |
|||
} |
|||
} |
|||
StateHasChanged(); |
|||
} |
|||
|
|||
protected virtual Color GetAlertColor(AlertType alertType) |
|||
{ |
|||
var color = alertType switch |
|||
{ |
|||
AlertType.Info => Color.Info, |
|||
AlertType.Success => Color.Success, |
|||
AlertType.Warning => Color.Warning, |
|||
AlertType.Danger => Color.Danger, |
|||
AlertType.Dark => Color.Dark, |
|||
AlertType.Default => Color.None, |
|||
AlertType.Light => Color.Light, |
|||
AlertType.Primary => Color.Primary, |
|||
AlertType.Secondary => Color.Secondary, |
|||
_ => Color.None, |
|||
}; |
|||
|
|||
return color; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
NavigationManager.LocationChanged -= NavigationManager_LocationChanged; |
|||
AlertManager.Alerts.CollectionChanged -= Alerts_CollectionChanged; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue