diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageService.cs index ff86c1c867a..fd70cc40e16 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiMessageService.cs @@ -1,17 +1,18 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace Volo.Abp.AspNetCore.Components.WebAssembly { public interface IUiMessageService { - Task InfoAsync(string message, string title = null, UiMessageOptions options = null); + Task InfoAsync(string message, string title = null, Action options = null); - Task SuccessAsync(string message, string title = null, UiMessageOptions options = null); + Task SuccessAsync(string message, string title = null, Action options = null); - Task WarnAsync(string message, string title = null, UiMessageOptions options = null); + Task WarnAsync(string message, string title = null, Action options = null); - Task ErrorAsync(string message, string title = null, UiMessageOptions options = null); + Task ErrorAsync(string message, string title = null, Action options = null); - Task ConfirmAsync(string message, string title = null, UiMessageOptions options = null); + Task ConfirmAsync(string message, string title = null, Action options = null); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiMessageService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiMessageService.cs index 0c7df5780d8..2a00d29ec5e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiMessageService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiMessageService.cs @@ -1,31 +1,32 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Components.WebAssembly { public class NullUiMessageService : IUiMessageService, ITransientDependency { - public Task InfoAsync(string message, string title = null, UiMessageOptions options = null) + public Task InfoAsync(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task SuccessAsync(string message, string title = null, UiMessageOptions options = null) + public Task SuccessAsync(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task WarnAsync(string message, string title = null, UiMessageOptions options = null) + public Task WarnAsync(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task ErrorAsync(string message, string title = null, UiMessageOptions options = null) + public Task ErrorAsync(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task ConfirmAsync(string message, string title = null, UiMessageOptions options = null) + public Task ConfirmAsync(string message, string title = null, Action options = null) { return Task.FromResult(true); } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs index 85b26bc4b6d..59b2e8f4000 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageOptions.cs @@ -6,9 +6,14 @@ public class UiMessageOptions { /// - /// If true, the message dialog will show the large icon for the current message type. + /// If true, the message dialogue will be centered on the screen. /// - public bool ShowMessageIcon { get; set; } = true; + public bool CenterMessage { get; set; } + + /// + /// If true, the message dialogue will show the large icon for the current message type. + /// + public bool ShowMessageIcon { get; set; } /// /// Overrides the build-in message icon. diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageService.cs index dfdc09f55d9..93970de348e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiMessageService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.JSInterop; using Volo.Abp.DependencyInjection; @@ -14,27 +15,27 @@ public UiMessageService(IJSRuntime jsRuntime) JsRuntime = jsRuntime; } - public async Task InfoAsync(string message, string title = null, UiMessageOptions options = null) + public async Task InfoAsync(string message, string title = null, Action options = null) { await JsRuntime.InvokeVoidAsync("alert", message); } - public async Task SuccessAsync(string message, string title = null, UiMessageOptions options = null) + public async Task SuccessAsync(string message, string title = null, Action options = null) { await JsRuntime.InvokeVoidAsync("alert", message); } - public async Task WarnAsync(string message, string title = null, UiMessageOptions options = null) + public async Task WarnAsync(string message, string title = null, Action options = null) { await JsRuntime.InvokeVoidAsync("alert", message); } - public async Task ErrorAsync(string message, string title = null, UiMessageOptions options = null) + public async Task ErrorAsync(string message, string title = null, Action options = null) { await JsRuntime.InvokeVoidAsync("alert", message); } - public async Task ConfirmAsync(string message, string title = null, UiMessageOptions options = null) + public async Task ConfirmAsync(string message, string title = null, Action options = null) { return await JsRuntime.InvokeAsync("confirm", message); } diff --git a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs index 70643069be7..f22ec3b6532 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiMessageService.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Localization.Resources.AbpUi; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.AspNetCore.Components.WebAssembly; @@ -9,44 +12,85 @@ namespace Volo.Abp.BlazoriseUI [Dependency(ReplaceServices = true)] public class BlazoriseUiMessageService : IUiMessageService, IScopedDependency { - private readonly UiMessageNotifierService uiMessageNotifierService; + /// + /// An event raised after the message is received. Used to notify the message dialog. + /// + public event EventHandler MessageReceived; + + private readonly IStringLocalizer localizer; public ILogger Logger { get; set; } - public BlazoriseUiMessageService(UiMessageNotifierService uiMessageNotifierService) + public BlazoriseUiMessageService( + IStringLocalizer localizer) { - this.uiMessageNotifierService = uiMessageNotifierService; + this.localizer = localizer; Logger = NullLogger.Instance; } - public Task InfoAsync(string message, string title = null, UiMessageOptions options = null) + public Task InfoAsync(string message, string title = null, Action options = null) { - return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Info, message, title, options); + var uiMessageOptions = CreateDefaultOptions(); + options?.Invoke(uiMessageOptions); + + MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Info, message, title, uiMessageOptions)); + + return Task.CompletedTask; } - public Task SuccessAsync(string message, string title = null, UiMessageOptions options = null) + public Task SuccessAsync(string message, string title = null, Action options = null) { - return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Success, message, title, options); + var uiMessageOptions = CreateDefaultOptions(); + options?.Invoke(uiMessageOptions); + + MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Success, message, title, uiMessageOptions)); + + return Task.CompletedTask; } - public Task WarnAsync(string message, string title = null, UiMessageOptions options = null) + public Task WarnAsync(string message, string title = null, Action options = null) { - return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Warning, message, title, options); + var uiMessageOptions = CreateDefaultOptions(); + options?.Invoke(uiMessageOptions); + + MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Warning, message, title, uiMessageOptions)); + + return Task.CompletedTask; } - public Task ErrorAsync(string message, string title = null, UiMessageOptions options = null) + public Task ErrorAsync(string message, string title = null, Action options = null) { - return uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Error, message, title, options); + var uiMessageOptions = CreateDefaultOptions(); + options?.Invoke(uiMessageOptions); + + MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Error, message, title, uiMessageOptions)); + + return Task.CompletedTask; } - public async Task ConfirmAsync(string message, string title = null, UiMessageOptions options = null) + public async Task ConfirmAsync(string message, string title = null, Action options = null) { + var uiMessageOptions = CreateDefaultOptions(); + options?.Invoke(uiMessageOptions); + var callback = new TaskCompletionSource(); - await uiMessageNotifierService.NotifyMessageReceivedAsync(UiMessageType.Confirmation, message, title, options, callback); + MessageReceived?.Invoke(this, new UiMessageEventArgs(UiMessageType.Confirmation, message, title, uiMessageOptions)); return await callback.Task; } + + protected virtual UiMessageOptions CreateDefaultOptions() + { + return new UiMessageOptions + { + CenterMessage = true, + ShowMessageIcon = true, + OkButtonText = localizer["Ok"], + CancelButtonText = localizer["Cancel"], + ConfirmButtonText = localizer["Yes"], + }; + } } } diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor index 6caf37c0b98..cd8fd5cadac 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor +++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor @@ -1,6 +1,6 @@  - + @Title diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs index 7dada11bd33..59e4aa38202 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiMessageAlert.razor.cs @@ -9,68 +9,14 @@ namespace Volo.Abp.BlazoriseUI.Components { public partial class UiMessageAlert : ComponentBase, IDisposable { - protected override void OnInitialized() - { - UiMessageNotifierService.MessageReceived += OnMessageReceived; - - base.OnInitialized(); - } - - private void OnMessageReceived(object sender, UiMessageEventArgs e) - { - MessageType = e.MessageType; - Message = e.Message; - Title = e.Title; - Options = e.Options; - Callback = e.Callback; - - ModalRef.Show(); - } - - public void Dispose() - { - if (UiMessageNotifierService != null) - { - UiMessageNotifierService.MessageReceived -= OnMessageReceived; - } - } - - protected Task OnOkClicked() - { - ModalRef.Hide(); - - return Okayed.InvokeAsync(null); - } - - protected Task OnConfirmClicked() - { - ModalRef.Hide(); - - if (IsConfirmation && Callback != null) - { - Callback.SetResult(true); - } - - return Confirmed.InvokeAsync(null); - } - - protected Task OnCancelClicked() - { - ModalRef.Hide(); - - if (IsConfirmation && Callback != null) - { - Callback.SetResult(false); - } - - return Canceled.InvokeAsync(null); - } - protected Modal ModalRef { get; set; } protected virtual bool IsConfirmation => MessageType == UiMessageType.Confirmation; + protected virtual bool CenterMessage + => Options?.CenterMessage ?? true; + protected virtual bool ShowMessageIcon => Options?.ShowMessageIcon ?? true; @@ -126,12 +72,69 @@ protected virtual string CancelButtonText [Parameter] public UiMessageOptions Options { get; set; } - [Parameter] public EventCallback Okayed { get; set; } // TODO: ? + [Parameter] public EventCallback Okayed { get; set; } [Parameter] public EventCallback Confirmed { get; set; } [Parameter] public EventCallback Canceled { get; set; } - [Inject] protected UiMessageNotifierService UiMessageNotifierService { get; set; } + [Inject] protected BlazoriseUiMessageService UiMessageService { get; set; } + + protected override void OnInitialized() + { + base.OnInitialized(); + + UiMessageService.MessageReceived += OnMessageReceived; + } + + private void OnMessageReceived(object sender, UiMessageEventArgs e) + { + MessageType = e.MessageType; + Message = e.Message; + Title = e.Title; + Options = e.Options; + Callback = e.Callback; + + ModalRef.Show(); + } + + public void Dispose() + { + if (UiMessageService != null) + { + UiMessageService.MessageReceived -= OnMessageReceived; + } + } + + protected Task OnOkClicked() + { + ModalRef.Hide(); + + return Okayed.InvokeAsync(null); + } + + protected Task OnConfirmClicked() + { + ModalRef.Hide(); + + if (IsConfirmation && Callback != null) + { + Callback.SetResult(true); + } + + return Confirmed.InvokeAsync(null); + } + + protected Task OnCancelClicked() + { + ModalRef.Hide(); + + if (IsConfirmation && Callback != null) + { + Callback.SetResult(false); + } + + return Canceled.InvokeAsync(null); + } } } diff --git a/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs b/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs deleted file mode 100644 index af49f993b5a..00000000000 --- a/framework/src/Volo.Abp.BlazoriseUI/UiMessageNotifierService.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Threading.Tasks; -using Volo.Abp.AspNetCore.Components.WebAssembly; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.BlazoriseUI -{ - [Dependency(ReplaceServices = true)] - public class UiMessageNotifierService : IScopedDependency - { - public event EventHandler MessageReceived; - - public Task NotifyMessageReceivedAsync(UiMessageType messageType, string message, string title, UiMessageOptions options, TaskCompletionSource callback = null) - { - MessageReceived?.Invoke(this, new UiMessageEventArgs(messageType, message, title, options, callback)); - - return Task.CompletedTask; - } - } -} diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs index 7ac66826597..0724ee6c979 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Pages/Index.razor.cs @@ -23,10 +23,10 @@ protected override async Task OnInitializedAsync() Task OnInfoTestClicked() { - return UiMessageService.InfoAsync( "This is the Info message", "Info", new UiMessageOptions + return UiMessageService.InfoAsync( "This is the Info message", "Info", options => { - OkButtonIcon = IconName.InfoCircle, - OkButtonText = "Hello info" + options.OkButtonIcon = IconName.InfoCircle; + options.OkButtonText = "Hello info"; } ); } @@ -47,7 +47,11 @@ Task OnErrorTestClicked() Task OnConfirmTestClicked() { - return UiMessageService.ConfirmAsync( "This is the Confirm message", "Confirm" ) + return UiMessageService.ConfirmAsync( "Are you sure you want to delete the item?", "Confirm", options => + { + options.CancelButtonText = "Do not delete it"; + options.ConfirmButtonText = "Yes I'm sure"; + } ) .ContinueWith( result => { if ( result.Result )