312 lines
9.7 KiB
Plaintext
312 lines
9.7 KiB
Plaintext
@using System
|
|
@using Oqtane.Services
|
|
@using Oqtane.Models
|
|
@using System.Linq
|
|
@using System.Collections.Generic
|
|
@using Oqtane.Shared
|
|
@using Microsoft.JSInterop
|
|
@inject IUriHelper UriHelper
|
|
@inject IJSRuntime jsRuntime
|
|
@inject ITenantService TenantService
|
|
@inject ISiteService SiteService
|
|
@inject IPageService PageService
|
|
@inject IUserService UserService
|
|
@inject IModuleService ModuleService
|
|
@inject IModuleDefinitionService ModuleDefinitionService
|
|
@inject ISkinService SkinService
|
|
|
|
@DynamicComponent
|
|
|
|
@functions {
|
|
|
|
[CascadingParameter] PageState PageState { get; set; }
|
|
|
|
[Parameter] Action<PageState> OnStateChange { get; set; }
|
|
|
|
RenderFragment DynamicComponent { get; set; }
|
|
private string _absoluteUri;
|
|
string alias;
|
|
Site site;
|
|
List<Page> pages;
|
|
Page page;
|
|
User user;
|
|
List<Module> modules;
|
|
PageState pagestate;
|
|
|
|
protected override void OnInit()
|
|
{
|
|
_absoluteUri = UriHelper.GetAbsoluteUri();
|
|
UriHelper.OnLocationChanged += OnLocationChanged;
|
|
|
|
DynamicComponent = builder =>
|
|
{
|
|
if (pagestate != null)
|
|
{
|
|
builder.OpenComponent(0, Type.GetType(Constants.DefaultPage));
|
|
builder.CloseComponent();
|
|
}
|
|
};
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
UriHelper.OnLocationChanged -= OnLocationChanged;
|
|
}
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
if (PageState == null)
|
|
{
|
|
await Refresh();
|
|
}
|
|
}
|
|
|
|
private async Task Refresh()
|
|
{
|
|
List<ModuleDefinition> moduledefinitions = await ModuleDefinitionService.GetModuleDefinitionsAsync();
|
|
List<Models.Skin> skins = await SkinService.GetSkinsAsync();
|
|
|
|
bool reload = false;
|
|
if (PageState == null)
|
|
{
|
|
Tenant tenant = await TenantService.GetTenantAsync();
|
|
site = await SiteService.GetSiteAsync(tenant.SiteId);
|
|
alias = Utilities.GetAlias(_absoluteUri);
|
|
}
|
|
else
|
|
{
|
|
site = PageState.Site;
|
|
alias = PageState.Alias;
|
|
}
|
|
if (Utilities.GetAlias(_absoluteUri) != alias)
|
|
{
|
|
Tenant tenant = await TenantService.GetTenantAsync();
|
|
site = await SiteService.GetSiteAsync(tenant.SiteId);
|
|
alias = Utilities.GetAlias(_absoluteUri);
|
|
reload = true;
|
|
}
|
|
if (site != null)
|
|
{
|
|
var interop = new Interop(jsRuntime);
|
|
string userid = await interop.GetCookie("user");
|
|
|
|
if (PageState == null || reload == true)
|
|
{
|
|
if (!string.IsNullOrEmpty(userid))
|
|
{
|
|
user = await UserService.GetUserAsync(int.Parse(userid));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
user = PageState.User;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(userid))
|
|
{
|
|
if (user != null && user.UserId != int.Parse(userid))
|
|
{
|
|
user = await UserService.GetUserAsync(int.Parse(userid));
|
|
}
|
|
// this is a hack for server-side Blazor where JSInterop is not working OnInit() which means the userid is not being retrieved from the cookie on the initial render and is not being loaded into PageState
|
|
if (user == null)
|
|
{
|
|
user = await UserService.GetUserAsync(int.Parse(userid));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
user = null;
|
|
}
|
|
|
|
string path = new Uri(_absoluteUri).PathAndQuery.Substring(1);
|
|
if (alias != "")
|
|
{
|
|
path = path.Replace(alias, "");
|
|
}
|
|
Dictionary<string, string> querystring = ParseQueryString(path);
|
|
|
|
if (querystring.ContainsKey("reload"))
|
|
{
|
|
reload = true;
|
|
}
|
|
|
|
if (PageState == null || reload == true)
|
|
{
|
|
pages = await PageService.GetPagesAsync(site.SiteId);
|
|
}
|
|
else
|
|
{
|
|
pages = PageState.Pages;
|
|
}
|
|
|
|
if (path.IndexOf("?") != -1)
|
|
{
|
|
path = path.Substring(0, path.IndexOf("?"));
|
|
}
|
|
|
|
if (PageState == null || reload == true)
|
|
{
|
|
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
page = PageState.Page;
|
|
}
|
|
if (page.Path != path)
|
|
{
|
|
page = pages.Where(item => item.Path == path).FirstOrDefault();
|
|
reload = true;
|
|
}
|
|
|
|
if (page != null)
|
|
{
|
|
// check if user is authorized to view page
|
|
if (UserService.IsAuthorized(user, page.ViewPermissions))
|
|
{
|
|
pagestate = new PageState();
|
|
pagestate.Alias = alias;
|
|
pagestate.Site = site;
|
|
pagestate.Pages = pages;
|
|
pagestate.Page = page;
|
|
pagestate.User = user;
|
|
pagestate.Uri = new Uri(_absoluteUri, UriKind.Absolute);
|
|
pagestate.QueryString = querystring;
|
|
pagestate.ModuleId = -1;
|
|
pagestate.Control = "";
|
|
pagestate.Mode = "client";
|
|
|
|
if (querystring.ContainsKey("mid"))
|
|
{
|
|
pagestate.ModuleId = int.Parse(querystring["mid"]);
|
|
}
|
|
if (querystring.ContainsKey("ctl"))
|
|
{
|
|
pagestate.Control = querystring["ctl"];
|
|
}
|
|
if (PageState != null && (PageState.ModuleId != pagestate.ModuleId || PageState.Control != pagestate.Control))
|
|
{
|
|
reload = true;
|
|
}
|
|
|
|
if (PageState == null || reload == true)
|
|
{
|
|
modules = await ModuleService.GetModulesAsync(page.PageId);
|
|
modules = ProcessModules(modules, moduledefinitions, pagestate.Control, page.Panes);
|
|
}
|
|
else
|
|
{
|
|
modules = PageState.Modules;
|
|
}
|
|
pagestate.Modules = modules;
|
|
|
|
OnStateChange?.Invoke(pagestate);
|
|
}
|
|
else
|
|
{
|
|
// user is not authorized to view page
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// page does not exist
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// site does not exist
|
|
}
|
|
StateHasChanged();
|
|
}
|
|
|
|
private async void OnLocationChanged(object sender, string AbsoluteUri)
|
|
{
|
|
_absoluteUri = AbsoluteUri;
|
|
await LocationChanged();
|
|
}
|
|
|
|
public async Task LocationChanged()
|
|
{
|
|
await Refresh();
|
|
}
|
|
|
|
private Dictionary<string, string> ParseQueryString(string path)
|
|
{
|
|
Dictionary<string, string> querystring = new Dictionary<string, string>();
|
|
if (path.IndexOf("?") != -1)
|
|
{
|
|
foreach (string kvp in path.Substring(path.IndexOf("?") + 1).Split('&'))
|
|
{
|
|
if (kvp != "")
|
|
{
|
|
if (kvp.Contains("="))
|
|
{
|
|
string[] pair = kvp.Split('=');
|
|
querystring.Add(pair[0], pair[1]);
|
|
}
|
|
else
|
|
{
|
|
querystring.Add(kvp, "true"); // default querystring when no value is provided
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return querystring;
|
|
}
|
|
|
|
private List<Module> ProcessModules(List<Module> modules, List<ModuleDefinition> moduledefinitions, string control, string panes)
|
|
{
|
|
ModuleDefinition moduledefinition;
|
|
|
|
if (control == "")
|
|
{
|
|
control = Constants.DefaultControl;
|
|
}
|
|
|
|
Dictionary<string, int> paneindex = new Dictionary<string, int>();
|
|
foreach (Module module in modules)
|
|
{
|
|
// set the type based on the template and action
|
|
moduledefinition = moduledefinitions.Where(item => item.ModuleDefinitionName == module.ModuleDefinitionName).FirstOrDefault();
|
|
if (moduledefinition != null)
|
|
{
|
|
string typename = moduledefinition.ControlTypeTemplate;
|
|
if (moduledefinition.ControlTypeRoutes != "")
|
|
{
|
|
foreach (string route in moduledefinition.ControlTypeRoutes.Split(';'))
|
|
{
|
|
if (route.StartsWith(control + "="))
|
|
{
|
|
typename = route.Replace(control + "=", "");
|
|
}
|
|
}
|
|
}
|
|
module.ModuleType = typename.Replace("{Control}", control);
|
|
}
|
|
|
|
// ensure module's pane exists in current page and if not, assign it to the Admin pane
|
|
if (!panes.Contains(module.Pane))
|
|
{
|
|
module.Pane = Constants.AdminPane;
|
|
}
|
|
|
|
// calculate module position within pane
|
|
if (paneindex.ContainsKey(module.Pane))
|
|
{
|
|
paneindex[module.Pane] += 1;
|
|
}
|
|
else
|
|
{
|
|
paneindex.Add(module.Pane, 0);
|
|
}
|
|
module.PaneModuleIndex = paneindex[module.Pane];
|
|
}
|
|
|
|
foreach (Module module in modules)
|
|
{
|
|
module.PaneModuleCount = paneindex[module.Pane] + 1;
|
|
}
|
|
return modules;
|
|
}
|
|
|
|
} |