Merge pull request #714 from sbwalker/master

added support for dynamic inclusion of global resources in _host.cshtml ( ie. global stylesheets and scripts such as those required by UI component suites )
This commit is contained in:
Shaun Walker 2020-08-28 11:25:38 -04:00 committed by GitHub
commit eb9acc770c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,11 @@
using Oqtane.Models;
using System.Collections.Generic;
namespace Oqtane.Infrastructure
{
public interface IHostResources
{
List<Resource> Resources { get; } // identifies global resources for an application
}
}

View File

@ -3,6 +3,7 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@model Oqtane.Pages.HostModel
<!DOCTYPE html>
<html>
@ -16,6 +17,7 @@
<link id="app-manifest" rel="manifest" />
<link rel="stylesheet" href="css/app.css" />
<script src="js/loadjs.min.js"></script>
@Html.Raw(@Model.Resources)
</head>
<body>
@(Html.AntiForgeryToken())

View File

@ -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 += "<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 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 "";
}
}
}
}