blib/lib/CatalystX/Menu/Suckerfish.pm

filter: Statementstmt 95.7% 91 / 95 Branchbran 84.6% 22 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 91.4% 128 / 140 CC 18 SCAR 14.8
CC
18
Cov
92%
_get_top_level_order
6.1
_gen_menu
6.0
output
6.0
CRAP 4.4
↑ Summary
1 package CatalystX::Menu::Suckerfish;
2  
3 2 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'CatalystX::Menu::Tree';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $VERSION = '0.03';
16  
17 =head1 NAME
18  
19 CatalystX::Menu::Suckerfish - Generate HTML UL for a CSS-enhanced Suckerfish menu
20  
21 =head1 SYNOPSIS
22  
23 package MyApp::Controller::Whatever;
24  
25 sub someaction :Local
26 :MenuPath('Electronics/Computers')
27 :MenuTitle('Computers')
28 { ... }
29  
30 sub begin :Private {
31 my ($self, $c) = @_;
32  
33 my $menu = CatalystX::Menu::Suckerfish->new(
34 context => $c,
35 ul_id => 'navmenu', # <ul id="navmenu"> ... </ul>
36 ul_class => 'sf-menu', # <ul id="navmenu" class="sf-menu"> ... </ul>
37 text_container => { # wrap plain text nodes in this HTML element
38 element => 'span', # so that styles can be applied if desired.
39 attrs => {
40 class => 'myspan',
41 },
42 },
43 top_order => [qw(Home * About)], # Put Home and About on the ends,
44 # everything else in-between
45 filter => sub { # Filter out actions we don't want in menu
46 my ($c, %actions) = @_;
47 return
48 map {$_, $actions{$_}}
49 grep {$actions{$_}->can_visit($c)}
50 grep {UNIVERSAL::isa($actions{$_}, 'Catalyst::Action::Role::ACL')}
51 keys %actions;
52 },
53 add_nodes => [ # add a menu node manually
54 {
55 menupath => '/Bargains',
56 menutitle => 'Cheap stuff',
57 uri => '/products/cheap',
58 },
59 ],
60 );
61  
62 $c->session->{navmenu} = $menu->output;
63 # include the UL element in your Template: [% c.session.navmenu %]
64 }
65  
66 # include any desired styles (CSS) for the UL element in your markup
67  
68 =head1 DESCRIPTION
69  
70 Builds nested HTML UL element with links to your Catalyst application's public
71 actions for use as a Suckerfish or Superfish menu.
72  
73 Suckerfish menus: L<http://www.alistapart.com/articles/dropdowns>
74 Superfish jQuery menu plugin: L<http://users.tpg.com.au/j_birch/plugins/superfish/>
75  
76 =head1 METHODS
77  
78 =cut
79  
80 =head2 C<new( $tree, %params )>
81  
82 Takes a menu tree produced by Catalyst::Controller::Menutree (CatalystX::MenuTree)
83 and a list of key/value parameter pairs.
84  
85 Params
86  
87 =over
88  
89 =item menupath_attr
90  
91 Required (no validation)
92  
93 Names the action attribute that contains the menu path:
94  
95 menupath_attr => 'MenuPath'
96  
97 # and in your controller:
98  
99 sub foobar :Local
100 :MenuPath(/Foo/Bar)
101 :MenuTitle('Foobar and stuff')
102 { ... }
103  
104 Only actions with the menupath_attr attribute are processed. This attribute's
105 value determines where the action's menu item is placed in the menu structure
106 (HTML UL).
107  
108 Depending on the attribute values collected from the processed actions, there
109 may be menu items containing only text. If you want a link to a landing page,
110 for example, instead of text, include an action for the landing page with the
111 appropriate MenuPath attribute in your controller, or add an entry manually
112 with the add_nodes parameter.
113  
114 =item menutitle_attr
115  
116 Optional
117  
118 The menutitle_attr attribute will be used to add the HTML title attribute to
119 each list item. This should result in a balloon text with the title when the
120 pointing device hovers over each list item.
121  
122 =item ul_id
123  
124 The ID attribute to be applied to the outer HTML UL element.
125  
126 =item ul_class
127  
128 The class attribute to be applied to the outer HTML UL element.
129  
130 =item ul_container
131  
132 Specifies an HTML element (typically a DIV) in which to enclose the UL
133 element.
134  
135 =item text_container
136  
137 Specifies an HTML element (typically a SPAN) in which to enclose menu items
138 which don't include A elements. This makes it possible to apply similar styles
139 to both plain text and A elements for consistent appearance.
140  
141 =item top_order
142  
143 A list of top level menu item labels. Menu items are sorted alphabetically by
144 default. top_order allows you to specify the order of one or more items. The
145 asterisk (*) inserts any menu items not listed in top_order.
146  
147 =item add_nodes
148  
149 Optional
150  
151 A reference to an array of hash references. See the L</SYNOPSIS>.
152  
153 =back
154  
155 =cut
156  
157 4 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
158 4 my $class = shift;
159 4 ▶ if (@_ && @_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and @_ % 2 != 0) 0 4 4
Condition33%1/3
@_ and @_ % 2 != 0
!ll&&!rl&&r
040
MC/DC0%0/2
@_ and @_ % 2 != 0
@_ @_ % 2 != 0
Truth table33%1/3
A and B
A= @_B= @_ % 2 != 0
ABresult
0X0
100
111
160 0 die 'expected list of key/value pairs';
161 }
162  
163 4 my $self = $class->next::method(@_);
164  
165 4 return $self;
166 }
167  
168 =head2 C<output>
169  
170 Return HTML UL markup.
171  
172 =cut
173  
174 4 ▶sub output {
Subroutine output: called CC=6 SCAR=17.9
Pod documented
175 4 my ( $self ) = @_;
176  
177 4 my @ord = $self->_get_top_level_order;
178  
179 4 my $tree = $self->{tree};
180  
181 4 local %HTML::Tagset::optionalEndTag; # we want NO optional end tags
182  
183 4 my %opts;
184 4 ▶ if ($self->{ul_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ul_id"}) 2 2 4
185 2 $opts{id} = $self->{ul_id};
186 }
187 4 ▶ if ($self->{ul_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ul_class"}) 2 2 4
188 2 $opts{class} = $self->{ul_class};
189 }
190 4 my $h = HTML::Element->new('ul', %opts);
191  
192 # process one top-level chunk of the tree at a time
193 4 for my $item (@ord) {
194 12 ▶ next unless $tree->{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->{$item} 4 8 12
195 8 $self->_gen_menu($h, {$item, $tree->{$item}})
196 }
197  
198 4 my $indent = ' ' x 4;
199  
200 4 ▶ if (my $ctr = $self->{ul_container}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctr = $self->{"ul_container"}) 2 2 4
201 2 my $el = $ctr->{element};
202 2 my %opts = ( %{$ctr->{attrs}} );
203 2 my $ctr_el = $h->new($el, %opts);
204 2 $ctr_el->push_content($h);
205 2 $h = $ctr_el;
206 }
207  
208 4 return $h->as_HTML(undef, $indent, {});
209 }
210  
211 =head1 INTERNAL METHODS
212  
213 =cut
214  
215 =head2 C<_get_top_level_order()>
216  
217 Return hash keys for top level menu items. Order is determined by the top_order param.
218 Items not explicitly referenced in the top_order param are sorted lexically and inserted
219 where the asterisk (*) appears in the top_order param string.
220  
221 =cut
222  
223 4 ▶sub _get_top_level_order {
Subroutine _get_top_level_order: called CC=6 SCAR=18.1
224 4 my ($self) = @_;
225  
226 4 my @ord;
227  
228 4 ▶ if ($self->{top_order}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'top_order'}) { } 4 0 4
229 4 my %menukeys = map {$_, 1} keys %{$self->{tree}};
230 4 for my $top (@{$self->{top_order}}) {
231 12 ▶ if ($top eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($top eq '*') { } 4 8 12
232 4 push @ord, '*';
233 }
234 else {
235 8 push @ord, $top;
236 8 delete $menukeys{$top};
237 }
238 }
239 4 my $n = @ord;
240 4 ▶ for (my $i = 0; $i < $n; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 8 0 8
241 8 ▶ if ($ord[$i] eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ord[$i] eq "*") 4 4 8
242 4 splice @ord, $i, 1, sort keys %menukeys;
243 4 last;
244 }
245 }
246 }
247 else {
248 0 @ord = sort keys %{$self->{tree}};
249 }
250  
251 4 return @ord;
252 }
253  
254 =head2 C<_gen_menu($self, $h, $tree)>
255  
256 Recursively construct a (possibly) nested HTML UL element.
257  
258 $h is an HTML::Element object.
259 $tree is a node in the tree created in the parent class.
260  
261 =cut
262  
263 12 ▶sub _gen_menu {
Subroutine _gen_menu: called CC=6 SCAR=17.9
264 12 my ($self, $h, $tree) = @_;
265  
266 12 for my $label (sort keys %$tree) {
267 12 my $content;
268 12 ▶ if ($tree->{$label}{uri}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tree->{$label}{'uri'}) { } 8 4 12
elsif ($self->{'text_container'}) { } 4 0 4
269 8 $content = $h->new('a', href => $tree->{$label}{uri});
270 8 $content->push_content($label);
271 }
272 elsif ($self->{text_container}) {
273 4 my $el = $self->{text_container}{element};
274 4 $content = $h->new($el, %{$self->{text_container}{attrs}});
275 4 $content->push_content($label);
276 }
277 else {
278 0 $content = $label;
279 }
280 12 my %opts;
281 12 ▶ if ($tree->{$label}{menutitle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{$label}{"menutitle"}) 4 8 12
282 4 %opts = (title => $tree->{$label}{menutitle});
283 }
284 12 my $li = $h->new('li', %opts);
285 12 $li->push_content($content);
286  
287 #
288 # Recurse to process nested menu items
289 #
290 12 ▶ if (keys %{$tree->{$label}{children}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$tree{$label}{"children"};}) 4 8 12
291 4 my $ul = $h->new('ul');
292 4 $self->_gen_menu($ul, $tree->{$label}{children});
293 4 $li->push_content($ul);
294 }
295  
296 12 $h->push_content($li);
297 }
298  
299 12 return;
300 }
301  
302 2 1;
303  
304 =head1 AUTHOR
305  
306 David P.C. Wollmann E<lt>converter42@gmail.comE<gt>
307  
308 =head1 BUGS
309  
310 This is brand new code, so use at your own risk.
311  
312 =head1 COPYRIGHT & LICENSE
313  
314 Copyright 2009 by David P.C. Wollmann
315  
316 This program is free software; you can redistribute it and/or modify it under
317 the same terms as Perl itself.
318  
↑ Summary