Skip to content

Commit

Permalink
Merge pull request #2098 from sbwalker/dev
Browse files Browse the repository at this point in the history
filter deleted pages and modules in the router, provide support for cascading aspect of style sheets, replace ResourceDeclaration concept with ResourceLevel
  • Loading branch information
sbwalker authored Apr 1, 2022
2 parents c15b6cd + 0fcf1c2 commit 57def7d
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Oqtane.Client/Installer/Installer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@
if (firstRender)
{
var interop = new Interop(JSRuntime);
await interop.IncludeLink("", "stylesheet", "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css", "text/css", "sha512-GQGU0fMMi238uA+a/bdWJfpUGKUkBdgfFdgBm72SUQ6BeyWjoY/ton0tEjH+OSH9iP4Dfh+7HM0I9f5eR0L/4w==", "anonymous", "");
await interop.IncludeScript("", "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.bundle.min.js", "sha512-pax4MlgXjHEPfCwcJLQhigY7+N8rt6bVvWLFyUMuxShv170X53TRzGPmPkZmGBhk+jikR8WBM4yl7A9WMHHqvg==", "anonymous", "", "head", "");
await interop.IncludeLink("", "stylesheet", "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css", "text/css", "sha512-GQGU0fMMi238uA+a/bdWJfpUGKUkBdgfFdgBm72SUQ6BeyWjoY/ton0tEjH+OSH9iP4Dfh+7HM0I9f5eR0L/4w==", "anonymous");
await interop.IncludeScript("", "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.bundle.min.js", "sha512-pax4MlgXjHEPfCwcJLQhigY7+N8rt6bVvWLFyUMuxShv170X53TRzGPmPkZmGBhk+jikR8WBM4yl7A9WMHHqvg==", "anonymous", "", "head");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Oqtane.Client/Modules/Admin/Dashboard/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
protected override void OnInitialized()
{
var admin = PageState.Pages.FirstOrDefault(item => item.Path == "admin");
_pages = PageState.Pages.Where(item => item.ParentId == admin?.PageId && !item.IsDeleted).ToList();
_pages = PageState.Pages.Where(item => item.ParentId == admin?.PageId).ToList();
}
}
2 changes: 1 addition & 1 deletion Oqtane.Client/Modules/Admin/Pages/Edit.razor
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
_title = page.Title;
_meta = page.Meta;
_path = page.Path;
_pageModules = PageState.Modules.Where(m => m.PageId == page.PageId && m.IsDeleted == false).ToList();
_pageModules = PageState.Modules.Where(m => m.PageId == page.PageId).ToList();

if (string.IsNullOrEmpty(_path))
{
Expand Down
1 change: 1 addition & 0 deletions Oqtane.Client/Modules/Admin/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Header>
<th style="width: 1px;">&nbsp;</th>
<th style="width: 1px;">&nbsp;</th>
<th style="width: 1px;">&nbsp;</th>
<th>@SharedLocalizer["Name"]</th>
</Header>
<Row>
Expand Down
2 changes: 1 addition & 1 deletion Oqtane.Client/Modules/ModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
{
var scripts = new List<object>();
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script && item.Declaration != ResourceDeclaration.Global))
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module });
}
Expand Down
5 changes: 2 additions & 3 deletions Oqtane.Client/Themes/Controls/Theme/ControlPanel.razor
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@
if (PageId != "-")
{
_modules = PageState.Modules
.Where(module => module.PageId == int.Parse(PageId)
&& !module.IsDeleted
&& UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
.Where(module => module.PageId == int.Parse(PageId) &&
UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
.ToList();
}
ModuleId = "-";
Expand Down
2 changes: 1 addition & 1 deletion Oqtane.Client/Themes/Controls/Theme/MenuBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected string GetUrl(Page page)
private IEnumerable<Page> GetMenuPages()
{
var securityLevel = int.MaxValue;
foreach (Page p in PageState.Pages.Where(item => item.IsNavigation && !item.IsDeleted))
foreach (Page p in PageState.Pages.Where(item => item.IsNavigation))
{
if (p.Level <= securityLevel && UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, p.Permissions))
{
Expand Down
2 changes: 1 addition & 1 deletion Oqtane.Client/Themes/ThemeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
{
var scripts = new List<object>();
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script && item.Declaration != ResourceDeclaration.Global))
foreach (Resource resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
scripts.Add(new { href = resource.Url, bundle = resource.Bundle ?? "", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", es6module = resource.ES6Module });
}
Expand Down
8 changes: 4 additions & 4 deletions Oqtane.Client/UI/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ public Task IncludeMeta(string id, string attribute, string name, string content
}
}

public Task IncludeLink(string id, string rel, string href, string type, string integrity, string crossorigin, string key)
public Task IncludeLink(string id, string rel, string href, string type, string integrity, string crossorigin)
{
try
{
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeLink",
id, rel, href, type, integrity, crossorigin, key);
id, rel, href, type, integrity, crossorigin);
return Task.CompletedTask;
}
catch
Expand All @@ -102,13 +102,13 @@ public Task IncludeLinks(object[] links)
}
}

public Task IncludeScript(string id, string src, string integrity, string crossorigin, string content, string location, string key)
public Task IncludeScript(string id, string src, string integrity, string crossorigin, string content, string location)
{
try
{
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeScript",
id, src, integrity, crossorigin, content, location, key);
id, src, integrity, crossorigin, content, location);
return Task.CompletedTask;
}
catch
Expand Down
6 changes: 3 additions & 3 deletions Oqtane.Client/UI/Pane.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ else
if (Name.ToLower() == PaneNames.Admin.ToLower())
{
Module module = PageState.Modules.FirstOrDefault(item => item.ModuleId == PageState.ModuleId);
if (module != null && !module.IsDeleted)
if (module != null)
{
var moduleType = Type.GetType(module.ModuleType);
if (moduleType != null)
Expand Down Expand Up @@ -97,7 +97,7 @@ else
if (PageState.ModuleId != -1)
{
Module module = PageState.Modules.FirstOrDefault(item => item.ModuleId == PageState.ModuleId);
if (module != null && module.Pane.ToLower() == Name.ToLower() && !module.IsDeleted)
if (module != null && module.Pane.ToLower() == Name.ToLower())
{
// check if user is authorized to view module
if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
Expand All @@ -108,7 +108,7 @@ else
}
else
{
foreach (Module module in PageState.Modules.Where(item => item.PageId == PageState.Page.PageId && item.Pane.ToLower() == Name.ToLower() && !item.IsDeleted).OrderBy(x => x.Order).ToArray())
foreach (Module module in PageState.Modules.Where(item => item.PageId == PageState.Page.PageId && item.Pane.ToLower() == Name.ToLower()).OrderBy(x => x.Order).ToArray())
{
// check if user is authorized to view module
if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.Permissions))
Expand Down
27 changes: 15 additions & 12 deletions Oqtane.Client/UI/SiteRouter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
if (PageState == null || refresh == UI.Refresh.Site)
{
pages = await PageService.GetPagesAsync(site.SiteId);
pages = pages.Where(item => !item.IsDeleted).ToList();
}
else
{
Expand Down Expand Up @@ -206,6 +207,7 @@
if (PageState == null || refresh == UI.Refresh.Site)
{
modules = await ModuleService.GetModulesAsync(site.SiteId);
modules = modules.Where(item => !item.IsDeleted).ToList();
}
else
{
Expand Down Expand Up @@ -268,7 +270,7 @@
}
else
{
// site does not exist
// site does not exist
}
}

Expand Down Expand Up @@ -322,7 +324,7 @@
{
if (page.IsPersonalizable && user != null)
{
// load the personalized page
// load the personalized page
page = await PageService.GetPageAsync(page.PageId, user.UserId);
}

Expand All @@ -338,7 +340,7 @@
Type themetype = Type.GetType(page.ThemeType);
if (themetype == null)
{
// fallback
// fallback
page.ThemeType = Constants.DefaultTheme;
themetype = Type.GetType(Constants.DefaultTheme);
}
Expand All @@ -351,14 +353,14 @@
{
panes = themeobject.Panes;
}
page.Resources = ManagePageResources(page.Resources, themeobject.Resources);
page.Resources = ManagePageResources(page.Resources, themeobject.Resources, ResourceLevel.Page);
}
}
page.Panes = panes.Replace(";", ",").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
catch
{
// error loading theme or layout
// error loading theme or layout
}

return page;
Expand All @@ -369,7 +371,7 @@
var paneindex = new Dictionary<string, int>();
foreach (Module module in modules)
{
// initialize module control properties
// initialize module control properties
module.SecurityAccessLevel = SecurityAccessLevel.Host;
module.ControlTitle = "";
module.Actions = "";
Expand Down Expand Up @@ -422,17 +424,17 @@
// get additional metadata from IModuleControl interface
if (moduletype != null && module.ModuleType != "")
{
// retrieve module component resources
// retrieve module component resources
var moduleobject = Activator.CreateInstance(moduletype) as IModuleControl;
page.Resources = ManagePageResources(page.Resources, moduleobject.Resources);
page.Resources = ManagePageResources(page.Resources, moduleobject.Resources, ResourceLevel.Module);
if (action.ToLower() == "settings" && module.ModuleDefinition != null)
{
// settings components are embedded within a framework settings module
// settings components are embedded within a framework settings module
moduletype = Type.GetType(module.ModuleDefinition.ControlTypeTemplate.Replace(Constants.ActionToken, action), false, true);
if (moduletype != null)
{
moduleobject = Activator.CreateInstance(moduletype) as IModuleControl;
page.Resources = ManagePageResources(page.Resources, moduleobject.Resources);
page.Resources = ManagePageResources(page.Resources, moduleobject.Resources, ResourceLevel.Module);
}
}

Expand Down Expand Up @@ -483,15 +485,16 @@
return (page, modules);
}

private List<Resource> ManagePageResources(List<Resource> pageresources, List<Resource> resources)
private List<Resource> ManagePageResources(List<Resource> pageresources, List<Resource> resources, ResourceLevel level)
{
if (resources != null)
{
foreach (var resource in resources)
{
// ensure resource does not exist already
// ensure resource does not exist already
if (pageresources.Find(item => item.Url == resource.Url) == null)
{
resource.Level = level;
pageresources.Add(resource);
}
}
Expand Down
16 changes: 9 additions & 7 deletions Oqtane.Client/UI/ThemeBuilder.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@

protected override async Task OnAfterRenderAsync(bool firstRender)
{
var interop = new Interop(JsRuntime);
var interop = new Interop(JsRuntime);

// manage stylesheets for this page
string batch = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff");
var links = new List<object>();
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 = "" });
string batch = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff");
var links = new List<object>();
foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet))
{
var prefix = "app-stylesheet-" + resource.Level.ToString().ToLower();
links.Add(new { id = prefix + "-" + batch + "-" + (links.Count + 1).ToString("00"), rel = "stylesheet", href = resource.Url, type = "text/css", integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", insertbefore = prefix });
}
if (links.Any())
{
await interop.IncludeLinks(links.ToArray());
}
await interop.RemoveElementsById("app-stylesheet", "", "app-stylesheet-" + batch + "-00");
await interop.RemoveElementsById("app-stylesheet-page-", "", "app-stylesheet-page-" + batch + "-00");
await interop.RemoveElementsById("app-stylesheet-module-", "", "app-stylesheet-module-" + batch + "-00");

// set page title
if (!string.IsNullOrEmpty(PageState.Page.Title))
Expand Down
2 changes: 2 additions & 0 deletions Oqtane.Server/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<script src="js/loadjs.min.js"></script>
<link rel="stylesheet" href="css/app.css" />
@Html.Raw(Model.HeadResources)
<link id="app-stylesheet-page" rel="stylesheet" href="disabled" disabled />
<link id="app-stylesheet-module" rel="stylesheet" href="disabled" disabled />
</head>
<body>
@if (string.IsNullOrEmpty(Model.Message))
Expand Down
56 changes: 3 additions & 53 deletions Oqtane.Server/Pages/_Host.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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;
Expand Down Expand Up @@ -190,8 +188,6 @@ public IActionResult OnGet()
foreach (Assembly assembly in assemblies)
{
ProcessHostResources(assembly);
ProcessModuleControls(assembly);
ProcessThemeControls(assembly);
}

// set culture if not specified
Expand Down Expand Up @@ -411,65 +407,19 @@ private void ProcessHostResources(Assembly assembly)
var obj = Activator.CreateInstance(type) as IHostResources;
foreach (var resource in obj.Resources)
{
resource.Declaration = ResourceDeclaration.Global;
ProcessResource(resource, 0);
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)
{
foreach (var resource in obj.Resources)
{
if (resource.Declaration == ResourceDeclaration.Global)
{
ProcessResource(resource, 0);
}
}
}
}
}

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)
{
int count = 0; // required for local stylesheets for current theme
foreach (var resource in obj.Resources)
{
if (resource.Declaration == ResourceDeclaration.Global || (Utilities.GetFullTypeName(type.AssemblyQualifiedName) == ThemeType && resource.ResourceType == ResourceType.Stylesheet))
{
ProcessResource(resource, count++);
}
}
}
}
}
private void ProcessResource(Resource resource, int count)
private void ProcessResource(Resource resource)
{
switch (resource.ResourceType)
{
case ResourceType.Stylesheet:
if (!HeadResources.Contains(resource.Url, StringComparison.OrdinalIgnoreCase))
{
var id = (resource.Declaration == ResourceDeclaration.Global) ? "" : "id=\"app-stylesheet-" + DateTime.UtcNow.ToString("yyyyMMddHHmmssfff") + "-" + count.ToString("00") + "\" ";
HeadResources += "<link " + id + "rel=\"stylesheet\" href=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " />" + Environment.NewLine;
HeadResources += "<link rel=\"stylesheet\" href=\"" + resource.Url + "\"" + CrossOrigin(resource.CrossOrigin) + Integrity(resource.Integrity) + " />" + Environment.NewLine;
}
break;
case ResourceType.Script:
Expand Down
Loading

0 comments on commit 57def7d

Please sign in to comment.