introduce Resource Declaration and Location properties to offer more resource management options for developers
This commit is contained in:
parent
74026401a6
commit
ecacb681b4
|
@ -1,4 +1,4 @@
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -53,12 +53,15 @@ namespace Oqtane.Modules
|
||||||
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
|
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
|
||||||
{
|
{
|
||||||
var scripts = new List<object>();
|
var scripts = new List<object>();
|
||||||
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 ?? "" });
|
scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "" });
|
||||||
}
|
}
|
||||||
var interop = new Interop(JSRuntime);
|
if (scripts.Any())
|
||||||
await interop.IncludeScripts(scripts.ToArray());
|
{
|
||||||
|
var interop = new Interop(JSRuntime);
|
||||||
|
await interop.IncludeScripts(scripts.ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,12 +32,15 @@ namespace Oqtane.Themes
|
||||||
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
|
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
|
||||||
{
|
{
|
||||||
var scripts = new List<object>();
|
var scripts = new List<object>();
|
||||||
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 ?? "" });
|
scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "" });
|
||||||
}
|
}
|
||||||
var interop = new Interop(JSRuntime);
|
if (scripts.Any())
|
||||||
await interop.IncludeScripts(scripts.ToArray());
|
{
|
||||||
|
var interop = new Interop(JSRuntime);
|
||||||
|
await interop.IncludeScripts(scripts.ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,11 +33,14 @@
|
||||||
// manage stylesheets for this page
|
// manage stylesheets for this page
|
||||||
string batch = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
string batch = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||||||
var links = new List<object>();
|
var links = new List<object>();
|
||||||
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 = "" });
|
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");
|
await interop.RemoveElementsById("app-stylesheet", "", "app-stylesheet-" + batch + "-00");
|
||||||
|
|
||||||
// add favicon
|
// 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");
|
await interop.IncludeLink("app-favicon", "shortcut icon", Utilities.ContentUrl(PageState.Alias, PageState.Site.FaviconFileId.Value), "image/x-icon", "", "", "id");
|
||||||
}
|
}
|
||||||
// add PWA support
|
// add PWA support
|
||||||
if (PageState.Site.PwaIsEnabled)
|
if (PageState.Site.PwaIsEnabled && PageState.Site.PwaAppIconFileId != null && PageState.Site.PwaSplashIconFileId != null)
|
||||||
{
|
{
|
||||||
await InitializePwa(interop);
|
await InitializePwa(interop);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Oqtane.Infrastructure
|
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
|
public interface IHostResources
|
||||||
{
|
{
|
||||||
List<Resource> Resources { get; } // identifies global resources for an application
|
List<Resource> Resources { get; } // identifies global resources for an application
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
@page "/"
|
@page "/"
|
||||||
@namespace Oqtane.Pages
|
@namespace Oqtane.Pages
|
||||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||||
@using System.Globalization
|
@using System.Globalization
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
<link id="app-manifest" rel="manifest" />
|
<link id="app-manifest" rel="manifest" />
|
||||||
<link rel="stylesheet" href="css/app.css" />
|
<link rel="stylesheet" href="css/app.css" />
|
||||||
<script src="js/loadjs.min.js"></script>
|
<script src="js/loadjs.min.js"></script>
|
||||||
@Html.Raw(@Model.Resources)
|
@Html.Raw(@Model.HeadResources)
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@(Html.AntiForgeryToken())
|
@(Html.AntiForgeryToken())
|
||||||
|
@ -53,5 +53,6 @@
|
||||||
{
|
{
|
||||||
<script src="_framework/blazor.server.js"></script>
|
<script src="_framework/blazor.server.js"></script>
|
||||||
}
|
}
|
||||||
|
@Html.Raw(@Model.BodyResources)
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
using Oqtane.Modules;
|
||||||
|
using Oqtane.Models;
|
||||||
|
using Oqtane.Themes;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
@ -9,33 +12,106 @@ namespace Oqtane.Pages
|
||||||
{
|
{
|
||||||
public class HostModel : PageModel
|
public class HostModel : PageModel
|
||||||
{
|
{
|
||||||
public string Resources = "";
|
public string HeadResources = "";
|
||||||
|
public string BodyResources = "";
|
||||||
|
|
||||||
public void OnGet()
|
public void OnGet()
|
||||||
{
|
{
|
||||||
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
|
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
|
||||||
foreach (Assembly assembly in assemblies)
|
foreach (Assembly assembly in assemblies)
|
||||||
{
|
{
|
||||||
var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IHostResources)));
|
ProcessHostResources(assembly);
|
||||||
foreach (var type in types)
|
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)
|
foreach (var resource in obj.Resources)
|
||||||
{
|
{
|
||||||
switch (resource.ResourceType)
|
if (resource.Declaration == ResourceDeclaration.Global)
|
||||||
{
|
{
|
||||||
case ResourceType.Stylesheet:
|
ProcessResource(resource);
|
||||||
Resources += "<link rel=\"stylesheet\" href=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " />" + Environment.NewLine;
|
|
||||||
break;
|
|
||||||
case ResourceType.Script:
|
|
||||||
Resources += "<script src=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 += "<link rel=\"stylesheet\" href=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " />" + Environment.NewLine;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ResourceType.Script:
|
||||||
|
if (resource.Location == Shared.ResourceLocation.Body)
|
||||||
|
{
|
||||||
|
if (!BodyResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
BodyResources += "<script src=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
HeadResources += "<script src=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + "></script>" + Environment.NewLine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string CrossOrigin(string crossorigin)
|
private string CrossOrigin(string crossorigin)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(crossorigin))
|
if (!string.IsNullOrEmpty(crossorigin))
|
||||||
|
|
8
Oqtane.Shared/Enums/ResourceDeclaration.cs
Normal file
8
Oqtane.Shared/Enums/ResourceDeclaration.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Oqtane.Shared
|
||||||
|
{
|
||||||
|
public enum ResourceDeclaration
|
||||||
|
{
|
||||||
|
Local,
|
||||||
|
Global
|
||||||
|
}
|
||||||
|
}
|
8
Oqtane.Shared/Enums/ResourceLocation.cs
Normal file
8
Oqtane.Shared/Enums/ResourceLocation.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Oqtane.Shared
|
||||||
|
{
|
||||||
|
public enum ResourceLocation
|
||||||
|
{
|
||||||
|
Head,
|
||||||
|
Body
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
|
||||||
namespace Oqtane.Models
|
namespace Oqtane.Models
|
||||||
{
|
{
|
||||||
|
@ -9,5 +9,7 @@ namespace Oqtane.Models
|
||||||
public string Integrity { get; set; }
|
public string Integrity { get; set; }
|
||||||
public string CrossOrigin { get; set; }
|
public string CrossOrigin { get; set; }
|
||||||
public string Bundle { get; set; }
|
public string Bundle { get; set; }
|
||||||
|
public ResourceDeclaration Declaration { get; set; }
|
||||||
|
public ResourceLocation Location { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user