| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @using Chronicis.Client.Components.Shared |
| | | 3 | | |
| | | 4 | | <div class="public-tree-item @(_isExpanded ? "expanded" : "") @(IsSelected ? "selected" : "")" |
| | | 5 | | style="padding-left: @(Level * 12)px;"> |
| | | 6 | | |
| | | 7 | | <div class="public-tree-item-content" @onclick="OnItemClick"> |
| | 0 | 8 | | @if (Article.HasChildren) |
| | | 9 | | { |
| | | 10 | | <span class="public-tree-expand-btn" @onclick="ToggleExpand" @onclick:stopPropagation="true"> |
| | | 11 | | <MudIcon Icon="@(_isExpanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ChevronRight)" |
| | | 12 | | Size="Size.Small" /> |
| | | 13 | | </span> |
| | | 14 | | } |
| | | 15 | | else |
| | | 16 | | { |
| | | 17 | | <span class="public-tree-spacer"></span> |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | <span class="public-tree-icon"> |
| | | 21 | | <IconDisplay Icon="@Article.IconEmoji" DefaultIcon="" CssClass="tree-icon" /> |
| | | 22 | | </span> |
| | | 23 | | |
| | 0 | 24 | | <span class="public-tree-title" title="@Article.Title">@Article.Title</span> |
| | | 25 | | </div> |
| | | 26 | | |
| | 0 | 27 | | @if (_isExpanded && Article.Children?.Any() == true) |
| | | 28 | | { |
| | | 29 | | <div class="public-tree-children"> |
| | 0 | 30 | | @foreach (var child in Article.Children.OrderBy(c => c.Title)) |
| | | 31 | | { |
| | | 32 | | @* Virtual groups don't contribute to path - pass through the parent's real path *@ |
| | | 33 | | <PublicArticleTreeItem Article="@child" |
| | | 34 | | PublicSlug="@PublicSlug" |
| | | 35 | | CurrentPath="@CurrentPath" |
| | | 36 | | ParentPath="@GetPathForChildren()" |
| | | 37 | | Level="@(Level + 1)" |
| | | 38 | | OnArticleSelected="OnArticleSelected" /> |
| | | 39 | | } |
| | | 40 | | </div> |
| | | 41 | | } |
| | | 42 | | </div> |
| | | 43 | | |
| | | 44 | | @code { |
| | | 45 | | [Parameter] |
| | 0 | 46 | | public ArticleTreeDto Article { get; set; } = null!; |
| | | 47 | | |
| | | 48 | | [Parameter] |
| | 0 | 49 | | public string PublicSlug { get; set; } = string.Empty; |
| | | 50 | | |
| | | 51 | | [Parameter] |
| | 0 | 52 | | public string? CurrentPath { get; set; } |
| | | 53 | | |
| | | 54 | | [Parameter] |
| | 0 | 55 | | public string? ParentPath { get; set; } |
| | | 56 | | |
| | | 57 | | [Parameter] |
| | 0 | 58 | | public int Level { get; set; } = 0; |
| | | 59 | | |
| | | 60 | | [Parameter] |
| | 0 | 61 | | public EventCallback<string> OnArticleSelected { get; set; } |
| | | 62 | | |
| | | 63 | | private bool _isExpanded = false; |
| | | 64 | | |
| | | 65 | | private bool IsSelected |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | | 69 | | // Virtual groups can't be selected |
| | 0 | 70 | | if (Article.IsVirtualGroup) return false; |
| | | 71 | | |
| | 0 | 72 | | var itemPath = GetArticlePath(); |
| | 0 | 73 | | return !string.IsNullOrEmpty(CurrentPath) && |
| | 0 | 74 | | CurrentPath.Equals(itemPath, StringComparison.OrdinalIgnoreCase); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | protected override void OnParametersSet() |
| | | 79 | | { |
| | | 80 | | // Auto-expand if this node or any descendant matches the current path |
| | 0 | 81 | | if (!string.IsNullOrEmpty(CurrentPath)) |
| | | 82 | | { |
| | | 83 | | // Check if THIS node is selected (for non-virtual groups) |
| | 0 | 84 | | if (!Article.IsVirtualGroup) |
| | | 85 | | { |
| | 0 | 86 | | var myPath = GetArticlePath(); |
| | 0 | 87 | | if (CurrentPath.StartsWith(myPath, StringComparison.OrdinalIgnoreCase)) |
| | | 88 | | { |
| | | 89 | | // Current path starts with our path - we're on the path to the selected article |
| | | 90 | | // Expand if we have children and the path continues past us |
| | 0 | 91 | | if (Article.HasChildren && |
| | 0 | 92 | | (CurrentPath.Length > myPath.Length || HasMatchingDescendant())) |
| | | 93 | | { |
| | 0 | 94 | | _isExpanded = true; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | | 100 | | // Virtual groups: check if any descendant matches |
| | 0 | 101 | | if (HasMatchingDescendant()) |
| | | 102 | | { |
| | 0 | 103 | | _isExpanded = true; |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | private bool HasMatchingDescendant() |
| | | 110 | | { |
| | 0 | 111 | | if (Article.Children == null || string.IsNullOrEmpty(CurrentPath)) |
| | 0 | 112 | | return false; |
| | | 113 | | |
| | | 114 | | // Get the path prefix for children of this node |
| | 0 | 115 | | var childPathPrefix = GetPathForChildren() ?? ""; |
| | | 116 | | |
| | 0 | 117 | | return CheckDescendantsRecursively(Article.Children, childPathPrefix); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | private bool CheckDescendantsRecursively(IEnumerable<ArticleTreeDto> children, string pathPrefix) |
| | | 121 | | { |
| | 0 | 122 | | foreach (var child in children) |
| | | 123 | | { |
| | 0 | 124 | | if (!child.IsVirtualGroup) |
| | | 125 | | { |
| | | 126 | | // Build the child's full path |
| | 0 | 127 | | var childPath = string.IsNullOrEmpty(pathPrefix) |
| | 0 | 128 | | ? child.Slug |
| | 0 | 129 | | : $"{pathPrefix}/{child.Slug}"; |
| | | 130 | | |
| | 0 | 131 | | if (CurrentPath!.Equals(childPath, StringComparison.OrdinalIgnoreCase) || |
| | 0 | 132 | | CurrentPath.StartsWith(childPath + "/", StringComparison.OrdinalIgnoreCase)) |
| | | 133 | | { |
| | 0 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | // Check this child's descendants |
| | 0 | 138 | | if (child.Children?.Any() == true) |
| | | 139 | | { |
| | 0 | 140 | | if (CheckDescendantsRecursively(child.Children, childPath)) |
| | 0 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | else |
| | | 145 | | { |
| | | 146 | | // Virtual groups pass through the path prefix |
| | 0 | 147 | | if (child.Children?.Any() == true) |
| | | 148 | | { |
| | 0 | 149 | | if (CheckDescendantsRecursively(child.Children, pathPrefix)) |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return false; |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Get the path for this article (only for real articles, not virtual groups) |
| | | 160 | | /// </summary> |
| | | 161 | | private string GetArticlePath() |
| | | 162 | | { |
| | 0 | 163 | | if (Article.IsVirtualGroup) return string.Empty; |
| | 0 | 164 | | return BuildPathForArticle(Article); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Build path for a specific article, using ParentPath as base |
| | | 169 | | /// </summary> |
| | | 170 | | private string BuildPathForArticle(ArticleTreeDto article) |
| | | 171 | | { |
| | 0 | 172 | | if (string.IsNullOrEmpty(ParentPath)) |
| | | 173 | | { |
| | 0 | 174 | | return article.Slug; |
| | | 175 | | } |
| | 0 | 176 | | return $"{ParentPath}/{article.Slug}"; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Get the path to pass to children. |
| | | 181 | | /// Virtual groups don't contribute to the path - pass through ParentPath. |
| | | 182 | | /// Real articles add their slug to the path. |
| | | 183 | | /// </summary> |
| | | 184 | | private string? GetPathForChildren() |
| | | 185 | | { |
| | 0 | 186 | | if (Article.IsVirtualGroup) |
| | | 187 | | { |
| | | 188 | | // Virtual groups don't contribute to path - pass through parent's path |
| | 0 | 189 | | return ParentPath; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | // Real articles add their slug |
| | 0 | 193 | | return GetArticlePath(); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private void ToggleExpand() |
| | | 197 | | { |
| | 0 | 198 | | _isExpanded = !_isExpanded; |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | private async Task OnItemClick() |
| | | 202 | | { |
| | | 203 | | // Virtual groups only expand/collapse, don't navigate |
| | 0 | 204 | | if (Article.IsVirtualGroup) |
| | | 205 | | { |
| | 0 | 206 | | _isExpanded = !_isExpanded; |
| | 0 | 207 | | return; |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | var path = GetArticlePath(); |
| | 0 | 211 | | await OnArticleSelected.InvokeAsync(path); |
| | 0 | 212 | | } |
| | | 213 | | } |