| 1 | # | ||
| 2 | # YUI::MenuBar::Markup | ||
| 3 | # | ||
| 4 | # Author(s): Pablo Fischer (pfischer@cpan.org) | ||
| 5 | # Created: 01/19/2010 10:05:38 PST 10:05:38 | ||
| 6 | package YUI::MenuBar::Markup; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | YUI::MenuBar::Markup - Generate YUI markup menus | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | YUI::MenuBar::Markup will help you create your YUI menus by using markup (html). | ||
| 15 | |||
| 16 | It offers a very light interface to create your modules from any kind of data, | ||
| 17 | like I<perl raw> (hashes) to even L<YAML>. | ||
| 18 | |||
| 19 | There are no plans in giving support for creating YUI menus by javascript data. | ||
| 20 | Even, the html genearated doesn't allow modifications such as changing the CSS | ||
| 21 | class name of the items. | ||
| 22 | |||
| 23 | Please note that the string returned by L<generated()> is the pure javascript | ||
| 24 | to start the menubar and the HTML of the menubar. It's your B<responsibility> | ||
| 25 | to include the CSS and JS files. You can use L<YUI::Loader> for this. | ||
| 26 | |||
| 27 | =head1 SYNOPSYS | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | use YUI::MenuBar::Markup; | ||
| 32 | use YUI::MenuBar::Markup::YAML; | ||
| 33 | |||
| 34 | my $markup_yaml = YUI::MenuBar::Markup::YAML->new( | ||
| 35 | filename => 'examples/menu_yaml.yaml'); | ||
| 36 | my $markup = YUI::MenuBar::Markup->new( | ||
| 37 | source_ref => $markup_yaml); | ||
| 38 | print $markup->generate(); | ||
| 39 | |||
| 40 | =cut | ||
| 41 | use Mouse; | ||
| 42 | |||
| 43 | our $VERSION = '0.02'; | ||
| 44 | |||
| 45 | =head1 Attributes | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item B<source_class> | ||
| 50 | |||
| 51 | If you have your own class (or module) for getting the data of the menu please | ||
| 52 | use this attribute. It should be the module name (I<Foo::Bar>) and have the | ||
| 53 | module should have a I<new()> method. | ||
| 54 | |||
| 55 | If your module doesn't have anything of the above please take a look to | ||
| 56 | L<source_ref>. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | has 'source_class' => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => 'Str'); | ||
| 62 | |||
| 63 | =item B<source_ref> | ||
| 64 | |||
| 65 | Similar to L<source_class> but it should be the reference (eg, the object) to | ||
| 66 | the source. | ||
| 67 | |||
| 68 | The class contained in this refernece should have a method named L<get_source> | ||
| 69 | and should return an array of hashes to be used for building the menu. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | has 'source_ref' => ( | ||
| 73 | is => 'rw', | ||
| 74 | isa => 'Ref'); | ||
| 75 | |||
| 76 | =item B<data> | ||
| 77 | |||
| 78 | The data that will be used for building the menu, should be an array of hashes | ||
| 79 | where | ||
| 80 | |||
| 81 | =cut | ||
| 82 | has 'data' => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'ArrayRef'); | ||
| 85 | |||
| 86 | =item B<top_id> | ||
| 87 | |||
| 88 | The menubar is made of lists (ul, li) and all these lists are contained in a | ||
| 89 | div element. By default the div element ID is automatically generated (with a | ||
| 90 | random number). In case you want to use a different ID just set the value of | ||
| 91 | this attribute to I<that>. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | has 'top_id' => ( | ||
| 95 | is => 'rw', | ||
| 96 | isa => 'Str'); | ||
| 97 | |||
| 98 | =item B<autosubmenu> | ||
| 99 | |||
| 100 | When the cursor is over a menu that has a submenu, then the submenu will be | ||
| 101 | displayed automatically without clicking on it. | ||
| 102 | |||
| 103 | This is turned on by default. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | has 'autosubmenu' => ( | ||
| 107 | is => 'rw', | ||
| 108 | isa => 'Bool', | ||
| 109 | default => 1); | ||
| 110 | |||
| 111 | =item B<delay> | ||
| 112 | |||
| 113 | Indicates the time (milliseconds) that should expire before a menu is hidden. | ||
| 114 | |||
| 115 | In YUI this is known as the I<hidedelay>. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | has 'delay' => ( | ||
| 119 | is => 'rw', | ||
| 120 | isa => 'Int', | ||
| 121 | default => 750); | ||
| 122 | |||
| 123 | =item B<lazy> | ||
| 124 | |||
| 125 | Modifies the lazyload parameter of the YUI menubar to improve the performance | ||
| 126 | of the menubar by deferring the initialization and rendering of submenus until | ||
| 127 | the first time they are made visible. | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =cut | ||
| 132 | has 'lazy' => ( | ||
| 133 | is => 'rw', | ||
| 134 | isa => 'Bool', | ||
| 135 | default => 1); | ||
| 136 | |||
| 137 | =head1 Methods | ||
| 138 | |||
| 139 | =head2 B<generate()> | ||
| 140 | |||
| 141 | Builds the HTML and javascript caller for the menu and returns it as a string | ||
| 142 | |||
| 143 | =cut | ||
| 144 | sub generate { | ||
| 145 | my ($self) = @_; | ||
| 146 | |||
| 147 | if (defined $self->{'source'}) { | ||
| 148 | my $sref = "$self->{'source'}"->new(); | ||
| 149 | if ($sref->can('get_data')) { | ||
| 150 | $self->{'data'} = $sref->get_data(); | ||
| 151 | } else { | ||
| 152 | warn "$self->{'source'} does not have a get_data() method"; | ||
| 153 | } | ||
| 154 | } elsif (defined $self->{'source_ref'}) { | ||
| 155 | if ($self->{'source_ref'}->can('get_data')) { | ||
| 156 | $self->{'data'} = $self->{'source_ref'}->get_data(); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | if ($self->{'data'}) { | ||
| 161 | # The YUI loader | ||
| 162 | my $html = ''; | ||
| 163 | my $menu_id = $self->_get_id(); | ||
| 164 | $html .= "<script type=\"text/javascript\">\n"; | ||
| 165 | $html .= $self->_generate_javascript($menu_id); | ||
| 166 | $html .= "</script>\n"; | ||
| 167 | $html .= "<div id=\"" . $menu_id . "\" class=\"yuimenubar yuimenubarnav\">\n"; | ||
| 168 | $html .= "<div class=\"bd\">\n"; | ||
| 169 | $html .= $self->_generate_child_menu( | ||
| 170 | $self->{'data'}, | ||
| 171 | 1); | ||
| 172 | $html .= "</div>\n"; | ||
| 173 | $html .= "</div>\n"; | ||
| 174 | return $html; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | ############################## PRIVATE METHODS ################################# | ||
| 179 | # Builds the <ul>, <li> and <div> for the menus and submenus | ||
| 180 | sub _generate_child_menu { | ||
| 181 | my ($self, $menu, $level) = @_; | ||
| 182 | |||
| 183 | my $tab = "\t" x $level; | ||
| 184 | my $html = ""; | ||
| 185 | $html .= "$tab<ul class=\"first-of-type\">\n"; | ||
| 186 | my $is_first = 1; | ||
| 187 | foreach my $item (@{$menu}) { | ||
| 188 | if (defined $item->{'name'}) { | ||
| 189 | my $extra_class = ''; | ||
| 190 | if (defined $item->{'classes'}) { | ||
| 191 | $extra_class = $item->{'classes'}; | ||
| 192 | } | ||
| 193 | if ($is_first) { | ||
| 194 | if ($extra_class) { | ||
| 195 | $extra_class = "$extra_class first-of-type"; | ||
| 196 | } else { | ||
| 197 | $extra_class = "first-of-type"; | ||
| 198 | } | ||
| 199 | $is_first = 0; | ||
| 200 | } | ||
| 201 | my $link = '#'; | ||
| 202 | if (defined $item->{'link'}) { | ||
| 203 | $link = $item->{'link'}; | ||
| 204 | } | ||
| 205 | my $menuitemclass = 'menubaritem'; | ||
| 206 | if ($level gt 1) { | ||
| 207 | $menuitemclass = 'menuitem'; | ||
| 208 | } | ||
| 209 | if ($extra_class) { | ||
| 210 | $html .= "$tab<li class=\"$menuitemclass $extra_class\">"; | ||
| 211 | } else { | ||
| 212 | $html .= "$tab<li class=\"$menuitemclass\">"; | ||
| 213 | } | ||
| 214 | $html .= "<a class=\"" . $menuitemclass . "label\" href=\"$link\">"; | ||
| 215 | $html .= $item->{'name'} . "</a>"; | ||
| 216 | if (defined $item->{'menu'}) { | ||
| 217 | $html .= "\n"; | ||
| 218 | my $id = (defined $item->{'id'}) ? | ||
| 219 | $item->{'id'} : | ||
| 220 | $self->_generate_random_id(); | ||
| 221 | $html .= "$tab<div id=\"$id\">\n"; | ||
| 222 | $html .= "$tab<div class=\"bd\">\n"; | ||
| 223 | $html .= $self->_generate_child_menu( | ||
| 224 | $item->{'menu'}, | ||
| 225 | $level+1); | ||
| 226 | $html .= "$tab</div>\n"; | ||
| 227 | $html .= "$tab</div>\n"; | ||
| 228 | $html .= "$tab</li>\n"; | ||
| 229 | } else { | ||
| 230 | $html .= "</li>\n"; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | $html .= "$tab</ul>\n"; | ||
| 235 | return $html; | ||
| 236 | } | ||
| 237 | |||
| 238 | # Generates the javascript section | ||
| 239 | sub _generate_javascript { | ||
| 240 | my ($self, $id) = @_; | ||
| 241 | |||
| 242 | my $autosubmenu = ($self->{'autosubmenu'}) ? 'true' : 'false'; | ||
| 243 | my $lazy = ($self->{'lazy'}) ? 'true' : 'false'; | ||
| 244 | my $js_menu = <<JS; | ||
| 245 | YAHOO.util.Event.onContentReady("$id", function () { | ||
| 246 | var oMenuBar = new YAHOO.widget.MenuBar("$id", { | ||
| 247 | autosubmenudisplay: $autosubmenu, | ||
| 248 | hidedelay: $self->{'delay'}, | ||
| 249 | lazyload: $lazy }); | ||
| 250 | oMenuBar.render(); | ||
| 251 | }); | ||
| 252 | JS | ||
| 253 | return $js_menu; | ||
| 254 | } | ||
| 255 | |||
| 256 | # Generates a random ID | ||
| 257 | sub _generate_random_id { | ||
| 258 | my ($self) = @_; | ||
| 259 | |||
| 260 | return 'rand_yui_menu_markup_id' . int(rand(100)); | ||
| 261 | } | ||
| 262 | |||
| 263 | # Gets the ID of the complete menu, the top ID. If no ID is set then a random | ||
| 264 | # ID is returned | ||
| 265 | sub _get_id { | ||
| 266 | my ($self) = @_; | ||
| 267 | |||
| 268 | if ($self->{'top_id'}) { | ||
| 269 | return $self->{'top_id'}; | ||
| 270 | } | ||
| 271 | |||
| 272 | return $self->_generate_random_id(); | ||
| 273 | } | ||
| 274 | |||
| 275 | =head1 SEE ALSO | ||
| 276 | |||
| 277 | =over 4 | ||
| 278 | |||
| 279 | =item * YUI Menu documentation: http://developer.yahoo.com/yui/menu | ||
| 280 | |||
| 281 | =item * L<YUI::Loader> | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | =head1 AUTHOR | ||
| 286 | |||
| 287 | Pablo Fischer (pablo@pablo.com.mx). | ||
| 288 | |||
| 289 | =head1 COPYRIGHT | ||
| 290 | |||
| 291 | Copyright (C) 2010 by Pablo Fischer. | ||
| 292 | |||
| 293 | This library is free software; you can redistribute it and/or modify it under | ||
| 294 | the same terms as Perl itself. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | 1; | ||
| 299 |