NavSidebarComponent.vala 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Spry;
  2. /**
  3. * NavSidebarComponent - A tree-based navigation sidebar for the documentation site
  4. *
  5. * Features:
  6. * - Collapsible sections for parent items
  7. * - Current page highlighting
  8. * - Automatic expansion of section containing current page
  9. */
  10. public class NavSidebarComponent : Component {
  11. public string current_path { get; set; default = "/"; }
  12. // Track expanded state for each section
  13. private bool _components_expanded = false;
  14. private bool _page_components_expanded = false;
  15. private bool _static_resources_expanded = false;
  16. public override string markup { get {
  17. return """
  18. <aside class="nav-sidebar" sid="sidebar">
  19. <!-- Home - Single link -->
  20. <div class="nav-section">
  21. <a href="/" class="nav-item" sid="home-link">Home</a>
  22. </div>
  23. <!-- Components Section -->
  24. <div class="nav-section" sid="components-section">
  25. <button class="nav-section-header" spry-action=":ToggleComponents" spry-target="components-items">
  26. Components
  27. </button>
  28. <ul class="nav-items" sid="components-items">
  29. <li><a href="/components/overview" class="nav-item" sid="components-overview">Overview</a></li>
  30. <li><a href="/components/template-syntax" class="nav-item" sid="components-template-syntax">Template Syntax</a></li>
  31. <li><a href="/components/actions" class="nav-item" sid="components-actions">Actions</a></li>
  32. <li><a href="/components/outlets" class="nav-item" sid="components-outlets">Outlets</a></li>
  33. <li><a href="/components/continuations" class="nav-item" sid="components-continuations">Continuations</a></li>
  34. </ul>
  35. </div>
  36. <!-- Page Components Section -->
  37. <div class="nav-section" sid="page-components-section">
  38. <button class="nav-section-header" spry-action=":TogglePageComponents" spry-target="page-components-items">
  39. Page Components
  40. </button>
  41. <ul class="nav-items" sid="page-components-items">
  42. <li><a href="/page-components/overview" class="nav-item" sid="page-components-overview">Overview</a></li>
  43. <li><a href="/page-components/templates" class="nav-item" sid="page-components-templates">Page Templates</a></li>
  44. </ul>
  45. </div>
  46. <!-- Static Resources Section -->
  47. <div class="nav-section" sid="static-resources-section">
  48. <button class="nav-section-header" spry-action=":ToggleStaticResources" spry-target="static-resources-items">
  49. Static Resources
  50. </button>
  51. <ul class="nav-items" sid="static-resources-items">
  52. <li><a href="/static-resources/overview" class="nav-item" sid="static-resources-overview">Overview</a></li>
  53. <li><a href="/static-resources/spry-mkssr" class="nav-item" sid="static-resources-spry-mkssr">Using spry-mkssr</a></li>
  54. </ul>
  55. </div>
  56. </aside>
  57. """;
  58. }}
  59. public override async void prepare() throws Error {
  60. // Determine which sections should be expanded based on current path
  61. _components_expanded = current_path.has_prefix("/components");
  62. _page_components_expanded = current_path.has_prefix("/page-components");
  63. _static_resources_expanded = current_path.has_prefix("/static-resources");
  64. // Apply expanded state to sections
  65. update_section_state("components-section", "components-items", _components_expanded);
  66. update_section_state("page-components-section", "page-components-items", _page_components_expanded);
  67. update_section_state("static-resources-section", "static-resources-items", _static_resources_expanded);
  68. // Highlight the current page link
  69. highlight_current_link();
  70. }
  71. private void update_section_state(string section_sid, string items_sid, bool expanded) {
  72. var section = this[section_sid];
  73. var items = this[items_sid];
  74. if (section != null) {
  75. if (expanded) {
  76. section.add_class("expanded");
  77. } else {
  78. section.remove_class("expanded");
  79. }
  80. }
  81. if (items != null) {
  82. if (expanded) {
  83. items.remove_attribute("hidden");
  84. } else {
  85. items.set_attribute("hidden", "hidden");
  86. }
  87. }
  88. }
  89. private void highlight_current_link() {
  90. // Get the sid for the current path and add active class
  91. string? sid = get_sid_for_path(current_path);
  92. if (sid != null) {
  93. var link = this[sid];
  94. if (link != null) {
  95. link.add_class("active");
  96. }
  97. }
  98. }
  99. private string? get_sid_for_path(string path) {
  100. switch (path) {
  101. case "/": return "home-link";
  102. case "/components/overview": return "components-overview";
  103. case "/components/template-syntax": return "components-template-syntax";
  104. case "/components/actions": return "components-actions";
  105. case "/components/outlets": return "components-outlets";
  106. case "/components/continuations": return "components-continuations";
  107. case "/page-components/overview": return "page-components-overview";
  108. case "/page-components/templates": return "page-components-templates";
  109. case "/static-resources/overview": return "static-resources-overview";
  110. case "/static-resources/spry-mkssr": return "static-resources-spry-mkssr";
  111. default: return null;
  112. }
  113. }
  114. public override async void handle_action(string action) throws Error {
  115. switch (action) {
  116. case "ToggleComponents":
  117. _components_expanded = !_components_expanded;
  118. update_section_state("components-section", "components-items", _components_expanded);
  119. break;
  120. case "TogglePageComponents":
  121. _page_components_expanded = !_page_components_expanded;
  122. update_section_state("page-components-section", "page-components-items", _page_components_expanded);
  123. break;
  124. case "ToggleStaticResources":
  125. _static_resources_expanded = !_static_resources_expanded;
  126. update_section_state("static-resources-section", "static-resources-items", _static_resources_expanded);
  127. break;
  128. }
  129. }
  130. }