_MenuItem.cshtml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. };
  17. if (multiPathMenus.TryGetValue(Model.Name, out var activePrefixes))
  18. {
  19. isActive = currentPath != null && activePrefixes.Any(prefix =>
  20. currentPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
  21. );
  22. }
  23. else if (Model.Children != null && Model.Children.Any())
  24. {
  25. // 자식 메뉴 중 하나가 활성화되었는지 확인
  26. isParentActive = Model.Children.Any(child =>
  27. !string.IsNullOrEmpty(currentPath) &&
  28. !string.IsNullOrEmpty(child.Path) &&
  29. (currentPath.Equals(child.Path, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(child.Path + "/", StringComparison.OrdinalIgnoreCase))
  30. );
  31. // 부모 메뉴 자체는 active 주지 않음
  32. isActive = false;
  33. }
  34. else
  35. {
  36. // 일반 메뉴 active 여부 판단
  37. isActive = !string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(menuPath) && (currentPath.Equals(menuPath, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(menuPath + "/", StringComparison.OrdinalIgnoreCase));
  38. }
  39. }
  40. <li class="nav-item">
  41. <a href="@Model.Path" class="nav-link @(isActive ? "active" : "") @Html.Raw(Model.Children == null || !Model.Children.Any() ? "collapsed" : "")"
  42. @Html.Raw(Model.Children != null && Model.Children.Any() ? "data-bs-toggle=\"collapse\"" : "")
  43. @Html.Raw(Model.Children != null && Model.Children.Any() ? $"data-bs-target=\"#menu-{Model.Id}\"" : "")
  44. >
  45. @if (!string.IsNullOrEmpty(Model.Icon))
  46. {
  47. @Html.Raw(Model.Icon);
  48. }
  49. @Model.Name
  50. </a>
  51. @if (Model.Children != null && Model.Children.Any())
  52. {
  53. <ul id="menu-@Model.Id" class="nav flex-column flex-nowrap ps-3 collapse">
  54. @foreach (var child in Model.Children)
  55. {
  56. @* @Html.Partial("_MenuItem", child); *@
  57. <partial name="_MenuItem" model="child" />
  58. }
  59. </ul>
  60. }
  61. </li>