_MenuItem.cshtml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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[] { "/crypto/list", "/crypto/board" } }
  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. @Html.Raw(Model.Children != null && Model.Children.Any() ? "data-bs-toggle=\"collapse\"" : "")
  44. @Html.Raw(Model.Children != null && Model.Children.Any() ? $"data-bs-target=\"#menu-{Model.Id}\"" : "")
  45. >
  46. @if (!string.IsNullOrEmpty(Model.Icon))
  47. {
  48. @Html.Raw(Model.Icon);
  49. }
  50. @Model.Name
  51. </a>
  52. @if (Model.Children != null && Model.Children.Any())
  53. {
  54. <ul id="menu-@Model.Id" class="nav flex-column flex-nowrap ps-3 collapse">
  55. @foreach (var child in Model.Children)
  56. {
  57. @* @Html.Partial("_MenuItem", child); *@
  58. <partial name="_MenuItem" model="child" />
  59. }
  60. </ul>
  61. }
  62. </li>