introduce Resource Declaration and Location properties to offer more resource management options for developers
This commit is contained in:
@ -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 += "<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;
|
||||
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 += "<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)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(crossorigin))
|
||||
@ -59,4 +135,4 @@ namespace Oqtane.Pages
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user