_MenuItem.cshtml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. @model SharedKernel.Constants.Menu
  2. @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
  3. @{
  4. // 현재 요청 경로 가져오기
  5. var currentPath = HttpContextAccessor.HttpContext?.Request?.Path.ToString()?.TrimEnd('/');
  6. // 고정된 메뉴 경로
  7. var menuPath = (Model.Path?.TrimEnd('/') ?? string.Empty).ToLower();
  8. bool isActive = false;
  9. bool isParentActive = false;
  10. // 특별 처리: navtabs가 있는 메뉴 (하위 경로가 메뉴 Path와 다른 경우)
  11. var multiPathMenus = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
  12. {
  13. { "게시판 관리", new[] { "/forum/board/meta", "/forum/board/prefix", "/forum/board/manager", "/forum/board/list" } },
  14. { "FAQ 관리", new[] { "/faq/list", "/faq/category" } },
  15. { "배너 관리", new[] { "/banner/list", "/banner/position" } },
  16. { "상단 메뉴 관리", new[] { "/navmenu" } },
  17. };
  18. if (multiPathMenus.TryGetValue(Model.Name, out var activePrefixes))
  19. {
  20. isActive = currentPath != null && activePrefixes.Any(prefix =>
  21. currentPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
  22. );
  23. }
  24. else if (Model.Children != null && Model.Children.Any())
  25. {
  26. // 자식 메뉴 중 하나가 활성화되었는지 확인
  27. isParentActive = Model.Children.Any(child =>
  28. !string.IsNullOrEmpty(currentPath) &&
  29. !string.IsNullOrEmpty(child.Path) &&
  30. (currentPath.Equals(child.Path, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(child.Path + "/", StringComparison.OrdinalIgnoreCase))
  31. );
  32. // 부모 메뉴 자체는 active 주지 않음
  33. isActive = false;
  34. }
  35. else
  36. {
  37. // 일반 메뉴 active 여부 판단
  38. isActive = !string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(menuPath) && (currentPath.Equals(menuPath, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(menuPath + "/", StringComparison.OrdinalIgnoreCase));
  39. }
  40. }
  41. <li class="nav-item">
  42. <a href="@Model.Path" class="nav-link @(isActive ? "active" : "") @Html.Raw(Model.Children == null || !Model.Children.Any() ? "collapsed" : "")"
  43. title="@Model.Name"
  44. @Html.Raw(Model.Children != null && Model.Children.Any() ? "data-bs-toggle=\"collapse\"" : "")
  45. @Html.Raw(Model.Children != null && Model.Children.Any() ? $"data-bs-target=\"#menu-{Model.Id}\"" : "")
  46. >
  47. @if (!string.IsNullOrEmpty(Model.Icon))
  48. {
  49. @Html.Raw(Model.Icon);
  50. }
  51. <span class="menu-label">@Model.Name</span>
  52. @if (Model.Children != null && Model.Children.Any())
  53. {
  54. <i class="bi bi-chevron-down menu-chevron"></i>
  55. }
  56. </a>
  57. @if (Model.Children != null && Model.Children.Any())
  58. {
  59. <ul id="menu-@Model.Id" class="nav flex-column flex-nowrap ps-3 collapse">
  60. @foreach (var child in Model.Children)
  61. {
  62. @* @Html.Partial("_MenuItem", child); *@
  63. <partial name="_MenuItem" model="child" />
  64. }
  65. </ul>
  66. }
  67. </li>