_MenuItem.cshtml 2.5 KB

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