From ecacb681b4587217ba646014127d6699d35c96ad Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 19 Oct 2020 08:03:04 -0400 Subject: [PATCH] introduce Resource Declaration and Location properties to offer more resource management options for developers --- Oqtane.Client/Modules/ModuleBase.cs | 13 ++- Oqtane.Client/Themes/ThemeBase.cs | 9 +- Oqtane.Client/UI/ThemeBuilder.razor | 9 +- .../Interfaces/IHostResources.cs | 4 +- Oqtane.Server/Pages/_Host.cshtml | 5 +- Oqtane.Server/Pages/_Host.cshtml.cs | 102 +++++++++++++++--- Oqtane.Shared/Enums/ResourceDeclaration.cs | 8 ++ Oqtane.Shared/Enums/ResourceLocation.cs | 8 ++ Oqtane.Shared/Models/Resource.cs | 4 +- 9 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 Oqtane.Shared/Enums/ResourceDeclaration.cs create mode 100644 Oqtane.Shared/Enums/ResourceLocation.cs diff --git a/Oqtane.Client/Modules/ModuleBase.cs b/Oqtane.Client/Modules/ModuleBase.cs index 633caed5..e51b9873 100644 --- a/Oqtane.Client/Modules/ModuleBase.cs +++ b/Oqtane.Client/Modules/ModuleBase.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components; using Oqtane.Shared; using Oqtane.Models; using System.Threading.Tasks; @@ -53,12 +53,15 @@ namespace Oqtane.Modules if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script)) { var scripts = new List(); - foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script)) + foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script && item.Declaration != ResourceDeclaration.Global)) { scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "" }); } - var interop = new Interop(JSRuntime); - await interop.IncludeScripts(scripts.ToArray()); + if (scripts.Any()) + { + var interop = new Interop(JSRuntime); + await interop.IncludeScripts(scripts.ToArray()); + } } } } @@ -292,4 +295,4 @@ namespace Oqtane.Modules } } } -} \ No newline at end of file +} diff --git a/Oqtane.Client/Themes/ThemeBase.cs b/Oqtane.Client/Themes/ThemeBase.cs index 32b9c564..7f57ec70 100644 --- a/Oqtane.Client/Themes/ThemeBase.cs +++ b/Oqtane.Client/Themes/ThemeBase.cs @@ -32,12 +32,15 @@ namespace Oqtane.Themes if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script)) { var scripts = new List(); - foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script)) + foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script && item.Declaration != ResourceDeclaration.Global)) { scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "" }); } - var interop = new Interop(JSRuntime); - await interop.IncludeScripts(scripts.ToArray()); + if (scripts.Any()) + { + var interop = new Interop(JSRuntime); + await interop.IncludeScripts(scripts.ToArray()); + } } } } diff --git a/Oqtane.Client/UI/ThemeBuilder.razor b/Oqtane.Client/UI/ThemeBuilder.razor index d4c20b37..ef8057f6 100644 --- a/Oqtane.Client/UI/ThemeBuilder.razor +++ b/Oqtane.Client/UI/ThemeBuilder.razor @@ -33,11 +33,14 @@ // manage stylesheets for this page string batch = DateTime.Now.ToString("yyyyMMddHHmmssfff"); var links = new List(); - foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet)) + foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet && item.Declaration != ResourceDeclaration.Global)) { links.Add(new { id = "app-stylesheet-" + batch + "-" + (links.Count + 1).ToString("00"), rel = "stylesheet", href = resource.Url, type = "text/css", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", key = "" }); } - await interop.IncludeLinks(links.ToArray()); + if (links.Any()) + { + await interop.IncludeLinks(links.ToArray()); + } await interop.RemoveElementsById("app-stylesheet", "", "app-stylesheet-" + batch + "-00"); // add favicon @@ -46,7 +49,7 @@ await interop.IncludeLink("app-favicon", "shortcut icon", Utilities.ContentUrl(PageState.Alias, PageState.Site.FaviconFileId.Value), "image/x-icon", "", "", "id"); } // add PWA support - if (PageState.Site.PwaIsEnabled) + if (PageState.Site.PwaIsEnabled && PageState.Site.PwaAppIconFileId != null && PageState.Site.PwaSplashIconFileId != null) { await InitializePwa(interop); } diff --git a/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs b/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs index e51042ae..d67b54cc 100644 --- a/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs +++ b/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs @@ -1,8 +1,10 @@ -using Oqtane.Models; +using Oqtane.Models; using System.Collections.Generic; namespace Oqtane.Infrastructure { + // Obsolete - use the Resources collection as part of IModuleControl or IThemeCOntrol and use the ResourceDeclaration.Global property + // note - not using the [Obsolete] attribute in this case as we want to avoid compilation warnings in the core framework public interface IHostResources { List Resources { get; } // identifies global resources for an application diff --git a/Oqtane.Server/Pages/_Host.cshtml b/Oqtane.Server/Pages/_Host.cshtml index e4b6e5a7..2aa0dd9d 100644 --- a/Oqtane.Server/Pages/_Host.cshtml +++ b/Oqtane.Server/Pages/_Host.cshtml @@ -1,4 +1,4 @@ -@page "/" +@page "/" @namespace Oqtane.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @using System.Globalization @@ -24,7 +24,7 @@ - @Html.Raw(@Model.Resources) + @Html.Raw(@Model.HeadResources) @(Html.AntiForgeryToken()) @@ -53,5 +53,6 @@ { } + @Html.Raw(@Model.BodyResources) diff --git a/Oqtane.Server/Pages/_Host.cshtml.cs b/Oqtane.Server/Pages/_Host.cshtml.cs index ed0f4124..c50e5b1f 100644 --- a/Oqtane.Server/Pages/_Host.cshtml.cs +++ b/Oqtane.Server/Pages/_Host.cshtml.cs @@ -1,6 +1,9 @@ -using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Mvc.RazorPages; using Oqtane.Infrastructure; using Oqtane.Shared; +using Oqtane.Modules; +using Oqtane.Models; +using Oqtane.Themes; using System; using System.Linq; using System.Reflection; @@ -9,33 +12,106 @@ namespace Oqtane.Pages { public class HostModel : PageModel { - public string Resources = ""; + public string HeadResources = ""; + public string BodyResources = ""; public void OnGet() { var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies(); foreach (Assembly assembly in assemblies) { - var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IHostResources))); - foreach (var type in types) + ProcessHostResources(assembly); + ProcessModuleControls(assembly); + ProcessThemeControls(assembly); + } + } + + private void ProcessHostResources(Assembly assembly) + { + var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IHostResources))); + foreach (var type in types) + { + var obj = Activator.CreateInstance(type) as IHostResources; + foreach (var resource in obj.Resources) + { + ProcessResource(resource); + } + } + } + + private void ProcessModuleControls(Assembly assembly) + { + var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModuleControl))); + foreach (var type in types) + { + // Check if type should be ignored + if (type.IsOqtaneIgnore()) continue; + + var obj = Activator.CreateInstance(type) as IModuleControl; + if (obj.Resources != null) { - var obj = Activator.CreateInstance(type) as IHostResources; foreach (var resource in obj.Resources) { - switch (resource.ResourceType) + if (resource.Declaration == ResourceDeclaration.Global) { - case ResourceType.Stylesheet: - Resources += "" + Environment.NewLine; - break; - case ResourceType.Script: - Resources += "" + Environment.NewLine; - break; + ProcessResource(resource); } } } } } + private void ProcessThemeControls(Assembly assembly) + { + var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IThemeControl))); + foreach (var type in types) + { + // Check if type should be ignored + if (type.IsOqtaneIgnore()) continue; + + var obj = Activator.CreateInstance(type) as IThemeControl; + if (obj.Resources != null) + { + foreach (var resource in obj.Resources) + { + if (resource.Declaration == ResourceDeclaration.Global) + { + ProcessResource(resource); + } + } + } + } + } + + private void ProcessResource(Resource resource) + { + switch (resource.ResourceType) + { + case ResourceType.Stylesheet: + if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase)) + { + HeadResources += "" + Environment.NewLine; + } + break; + case ResourceType.Script: + if (resource.Location == Shared.ResourceLocation.Body) + { + if (!BodyResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase)) + { + BodyResources += "" + Environment.NewLine; + } + } + else + { + if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase)) + { + HeadResources += "" + Environment.NewLine; + } + } + break; + } + } + private string CrossOrigin(string crossorigin) { if (!string.IsNullOrEmpty(crossorigin)) @@ -59,4 +135,4 @@ namespace Oqtane.Pages } } } -} \ No newline at end of file +} diff --git a/Oqtane.Shared/Enums/ResourceDeclaration.cs b/Oqtane.Shared/Enums/ResourceDeclaration.cs new file mode 100644 index 00000000..2d86adcb --- /dev/null +++ b/Oqtane.Shared/Enums/ResourceDeclaration.cs @@ -0,0 +1,8 @@ +namespace Oqtane.Shared +{ + public enum ResourceDeclaration + { + Local, + Global + } +} diff --git a/Oqtane.Shared/Enums/ResourceLocation.cs b/Oqtane.Shared/Enums/ResourceLocation.cs new file mode 100644 index 00000000..7e725a72 --- /dev/null +++ b/Oqtane.Shared/Enums/ResourceLocation.cs @@ -0,0 +1,8 @@ +namespace Oqtane.Shared +{ + public enum ResourceLocation + { + Head, + Body + } +} diff --git a/Oqtane.Shared/Models/Resource.cs b/Oqtane.Shared/Models/Resource.cs index 01bc57ba..72f26e4b 100644 --- a/Oqtane.Shared/Models/Resource.cs +++ b/Oqtane.Shared/Models/Resource.cs @@ -1,4 +1,4 @@ -using Oqtane.Shared; +using Oqtane.Shared; namespace Oqtane.Models { @@ -9,5 +9,7 @@ namespace Oqtane.Models public string Integrity { get; set; } public string CrossOrigin { get; set; } public string Bundle { get; set; } + public ResourceDeclaration Declaration { get; set; } + public ResourceLocation Location { get; set; } } }