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
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@inject IUserService UserService
@inject IServiceProvider ServiceProvider
@ -96,7 +95,7 @@
{
await logger.LogInformation("Login Successful For Username {Username}", _username);
// 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");
var fields = new { __RequestVerificationToken = antiforgerytoken, username = _username, password = _password, remember = _remember, returnurl = _returnUrl };
await interop.SubmitForm($"/{PageState.Alias.AliasId}/pages/login/", fields);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,7 +27,6 @@
public override List<Resource> Resources => new List<Resource>()
{
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.snow.css" }
};

View File

@ -7,6 +7,8 @@ using System;
using Oqtane.Enums;
using Oqtane.UI;
using System.Collections.Generic;
using Microsoft.JSInterop;
using System.Linq;
namespace Oqtane.Modules
{
@ -19,6 +21,9 @@ namespace Oqtane.Modules
[Inject]
protected ILogService LoggingService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[CascadingParameter]
protected PageState PageState { get; set; }
@ -28,7 +33,6 @@ namespace Oqtane.Modules
[CascadingParameter]
protected ModuleInstance ModuleInstance { get; set; }
// optional interface properties
public virtual SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.View; } set { } } // default security
@ -40,7 +44,23 @@ namespace Oqtane.Modules
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
public string ModulePath()

View File

@ -1,64 +1,11 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Oqtane.Shared;
using Oqtane.Models;
using Oqtane.UI;
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]
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;
using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Themes
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.UI;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Oqtane.Themes
@ -22,6 +23,23 @@ namespace Oqtane.Themes
public virtual string Panes { 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
public string ThemePath()
@ -60,5 +78,10 @@ namespace Oqtane.Themes
{
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;
using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Themes
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
{
_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)

View File

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