Skip to content

Commit d2938dd

Browse files
authored
Merge pull request #152 from cloudscribe/feature/151
#151 Add nav suppressor support
2 parents 02663ab + 6aea979 commit d2938dd

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

src/cloudscribe.Web.Navigation/CachingNavigationViewComponent.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ public CachingNavigationViewComponent(
6464
// In a simplecontent system I'd probably need to use the IHandlePageCreated (etc)
6565
// hooks to clear a navcache of known name.
6666

67-
public async Task<IViewComponentResult> InvokeAsync(string viewName,
68-
string filterName,
69-
string startingNodeKey,
67+
public async Task<IViewComponentResult> InvokeAsync(string viewName,
68+
string filterName,
69+
string startingNodeKey,
7070
int expirationSeconds = 60,
7171
bool testMode = false)
7272
{
73+
if (NavigationSuppressor.IsFilterSuppressed(Request.HttpContext, filterName))
74+
{
75+
return Content(string.Empty);
76+
}
77+
7378
NavigationViewModel model = null;
7479

7580
string cacheKey = $"{viewName}_{filterName}_{startingNodeKey}";

src/cloudscribe.Web.Navigation/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace cloudscribe.Web.Navigation
99
public static class Constants
1010
{
1111
public static readonly string TailCrumbsContexctKey = "navigationtailcrumbs";
12+
public static readonly string NavigationSuppressContextKey = "navigation-suppress";
1213
}
1314
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Http;
5+
using System;
6+
using System.Collections.Generic;
7+
8+
namespace cloudscribe.Web.Navigation
9+
{
10+
/// <summary>
11+
/// Allows consuming applications to suppress specific navigation filters for the current request.
12+
/// When a filter is suppressed, the NavigationViewComponent and CachingNavigationViewComponent
13+
/// will return empty content immediately, avoiding all tree-building computation.
14+
/// </summary>
15+
public static class NavigationSuppressor
16+
{
17+
public static void SuppressFilter(HttpContext context, string filterName)
18+
{
19+
if (context == null) throw new ArgumentNullException(nameof(context));
20+
if (string.IsNullOrEmpty(filterName)) return;
21+
22+
var key = Constants.NavigationSuppressContextKey;
23+
if (context.Items[key] is not HashSet<string> suppressed)
24+
{
25+
suppressed = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
26+
context.Items[key] = suppressed;
27+
}
28+
suppressed.Add(filterName);
29+
}
30+
31+
public static bool IsFilterSuppressed(HttpContext context, string filterName)
32+
{
33+
if (context == null) return false;
34+
if (string.IsNullOrEmpty(filterName)) return false;
35+
36+
if (context.Items[Constants.NavigationSuppressContextKey] is HashSet<string> suppressed)
37+
{
38+
return suppressed.Contains(filterName);
39+
}
40+
return false;
41+
}
42+
}
43+
}

src/cloudscribe.Web.Navigation/NavigationViewComponent.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public NavigationViewComponent(
4747

4848
public async Task<IViewComponentResult> InvokeAsync(string viewName, string filterName, string startingNodeKey)
4949
{
50+
if (NavigationSuppressor.IsFilterSuppressed(Request.HttpContext, filterName))
51+
{
52+
return Content(string.Empty);
53+
}
54+
5055
var rootNode = await _builder.GetTree();
5156
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccesor.ActionContext);
5257
NavigationViewModel model = new NavigationViewModel(

0 commit comments

Comments
 (0)