@page "/scripts/{activeTab?}"
@inherits AuthComponentBase
@using System.Collections
@inject IDataService DataService
Saved Scripts
Run Script
Script Schedules
@code {
private readonly List _treeNodes = new();
private IEnumerable _allScripts = Enumerable.Empty();
private bool _showOnlyMyScripts = true;
[Parameter]
public string ActiveTab { get; set; }
public bool ShowOnlyMyScripts
{
get => _showOnlyMyScripts;
set
{
_showOnlyMyScripts = value;
_treeNodes.Clear();
}
}
public IEnumerable TreeNodes
{
get
{
if (_treeNodes?.Any() == true)
{
return _treeNodes;
}
RefreshTreeNodes();
return _treeNodes;
}
}
public string GetItemIconCss(ScriptTreeNode viewModel)
{
if (viewModel.ItemType == TreeItemType.Folder)
{
return "oi oi-folder text-warning";
}
return "oi oi-script text-success";
}
public async Task RefreshScripts()
{
_treeNodes.Clear();
_allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);
}
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await RefreshScripts();
}
private void CreateTreeNode(SavedScript script)
{
var root = _treeNodes;
ScriptTreeNode? targetParent = null;
if (!string.IsNullOrWhiteSpace(script.FolderPath))
{
var paths = script.FolderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < paths.Length; i++)
{
var existingParent = root.Find(x => x.Name == paths[i]);
if (existingParent is null)
{
var newItem = new ScriptTreeNode()
{
Name = paths[i],
ItemType = TreeItemType.Folder,
ParentNode = existingParent
};
root.Add(newItem);
root = newItem.ChildItems;
targetParent = newItem;
}
else
{
root = existingParent.ChildItems;
targetParent = existingParent;
}
}
}
var scriptNode = new ScriptTreeNode()
{
Name = script.Name,
Script = script,
ItemType = TreeItemType.Item,
ParentNode = targetParent
};
root.Add(scriptNode);
}
private void RefreshTreeNodes()
{
_treeNodes.Clear();
foreach (var script in _allScripts)
{
var showScript = ShowOnlyMyScripts ?
script.CreatorId == User.Id :
script.CreatorId == User.Id || script.IsPublic;
if (!showScript)
{
continue;
}
CreateTreeNode(script);
}
_treeNodes.Sort((a, b) =>
{
if (a.ItemType != b.ItemType)
{
return Comparer.Default.Compare(a.ItemType, b.ItemType);
}
return Comparer.Default.Compare(a.Name, b.Name);
});
}
}