Commits

Shlomi Fish committed a69cd0f

One less level of nesting and a separate module for JSON encoding.

Comments (0)

Files changed (4)

bin/nav-data-as-json

 
 use IO::All;
 
-use MyNavData;
+use Shlomif::Homepage::NavData::JSON;
 
 my $output_fn;
 
 or die "Wrong options - $!";
 
 io->file($output_fn)->println(
-    MyNavData->output_fully_expanded_as_json()
+    Shlomif::Homepage::NavData::JSON->output_fully_expanded_as_json()
 );

common/js/to-jqtree-2.js

         function(json_input) {
             $('#nav_menu').tree({
                 autoEscape: false,
-                autoOpen: 0,
-                data: [ calc_jqtree_data_from_html_w_nav_menu_json(
+                autoOpen: 1,
+                data: calc_jqtree_data_from_html_w_nav_menu_json(
                     {
                         input: json_input,
                 base: b,
                 current: b + page_path
                     }
-                ) ]
+                )
             });
         }
     );
 
 use utf8;
 
-use JSON qw(encode_json);
-
 use Shlomif::Homepage::SectionMenu;
 
 my $hosts =
     );
 }
 
-sub output_fully_expanded_as_json
-{
-    my $class = shift;
-
-    my %params =
-    $class->generic_get_params(
-        {
-            fully_expanded => 1,
-        },
-    );
-
-    my $filter_out_seperators;
-
-    $filter_out_seperators = sub {
-        my ($sub_tree) = @_;
-
-        my @keys = (grep { $_ ne 'subs' } keys %{$sub_tree});
-
-        my $has_subs = exists($sub_tree->{subs});
-
-        return {
-            (map { $_ => $sub_tree->{$_} } @keys),
-            $has_subs
-            ?  (subs => [ map { $filter_out_seperators->($_) }
-                    grep { ! exists($_->{separator}) }
-                    @{$sub_tree->{subs}}
-                ])
-            : (),
-        },
-    };
-
-    return encode_json($filter_out_seperators->($params{tree_contents}));
-}
-
 1;

lib/Shlomif/Homepage/NavData/JSON.pm

+package Shlomif::Homepage::NavData::JSON;
+
+use strict;
+use warnings;
+
+use MyNavData;
+
+use JSON qw(encode_json);
+
+sub output_fully_expanded_as_json
+{
+    my $class = shift;
+
+    my %params =
+    MyNavData->generic_get_params(
+        {
+            fully_expanded => 1,
+        },
+    );
+
+    my $filter_out_seperators;
+
+    $filter_out_seperators = sub {
+        my ($sub_tree) = @_;
+
+        my @keys = (grep { $_ ne 'subs' } keys %{$sub_tree});
+
+        my $has_subs = exists($sub_tree->{subs});
+
+        return {
+            (map { $_ => $sub_tree->{$_} } @keys),
+            $has_subs
+            ?  (subs => [ map { $filter_out_seperators->($_) }
+                    grep { ! exists($_->{separator}) }
+                    @{$sub_tree->{subs}}
+                ])
+            : (),
+        },
+    };
+
+    return encode_json($filter_out_seperators->($params{tree_contents})->{'subs'});
+}
+
+1;