INavigationProvider
A navigation provider supplies the tree of links that the Navigation menu component renders. By default the menu shows the website's page tree. Implement INavigationProvider to fill a menu from somewhere else, such as your own data, a list the editor types in or an external system.
Interface declaration
namespace Veva.Core.Domain.Navigation; public interface INavigationProvider { Task<IEnumerable<NavigationNode>> GetNavigationNodes(NavigationNode parentNode); Task<NavigationNode> GetRootNode(); void SetContext(PageRequest pageRequest, DataBindingContext dataBindingContext); }
Example
This provider shows a flat list of links that the editor types into the provider's settings, and marks the link to the current page as selected.
using System.ComponentModel; using Veva.Core.Domain.DataBinding; using Veva.Core.Domain.Navigation; using Veva.Core.Domain.Pages; [DisplayName("Link list")] [Description("Shows a flat list of links that the editor types in")] public class LinkListNavigationProvider : INavigationProvider { private PageRequest _pageRequest; [EditorBrowsable] [DisplayName("Links")] [Description("The link text and the address of each link")] public Dictionary<string, string> Links { get; set; } public void SetContext(PageRequest pageRequest, DataBindingContext dataBindingContext) { _pageRequest = pageRequest; } public Task<NavigationNode> GetRootNode() { // IsRoot makes the menu ask this provider for the root's children. return Task.FromResult(new NavigationNode { Id = Guid.Empty, IsRoot = true, NavigationProvider = this }); } public Task<IEnumerable<NavigationNode>> GetNavigationNodes(NavigationNode parentNode) { if (!parentNode.IsRoot || Links == null) return Task.FromResult(Enumerable.Empty<NavigationNode>()); string currentPath = _pageRequest?.Uri?.AbsolutePath; var nodes = Links.Select(link => new NavigationNode { Id = Guid.NewGuid(), Text = link.Key, Url = new Url(link.Value), IsSelected = string.Equals(link.Value, currentPath, StringComparison.OrdinalIgnoreCase), // The menu asks this provider for the node's children; there are none here. NavigationProvider = this, }).ToList(); return Task.FromResult<IEnumerable<NavigationNode>>(nodes); } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
using Veva.Core.Application.DependencyInjection; using Veva.Core.Domain.Navigation; public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { // The key is the class name; the menu's content stores it when an editor picks the provider. services.AddNamed<INavigationProvider, LinkListNavigationProvider>(ServiceLifetime.Transient); }
Turning it on. Registering a provider adds it to the Navigation provider setting of the Navigation menu component. The list shows the class's [DisplayName] and [Description], and public read/write properties marked [EditorBrowsable] become the provider's settings, like Links above. The menu stores the provider under its registration name, which is the class name when you call AddNamed without a name, so renaming the class breaks the menus that use it. When the setting is empty the menu uses the page tree provider, registered as PageProvider.
Built-in providers. PageNavigationProvider (PageProvider) shows the website's pages. CustomNavigationProvider shows a menu built in the Navigation section of the admin. DocumentFolderNavigationProvider shows the folders of a document model as links to a page.
How the menu calls it. Before it builds the tree the menu calls SetContext with the page request and the data binding values at that place on the page; keep them for the other two calls. It then calls GetRootNode and asks for children with GetNavigationNodes, level by level. It only asks for the children of the root, of selected nodes, of nodes with a selected child, and of every node when the menu's Expanded setting is on, down to its Sub levels. It asks the provider in each node's NavigationProvider, so a node with none gets no children, and a node can hand its subtree to another provider.
What you set on a node. Text, Url and Target make the link. Set IsSelected on the node for the current page; the menu sets IsChildSelected on its parents, and Level, IsFirst, IsLast and IsSingle on every node it expands. ChildCount, Description, Properties and ProviderItem (your own object) are passed on to the menu's design view, which renders the markup.
Speed. The menu rebuilds its tree only when the provider, the page request, the data binding context or the Expanded and Sub levels settings change. It may still ask for many nodes in one render, so load your data once per SetContext and answer GetNavigationNodes from memory, as CustomNavigationProvider does. The provider is created from the service container, so constructor injection works.