From 424950bd3e8d0f675b2600699114117b1ba44abb Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Sat, 12 Nov 2022 10:58:58 -0500 Subject: [PATCH] Fix #2488 - add ability to include inline script resource definitions in modules and themes --- Oqtane.Client/Modules/ModuleBase.cs | 15 ++++++++++++--- Oqtane.Client/Themes/ThemeBase.cs | 15 ++++++++++++--- Oqtane.Shared/Models/Resource.cs | 7 ++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Oqtane.Client/Modules/ModuleBase.cs b/Oqtane.Client/Modules/ModuleBase.cs index 0d0b0eec..77ad0423 100644 --- a/Oqtane.Client/Modules/ModuleBase.cs +++ b/Oqtane.Client/Modules/ModuleBase.cs @@ -72,15 +72,24 @@ namespace Oqtane.Modules { if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script)) { + var interop = new Interop(JSRuntime); var scripts = new List(); + var inline = 0; foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script)) { - var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url; - scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module }); + if (!string.IsNullOrEmpty(resource.Url)) + { + var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url; + scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module }); + } + else + { + inline += 1; + await interop.IncludeScript(GetType().Namespace.ToLower() + inline.ToString(), "", "", "", resource.Content, resource.Location.ToString().ToLower()); + } } if (scripts.Any()) { - var interop = new Interop(JSRuntime); await interop.IncludeScripts(scripts.ToArray()); } } diff --git a/Oqtane.Client/Themes/ThemeBase.cs b/Oqtane.Client/Themes/ThemeBase.cs index 45584ffb..5990bc05 100644 --- a/Oqtane.Client/Themes/ThemeBase.cs +++ b/Oqtane.Client/Themes/ThemeBase.cs @@ -32,15 +32,24 @@ namespace Oqtane.Themes { if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script)) { + var interop = new Interop(JSRuntime); var scripts = new List(); + var inline = 0; foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script)) { - var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url; - scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module }); + if (!string.IsNullOrEmpty(resource.Url)) + { + var url = (resource.Url.Contains("://")) ? resource.Url : PageState.Alias.BaseUrl + resource.Url; + scripts.Add(new { href = url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module }); + } + else + { + inline += 1; + await interop.IncludeScript(GetType().Namespace.ToLower() + inline.ToString(), "", "", "", resource.Content, resource.Location.ToString().ToLower()); + } } if (scripts.Any()) { - var interop = new Interop(JSRuntime); await interop.IncludeScripts(scripts.ToArray()); } } diff --git a/Oqtane.Shared/Models/Resource.cs b/Oqtane.Shared/Models/Resource.cs index 96448b4c..7afe1474 100644 --- a/Oqtane.Shared/Models/Resource.cs +++ b/Oqtane.Shared/Models/Resource.cs @@ -38,7 +38,7 @@ namespace Oqtane.Models public string CrossOrigin { get; set; } /// - /// Bundle ID in case this Resource belongs to a set of Resources, which may have already been loaded using LoadJS + /// For Scripts a Bundle can be used to identify dependencies and ordering in the script loading process /// public string Bundle { get; set; } @@ -57,6 +57,11 @@ namespace Oqtane.Models /// public bool ES6Module { get; set; } + /// + /// Allows specification of inline script - not applicable to Stylesheets + /// + public string Content { get; set; } + [Obsolete("ResourceDeclaration is deprecated", false)] public ResourceDeclaration Declaration { get; set; }