Skip to content

Commit

Permalink
refactoring of oqtane#518 to simplify registration of scripts in modu…
Browse files Browse the repository at this point in the history
…les and themes
  • Loading branch information
sbwalker committed Jun 14, 2020
1 parent ea89cc1 commit 4e6b4a2
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 207 deletions.
3 changes: 1 addition & 2 deletions Oqtane.Client/Modules/Admin/Login/Index.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Admin.Login
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@inject IUserService UserService
@inject IServiceProvider ServiceProvider

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions Oqtane.Client/Modules/Admin/ModuleDefinitions/Add.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject IFileService FileService
@inject IModuleDefinitionService ModuleDefinitionService
@inject IPackageService PackageService
@inject IJSRuntime JsRuntime

@if (_packages != null)
{
Expand Down Expand Up @@ -79,7 +78,7 @@
try
{
ShowProgressIndicator();
var interop = new Interop(JsRuntime);
var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3);
await ModuleDefinitionService.InstallModuleDefinitionsAsync();
}
Expand Down
5 changes: 2 additions & 3 deletions Oqtane.Client/Modules/Admin/ModuleDefinitions/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@inject NavigationManager NavigationManager
@inject IModuleDefinitionService ModuleDefinitionService
@inject IPackageService PackageService
@inject IJSRuntime JsRuntime

@if (_moduleDefinitions == null)
{
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Oqtane.Client/Modules/Admin/Themes/Add.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject IFileService FileService
@inject IThemeService ThemeService
@inject IPackageService PackageService
@inject IJSRuntime JsRuntime

@if (_packages != null)
{
Expand Down Expand Up @@ -79,7 +78,7 @@
try
{
ShowProgressIndicator();
var interop = new Interop(JsRuntime);
var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3);
await ThemeService.InstallThemesAsync();
}
Expand Down
5 changes: 2 additions & 3 deletions Oqtane.Client/Modules/Admin/Themes/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject NavigationManager NavigationManager
@inject IThemeService ThemeService
@inject IPackageService PackageService
@inject IJSRuntime JsRuntime

@if (_themes == null)
{
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions Oqtane.Client/Modules/Admin/Upgrade/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject IFileService FileService
@inject IPackageService PackageService
@inject IInstallationService InstallationService
@inject IJSRuntime JsRuntime

@if (_package != null)
{
Expand Down Expand Up @@ -71,7 +70,7 @@
try
{
ShowProgressIndicator();
var interop = new Interop(JsRuntime);
var interop = new Interop(JSRuntime);
await interop.RedirectBrowser(NavigateUrl(), 3);
await InstallationService.Upgrade();
}
Expand All @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions Oqtane.Client/Modules/Controls/FileManager.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@attribute [OqtaneIgnore]
@inject IFolderService FolderService
@inject IFileService FileService
@inject IJSRuntime JsRuntime

@if (_folders != null)
{
Expand Down Expand Up @@ -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)
{
Expand Down
27 changes: 14 additions & 13 deletions Oqtane.Client/Modules/Controls/RichTextEditor.razor
Original file line number Diff line number Diff line change
@@ -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">
Expand Down Expand Up @@ -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
Expand All @@ -117,12 +121,9 @@
{
if (firstRender)
{
var oqtaneInterop = new Interop(JsRuntime);

await oqtaneInterop.LoadInteropScript("js/quill-interop.js");

await base.OnAfterRenderAsync(firstRender);

var interop = new RichTextEditorInterop(JsRuntime);
var interop = new RichTextEditorInterop(JSRuntime);

await interop.CreateEditor(
_editorElement,
Expand All @@ -148,21 +149,21 @@

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();
}

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)
Expand All @@ -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;
Expand All @@ -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);
}
}
1 change: 0 additions & 1 deletion Oqtane.Client/Modules/HtmlText/Edit.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
};
Expand Down
22 changes: 21 additions & 1 deletion Oqtane.Client/Modules/ModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Oqtane.Enums;
using Oqtane.UI;
using System.Collections.Generic;
using Microsoft.JSInterop;
using System.Linq;

namespace Oqtane.Modules
{
Expand All @@ -19,6 +21,9 @@ public abstract class ModuleBase : ComponentBase, IModuleControl
[Inject]
protected ILogService LoggingService { get; set; }

[Inject]
protected IJSRuntime JSRuntime { get; set; }

[CascadingParameter]
protected PageState PageState { get; set; }

Expand All @@ -28,7 +33,6 @@ public abstract class ModuleBase : ComponentBase, IModuleControl
[CascadingParameter]
protected ModuleInstance ModuleInstance { get; set; }


// optional interface properties
public virtual SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.View; } set { } } // default security

Expand All @@ -40,7 +44,23 @@ public abstract class ModuleBase : ComponentBase, IModuleControl

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()
Expand Down
55 changes: 1 addition & 54 deletions Oqtane.Client/Themes/ContainerBase.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
18 changes: 2 additions & 16 deletions Oqtane.Client/Themes/LayoutBase.cs
Original file line number Diff line number Diff line change
@@ -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 + "/";
}

}
}
Loading

0 comments on commit 4e6b4a2

Please sign in to comment.