// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.Web.AdminPanel.Pages; using System.Globalization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; using MUnique.OpenMU.DataModel.Configuration; using MUnique.OpenMU.DataModel.Entities; using MUnique.OpenMU.Web.AdminPanel.Properties; using MUnique.OpenMU.Web.Shared.Components.Form; using MUnique.OpenMU.Web.Shared.Components.ItemEdit; /// /// A generic edit page, which shows an for the given and . /// [Route("/edit-config/{typeString}/")] [Route("/edit-config/{typeString}/{id:guid}")] [Route("/edit-config/{typeString}/{id:guid}/hide-collections")] public sealed class EditConfig : EditBase { private static readonly IDictionary> EditorPages = new Dictionary> { { typeof(GameMapDefinition), new List<(string, string)> { (Resources.MapEditor, "/map-editor/{0}") } }, }; /// /// Gets or sets the optional search term to pre-filter fields. /// [SupplyParameterFromQuery(Name = "search")] public string? SearchTerm { get; set; } /// protected override void AddFormToRenderTree(RenderTreeBuilder builder, ref int currentSequence) { var hideCollections = this.NavigationManager.Uri.EndsWith("hide-collections"); if (this.Type == typeof(Item)) { builder.OpenComponent(++currentSequence, typeof(ItemEdit)); builder.AddAttribute(++currentSequence, nameof(ItemEdit.Item), this.Model); builder.AddAttribute(++currentSequence, nameof(ItemEdit.OnValidSubmit), EventCallback.Factory.Create(this, this.SaveChangesAsync)); builder.CloseComponent(); } else { builder.OpenComponent(++currentSequence, typeof(AutoForm<>).MakeGenericType(this.Type!)); builder.AddAttribute(++currentSequence, nameof(AutoForm.Model), this.Model); builder.AddAttribute(++currentSequence, nameof(AutoForm.HideCollections), hideCollections); builder.AddAttribute(++currentSequence, nameof(AutoForm.SearchTerm), this.SearchTerm); builder.AddAttribute(++currentSequence, nameof(AutoForm.OnValidSubmit), EventCallback.Factory.Create(this, this.SaveChangesAsync)); builder.AddAttribute(++currentSequence, nameof(AutoForm.OnRefresh), EventCallback.Factory.Create(this, this.RefreshAsync)); builder.CloseComponent(); } } /// protected override string? GetEditorsMarkup() { StringBuilder? stringBuilder = null; if (this.Type is not null && (EditorPages.TryGetValue(this.Type, out var editors) || (this.Type.BaseType is { } baseType && EditorPages.TryGetValue(baseType, out editors)))) { foreach (var editor in editors) { var uri = string.Format(CultureInfo.InvariantCulture, editor.Path, this.Id); stringBuilder ??= new StringBuilder(); stringBuilder.Append($@"

{editor.Caption}

"); } } return stringBuilder?.ToString(); } }