diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 9e50e7e8394763cffcaf5a1a8ed98a5cd5692dae..7e65f29a248b23bd5f1d8370ee8c1f1432a41e43 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.11.2 + 6.11.3 diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapModuleComponentBase.cs b/src/BootstrapBlazor/Components/BaseComponents/BootstrapModuleComponentBase.cs index 1bf4ee11180e6a403476c361c83d59b1f9d6bd3d..cf335f0e666f784075a5e5a0d538fe7315d42220 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapModuleComponentBase.cs +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapModuleComponentBase.cs @@ -70,6 +70,10 @@ public abstract class BootstrapModuleComponentBase : IdComponentBase, IAsyncDisp { await ModuleInitAsync(); } + else + { + await ModuleExecuteAsync(); + } } /// diff --git a/src/BootstrapBlazor/Components/Menu/Menu.razor.cs b/src/BootstrapBlazor/Components/Menu/Menu.razor.cs index 5bd6411ca3e774dca7347e43614078f7102cec9a..c66f6c581954d416d5f00d166945350a7f60b501 100644 --- a/src/BootstrapBlazor/Components/Menu/Menu.razor.cs +++ b/src/BootstrapBlazor/Components/Menu/Menu.razor.cs @@ -151,22 +151,14 @@ public partial class Menu /// /// /// - /// /// - protected override async Task ModuleInvokeVoidAsync(bool firstRender) + protected override async Task ModuleExecuteAsync() { - if (IsVertical && Module != null) + if (IsVertical && InvokeJSInterop && Module != null) { - if (firstRender) - { - await base.ModuleInvokeVoidAsync(firstRender); - } - else if (InvokeJSInterop) - { - await Module.InvokeVoidAsync("Menu.reset", Id); - } + InvokeJSInterop = false; + await Module.InvokeVoidAsync($"{ModuleName}.execute", Id); } - InvokeJSInterop = false; } private void InitMenus(MenuItem? parent, IEnumerable menus, string url) diff --git a/src/BootstrapBlazor/Utils/JSModule.cs b/src/BootstrapBlazor/Utils/JSModule.cs index 318cb929d74991034d460b4d923eab779707604f..2c43cd4d2c4b0ab83a917f628727706f9193a1ff 100644 --- a/src/BootstrapBlazor/Utils/JSModule.cs +++ b/src/BootstrapBlazor/Utils/JSModule.cs @@ -24,49 +24,40 @@ public class JSModule : IAsyncDisposable Module = jSObjectReference ?? throw new ArgumentNullException(nameof(jSObjectReference)); } - #region Element InvokeVoidAsync /// /// InvokeVoidAsync 方法 /// /// - /// /// /// - public virtual ValueTask InvokeVoidAsync(string identifier, ElementReference element, params object?[]? args) => InvokeVoidAsync(identifier, element, CancellationToken.None, args); + public virtual ValueTask InvokeVoidAsync(string identifier, params object?[]? args) => InvokeVoidAsync(identifier, CancellationToken.None, args); /// /// InvokeVoidAsync 方法 /// /// - /// /// /// /// - public virtual ValueTask InvokeVoidAsync(string identifier, ElementReference element, TimeSpan timeout = default, params object?[]? args) + public virtual ValueTask InvokeVoidAsync(string identifier, TimeSpan timeout = default, params object?[]? args) { var cancellationTokenSource = new CancellationTokenSource(timeout); - return InvokeVoidAsync(identifier, element, cancellationTokenSource.Token, args); + return InvokeVoidAsync(identifier, cancellationTokenSource.Token, args); } /// /// InvokeVoidAsync 方法 /// /// - /// /// /// /// - public virtual async ValueTask InvokeVoidAsync(string identifier, ElementReference element, CancellationToken cancellationToken = default, params object?[]? args) + public virtual async ValueTask InvokeVoidAsync(string identifier, CancellationToken cancellationToken = default, params object?[]? args) { -#if NET5_0 - var paras = new List(); -#else var paras = new List(); -#endif - paras.Add(element); if (args != null) { - paras.AddRange(args!); + paras.AddRange(args); } await InvokeVoidAsync(); @@ -86,185 +77,38 @@ public class JSModule : IAsyncDisposable catch (TaskCanceledException) { } } } - #endregion - #region Element InvokeAsync /// /// InvokeAsync 方法 /// /// - /// /// /// - public virtual ValueTask InvokeAsync(string identifier, ElementReference element, params object?[]? args) => InvokeAsync(identifier, element, CancellationToken.None, args); + public virtual ValueTask InvokeAsync(string identifier, params object?[]? args) => InvokeAsync(identifier, CancellationToken.None, args); /// /// InvokeAsync 方法 /// /// - /// /// /// /// - public virtual ValueTask InvokeAsync(string identifier, ElementReference element, TimeSpan timeout = default, params object?[]? args) + public virtual ValueTask InvokeAsync(string identifier, TimeSpan timeout = default, params object?[]? args) { var cancellationTokenSource = new CancellationTokenSource(timeout); - return InvokeAsync(identifier, element, cancellationTokenSource.Token, args); + return InvokeAsync(identifier, cancellationTokenSource.Token, args); } /// /// InvokeAsync 方法 /// /// - /// - /// - /// - /// - public virtual async ValueTask InvokeAsync(string identifier, ElementReference element, CancellationToken cancellationToken = default, params object?[]? args) - { -#if NET5_0 - var paras = new List(); -#else - var paras = new List(); -#endif - paras.Add(element); - if (args != null) - { - paras.AddRange(args!); - } - return await InvokeAsync(); - - [ExcludeFromCodeCoverage] - async ValueTask InvokeAsync() - { - TValue ret = default!; - try - { - ret = await Module.InvokeAsync(identifier, cancellationToken, paras.ToArray()); - } -#if NET6_0_OR_GREATER - catch (JSDisconnectedException) { } -#endif - catch (JSException) { } - catch (AggregateException) { } - catch (InvalidOperationException) { } - catch (TaskCanceledException) { } - - return ret; - } - } - #endregion - - #region Id InvokeAsync - /// - /// InvokeVoidAsync 方法 - /// - /// - /// - /// - /// - public virtual ValueTask InvokeVoidAsync(string identifier, string? id, params object?[]? args) => InvokeVoidAsync(identifier, id, CancellationToken.None, args); - - /// - /// InvokeVoidAsync 方法 - /// - /// - /// - /// - /// - /// - public virtual ValueTask InvokeVoidAsync(string identifier, string? id, TimeSpan timeout = default, params object?[]? args) - { - var cancellationTokenSource = new CancellationTokenSource(timeout); - return InvokeVoidAsync(identifier, id, cancellationTokenSource.Token, args); - } - - /// - /// InvokeVoidAsync 方法 - /// - /// - /// /// /// /// - public virtual async ValueTask InvokeVoidAsync(string identifier, string? id, CancellationToken cancellationToken = default, params object?[]? args) + public virtual async ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken = default, params object?[]? args) { -#if NET5_0 - var paras = new List(); -#else var paras = new List(); -#endif - if (!string.IsNullOrEmpty(id)) - { - paras.Add($"#{id}"); - } - if (args != null) - { - paras.AddRange(args!); - } - await InvokeVoidAsync(); - - [ExcludeFromCodeCoverage] - async ValueTask InvokeVoidAsync() - { - try - { - await Module.InvokeVoidAsync(identifier, cancellationToken, paras.ToArray()); - } -#if NET6_0_OR_GREATER - catch (JSDisconnectedException) { } -#endif - catch (JSException) { } - catch (AggregateException) { } - catch (InvalidOperationException) { } - catch (TaskCanceledException) { } - } - } - #endregion - - #region Id InvokeAsync - /// - /// InvokeVoidAsync 方法 - /// - /// - /// - /// - /// - public virtual ValueTask InvokeAsync(string identifier, string? id, params object?[]? args) => InvokeAsync(identifier, id, CancellationToken.None, args); - - /// - /// Id InvokeAsync 方法 - /// - /// - /// - /// - /// - /// - public virtual ValueTask InvokeAsync(string identifier, string? id, TimeSpan timeout = default, params object?[]? args) - { - var cancellationTokenSource = new CancellationTokenSource(timeout); - return InvokeAsync(identifier, id, cancellationTokenSource.Token, args); - } - - /// - /// InvokeAsync 方法 - /// - /// - /// - /// - /// - /// - public virtual async ValueTask InvokeAsync(string identifier, string? id, CancellationToken cancellationToken = default, params object?[]? args) - { -#if NET5_0 - var paras = new List(); -#else - var paras = new List(); -#endif - if (!string.IsNullOrEmpty(id)) - { - paras.Add($"#{id}"); - } if (args != null) { paras.AddRange(args!); @@ -290,7 +134,6 @@ public class JSModule : IAsyncDisposable return ret; } } - #endregion /// /// Dispose 方法 diff --git a/src/BootstrapBlazor/Utils/JSModuleOfType.cs b/src/BootstrapBlazor/Utils/JSModuleOfType.cs index a6c3181e1505f403ab03ec444d61950412397992..83eaf20160e97faddb1403b38bb2d450b96b580e 100644 --- a/src/BootstrapBlazor/Utils/JSModuleOfType.cs +++ b/src/BootstrapBlazor/Utils/JSModuleOfType.cs @@ -29,101 +29,24 @@ public class JSModule : JSModule where TCom : class /// InvokeVoidAsync 方法 /// /// - /// /// /// /// - public override async ValueTask InvokeVoidAsync(string identifier, ElementReference element, CancellationToken cancellationToken = default, params object?[]? args) + public override async ValueTask InvokeVoidAsync(string identifier, CancellationToken cancellationToken = default, params object?[]? args) { -#if NET5_0 - var paras = new List(); -#else var paras = new List(); -#endif - paras.Add(element); - paras.Add(DotNetReference); if (args != null) { - paras.AddRange(args!); - } - await InvokeVoidAsync(); - - [ExcludeFromCodeCoverage] - async ValueTask InvokeVoidAsync() - { - try + if (args.Length > 0) { - await Module.InvokeVoidAsync(identifier, cancellationToken, paras.ToArray()); + paras.Add(args[0]); } -#if NET6_0_OR_GREATER - catch (JSDisconnectedException) { } -#endif - catch (JSException) { } - catch (AggregateException) { } - catch (InvalidOperationException) { } - catch (TaskCanceledException) { } - } - } - - /// - /// - /// - public override async ValueTask InvokeAsync(string identifier, ElementReference element, CancellationToken cancellationToken = default, params object?[]? args) - { -#if NET5_0 - var paras = new List(); -#else - var paras = new List(); -#endif - paras.Add(element); - paras.Add(DotNetReference); - if (args != null) - { - paras.AddRange(args!); - } - - return await InvokeAsync(); - - [ExcludeFromCodeCoverage] - async ValueTask InvokeAsync() - { - TValue ret = default!; - try + paras.Add(DotNetReference); + if (args.Length > 1) { - ret = await Module.InvokeAsync(identifier, cancellationToken, paras.ToArray()); + paras.AddRange(args.Skip(1).Take(args.Length - 1)); } -#if NET6_0_OR_GREATER - catch (JSDisconnectedException) { } -#endif - catch (JSException) { } - catch (AggregateException) { } - catch (InvalidOperationException) { } - catch (TaskCanceledException) { } - - return ret; - } - } - - /// - /// - /// - public override async ValueTask InvokeVoidAsync(string identifier, string? id, CancellationToken cancellationToken = default, params object?[]? args) - { -#if NET5_0 - var paras = new List(); -#else - var paras = new List(); -#endif - if (!string.IsNullOrEmpty(id)) - { - paras.Add($"#{id}"); } - paras.Add(DotNetReference); - if (args != null) - { - paras.AddRange(args!); - } - await InvokeVoidAsync(); [ExcludeFromCodeCoverage] @@ -146,23 +69,21 @@ public class JSModule : JSModule where TCom : class /// /// /// - public override async ValueTask InvokeAsync(string identifier, string? id, CancellationToken cancellationToken = default, params object?[]? args) + public override async ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken = default, params object?[]? args) { -#if NET5_0 - var paras = new List(); -#else var paras = new List(); -#endif - if (!string.IsNullOrEmpty(id)) - { - paras.Add($"#{id}"); - } - paras.Add(DotNetReference); if (args != null) { - paras.AddRange(args!); + if (args.Length > 0) + { + paras.Add(args[0]); + } + paras.Add(DotNetReference); + if (args.Length > 1) + { + paras.AddRange(args.Skip(1).Take(args.Length - 1)); + } } - return await InvokeAsync(); [ExcludeFromCodeCoverage] diff --git a/src/BootstrapBlazor/wwwroot/modules/base/blazor-component.js b/src/BootstrapBlazor/wwwroot/modules/base/blazor-component.js index 4dcab527d3ddf0c5e2a8ba1f954a90c0e4ecd446..332801acebf21ae49e0f2a2852c7775af933ada6 100644 --- a/src/BootstrapBlazor/wwwroot/modules/base/blazor-component.js +++ b/src/BootstrapBlazor/wwwroot/modules/base/blazor-component.js @@ -1,6 +1,14 @@ import BaseComponent from "./base-component.js" import { getElement } from "./index.js" +const getElementById = object => { + if (typeof object === 'string' && object.length > 0) { + object= `#${object}` + } + + return getElement(object); +} + class BlazorComponent extends BaseComponent { constructor(element, config) { super(element, config) @@ -24,6 +32,7 @@ class BlazorComponent extends BaseComponent { } static dispose(element) { + element = getElementById(element) const component = this.getInstance(element) if (component) { component.dispose() @@ -31,14 +40,14 @@ class BlazorComponent extends BaseComponent { } static init(element) { - element = getElement(element) + element = getElementById(element) if (element) { new this(element, { arguments: [].slice.call(arguments, 1) }) } } static execute(element) { - element = getElement(element) + element = getElementById(element) if (element) { var instance = this.getInstance(element) instance._execute([].slice.call(arguments, 1)) diff --git a/src/BootstrapBlazor/wwwroot/modules/menu.js b/src/BootstrapBlazor/wwwroot/modules/menu.js index 94677b31129ba6bd449367a23d5eb6c38122d1d8..3a7009e95ce662d3969a2fc46e7be861a2c29a25 100644 --- a/src/BootstrapBlazor/wwwroot/modules/menu.js +++ b/src/BootstrapBlazor/wwwroot/modules/menu.js @@ -30,7 +30,7 @@ class Menu extends BlazorComponent { } } - _reset() { + _execute() { const expandAll = this._element.getAttribute('data-bb-expand') === 'true' this._collapses.forEach(el => { const target = getTargetElement(el) @@ -79,15 +79,6 @@ class Menu extends BlazorComponent { } }) } - - static reset(element) { - const menu = this.getInstance(element) - menu._reset() - } - - static get NAME() { - return 'menu' - } } export { diff --git a/src/BootstrapBlazor/wwwroot/modules/upload.js b/src/BootstrapBlazor/wwwroot/modules/upload.js index f01de4d2302d37105ef4ae9f59c40733778b7719..1c670e192bf625b1a136d4979ec79b2e9eff93a4 100644 --- a/src/BootstrapBlazor/wwwroot/modules/upload.js +++ b/src/BootstrapBlazor/wwwroot/modules/upload.js @@ -64,10 +64,6 @@ class Upload extends BlazorComponent { EventHandler.off(this._element, 'drop'); EventHandler.off(this._element, 'paste'); } - - static get NAME() { - return 'upload' - } } export { diff --git a/test/UnitTest/Utils/JSModuleTest.cs b/test/UnitTest/Utils/JSModuleTest.cs index 3d974479ea3482b44ec61540beecae3979ea49c2..a00237a3b9e19a31b35c704b791dc27204ad7aac 100644 --- a/test/UnitTest/Utils/JSModuleTest.cs +++ b/test/UnitTest/Utils/JSModuleTest.cs @@ -25,11 +25,11 @@ public class JSModuleTest var js = new MockJSObjectReference(); var module = new JSModule(js); await module.InvokeVoidAsync("Test.init", "bb_id"); - await module.InvokeVoidAsync("Test.init", "bb_id", TimeSpan.Zero); - await module.InvokeVoidAsync("Test.init", "bb_id", CancellationToken.None); + await module.InvokeVoidAsync("Test.init", TimeSpan.Zero, "bb_id"); + await module.InvokeVoidAsync("Test.init", CancellationToken.None, "bb_id"); await module.InvokeAsync("Test.init", "bb_id"); - await module.InvokeAsync("Test.init", "bb_id", TimeSpan.Zero); - await module.InvokeAsync("Test.init", "bb_id", CancellationToken.None); + await module.InvokeAsync("Test.init", TimeSpan.Zero, "bb_id"); + await module.InvokeAsync("Test.init", CancellationToken.None, "bb_id"); } [Fact] @@ -56,11 +56,11 @@ public class JSModuleTest Assert.NotNull(module); await module.InvokeVoidAsync("Test.init", "bb_id"); - await module.InvokeVoidAsync("Test.init", "bb_id", TimeSpan.Zero); - await module.InvokeVoidAsync("Test.init", "bb_id", CancellationToken.None); + await module.InvokeVoidAsync("Test.init", TimeSpan.Zero, "bb_id"); + await module.InvokeVoidAsync("Test.init", CancellationToken.None, "bb_id"); await module.InvokeAsync("Test.init", "bb_id"); - await module.InvokeAsync("Test.init", "bb_id", TimeSpan.Zero); - await module.InvokeAsync("Test.init", "bb_id", CancellationToken.None); + await module.InvokeAsync("Test.init", TimeSpan.Zero, "bb_id"); + await module.InvokeAsync("Test.init", CancellationToken.None, "bb_id"); await module.DisposeAsync(); }