From a8cd84e7986ec84935c28e13022c9f45fcb379d0 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Fri, 28 Aug 2020 11:24:43 -0400 Subject: [PATCH] added support for dynamic inclusion of global resources in _host.cshtml ( ie. global stylesheets and scripts such as those required by UI component suites ) --- .../Interfaces/IHostResources.cs | 11 ++++ Oqtane.Server/Pages/_Host.cshtml | 2 + Oqtane.Server/Pages/_Host.cshtml.cs | 62 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs create mode 100644 Oqtane.Server/Pages/_Host.cshtml.cs diff --git a/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs b/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs new file mode 100644 index 00000000..e51042ae --- /dev/null +++ b/Oqtane.Server/Infrastructure/Interfaces/IHostResources.cs @@ -0,0 +1,11 @@ +using Oqtane.Models; +using System.Collections.Generic; + +namespace Oqtane.Infrastructure +{ + 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 c48f226b..bd12460c 100644 --- a/Oqtane.Server/Pages/_Host.cshtml +++ b/Oqtane.Server/Pages/_Host.cshtml @@ -3,6 +3,7 @@ @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @using Microsoft.Extensions.Configuration @inject IConfiguration Configuration +@model Oqtane.Pages.HostModel @@ -16,6 +17,7 @@ + @Html.Raw(@Model.Resources) @(Html.AntiForgeryToken()) diff --git a/Oqtane.Server/Pages/_Host.cshtml.cs b/Oqtane.Server/Pages/_Host.cshtml.cs new file mode 100644 index 00000000..ed0f4124 --- /dev/null +++ b/Oqtane.Server/Pages/_Host.cshtml.cs @@ -0,0 +1,62 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; +using Oqtane.Infrastructure; +using Oqtane.Shared; +using System; +using System.Linq; +using System.Reflection; + +namespace Oqtane.Pages +{ + public class HostModel : PageModel + { + public string Resources = ""; + + 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) + { + var obj = Activator.CreateInstance(type) as IHostResources; + foreach (var resource in obj.Resources) + { + switch (resource.ResourceType) + { + case ResourceType.Stylesheet: + Resources += "" + Environment.NewLine; + break; + case ResourceType.Script: + Resources += "" + Environment.NewLine; + break; + } + } + } + } + } + + private string CrossOrigin(string crossorigin) + { + if (!string.IsNullOrEmpty(crossorigin)) + { + return " crossorigin=\"" + crossorigin + "\""; + } + else + { + return ""; + } + } + private string Integrity(string integrity) + { + if (!string.IsNullOrEmpty(integrity)) + { + return " integrity=\"" + integrity + "\""; + } + else + { + return ""; + } + } + } +} \ No newline at end of file