From 797695ceb438d0ca8b9db07f74904e46f96e3a1b Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 4 Mar 2020 20:05:28 +0800 Subject: [PATCH] Allow replace & delete BundleContributor. --- .../Bundling/BundleContributorCollection.cs | 47 +++++++++++++++++++ .../BootstrapDatepickerScriptContributor.cs | 7 ++- .../JQueryValidationScriptContributor.cs | 9 +++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs index 8389f7b9ed..42a0cd9c5f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs @@ -39,6 +39,43 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling AddWithDependencies(contributorType); } + public void Replace(bool includeDependencies = false) + where TSourceContributor : IBundleContributor, new() + where TDestContributorType : IBundleContributor, new() + { + Replace(typeof(TSourceContributor), typeof(TDestContributorType), includeDependencies); + } + + public void Replace([NotNull] Type sourceContributorType, [NotNull] Type destContributorType, bool includeDependencies = false) + { + Check.NotNull(sourceContributorType, nameof(sourceContributorType)); + Check.NotNull(destContributorType, nameof(destContributorType)); + + if (!includeDependencies) + { + _contributors.ReplaceOne(x => x.GetType() == sourceContributorType, + contributor => (IBundleContributor) Activator.CreateInstance(destContributorType)); + } + else + { + RemoveWithDependencies(sourceContributorType); + Add(destContributorType); + } + } + + public void Remove(bool includeDependencies = false) + where TContributor : IBundleContributor, new() + { + if (!includeDependencies) + { + _contributors.RemoveAll(x => x.GetType() == typeof(TContributor)); + } + else + { + RemoveWithDependencies(typeof(TContributor)); + } + } + public IReadOnlyList GetAll() { return _contributors.ToImmutableList(); @@ -58,6 +95,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling AddInstanceToContributors(contributorType); } + + private void RemoveWithDependencies(Type contributorType) + { + foreach (var dependedType in GetDirectDependencies(contributorType)) + { + RemoveWithDependencies(dependedType); //Recursive call + } + + _contributors.RemoveAll(x => x.GetType() == contributorType); + } private IEnumerable GetDirectDependencies(Type contributorType) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/BootstrapDatepicker/BootstrapDatepickerScriptContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/BootstrapDatepicker/BootstrapDatepickerScriptContributor.cs index 8de698c723..7e21c8cef5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/BootstrapDatepicker/BootstrapDatepickerScriptContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/BootstrapDatepicker/BootstrapDatepickerScriptContributor.cs @@ -21,7 +21,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.Timeago ? "en" : CultureInfo.CurrentUICulture.Name; - if (TryAddCultureFile(context, cultureName)) + if (TryAddCultureFile(context, MapCultureName(cultureName))) { return; } @@ -39,5 +39,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.Timeago context.Files.AddIfNotContains(filePath); return true; } + + protected virtual string MapCultureName(string cultureName) + { + return cultureName; + } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs index 2ac0d58acf..b4acf05ec6 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs @@ -25,7 +25,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation var cultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Replace('-', '_'); - if (TryAddCultureFile(context, cultureName)) + if (TryAddCultureFile(context, MapCultureName(cultureName))) { return; } @@ -35,7 +35,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation return; } - TryAddCultureFile(context, cultureName.Substring(0, cultureName.IndexOf('_'))); + TryAddCultureFile(context, MapCultureName(cultureName.Substring(0, cultureName.IndexOf('_')))); } protected virtual bool TryAddCultureFile(BundleConfigurationContext context, string cultureName) @@ -51,5 +51,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation context.Files.AddIfNotContains(filePath); return true; } + + protected virtual string MapCultureName(string cultureName) + { + return cultureName; + } } }