From ced2051704255bef2a2ec3bfcdadfd9936a8ffef Mon Sep 17 00:00:00 2001 From: Pavel Vesely Date: Mon, 27 Apr 2020 16:50:36 +0200 Subject: [PATCH] Theme base components refactoring --- .../Themes/Controls/Breadcrumbs.razor | 42 ++--- Oqtane.Client/Themes/Controls/Logo.razor | 17 +- Oqtane.Client/Themes/Controls/MenuBase.cs | 6 +- .../Themes/Controls/ModuleActions.razor | 125 +------------- .../Themes/Controls/ModuleActionsBase.cs | 154 ++++++++++++++++++ 5 files changed, 193 insertions(+), 151 deletions(-) create mode 100644 Oqtane.Client/Themes/Controls/ModuleActionsBase.cs diff --git a/Oqtane.Client/Themes/Controls/Breadcrumbs.razor b/Oqtane.Client/Themes/Controls/Breadcrumbs.razor index c0107601..7175f85d 100644 --- a/Oqtane.Client/Themes/Controls/Breadcrumbs.razor +++ b/Oqtane.Client/Themes/Controls/Breadcrumbs.razor @@ -1,32 +1,34 @@ @namespace Oqtane.Themes.Controls @inherits ThemeControlBase -@if (breadcrumbs != string.Empty) +@if (BreadCrumbPages.Any()) { - @((MarkupString)breadcrumbs) + } @code { - string breadcrumbs = string.Empty; - protected override void OnParametersSet() + protected IEnumerable BreadCrumbPages => GetBreadCrumbPages().Reverse().ToList(); + + protected string ActiveClass(Page page) { - breadcrumbs = string.Empty; - int? pageid = PageState.Page.PageId; - for (int i = PageState.Pages.Count - 1; i >= 0; i--) + return (page.PageId == PageState.Page.PageId) ? " active" : string.Empty; + } + + private IEnumerable GetBreadCrumbPages() + { + var page = PageState.Page; + do { - var p = PageState.Pages[i]; - if (p.PageId == pageid) - { - breadcrumbs = "
  • " + p.Name + "
  • " + breadcrumbs; - pageid = p.ParentId; - } - } - - if (breadcrumbs != "") - { - breadcrumbs = "
      " + breadcrumbs + "
    "; - } + yield return page; + page = PageState.Pages.FirstOrDefault(p => page != null && p.PageId == page.ParentId); + } while (page != null); } } diff --git a/Oqtane.Client/Themes/Controls/Logo.razor b/Oqtane.Client/Themes/Controls/Logo.razor index ee8151f5..998acfd6 100644 --- a/Oqtane.Client/Themes/Controls/Logo.razor +++ b/Oqtane.Client/Themes/Controls/Logo.razor @@ -2,17 +2,20 @@ @inherits ThemeControlBase @inject NavigationManager NavigationManager -@((MarkupString)logo) +@if (PageState.Site.LogoFileId != null) +{ + + @PageState.Site.Name + +} @code { - string logo = ""; - - protected override void OnParametersSet() + string Href { - if (PageState.Site.LogoFileId != null) + get { var uri = new Uri(NavigationManager.Uri); - logo = "\"""; + return $"{uri.Scheme}://{uri.Authority}"; } } -} \ No newline at end of file +} diff --git a/Oqtane.Client/Themes/Controls/MenuBase.cs b/Oqtane.Client/Themes/Controls/MenuBase.cs index 03eecdef..c63f0dc8 100644 --- a/Oqtane.Client/Themes/Controls/MenuBase.cs +++ b/Oqtane.Client/Themes/Controls/MenuBase.cs @@ -8,13 +8,11 @@ namespace Oqtane.Themes.Controls { public class MenuBase : ThemeControlBase { - private List _menuPages; - - protected IEnumerable MenuPages => _menuPages ?? (_menuPages = GetMenuPages().ToList()); + protected IEnumerable MenuPages => GetMenuPages().ToList(); protected string GetTarget(Page page) { - return page.Url.StartsWith("http") ? "_new" : string.Empty; + return page.Url != null && page.Url.StartsWith("http") ? "_new" : string.Empty; } protected string GetUrl(Page page) diff --git a/Oqtane.Client/Themes/Controls/ModuleActions.razor b/Oqtane.Client/Themes/Controls/ModuleActions.razor index a3d15e8c..6ca55f1a 100644 --- a/Oqtane.Client/Themes/Controls/ModuleActions.razor +++ b/Oqtane.Client/Themes/Controls/ModuleActions.razor @@ -1,135 +1,20 @@ @namespace Oqtane.Themes.Controls -@inherits ContainerBase -@inject NavigationManager NavigationManager -@inject IUserService UserService -@inject IPageModuleService PageModuleService +@inherits ModuleActionsBase @if (PageState.EditMode && !PageState.Page.EditMode && UserSecurity.IsAuthorized(PageState.User,PermissionNames.Edit, ModuleState.Permissions)) { } - -@code { - private List actions; - - protected override void OnParametersSet() - { - if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User,PermissionNames.Edit, ModuleState.Permissions)) - { - actions = new List(); - actions.Add(new ActionViewModel { Action = "settings", Name = "Manage Settings" }); - - if (ModuleState.ModuleDefinition != null && ModuleState.ModuleDefinition.ServerManagerType != "") - { - actions.Add(new ActionViewModel { Action = "import", Name = "Import Content" }); - actions.Add(new ActionViewModel { Action = "export", Name = "Export Content" }); - } - - actions.Add(new ActionViewModel { Action = "delete", Name = "Delete Module" }); - actions.Add(new ActionViewModel { Action = "", Name = "" }); - - if (ModuleState.PaneModuleIndex > 0) - { - actions.Add(new ActionViewModel { Action = "<<", Name = "Move To Top" }); - } - - if (ModuleState.PaneModuleIndex > 0) - { - actions.Add(new ActionViewModel { Action = "<", Name = "Move Up" }); - } - - if (ModuleState.PaneModuleIndex < (ModuleState.PaneModuleCount - 1)) - { - actions.Add(new ActionViewModel { Action = ">", Name = "Move Down" }); - } - - if (ModuleState.PaneModuleIndex < (ModuleState.PaneModuleCount - 1)) - { - actions.Add(new ActionViewModel { Action = ">>", Name = "Move To Bottom" }); - } - - foreach (string pane in PageState.Page.Panes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) - { - if (pane != ModuleState.Pane) - { - actions.Add(new ActionViewModel { Action = pane, Name = "Move To " + pane + " Pane" }); - } - } - } - } - - protected async Task ModuleAction(string action) - { - if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User,PermissionNames.Edit, ModuleState.Permissions)) - { - PageModule pagemodule = await PageModuleService.GetPageModuleAsync(ModuleState.PageModuleId); - - string url = NavigateUrl(); - switch (action) - { - case "<<": - pagemodule.Order = 0; - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - break; - case "<": - pagemodule.Order -= 3; - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - break; - case ">": - pagemodule.Order += 3; - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - break; - case ">>": - pagemodule.Order = int.MaxValue; - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - break; - case "settings": - url = EditUrl(pagemodule.ModuleId, "Settings"); - break; - case "import": - url = EditUrl(pagemodule.ModuleId, "Import"); - break; - case "export": - url = EditUrl(pagemodule.ModuleId, "Export"); - break; - case "delete": - pagemodule.IsDeleted = true; - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - break; - default: // move to pane - string pane = pagemodule.Pane; - pagemodule.Pane = action; - pagemodule.Order = int.MaxValue; // add to bottom of pane - await PageModuleService.UpdatePageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pane); - break; - } - NavigationManager.NavigateTo(url); - } - } - - public class ActionViewModel - { - public string Action { set; get; } - public string Name { set; get; } - } -} diff --git a/Oqtane.Client/Themes/Controls/ModuleActionsBase.cs b/Oqtane.Client/Themes/Controls/ModuleActionsBase.cs new file mode 100644 index 00000000..c840128b --- /dev/null +++ b/Oqtane.Client/Themes/Controls/ModuleActionsBase.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using Oqtane.Models; +using Oqtane.Security; +using Oqtane.Services; +using Oqtane.Shared; + +// ReSharper disable UnassignedGetOnlyAutoProperty +// ReSharper disable MemberCanBePrivate.Global + +namespace Oqtane.Themes.Controls +{ + public class ModuleActionsBase : ContainerBase + { + [Inject]public NavigationManager NavigationManager{get; set;} + [Inject]public IPageModuleService PageModuleService{get; set;} + + protected List Actions; + + protected override void OnParametersSet() + { + Actions = GetActions(); + } + + protected virtual List GetActions() + { + var actionList = new List(); + if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User,PermissionNames.Edit, ModuleState.Permissions)) + { + + actionList.Add(new ActionViewModel {Name = "Manage Settings", Action = async (u,m) => Settings(u, m)}); + + if (ModuleState.ModuleDefinition != null && ModuleState.ModuleDefinition.ServerManagerType != "") + { + actionList.Add(new ActionViewModel {Name = "Import Content", Action = async(u,m)=> EditUrl(m.ModuleId, "Import")}); + actionList.Add(new ActionViewModel {Name = "Export Content", Action = async(u,m)=> EditUrl(m.ModuleId, "Export")}); + } + + actionList.Add(new ActionViewModel {Name = "Delete Module" , Action = async (u,m) => await DeleteModule(u, m)}); + actionList.Add(new ActionViewModel {Name = "" }); + + if (ModuleState.PaneModuleIndex > 0) + { + actionList.Add(new ActionViewModel {Name = "Move To Top" , Action = async (s,m) => await MoveTop(s, m)}); + } + + if (ModuleState.PaneModuleIndex > 0) + { + actionList.Add(new ActionViewModel {Name = "Move Up" , Action = async (s,m) => await MoveUp(s, m)}); + } + + if (ModuleState.PaneModuleIndex < (ModuleState.PaneModuleCount - 1)) + { + actionList.Add(new ActionViewModel {Name = "Move Down", Action = async (s,m) => await MoveDown(s, m) }); + } + + if (ModuleState.PaneModuleIndex < (ModuleState.PaneModuleCount - 1)) + { + actionList.Add(new ActionViewModel {Name = "Move To Bottom", Action = async (s,m) => await MoveBottom(s, m) }); + } + + foreach (string pane in PageState.Page.Panes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + { + if (pane != ModuleState.Pane) + { + actionList.Add(new ActionViewModel {Name = "Move To " + pane + " Pane", Action = async (s,m) => await MoveToPane(s,pane, m) }); + } + } + } + return actionList; + } + + protected async Task ModuleAction(ActionViewModel action) + { + if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User,PermissionNames.Edit, ModuleState.Permissions)) + { + PageModule pagemodule = await PageModuleService.GetPageModuleAsync(ModuleState.PageModuleId); + + string url = NavigateUrl(); + + if (action.Action!=null) + { + url = await action.Action(url, pagemodule); + } + NavigationManager.NavigateTo(url); + } + } + + private async Task MoveToPane(string url, string newPane, PageModule pagemodule) + { + string oldPane = pagemodule.Pane; + pagemodule.Pane = newPane; + pagemodule.Order = int.MaxValue; // add to bottom of pane + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, oldPane); + return url; + } + + private async Task DeleteModule(string url,PageModule pagemodule) + { + pagemodule.IsDeleted = true; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + return url; + } + + private string Settings(string url, PageModule pagemodule) + { + url = EditUrl(pagemodule.ModuleId, "Settings"); + return url; + } + + private async Task MoveTop(string s, PageModule pagemodule) + { + pagemodule.Order = 0; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + return s; + } + + private async Task MoveBottom(string s, PageModule pagemodule) + { + pagemodule.Order = int.MaxValue; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + return s; + } + private async Task MoveUp(string s, PageModule pagemodule) + { + pagemodule.Order -= 3; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + return s; + } + private async Task MoveDown(string s, PageModule pagemodule) + { + pagemodule.Order += 3; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + return s; + } + + public class ActionViewModel + { + public string Name { set; get; } + + public Func> Action { set; get; } + + } + } +}