|
| 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 | +} |
0 commit comments