refactoring of #518 to simplify registration of scripts in modules and themes

This commit is contained in:
Shaun Walker
2020-06-14 12:07:16 -04:00
parent ea89cc1a64
commit 4e6b4a20ef
17 changed files with 106 additions and 207 deletions

View File

@ -17,9 +17,9 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
"Oqtane.Interop.setCookie",
name, value, days);
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.setCookie",
name, value, days);
return Task.CompletedTask;
}
catch
@ -46,7 +46,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.updateTitle",
title);
return Task.CompletedTask;
@ -61,7 +61,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeMeta",
id, attribute, name, content, key);
return Task.CompletedTask;
@ -76,7 +76,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeLink",
id, rel, href, type, integrity, crossorigin, key);
return Task.CompletedTask;
@ -91,7 +91,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeLinks",
(object) links);
return Task.CompletedTask;
@ -106,7 +106,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeScript",
id, src, integrity, crossorigin, content, location, key);
return Task.CompletedTask;
@ -117,18 +117,15 @@ namespace Oqtane.UI
}
}
public Task IncludeScripts(object[] scripts)
public async Task LoadScript(string path)
{
try
{
_jsRuntime.InvokeAsync<object>(
"Oqtane.Interop.includeScripts",
(object)scripts);
return Task.CompletedTask;
await _jsRuntime.InvokeVoidAsync("Oqtane.Interop.loadScript", path);
}
catch
{
return Task.CompletedTask;
// ignore exception
}
}
@ -136,7 +133,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.removeElementsById",
prefix, first, last);
return Task.CompletedTask;
@ -165,9 +162,9 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
"Oqtane.Interop.submitForm",
path, fields);
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.submitForm",
path, fields);
return Task.CompletedTask;
}
catch
@ -194,9 +191,9 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
"Oqtane.Interop.uploadFiles",
posturl, folder, id);
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.uploadFiles",
posturl, folder, id);
return Task.CompletedTask;
}
catch
@ -209,7 +206,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.refreshBrowser",
force, wait);
return Task.CompletedTask;
@ -224,7 +221,7 @@ namespace Oqtane.UI
{
try
{
_jsRuntime.InvokeAsync<object>(
_jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.redirectBrowser",
url, wait);
return Task.CompletedTask;
@ -235,17 +232,5 @@ namespace Oqtane.UI
}
}
public async Task LoadInteropScript(string filePath)
{
try
{
await _jsRuntime.InvokeAsync<bool>("Oqtane.Interop.loadInteropScript", filePath);
}
catch
{
// handle exception
}
}
}
}

View File

@ -30,27 +30,18 @@
await interop.UpdateTitle(PageState.Site.Name + " - " + PageState.Page.Name);
}
// include page resources
// manage stylesheets for this page
string batch = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var links = new List<object>();
var scripts = new List<object>();
foreach (Resource resource in PageState.Page.Resources)
{
switch (resource.ResourceType)
if (resource.ResourceType == ResourceType.Stylesheet)
{
case ResourceType.Stylesheet:
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 = "" });
break;
case ResourceType.Script:
scripts.Add(new { id = "app-script-" + batch + "-" + (scripts.Count + 1).ToString("00"), src = resource.Url, integrity = resource.Integrity ?? "", crossorigin = resource.CrossOrigin ?? "", content = "", location = "body", key = "" });
break;
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());
await interop.IncludeScripts(scripts.ToArray());
// remove any page resource references which are no longer required for this page
await interop.RemoveElementsById("app-stylesheet", "", "app-stylesheet-" + batch + "-00");
await interop.RemoveElementsById("app-script", "", "app-script-" + batch + "-00");
// add favicon
if (PageState.Site.FaviconFileId != null)