-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathIslandNavigationNode.cs
More file actions
68 lines (54 loc) · 2.29 KB
/
IslandNavigationNode.cs
File metadata and controls
68 lines (54 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using Elastic.Documentation.Extensions;
namespace Elastic.Documentation.Navigation.V2;
/// <summary>
/// A nav island that wraps an existing toc node. When a page belongs to this island,
/// the sidebar shows only the island's tree with a back arrow to the parent section.
/// In the parent section's sidebar, the island renders as a normal folder link.
/// </summary>
public class IslandNavigationNode(
string label,
IRootNavigationItem<IDocumentationFile, INavigationItem> source,
INodeNavigationItem<INavigationModel, INavigationItem>? parent
) : INodeNavigationItem<INavigationModel, INavigationItem>
{
/// <summary>The Id of the wrapped toc root, used for island lookup by nav ownership.</summary>
public string SourceTocRootId { get; } = source.Id;
/// <summary>
/// The URL to use for the back arrow when this island is active.
/// Walks up the parent chain to find the nearest ancestor with a meaningful URL,
/// falling back to "/" if none is found.
/// </summary>
public string ParentUrl { get; } = ResolveParentUrl(parent);
private static string ResolveParentUrl(INodeNavigationItem<INavigationModel, INavigationItem>? parent)
{
var current = parent;
while (current is not null)
{
if (!string.IsNullOrEmpty(current.Url) && current.Url != "/")
return current.Url;
current = current.Parent;
}
return "/";
}
/// <inheritdoc />
public string Id { get; } = ShortId.Create("island", label);
/// <inheritdoc />
public string Url => source.Url;
/// <inheritdoc />
public string NavigationTitle { get; } = label;
/// <inheritdoc />
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot { get; } = parent?.NavigationRoot!;
/// <inheritdoc />
public INodeNavigationItem<INavigationModel, INavigationItem>? Parent { get; set; } = parent;
/// <inheritdoc />
public bool Hidden => source.Hidden;
/// <inheritdoc />
public int NavigationIndex { get; set; }
/// <inheritdoc />
public ILeafNavigationItem<INavigationModel> Index => source.Index;
/// <inheritdoc />
public IReadOnlyCollection<INavigationItem> NavigationItems => source.NavigationItems;
}