Fix behaviour of folded subdirectories in navigation

Change-Id: I1d0de6f46291141a237aeea926a50a8abeccf284
diff --git a/Changes b/Changes
index e33b359..7882ef8 100755
--- a/Changes
+++ b/Changes
@@ -24,6 +24,7 @@
         - Fix init of matches.
         - Fix tests on text method to accept undef for Mojo >= 9.21.
         - Plugin::TagHelpers::ContenBlock now on CPAN.
+        - Fix navigation behaviour for folded subdirectories.
 
 0.42 2021-06-18
         - Added GitHub based CI for perl.
diff --git a/dev/scss/sidebar/sidebar.scss b/dev/scss/sidebar/sidebar.scss
index f67d4f3..496f701 100644
--- a/dev/scss/sidebar/sidebar.scss
+++ b/dev/scss/sidebar/sidebar.scss
@@ -85,6 +85,9 @@
         }
         ul {
           display: none;
+          &.active {
+            display: block !important;
+          }
         }
       }
 
diff --git a/lib/Kalamar/Plugin/KalamarPages.pm b/lib/Kalamar/Plugin/KalamarPages.pm
index ea7312c..d1ad118 100644
--- a/lib/Kalamar/Plugin/KalamarPages.pm
+++ b/lib/Kalamar/Plugin/KalamarPages.pm
@@ -163,7 +163,9 @@
       };
 
       # Create unordered list
-      my $html = '<ul class="nav nav-'.$realm.'">'."\n";
+      my $html = '<ul class="nav nav-'.$realm;
+      my $men_active = 0;
+      my $item_str = '';
 
       # Embed all link tags
       foreach (@$items) {
@@ -192,6 +194,7 @@
           # The item is active
           if ($c->stash('page') && $c->stash('page') eq $_->{id}) {
             $active = 1;
+            $men_active = 1;
           };
 
           # Generate url with query parameter inheritance
@@ -207,29 +210,33 @@
         push(@classes, 'active') if $active;
 
         # New list item
-        $html .= '<li';
+        $item_str .= '<li';
         if (@classes) {
-          $html .= ' class="' . join(' ', @classes) . '"';
+          $item_str .= ' class="' . join(' ', @classes) . '"';
         };
-        $html .= '>';
+        $item_str .= '>';
 
         # Translate title
         my $title = $c->loc('Nav_' . $_->{id}, $_->{title});
 
         # Generate link
-        $html .= $c->link_to($title, $url);
+        $item_str .= $c->link_to($title, $url);
 
         # Set sub entries
         if ($_->{items} && ref($_->{items}) eq 'ARRAY') {
-          $html .= "\n";
+          $item_str .= "\n";
           my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id};
-          $html .= $c->navigation($realm, $subscope, $_->{items});
-          $html .= "</li>\n";
+          $item_str .= $c->navigation($realm, $subscope, $_->{items});
+          $item_str .= "</li>\n";
         }
         else {
-          $html .= "</li>\n";
+          $item_str .= "</li>\n";
         };
       };
+
+      $html .= ' active' if $men_active;
+      $html .= '">'."\n";
+      $html .= $item_str;
       return $html . "</ul>\n";
     }
   );
diff --git a/t/navigation.t b/t/navigation.t
index a15792d..bcb9c93 100644
--- a/t/navigation.t
+++ b/t/navigation.t
@@ -188,6 +188,7 @@
   {
     title => 'Query Languages',
     id => 'ql',
+    class => 'folded',
     items => [
       {
         title => 'Cosmas II',
@@ -227,6 +228,14 @@
 like($render, qr!class="folded active".*?Poliqarp\+!, 'Active and folded value for Poliqarp+');
 
 
+$c->stash(page => 'cosmas2');
+$render = $c->navigation('doc', $navi);
+
+like($render, qr!\<li class=\"folded\">\s*<a href="/doc/ql\#page-top">Query Languages</a>\s*<ul class="nav nav-doc active">\s*<li class="active"><a href="/doc/ql/cosmas2\#page-top">Cosmas II</a></li>!);
+
+delete $c->stash->{cosmas2};
+
+
 # Test for translations
 $navi = [
   {
@@ -264,6 +273,8 @@
 like($render, qr!<a href="/doc/faq(?:#[^"]+)?">FAQ</a>!,
      'Path matches FAQ');
 
+
+
 done_testing;
 
 __END__