Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/cloudscribe.Web.Navigation/CachingNavigationViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ public CachingNavigationViewComponent(
// In a simplecontent system I'd probably need to use the IHandlePageCreated (etc)
// hooks to clear a navcache of known name.

public async Task<IViewComponentResult> InvokeAsync(string viewName,
string filterName,
string startingNodeKey,
public async Task<IViewComponentResult> InvokeAsync(string viewName,
string filterName,
string startingNodeKey,
int expirationSeconds = 60,
bool testMode = false)
{
if (NavigationSuppressor.IsFilterSuppressed(Request.HttpContext, filterName))
{
return Content(string.Empty);
}

NavigationViewModel model = null;

string cacheKey = $"{viewName}_{filterName}_{startingNodeKey}";
Expand Down
1 change: 1 addition & 0 deletions src/cloudscribe.Web.Navigation/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace cloudscribe.Web.Navigation
public static class Constants
{
public static readonly string TailCrumbsContexctKey = "navigationtailcrumbs";
public static readonly string NavigationSuppressContextKey = "navigation-suppress";
}
}
43 changes: 43 additions & 0 deletions src/cloudscribe.Web.Navigation/NavigationSuppressor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;

namespace cloudscribe.Web.Navigation
{
/// <summary>
/// Allows consuming applications to suppress specific navigation filters for the current request.
/// When a filter is suppressed, the NavigationViewComponent and CachingNavigationViewComponent
/// will return empty content immediately, avoiding all tree-building computation.
/// </summary>
public static class NavigationSuppressor
{
public static void SuppressFilter(HttpContext context, string filterName)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (string.IsNullOrEmpty(filterName)) return;

var key = Constants.NavigationSuppressContextKey;
if (context.Items[key] is not HashSet<string> suppressed)
{
suppressed = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
context.Items[key] = suppressed;
}
suppressed.Add(filterName);
}

public static bool IsFilterSuppressed(HttpContext context, string filterName)
{
if (context == null) return false;
if (string.IsNullOrEmpty(filterName)) return false;

if (context.Items[Constants.NavigationSuppressContextKey] is HashSet<string> suppressed)
{
return suppressed.Contains(filterName);
}
return false;
}
}
}
5 changes: 5 additions & 0 deletions src/cloudscribe.Web.Navigation/NavigationViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public NavigationViewComponent(

public async Task<IViewComponentResult> InvokeAsync(string viewName, string filterName, string startingNodeKey)
{
if (NavigationSuppressor.IsFilterSuppressed(Request.HttpContext, filterName))
{
return Content(string.Empty);
}

var rootNode = await _builder.GetTree();
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccesor.ActionContext);
NavigationViewModel model = new NavigationViewModel(
Expand Down