48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
namespace SharepointToolbox.Web.Core.Models;
|
|
|
|
/// <summary>
|
|
/// Editable node for the visual folder-structure builder. SharePoint folders
|
|
/// are limited to 4 nesting levels here, matching the CSV template (Level1..Level4).
|
|
/// </summary>
|
|
public class FolderNode
|
|
{
|
|
public const int MaxDepth = 4;
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
public List<FolderNode> Children { get; } = new();
|
|
|
|
/// <summary>Flatten the tree into one <see cref="FolderStructureRow"/> per leaf path.</summary>
|
|
public static List<FolderStructureRow> Flatten(IEnumerable<FolderNode> roots)
|
|
{
|
|
var rows = new List<FolderStructureRow>();
|
|
foreach (var root in roots)
|
|
Walk(root, new List<string>(), rows);
|
|
return rows;
|
|
}
|
|
|
|
private static void Walk(FolderNode node, List<string> ancestors, List<FolderStructureRow> rows)
|
|
{
|
|
var name = node.Name.Trim();
|
|
if (string.IsNullOrEmpty(name)) return;
|
|
|
|
var path = new List<string>(ancestors) { name };
|
|
|
|
// Only emit a row for leaves; intermediate folders are created as ancestors of the leaf path.
|
|
if (node.Children.Count == 0)
|
|
{
|
|
rows.Add(new FolderStructureRow
|
|
{
|
|
Level1 = path.ElementAtOrDefault(0) ?? string.Empty,
|
|
Level2 = path.ElementAtOrDefault(1) ?? string.Empty,
|
|
Level3 = path.ElementAtOrDefault(2) ?? string.Empty,
|
|
Level4 = path.ElementAtOrDefault(3) ?? string.Empty,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (path.Count >= MaxDepth) return; // can't nest deeper
|
|
foreach (var child in node.Children)
|
|
Walk(child, path, rows);
|
|
}
|
|
}
|