1. Shlomi Fish
  2. perl-HTML-Widgets-NavMenu

Commits

shl...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed 926e587

Nav-Menu:

Implemented the url_is_abs functionality in the navigation menu. This
involved quite a lot of refactorings - moving

  • Participants
  • Parent commits 098db99
  • Branches default

Comments (0)

Files changed (7)

File module/HTML/Widgets/NavMenu/Test/Data.pm

View file
  • Ignore whitespace
     },
 );
 
+my @url_is_abs_nav_menu =
+(
+    'current_host' => "default",
+    'hosts' => { 'default' => { 'base_url' => "http://www.hello.com/" }, },
+    'tree_contents' =>
+    {
+        'host' => "default",
+        'text' => "Top 1",
+        'title' => "T1 Title",
+        'subs' =>
+        [
+            {
+                'text' => "Home",
+                'url' => "",
+            },
+            {
+                'text' => "Link to Google",
+                'title' => "Google it!",
+                'url' => "http://www.google.com/",
+                'url_is_abs' => 1,
+            },
+        ],
+    },
+);
+
 sub get_test_data
 {
     return
             'selective_expand' => \@selective_expand_nav_menu,
             'url_type_menu' => \@url_type_menu,
             'rec_url_type_menu' => \@rec_url_type_menu,
+            'url_is_abs_menu' => \@url_is_abs_nav_menu,
         };
 }
 

File module/TODO

View file
  • Ignore whitespace
-* Write a unit-test for HTML::Widgets::NavMenu::get_cross_host_rel_url().
-
 * Include an "url_is_abs" flag that specifies that the url specified in 
 'url' is an absolute one. For example:
 
         'url' => "http://www.google.com/",
         'url_is_abs' => 1,
 
+* Write tests to test the url_is_abs propagation in the leading path, site-map,
+and navigation links. Implement them if necessary.
+
+* Document url_is_abs.
 
 Long-Term:
 ----------

File module/lib/HTML/Widgets/NavMenu.pm

View file
  • Ignore whitespace
     }
 }
 
+sub get_url_to_item
+{
+    my $self = shift;
+    my (%args) = (@_);
+    my $item = $args{'item'};
+
+    return $self->get_cross_host_rel_url(
+        'host' => $item->accum_state()->{'host'},
+        'host_url' => ($item->node->url() || ""),
+        'url_type' => $item->get_url_type(),
+        'url_is_abs' => $item->node->url_is_abs(),
+    );
+}
+
 sub gen_blank_nav_menu_tree_node
 {
     my $self = shift;
     my $node_ret = $iterator->find_node_by_coords($coords);
     my $item = $node_ret->{'item'};
 
-    return $self->get_cross_host_rel_url(
-        'host' => $item->accum_state()->{'host'},
-        'host_url' => ($item->node->url() || ""),
-        'url_type' => $iterator->get_url_type($item),
-    );
+    return $self->get_url_to_item('item' => $item);
 }
 
 sub fill_leading_path
                 my $host_url = (defined($node->url()) ? ($node->url()) : "");
                 my $host = $item->accum_state()->{'host'};
 
-                my $url_type = $iterator->get_url_type($item);
+                my $url_type = $item->get_url_type();
 
                 push @leading_path,
                     HTML::Widgets::NavMenu::LeadingPath::Component->new(
                         'title' => $node->title(),
                         'label' => $node->text(),
                         'direct_url' =>
-                            $self->get_cross_host_rel_url(
-                                'host' => $host,
-                                'host_url' => $host_url,
-                                'url_type' => $url_type,
-                            ),
+                            $self->get_url_to_item('item' => $item),
                         'url_type' => $url_type,
                     );
             };

File module/lib/HTML/Widgets/NavMenu/Iterator/Html.pm

View file
  • Ignore whitespace
+package HTML::Widgets::NavMenu::Iterator::Html::Item;
+
+use base qw(HTML::Widgets::NavMenu::Tree::Iterator::Item);
+
+sub get_url_type
+{
+    my $item = shift;
+    return
+        ($item->node()->url_type() ||
+            $item->accum_state()->{'rec_url_type'} ||
+            "rel");
+}
+
 package HTML::Widgets::NavMenu::Iterator::Html;
 
 use base qw(HTML::Widgets::NavMenu::Iterator::Base);
 
+sub construct_new_item
+{
+    my $self = shift;
+
+    return HTML::Widgets::NavMenu::Iterator::Html::Item->new(
+        @_
+    );
+}
+
 sub node_start
 {
     my $self = shift;
 
     $tag .= " href=\"" .
         CGI::escapeHTML(
-            $self->nav_menu()->get_cross_host_rel_url(
-                'host' => $self->_get_top_host(),
-                'host_url' => $node->url(),
-                'url_type' => $self->get_url_type($item),
+            $self->nav_menu()->get_url_to_item(
+                'item' => $item,
             )
         ). "\"";
     if (defined($title))
     return $tag;
 }
 
-sub get_url_type
-{
-    my $self = shift;
-    my $item = shift;
-    return
-        ($item->node()->url_type() ||
-            $item->accum_state()->{'rec_url_type'} ||
-            "rel");
-}
 
 1;
 

File module/lib/HTML/Widgets/NavMenu/Tree/Iterator.pm

View file
  • Ignore whitespace
     return $self->stack()->top();
 }
 
+sub construct_new_item
+{
+    my $self = shift;
+
+    return HTML::Widgets::NavMenu::Tree::Iterator::Item->new(
+        @_
+    );
+}
+
 sub get_new_item
 {
     my $self = shift;
     my $parent_item = $args{'parent_item'};
 
     return
-        HTML::Widgets::NavMenu::Tree::Iterator::Item->new(
+        $self->construct_new_item(
             'node' => $node,
             'subs' => $self->get_node_subs('node' => $node),
             'accum_state' => 

File module/lib/HTML/Widgets/NavMenu/Tree/Node.pm

View file
  • Ignore whitespace
 
 __PACKAGE__->mk_accessors(
     qw(CurrentlyActive expanded hide host role rec_url_type),
-    qw(separator show_always skip subs text title url url_type),
+    qw(separator show_always skip subs text title url url_is_abs url_type),
     );
 
 sub initialize
 {
     my $self = shift;
 
-    return (qw(hide separator skip));
+    return (qw(hide separator skip url_is_abs));
 }
 
 sub set_values_from_hash_ref

File module/t/04nav-menu.t

View file
  • Ignore whitespace
 
 use strict;
 
-use Test::More tests => 14;
+use Test::More tests => 15;
 
 use HTML::Widgets::NavMenu;
 use HTML::Widgets::NavMenu::HeaderRole;
     ok (validate_nav_menu($rendered, $expected_string), 
         "Nav Menu for rec_url_type - 1"); 
 }
+
+# Test the url_is_abs directive
+{
+    my $nav_menu = HTML::Widgets::NavMenu->new(
+        'path_info' => "/hello/",
+        @{$test_data->{'url_is_abs_menu'}},
+    );
+
+    my $rendered = 
+        $nav_menu->render();
+
+    my $expected_string = <<"EOF";
+<ul>
+<li>
+<a href="../">Home</a>
+</li>
+<li>
+<a href="http://www.google.com/" title="Google it!">Link to Google</a>
+</li>
+</ul>
+EOF
+
+    # TEST
+    ok (validate_nav_menu($rendered, $expected_string), 
+        "Nav Menu for url_is_asb - 1"); 
+}
+