Seenginx/Seenginx/Components/FilesWithEditor.razor.cs

136 lines
3.4 KiB
C#
Raw Normal View History

2020-05-03 02:10:32 +02:00
using Blazorise;
using Microsoft.AspNetCore.Components;
2020-04-17 00:50:23 +02:00
using Microsoft.AspNetCore.Components.Web;
2020-04-23 01:32:20 +02:00
using Microsoft.JSInterop;
2020-04-17 00:50:23 +02:00
using Seenginx.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Seenginx.Components
{
public partial class FilesWithEditor<CFile> : ComponentBase
where CFile : ConfigFile
{
2020-07-02 19:39:14 +02:00
[Inject] public IJSRuntime JsRuntime { get; set; }
2020-04-23 01:32:20 +02:00
2020-07-02 19:39:14 +02:00
[Parameter] public List<CFile> Files { get; set; } = new List<CFile>();
2020-04-17 00:50:23 +02:00
2020-07-02 19:39:14 +02:00
[Parameter] public EventCallback<CFile> UpdateFile { get; set; }
2020-04-17 00:50:23 +02:00
2020-07-02 19:39:14 +02:00
[Parameter] public RenderFragment<CFile> Editor { get; set; } = null;
[Parameter] public EventCallback TestConfiguration { get; set; }
[Parameter] public EventCallback ShowAddFileModal { get; set; }
[Parameter] public EventCallback DeleteFileCallback { get; set; }
[Parameter] public EventCallback<CFile> SelectedFileChanged { get; set; }
[Parameter] public CFile SelectedFile { get; set; } = default;
2020-04-17 00:50:23 +02:00
2020-04-18 20:41:46 +02:00
protected string SearchInput { get; set; }
2020-04-17 00:50:23 +02:00
protected bool IsAnyFileSelected => SelectedFile != default;
2020-04-22 00:25:21 +02:00
protected string IsSelectedFileDeletable
{
get
{
if (SelectedFile != null && SelectedFile.CanBeDeleted)
return string.Empty;
else
return "is-hidden";
}
}
2020-04-17 00:50:23 +02:00
2020-04-24 00:18:16 +02:00
protected async override Task OnAfterRenderAsync(bool firstRender)
2020-04-17 00:50:23 +02:00
{
2020-07-01 19:36:22 +02:00
try
{
await JsRuntime.InvokeVoidAsync("InitEditor");
await base.OnAfterRenderAsync(firstRender);
}
catch (Exception ex)
{
throw ex;
}
2020-04-17 00:50:23 +02:00
}
2020-07-01 19:36:22 +02:00
protected void SearchInputChanged(string searchInput)
2020-04-17 00:50:23 +02:00
{
2020-04-18 20:41:46 +02:00
SearchInput = searchInput;
SearchFile();
}
private void SearchFile()
{
2020-07-03 00:37:23 +02:00
Files.ForEach(f =>
2020-04-18 20:41:46 +02:00
{
2020-07-03 00:37:23 +02:00
if (f.Name.ToLower().Contains(SearchInput.ToLower()))
f.Unhide();
2020-04-18 20:41:46 +02:00
else
2020-07-03 00:37:23 +02:00
f.Hide();
});
2020-04-17 00:50:23 +02:00
}
protected async Task OnFileClick(MouseEventArgs e, CFile file)
{
2020-04-22 00:25:21 +02:00
Files.ForEach(f => f.Deselect());
file.Select();
2020-05-02 18:57:42 +02:00
await JsRuntime.InvokeVoidAsync("UpdateEditor", file.Body);
2020-04-17 00:50:23 +02:00
await SelectedFileChanged.InvokeAsync(file);
}
2020-04-24 00:18:16 +02:00
protected async Task OnFileCloseClick(MouseEventArgs e)
2020-04-17 00:50:23 +02:00
{
2020-04-22 00:25:21 +02:00
Files.ForEach(f => f.Deselect());
SelectedFile = null;
2020-04-24 00:18:16 +02:00
await JsRuntime.InvokeVoidAsync("ClearEditor");
}
protected async Task OnSaveDraft(MouseEventArgs e)
{
var draftCode = await JsRuntime.InvokeAsync<string>("GetEditorCode");
SelectedFile.DraftBody = draftCode;
}
2020-07-02 19:39:14 +02:00
2020-04-24 00:18:16 +02:00
protected async Task OnUndoChanges(MouseEventArgs e)
{
2020-05-02 18:57:42 +02:00
SelectedFile.DraftBody = SelectedFile.Body;
await JsRuntime.InvokeVoidAsync("UpdateEditor", SelectedFile.Body);
2020-04-24 00:18:16 +02:00
}
2020-07-02 19:39:14 +02:00
2020-04-24 00:18:16 +02:00
protected async Task OnSave(MouseEventArgs e)
{
var draftCode = await JsRuntime.InvokeAsync<string>("GetEditorCode");
2020-05-02 18:57:42 +02:00
SelectedFile.Body = draftCode;
2020-04-24 00:18:16 +02:00
}
2020-05-03 02:10:32 +02:00
2020-04-24 00:18:16 +02:00
protected async Task OnTest(MouseEventArgs e)
{
2020-05-03 02:10:32 +02:00
await TestConfiguration.InvokeAsync(null);
2020-04-17 00:50:23 +02:00
}
2020-04-22 00:25:21 +02:00
2020-07-02 19:39:14 +02:00
public async Task OnAddDialog()
2020-04-17 00:50:23 +02:00
{
2020-07-02 19:39:14 +02:00
await ShowAddFileModal.InvokeAsync(null);
await JsRuntime.InvokeVoidAsync("ClearEditor");
if (SelectedFile != null)
{
await JsRuntime.InvokeVoidAsync("UpdateEditor", SelectedFile.Body);
SearchInput = string.Empty;
SearchFile();
}
2020-04-17 00:50:23 +02:00
}
2020-05-03 02:10:32 +02:00
2020-07-02 19:39:14 +02:00
protected async Task OnDeleteDialog()
2020-04-17 00:50:23 +02:00
{
2020-07-02 19:39:14 +02:00
await DeleteFileCallback.InvokeAsync(null);
await JsRuntime.InvokeVoidAsync("ClearEditor");
SearchInput = string.Empty;
SearchFile();
2020-04-17 00:50:23 +02:00
}
2020-05-03 02:10:32 +02:00
2020-04-17 00:50:23 +02:00
}
}