From 4eb116d4ac22eb7de16e47d64ffe6f0b384b0faf Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 2 Apr 2020 21:29:48 +1100 Subject: [PATCH] removes CssInitialization and JsInitialization and merges/simplifies code --- ....cs => BackOfficeJavaScriptInitializer.cs} | 15 +++----- .../WebAssets/BackOfficeWebAssets.cs | 12 ++++++- .../WebAssets/CssInitialization.cs | 36 ------------------- .../WebAssets/JsInitialization.cs | 32 ----------------- .../WebAssets/RuntimeMinifierExtensions.cs | 23 +++++++++--- .../JsInitializationTests.cs | 9 +---- src/Umbraco.Web/Editors/PreviewController.cs | 2 +- 7 files changed, 37 insertions(+), 92 deletions(-) rename src/Umbraco.Infrastructure/WebAssets/{JavaScriptHelper.cs => BackOfficeJavaScriptInitializer.cs} (90%) delete mode 100644 src/Umbraco.Infrastructure/WebAssets/CssInitialization.cs delete mode 100644 src/Umbraco.Infrastructure/WebAssets/JsInitialization.cs diff --git a/src/Umbraco.Infrastructure/WebAssets/JavaScriptHelper.cs b/src/Umbraco.Infrastructure/WebAssets/BackOfficeJavaScriptInitializer.cs similarity index 90% rename from src/Umbraco.Infrastructure/WebAssets/JavaScriptHelper.cs rename to src/Umbraco.Infrastructure/WebAssets/BackOfficeJavaScriptInitializer.cs index 833d177ea7..64de289549 100644 --- a/src/Umbraco.Infrastructure/WebAssets/JavaScriptHelper.cs +++ b/src/Umbraco.Infrastructure/WebAssets/BackOfficeJavaScriptInitializer.cs @@ -1,20 +1,15 @@ using System.Collections.Generic; -using System.Linq; using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using Umbraco.Core.Configuration; -using Umbraco.Core.Hosting; using Umbraco.Core.IO; -using Umbraco.Core.WebAssets; -using Umbraco.Infrastructure.WebAssets; -namespace Umbraco.Web.JavaScript +namespace Umbraco.Web.WebAssets { - // TODO: Rename this - public class JavaScriptHelper + /// + /// Creates a JavaScript block to initialize the back office + /// + public class BackOfficeJavaScriptInitializer { // deal with javascript functions inside of json (not a supported json syntax) private const string PrefixJavaScriptObject = "@@@@"; diff --git a/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs b/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs index 04f725bd5d..e2364074fb 100644 --- a/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs +++ b/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs @@ -83,7 +83,7 @@ namespace Umbraco.Web.WebAssets /// private string[] GetScriptsForBackoffice(IEnumerable propertyEditorScripts) { - var umbracoInit = JsInitialization.GetDefaultInitialization(); + var umbracoInit = GetInitBackOfficeScripts(); var scripts = new HashSet(); foreach (var script in umbracoInit) scripts.Add(script); @@ -95,6 +95,16 @@ namespace Umbraco.Web.WebAssets return new HashSet(FormatPaths(scripts)).ToArray(); } + /// + /// Returns the list of scripts for back office initialization + /// + /// + private IEnumerable GetInitBackOfficeScripts() + { + var resources = JsonConvert.DeserializeObject(Resources.JsInitialize); + return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); + } + /// /// Returns stylesheets used to load the back office /// diff --git a/src/Umbraco.Infrastructure/WebAssets/CssInitialization.cs b/src/Umbraco.Infrastructure/WebAssets/CssInitialization.cs deleted file mode 100644 index 1b1d492332..0000000000 --- a/src/Umbraco.Infrastructure/WebAssets/CssInitialization.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Umbraco.Core.WebAssets; - -namespace Umbraco.Web.JavaScript -{ - public class CssInitialization - { - private readonly IRuntimeMinifier _runtimeMinifier; - - public CssInitialization(IRuntimeMinifier runtimeMinifier) - { - _runtimeMinifier = runtimeMinifier; - } - - /// - /// Processes all found manifest files, and outputs css inject calls for all css files found in all manifests. - /// - public async Task GetStylesheetInitializationAsync() - { - var files = await _runtimeMinifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoCssBundleName); - return WriteScript(files); - } - - internal static string WriteScript(IEnumerable files) - { - var sb = new StringBuilder(); - foreach (var file in files) - sb.AppendFormat("{0}LazyLoad.css('{1}');", Environment.NewLine, file); - return sb.ToString(); - } - - } -} diff --git a/src/Umbraco.Infrastructure/WebAssets/JsInitialization.cs b/src/Umbraco.Infrastructure/WebAssets/JsInitialization.cs deleted file mode 100644 index 27c6e26bd2..0000000000 --- a/src/Umbraco.Infrastructure/WebAssets/JsInitialization.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Umbraco.Core.Hosting; -using Umbraco.Core.Manifest; -using Umbraco.Core.PropertyEditors; -using Umbraco.Core.WebAssets; -using Umbraco.Infrastructure.WebAssets; - -namespace Umbraco.Web.JavaScript -{ - /// - /// Reads from all defined manifests and ensures that any of their initialization is output with the - /// main Umbraco initialization output. - /// - public class JsInitialization - { - - /// - /// Returns the default config as a JArray - /// - /// - public static IEnumerable GetDefaultInitialization() - { - var resources = JsonConvert.DeserializeObject(Resources.JsInitialize); - return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); - } - } -} diff --git a/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs b/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs index 090995879a..a7c9f65668 100644 --- a/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs +++ b/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.WebAssets; @@ -13,12 +16,24 @@ namespace Umbraco.Web.WebAssets /// public static async Task GetScriptForLoadingBackOfficeAsync(this IRuntimeMinifier minifier, IGlobalSettings globalSettings, IIOHelper ioHelper) { - var initCss = new CssInitialization(minifier); var files = await minifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoJsBundleName); - var result = JavaScriptHelper.GetJavascriptInitialization(files, "umbraco", globalSettings, ioHelper); - result += await initCss.GetStylesheetInitializationAsync(); + var result = BackOfficeJavaScriptInitializer.GetJavascriptInitialization(files, "umbraco", globalSettings, ioHelper); + result += await GetStylesheetInitializationAsync(minifier); return result; } + + /// + /// Gets the back office css bundle paths and formats a JS call to lazy load them + /// + private static async Task GetStylesheetInitializationAsync(IRuntimeMinifier minifier) + { + var files = await minifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoCssBundleName); + var sb = new StringBuilder(); + foreach (var file in files) + sb.AppendFormat("{0}LazyLoad.css('{1}');", Environment.NewLine, file); + return sb.ToString(); + } + } } diff --git a/src/Umbraco.Tests/Web/AngularIntegration/JsInitializationTests.cs b/src/Umbraco.Tests/Web/AngularIntegration/JsInitializationTests.cs index d2ea411db8..90355429c4 100644 --- a/src/Umbraco.Tests/Web/AngularIntegration/JsInitializationTests.cs +++ b/src/Umbraco.Tests/Web/AngularIntegration/JsInitializationTests.cs @@ -9,17 +9,10 @@ namespace Umbraco.Tests.Web.AngularIntegration public class JsInitializationTests { - [Test] - public void Get_Default_Init() - { - var init = JsInitialization.GetDefaultInitialization(); - Assert.IsTrue(init.Any()); - } - [Test] public void Parse_Main() { - var result = JavaScriptHelper.WriteScript("[World]", "Hello", "Blah"); + var result = BackOfficeJavaScriptInitializer.WriteScript("[World]", "Hello", "Blah"); Assert.AreEqual(@"LazyLoad.js([World], function () { //we need to set the legacy UmbClientMgr path diff --git a/src/Umbraco.Web/Editors/PreviewController.cs b/src/Umbraco.Web/Editors/PreviewController.cs index ed4746f342..db14be84ec 100644 --- a/src/Umbraco.Web/Editors/PreviewController.cs +++ b/src/Umbraco.Web/Editors/PreviewController.cs @@ -104,7 +104,7 @@ namespace Umbraco.Web.Editors public async Task Application() { var files = await _runtimeMinifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoPreviewJsBundleName); - var result = JavaScriptHelper.GetJavascriptInitialization(files, "umbraco.preview", _globalSettings, _ioHelper); + var result = BackOfficeJavaScriptInitializer.GetJavascriptInitialization(files, "umbraco.preview", _globalSettings, _ioHelper); return JavaScript(result); }