[Bio] / WebApplication / WebMenu.pm Repository:
ViewVC logotype

Diff of /WebApplication/WebMenu.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9, Wed Jan 16 22:17:51 2008 UTC revision 1.13, Mon Oct 12 18:58:32 2009 UTC
# Line 58  Line 58 
58                   categories => [],                   categories => [],
59                   categories_index => {},                   categories_index => {},
60                   search => 0,                   search => 0,
61                     style => 'vertical',
62      };      };
63      bless $self, $class;      bless $self, $class;
64    
# Line 206  Line 207 
207    
208  =pod  =pod
209    
210  =item * B<add_entry> (I<category>, I<entry>, I<url>)  =item * B<add_entry> (I<category>, I<entry>, I<url>, I<right>)
211    
212  Adds an entry and link to a existing category of the menu. I<category>, I<entry>  Adds an entry and link to a existing category of the menu. I<category>, I<entry>
213  and I<url> are mandatory. I<category> expects the name of the menu category.  and I<url> are mandatory. I<category> expects the name of the menu category.
214  I<entry> can be any string, I<url> expects a url. I<target> is optional and  I<entry> can be any string, I<url> expects a url. I<target> is optional and
215  defines a href target for that link.  defines a href target for that link.
216    The optional I<right> parameter specifies the right a user must have to
217    be able to see this category.
218    
219  =cut  =cut
220    
221  sub add_entry {  sub add_entry {
222      my ($self, $category, $entry, $url, $target) = @_;      my ($self, $category, $entry, $url, $target, $right) = @_;
223    
224      unless ($category and $entry){# and $url) {      unless ($category and $entry){# and $url) {
225          die "Missing parameter ('$category', '$entry', '$url').";          die "Missing parameter ('$category', '$entry', '$url').";
# Line 228  Line 231 
231    
232      $target = '' unless ($target);      $target = '' unless ($target);
233    
234      push @{$self->{entries}->{$category}}, [ $entry, $url, $target ];      push @{$self->{entries}->{$category}}, [ $entry, $url, $target, $right ];
235    
236      return $self;      return $self;
237  }  }
# Line 248  Line 251 
251    
252    return '' unless scalar(@{$self->{categories}});    return '' unless scalar(@{$self->{categories}});
253    
254      my $html = "";
255    
256      if ($self->style eq 'vertical') {
257        $html = $self->output_vertical($application);
258      } elsif ($self->style eq 'horizontal') {
259        $html = $self->output_horizontal($application);
260      }
261    
262      return $html;
263    }
264    
265    sub output_vertical {
266      my ($self, $application) = @_;
267    
268    my $html = "<div id='menu'>\n";    my $html = "<div id='menu'>\n";
269    $html .= "\t<ul id='nav'>\n";    $html .= "\t<ul id='nav'>\n";
270    
# Line 276  Line 293 
293    
294        foreach (@{$self->{entries}->{$cat}}) {        foreach (@{$self->{entries}->{$cat}}) {
295    
296          my ($entry, $e_url, $e_target) = @$_;          my ($entry, $e_url, $e_target, $e_right) = @$_;
297    
298            # check if a right is required to see this category
299            if (defined($e_right)) {
300              unless (defined($application) && ref($application) eq 'WebApplication') {
301                die "When using rights for a menu category, an application reference must be passed.";
302              }
303              next unless ($application->session->user &&
304                           $application->session->user->has_right($application, @$e_right));
305            }
306    
307          if ($e_url) {          if ($e_url) {
308            my $target = ($e_target) ? qq~target="$e_target"~ : '';            my $target = ($e_target) ? qq~target="$e_target"~ : '';
309            $html .= qq~\t\t\t<li><a href="$e_url" $target>$entry</a></li>\n~;            $html .= qq~\t\t\t<li><a href="$e_url" $target>$entry</a></li>\n~;
310          } else {          } else {
311            $html .= qq~\t\t\t<li>$entry</li>\n~;            $html .= qq~\t\t\t<li><span>$entry</span></li>\n~;
312          }          }
313        }        }
314    
# Line 298  Line 325 
325    $html .= "</div>\n";    $html .= "</div>\n";
326    
327    return $html;    return $html;
328    }
329    
330    sub output_horizontal {
331      my ($self, $application) = @_;
332    
333      my $html = "<table class='menu_table'><tr class='menu_cats'>";
334    
335      my @ordered_categories = sort { $a->[4] <=> $b->[4] } @{$self->{categories}};
336      my $i = 0;
337      foreach (@ordered_categories) {
338    
339        my ($cat, $c_url, $c_target, $right, $order) = @$_;
340    
341        # check if a right is required to see this category
342        if (defined($right)) {
343          unless (defined($application) && ref($application) eq 'WebApplication') {
344            die "When using rights for a menu category, an application reference must be passed.";
345          }
346          next unless ($application->session->user &&
347                       $application->session->user->has_right($application, @$right));
348        }
349    
350        $html .= qq~<td class='menu_cat_inactive' onclick='activate_menu_cat("$i");' id='menu_cat_$i'>$cat</td>~;
351        $i++;
352      }
353      $html .= "<td class='menu_filler'>&nbsp</td></tr></table>";
354    
355      $i = 0;
356      foreach (@ordered_categories) {
357    
358        my ($cat, $c_url, $c_target, $right, $order) = @$_;
359    
360        # check if a right is required to see this category
361        if (defined($right)) {
362          unless (defined($application) && ref($application) eq 'WebApplication') {
363            die "When using rights for a menu category, an application reference must be passed.";
364          }
365          next unless ($application->session->user &&
366                       $application->session->user->has_right($application, @$right));
367        }
368        if (scalar(@{$self->{entries}->{$cat}})) {
369    
370          $html .= "<table id='menu_cat_bar_$i' class='menu_cat_bar_inactive'><tr>";
371    
372          foreach (@{$self->{entries}->{$cat}}) {
373    
374            my ($entry, $e_url, $e_target, $e_right) = @$_;
375    
376            # check if a right is required to see this category
377            if (defined($e_right)) {
378              unless (defined($application) && ref($application) eq 'WebApplication') {
379                die "When using rights for a menu category, an application reference must be passed.";
380              }
381              next unless ($application->session->user &&
382                           $application->session->user->has_right($application, @$e_right));
383            }
384    
385            if ($e_url) {
386              my $target = ($e_target) ? qq~target="$e_target"~ : '';
387              $html .= qq~<td class='menu_item'><a href="$e_url" $target>$entry</a></td>~;
388            } else {
389              $html .= qq~<td class='menu_item'><span>$entry</span></td>~;
390            }
391          }
392    
393          $html .= "<td class='menu_item_filler'></td></tr></table>";
394    
395        }
396        $i++;
397      }
398    
399      $html .= qq~
400    <script>
401    function activate_menu_cat (which) {
402      for (i=0; i<1000; i++) {
403        var cat_bar = document.getElementById('menu_cat_bar_'+i);
404        var cat = document.getElementById('menu_cat_'+i);
405        if (cat_bar) {
406          if (i == which) {
407            cat_bar.className = 'menu_cat_bar_active';
408            cat.className = 'menu_cat_active';
409          } else {
410            cat_bar.className = 'menu_cat_bar_inactive';
411            cat.className = 'menu_cat_inactive';
412          }
413        } else {
414          break;
415        }
416      }
417    }
418    </script>
419    ~;
420    
421      return $html;
422    }
423    
424    sub style {
425      my ($self, $style) = @_;
426    
427      if (defined($style)) {
428        $self->{style} = $style;
429      }
430    
431      return $self->{style};
432  }  }
433    
434  1;  1;

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.13

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3