Merge pull request #610 from sbwalker/master

refactoring of #518 to simplify registration of scripts in modules and themes
This commit is contained in:
Shaun Walker 2020-06-14 12:08:05 -04:00 committed by GitHub
commit 1c0d24ac25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 106 additions and 207 deletions

View File

@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Admin.Login @namespace Oqtane.Modules.Admin.Login
@inherits ModuleBase @inherits ModuleBase
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@inject IUserService UserService @inject IUserService UserService
@inject IServiceProvider ServiceProvider @inject IServiceProvider ServiceProvider
@ -96,7 +95,7 @@
{ {
await logger.LogInformation("Login Successful For Username {Username}", _username); await logger.LogInformation("Login Successful For Username {Username}", _username);
// complete the login on the server so that the cookies are set correctly on SignalR // complete the login on the server so that the cookies are set correctly on SignalR
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
string antiforgerytoken = await interop.GetElementByName("__RequestVerificationToken"); string antiforgerytoken = await interop.GetElementByName("__RequestVerificationToken");
var fields = new { __RequestVerificationToken = antiforgerytoken, username = _username, password = _password, remember = _remember, returnurl = _returnUrl }; var fields = new { __RequestVerificationToken = antiforgerytoken, username = _username, password = _password, remember = _remember, returnurl = _returnUrl };
await interop.SubmitForm($"/{PageState.Alias.AliasId}/pages/login/", fields); await interop.SubmitForm($"/{PageState.Alias.AliasId}/pages/login/", fields);

View File

@ -4,7 +4,6 @@
@inject IFileService FileService @inject IFileService FileService
@inject IModuleDefinitionService ModuleDefinitionService @inject IModuleDefinitionService ModuleDefinitionService
@inject IPackageService PackageService @inject IPackageService PackageService
@inject IJSRuntime JsRuntime
@if (_packages != null) @if (_packages != null)
{ {
@ -79,7 +78,7 @@
try try
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ModuleDefinitionService.InstallModuleDefinitionsAsync(); await ModuleDefinitionService.InstallModuleDefinitionsAsync();
} }

View File

@ -3,7 +3,6 @@
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IModuleDefinitionService ModuleDefinitionService @inject IModuleDefinitionService ModuleDefinitionService
@inject IPackageService PackageService @inject IPackageService PackageService
@inject IJSRuntime JsRuntime
@if (_moduleDefinitions == null) @if (_moduleDefinitions == null)
{ {
@ -86,7 +85,7 @@ else
await PackageService.DownloadPackageAsync(moduledefinitionname, version, "Modules"); await PackageService.DownloadPackageAsync(moduledefinitionname, version, "Modules");
await logger.LogInformation("Module Downloaded {ModuleDefinitionName} {Version}", moduledefinitionname, version); await logger.LogInformation("Module Downloaded {ModuleDefinitionName} {Version}", moduledefinitionname, version);
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ModuleDefinitionService.InstallModuleDefinitionsAsync(); await ModuleDefinitionService.InstallModuleDefinitionsAsync();
} }
@ -102,7 +101,7 @@ else
try try
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ModuleDefinitionService.DeleteModuleDefinitionAsync(moduleDefinition.ModuleDefinitionId, moduleDefinition.SiteId); await ModuleDefinitionService.DeleteModuleDefinitionAsync(moduleDefinition.ModuleDefinitionId, moduleDefinition.SiteId);
} }

View File

@ -4,7 +4,6 @@
@inject IFileService FileService @inject IFileService FileService
@inject IThemeService ThemeService @inject IThemeService ThemeService
@inject IPackageService PackageService @inject IPackageService PackageService
@inject IJSRuntime JsRuntime
@if (_packages != null) @if (_packages != null)
{ {
@ -79,7 +78,7 @@
try try
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ThemeService.InstallThemesAsync(); await ThemeService.InstallThemesAsync();
} }

View File

@ -4,7 +4,6 @@
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IThemeService ThemeService @inject IThemeService ThemeService
@inject IPackageService PackageService @inject IPackageService PackageService
@inject IJSRuntime JsRuntime
@if (_themes == null) @if (_themes == null)
{ {
@ -86,7 +85,7 @@ else
await PackageService.DownloadPackageAsync(themename, version, "Themes"); await PackageService.DownloadPackageAsync(themename, version, "Themes");
await logger.LogInformation("Theme Downloaded {ThemeName} {Version}", themename, version); await logger.LogInformation("Theme Downloaded {ThemeName} {Version}", themename, version);
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ThemeService.InstallThemesAsync(); await ThemeService.InstallThemesAsync();
} }
@ -102,7 +101,7 @@ else
try try
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await ThemeService.DeleteThemeAsync(Theme.ThemeName); await ThemeService.DeleteThemeAsync(Theme.ThemeName);
} }

View File

@ -4,7 +4,6 @@
@inject IFileService FileService @inject IFileService FileService
@inject IPackageService PackageService @inject IPackageService PackageService
@inject IInstallationService InstallationService @inject IInstallationService InstallationService
@inject IJSRuntime JsRuntime
@if (_package != null) @if (_package != null)
{ {
@ -71,7 +70,7 @@
try try
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await InstallationService.Upgrade(); await InstallationService.Upgrade();
} }
@ -88,7 +87,7 @@
{ {
await PackageService.DownloadPackageAsync(packageid, version, "Framework"); await PackageService.DownloadPackageAsync(packageid, version, "Framework");
ShowProgressIndicator(); ShowProgressIndicator();
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3); await interop.RedirectBrowser(NavigateUrl(), 3);
await InstallationService.Upgrade(); await InstallationService.Upgrade();
} }

View File

@ -4,7 +4,6 @@
@attribute [OqtaneIgnore] @attribute [OqtaneIgnore]
@inject IFolderService FolderService @inject IFolderService FolderService
@inject IFileService FileService @inject IFileService FileService
@inject IJSRuntime JsRuntime
@if (_folders != null) @if (_folders != null)
{ {
@ -258,7 +257,7 @@
private async Task UploadFile() private async Task UploadFile()
{ {
var interop = new Interop(JsRuntime); var interop = new Interop(JSRuntime);
var upload = await interop.GetFiles(_fileinputid); var upload = await interop.GetFiles(_fileinputid);
if (upload.Length > 0) if (upload.Length > 0)
{ {

View File

@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Controls @namespace Oqtane.Modules.Controls
@inherits ModuleBase @inherits ModuleBase
@attribute [OqtaneIgnore] @attribute [OqtaneIgnore]
@inject IJSRuntime JsRuntime
<div class="row" style="margin-bottom: 50px;"> <div class="row" style="margin-bottom: 50px;">
<div class="col"> <div class="col">
@ -108,6 +107,11 @@
[Parameter] [Parameter]
public string DebugLevel { get; set; } = "info"; public string DebugLevel { get; set; } = "info";
public override List<Resource> Resources => new List<Resource>()
{
new Resource { ResourceType = ResourceType.Script, Url = "js/quill-interop.js" }
};
protected override void OnInitialized() protected override void OnInitialized()
{ {
_content = Content; // raw HTML _content = Content; // raw HTML
@ -117,12 +121,9 @@
{ {
if (firstRender) if (firstRender)
{ {
var oqtaneInterop = new Interop(JsRuntime); await base.OnAfterRenderAsync(firstRender);
await oqtaneInterop.LoadInteropScript("js/quill-interop.js"); var interop = new RichTextEditorInterop(JSRuntime);
var interop = new RichTextEditorInterop(JsRuntime);
await interop.CreateEditor( await interop.CreateEditor(
_editorElement, _editorElement,
@ -148,13 +149,13 @@
public async Task RefreshRichText() public async Task RefreshRichText()
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
await interop.LoadEditorContent(_editorElement, _content); await interop.LoadEditorContent(_editorElement, _content);
} }
public async Task RefreshRawHtml() public async Task RefreshRawHtml()
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
_content = await interop.GetHtml(_editorElement); _content = await interop.GetHtml(_editorElement);
StateHasChanged(); StateHasChanged();
} }
@ -162,7 +163,7 @@
public async Task<string> GetHtml() public async Task<string> GetHtml()
{ {
// get rich text content // get rich text content
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
string content = await interop.GetHtml(_editorElement); string content = await interop.GetHtml(_editorElement);
if (_original != content) if (_original != content)
@ -184,7 +185,7 @@
var fileid = _fileManager.GetFileId(); var fileid = _fileManager.GetFileId();
if (fileid != -1) if (fileid != -1)
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
await interop.InsertImage(_editorElement, ContentUrl(fileid)); await interop.InsertImage(_editorElement, ContentUrl(fileid));
_filemanagervisible = false; _filemanagervisible = false;
_message = string.Empty; _message = string.Empty;
@ -205,19 +206,19 @@
// other rich text editor methods which can be used by developers // other rich text editor methods which can be used by developers
public async Task<string> GetText() public async Task<string> GetText()
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
return await interop.GetText(_editorElement); return await interop.GetText(_editorElement);
} }
public async Task<string> GetContent() public async Task<string> GetContent()
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
return await interop.GetContent(_editorElement); return await interop.GetContent(_editorElement);
} }
public async Task EnableEditor(bool mode) public async Task EnableEditor(bool mode)
{ {
var interop = new RichTextEditorInterop(JsRuntime); var interop = new RichTextEditorInterop(JSRuntime);
await interop.EnableEditor(_editorElement, mode); await interop.EnableEditor(_editorElement, mode);
} }
} }

View File

@ -27,7 +27,6 @@
public override List<Resource> Resources => new List<Resource>() public override List<Resource> Resources => new List<Resource>()
{ {
new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" }, new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" },
// the following resources should be declared in the RichTextEditor component however the framework currently only supports resource management for modules and themes
new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill1.3.6.bubble.css" }, new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill1.3.6.bubble.css" },
new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill1.3.6.snow.css" } new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill1.3.6.snow.css" }
}; };

View File

@ -7,6 +7,8 @@ using System;
using Oqtane.Enums; using Oqtane.Enums;
using Oqtane.UI; using Oqtane.UI;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.JSInterop;
using System.Linq;
namespace Oqtane.Modules namespace Oqtane.Modules
{ {
@ -19,6 +21,9 @@ namespace Oqtane.Modules
[Inject] [Inject]
protected ILogService LoggingService { get; set; } protected ILogService LoggingService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[CascadingParameter] [CascadingParameter]
protected PageState PageState { get; set; } protected PageState PageState { get; set; }
@ -28,7 +33,6 @@ namespace Oqtane.Modules
[CascadingParameter] [CascadingParameter]
protected ModuleInstance ModuleInstance { get; set; } protected ModuleInstance ModuleInstance { get; set; }
// optional interface properties // optional interface properties
public virtual SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.View; } set { } } // default security public virtual SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.View; } set { } } // default security
@ -40,6 +44,22 @@ namespace Oqtane.Modules
public virtual List<Resource> Resources { get; set; } public virtual List<Resource> Resources { get; set; }
// base lifecycle method for handling JSInterop script registration
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
{
var interop = new Interop(JSRuntime);
foreach (var resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
await interop.LoadScript(resource.Url);
}
}
}
}
// path method // path method

View File

@ -1,64 +1,11 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Oqtane.Shared;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.UI;
namespace Oqtane.Themes namespace Oqtane.Themes
{ {
public abstract class ContainerBase : ComponentBase, IContainerControl public abstract class ContainerBase : ThemeBase, IContainerControl
{ {
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[CascadingParameter]
protected PageState PageState { get; set; }
[CascadingParameter] [CascadingParameter]
protected Module ModuleState { get; set; } protected Module ModuleState { get; set; }
public virtual string Name { get; set; }
public virtual string Thumbnail { get; set; }
public string ThemePath()
{
return "Themes/" + GetType().Namespace + "/";
}
public string NavigateUrl()
{
return NavigateUrl(PageState.Page.Path);
}
public string NavigateUrl(string path)
{
return NavigateUrl(path, "");
}
public string NavigateUrl(string path, string parameters)
{
return Utilities.NavigateUrl(PageState.Alias.Path, path, parameters);
}
public string EditUrl(string action, string parameters)
{
return EditUrl(ModuleState.ModuleId, action, parameters);
}
public string EditUrl(int moduleid, string action)
{
return EditUrl(moduleid, action, "");
}
public string EditUrl(int moduleid, string action, string parameters)
{
return EditUrl(PageState.Page.Path, moduleid, action, parameters);
}
public string EditUrl(string path, int moduleid, string action, string parameters)
{
return Utilities.EditUrl(PageState.Alias.Path, path, moduleid, action, parameters);
}
} }
} }

View File

@ -1,21 +1,7 @@
using Microsoft.AspNetCore.Components; namespace Oqtane.Themes
using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Themes
{ {
public abstract class LayoutBase : ComponentBase, ILayoutControl public abstract class LayoutBase : ThemeBase, ILayoutControl
{ {
[CascadingParameter]
protected PageState PageState { get; set; }
public virtual string Name { get; set; }
public virtual string Thumbnail { get; set; }
public virtual string Panes { get; set; }
public string LayoutPath()
{
return "Themes/" + GetType().Namespace + "/";
}
} }
} }

View File

@ -4,6 +4,7 @@ using Oqtane.Models;
using Oqtane.Shared; using Oqtane.Shared;
using Oqtane.UI; using Oqtane.UI;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Oqtane.Themes namespace Oqtane.Themes
@ -22,6 +23,23 @@ namespace Oqtane.Themes
public virtual string Panes { get; set; } public virtual string Panes { get; set; }
public virtual List<Resource> Resources { get; set; } public virtual List<Resource> Resources { get; set; }
// base lifecycle method for handling JSInterop script registration
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
if (Resources != null && Resources.Exists(item => item.ResourceType == ResourceType.Script))
{
var interop = new Interop(JSRuntime);
foreach (var resource in Resources.Where(item => item.ResourceType == ResourceType.Script))
{
await interop.LoadScript(resource.Url);
}
}
}
}
// path method // path method
public string ThemePath() public string ThemePath()
@ -60,5 +78,10 @@ namespace Oqtane.Themes
{ {
return Utilities.EditUrl(PageState.Alias.Path, path, moduleid, action, parameters); return Utilities.EditUrl(PageState.Alias.Path, path, moduleid, action, parameters);
} }
public string ContentUrl(int fileid)
{
return Utilities.ContentUrl(PageState.Alias, fileid);
}
} }
} }

View File

@ -1,47 +1,7 @@
using Microsoft.AspNetCore.Components; namespace Oqtane.Themes
using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Themes
{ {
public abstract class ThemeControlBase : ComponentBase public abstract class ThemeControlBase : ThemeBase
{ {
[CascadingParameter]
protected PageState PageState { get; set; }
public string NavigateUrl()
{
return NavigateUrl(PageState.Page.Path);
}
public string NavigateUrl(string path)
{
return NavigateUrl(path, "");
}
public string NavigateUrl(string path, string parameters)
{
return Utilities.NavigateUrl(PageState.Alias.Path, path, parameters);
}
public string EditUrl(int moduleid, string action)
{
return EditUrl(moduleid, action, "");
}
public string EditUrl(int moduleid, string action, string parameters)
{
return EditUrl(PageState.Page.Path, moduleid, action, parameters);
}
public string EditUrl(string path, int moduleid, string action, string parameters)
{
return Utilities.EditUrl(PageState.Alias.Path, path, moduleid, action, parameters);
}
public string ContentUrl(int fileid)
{
return Utilities.ContentUrl(PageState.Alias, fileid);
}
} }
} }

View File

@ -17,9 +17,9 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.setCookie", "Oqtane.Interop.setCookie",
name, value, days); name, value, days);
return Task.CompletedTask; return Task.CompletedTask;
} }
catch catch
@ -46,7 +46,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.updateTitle", "Oqtane.Interop.updateTitle",
title); title);
return Task.CompletedTask; return Task.CompletedTask;
@ -61,7 +61,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeMeta", "Oqtane.Interop.includeMeta",
id, attribute, name, content, key); id, attribute, name, content, key);
return Task.CompletedTask; return Task.CompletedTask;
@ -76,7 +76,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeLink", "Oqtane.Interop.includeLink",
id, rel, href, type, integrity, crossorigin, key); id, rel, href, type, integrity, crossorigin, key);
return Task.CompletedTask; return Task.CompletedTask;
@ -91,7 +91,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeLinks", "Oqtane.Interop.includeLinks",
(object) links); (object) links);
return Task.CompletedTask; return Task.CompletedTask;
@ -106,7 +106,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeScript", "Oqtane.Interop.includeScript",
id, src, integrity, crossorigin, content, location, key); id, src, integrity, crossorigin, content, location, key);
return Task.CompletedTask; return Task.CompletedTask;
@ -117,18 +117,15 @@ namespace Oqtane.UI
} }
} }
public Task IncludeScripts(object[] scripts) public async Task LoadScript(string path)
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( await _jsRuntime.InvokeVoidAsync("Oqtane.Interop.loadScript", path);
"Oqtane.Interop.includeScripts",
(object)scripts);
return Task.CompletedTask;
} }
catch catch
{ {
return Task.CompletedTask; // ignore exception
} }
} }
@ -136,7 +133,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.removeElementsById", "Oqtane.Interop.removeElementsById",
prefix, first, last); prefix, first, last);
return Task.CompletedTask; return Task.CompletedTask;
@ -165,9 +162,9 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.submitForm", "Oqtane.Interop.submitForm",
path, fields); path, fields);
return Task.CompletedTask; return Task.CompletedTask;
} }
catch catch
@ -194,9 +191,9 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.uploadFiles", "Oqtane.Interop.uploadFiles",
posturl, folder, id); posturl, folder, id);
return Task.CompletedTask; return Task.CompletedTask;
} }
catch catch
@ -209,7 +206,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.refreshBrowser", "Oqtane.Interop.refreshBrowser",
force, wait); force, wait);
return Task.CompletedTask; return Task.CompletedTask;
@ -224,7 +221,7 @@ namespace Oqtane.UI
{ {
try try
{ {
_jsRuntime.InvokeAsync<object>( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.redirectBrowser", "Oqtane.Interop.redirectBrowser",
url, wait); url, wait);
return Task.CompletedTask; 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); await interop.UpdateTitle(PageState.Site.Name + " - " + PageState.Page.Name);
} }
// include page resources // 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>();
var scripts = new List<object>();
foreach (Resource resource in PageState.Page.Resources) 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 = "" });
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;
} }
} }
await interop.IncludeLinks(links.ToArray()); 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-stylesheet", "", "app-stylesheet-" + batch + "-00");
await interop.RemoveElementsById("app-script", "", "app-script-" + batch + "-00");
// add favicon // add favicon
if (PageState.Site.FaviconFileId != null) if (PageState.Site.FaviconFileId != null)

View File

@ -145,7 +145,7 @@ Oqtane.Interop = {
script.innerHTML = content; script.innerHTML = content;
} }
script.async = false; script.async = false;
this.loadScript(script, location) this.addScript(script, location)
.then(() => { .then(() => {
console.log(src + ' loaded'); console.log(src + ' loaded');
}) })
@ -185,7 +185,7 @@ Oqtane.Interop = {
} }
} }
}, },
loadScript: function (script, location) { addScript: function (script, location) {
if (location === 'head') { if (location === 'head') {
document.head.appendChild(script); document.head.appendChild(script);
} }
@ -198,10 +198,14 @@ Oqtane.Interop = {
script.onerror = rej(); script.onerror = rej();
}); });
}, },
includeScripts: function (scripts) { loadScript: async function (path) {
for (let i = 0; i < scripts.length; i++) { const promise = new Promise((resolve, reject) => {
this.includeScript(scripts[i].id, scripts[i].src, scripts[i].integrity, scripts[i].crossorigin, scripts[i].content, scripts[i].location, scripts[i].key); loadjs(path, { returnPromise: true })
} .then(function () { resolve(true) })
.catch(function (pathsNotFound) { reject(false) });
});
const result = await promise;
return;
}, },
getAbsoluteUrl: function (url) { getAbsoluteUrl: function (url) {
var a = document.createElement('a'); var a = document.createElement('a');
@ -321,15 +325,5 @@ Oqtane.Interop = {
setInterval(function () { setInterval(function () {
window.location.href = url; window.location.href = url;
}, wait * 1000); }, wait * 1000);
},
loadInteropScript: async function (filePath) {
const promise = new Promise((resolve, reject) => {
loadjs(filePath, { returnPromise: true })
.then(function () { resolve(true) })
.catch(function (pathsNotFound) { reject(false) });
});
const result = await promise;
return;
} }
}; };