ability to add arbitrary content to head and body during client and server rendering

This commit is contained in:
sbwalker 2023-05-15 16:43:22 -04:00
parent 7f7dff7019
commit dbe7324c7f
6 changed files with 131 additions and 37 deletions

33
Oqtane.Client/Body.razor Normal file
View File

@ -0,0 +1,33 @@
@using System.ComponentModel
@using Oqtane.Shared
@inject SiteState SiteState
@((MarkupString)content)
@code {
private string content = "";
protected override void OnInitialized()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged;
}
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "BodyContent":
if (content != SiteState.Properties.BodyContent)
{
content = SiteState.Properties.BodyContent;
StateHasChanged();
}
break;
}
}
public void Dispose()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;
}
}

42
Oqtane.Client/Head.razor Normal file
View File

@ -0,0 +1,42 @@
@using System.ComponentModel
@using Oqtane.Shared
@inject SiteState SiteState
<title>@title</title>
@((MarkupString)content)
@code {
private string title = "";
private string content = "";
protected override void OnInitialized()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged;
}
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "PageTitle":
if (title != SiteState.Properties.PageTitle)
{
title = SiteState.Properties.PageTitle;
StateHasChanged();
}
break;
case "HeadContent":
if (content != SiteState.Properties.HeadContent)
{
content = SiteState.Properties.HeadContent;
StateHasChanged();
}
break;
}
}
public void Dispose()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;
}
}

View File

@ -273,6 +273,35 @@ namespace Oqtane.Modules
SiteState.Properties.ModuleVisibility = obj; SiteState.Properties.ModuleVisibility = obj;
} }
public void SetPageTitle(string title)
{
SiteState.Properties.PageTitle = title;
}
public void AddHeadContent(string content)
{
if (string.IsNullOrEmpty(SiteState.Properties.HeadContent))
{
SiteState.Properties.HeadContent = content;
}
else if (!SiteState.Properties.HeadContent.Contains(content))
{
SiteState.Properties.HeadContent += content;
}
}
public void AddBodyContent(string content)
{
if (string.IsNullOrEmpty(SiteState.Properties.BodyContent))
{
SiteState.Properties.BodyContent = content;
}
else if (!SiteState.Properties.BodyContent.Contains(content))
{
SiteState.Properties.BodyContent += content;
}
}
// logging methods // logging methods
public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args) public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args)
{ {

View File

@ -1,22 +1,36 @@
@namespace Oqtane.UI @namespace Oqtane.UI
@inject IJSRuntime JsRuntime @inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject SiteState SiteState
@DynamicComponent @DynamicComponent
@code { @code {
[CascadingParameter] PageState PageState { get; set; } [CascadingParameter] PageState PageState { get; set; }
RenderFragment DynamicComponent { get; set; } RenderFragment DynamicComponent { get; set; }
protected override void OnParametersSet() protected override void OnParametersSet()
{ {
// handle page redirection // handle page redirection
if (!string.IsNullOrEmpty(PageState.Page.Url)) if (!string.IsNullOrEmpty(PageState.Page.Url))
{ {
NavigationManager.NavigateTo(PageState.Page.Url); NavigationManager.NavigateTo(PageState.Page.Url);
return; return;
} }
// set page title
if (!string.IsNullOrEmpty(PageState.Page.Title))
{
SiteState.Properties.PageTitle = PageState.Page.Title;
}
else
{
SiteState.Properties.PageTitle = PageState.Site.Name + " - " + PageState.Page.Name;
}
// set page meta
SiteState.Properties.HeadContent = PageState.Page.Meta ?? "";
DynamicComponent = builder => DynamicComponent = builder =>
{ {
@ -45,15 +59,5 @@
} }
await interop.RemoveElementsById("app-stylesheet-page-", "", "app-stylesheet-page-" + batch + "-00"); await interop.RemoveElementsById("app-stylesheet-page-", "", "app-stylesheet-page-" + batch + "-00");
await interop.RemoveElementsById("app-stylesheet-module-", "", "app-stylesheet-module-" + batch + "-00"); await interop.RemoveElementsById("app-stylesheet-module-", "", "app-stylesheet-module-" + batch + "-00");
// set page title
if (!string.IsNullOrEmpty(PageState.Page.Title))
{
await interop.UpdateTitle(PageState.Page.Title);
}
else
{
await interop.UpdateTitle(PageState.Site.Name + " - " + PageState.Page.Name);
}
} }
} }

View File

@ -7,8 +7,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@Model.Title</title> <component type="typeof(Oqtane.Head)" render-mode="@((RenderMode)Enum.Parse(typeof(RenderMode), Model.RenderMode, true))" />
@Html.Raw(@Model.Meta)
<base href="~/" /> <base href="~/" />
<link id="app-favicon" rel="shortcut icon" type="image/x-icon" href="@Model.FavIcon" /> <link id="app-favicon" rel="shortcut icon" type="image/x-icon" href="@Model.FavIcon" />
@if (!string.IsNullOrEmpty(Model.PWAScript)) @if (!string.IsNullOrEmpty(Model.PWAScript))
@ -59,7 +58,8 @@
@Html.Raw(Model.PWAScript) @Html.Raw(Model.PWAScript)
} }
@Html.Raw(Model.BodyResources) @Html.Raw(Model.BodyResources)
} <component type="typeof(Oqtane.Body)" render-mode="@((RenderMode)Enum.Parse(typeof(RenderMode), Model.RenderMode, true))" />
}
else else
{ {
<div class="app-alert">@Model.Message</div> <div class="app-alert">@Model.Message</div>

View File

@ -65,8 +65,6 @@ namespace Oqtane.Pages
public string RemoteIPAddress = ""; public string RemoteIPAddress = "";
public string HeadResources = ""; public string HeadResources = "";
public string BodyResources = ""; public string BodyResources = "";
public string Title = "";
public string Meta = "";
public string FavIcon = "favicon.ico"; public string FavIcon = "favicon.ico";
public string PWAScript = ""; public string PWAScript = "";
public string ReconnectScript = ""; public string ReconnectScript = "";
@ -137,7 +135,6 @@ namespace Oqtane.Pages
{ {
PWAScript = CreatePWAScript(alias, site, route); PWAScript = CreatePWAScript(alias, site, route);
} }
Title = site.Name;
var ThemeType = site.DefaultThemeType; var ThemeType = site.DefaultThemeType;
// get jwt token for downstream APIs // get jwt token for downstream APIs
@ -163,17 +160,6 @@ namespace Oqtane.Pages
} }
if (page != null && !page.IsDeleted) if (page != null && !page.IsDeleted)
{ {
// set page title
if (!string.IsNullOrEmpty(page.Title))
{
Title = page.Title;
}
else
{
Title = Title + " - " + page.Name;
}
Meta = page.Meta;
// include theme resources // include theme resources
if (!string.IsNullOrEmpty(page.ThemeType)) if (!string.IsNullOrEmpty(page.ThemeType))
{ {