--- loncom/interface/lonmenu.pm 2016/08/13 21:14:50 1.369.2.57 +++ loncom/interface/lonmenu.pm 2016/09/15 02:25:12 1.369.2.58 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines to control the menu # -# $Id: lonmenu.pm,v 1.369.2.57 2016/08/13 21:14:50 raeburn Exp $ +# $Id: lonmenu.pm,v 1.369.2.58 2016/09/15 02:25:12 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -132,6 +132,33 @@ Inputs: 6 - (a) link and (b) target for (f) optional class for
  • element in primary menu, for which sub menu is being generated. +The underlying datastructure used in (d) contains data from mydesk.tab. +It consists of an array which has an array for each item appearing in +the menu (e.g. [["link", "title", "condition"]] for a single-item menu). +create_submenu() supports also the creation of XHTML for nested dropdown +menus represented by unordered lists. This is done by replacing the +scalar used for the link with an arrayreference containing the menuitems +for the nested menu. This can be done recursively so that the next menu +may also contain nested submenus. + + Example: + [ # begin of datastructure + ["/home/", "Home", "condition1"], # 1st item of the 1st layer menu + [ # 2nd item of the 1st layer menu + [ # anon. array for nested menu + ["/path1", "Path1", undef], # 1st item of the 2nd layer menu + ["/path2", "Path2", undef], # 2nd item of the 2nd layer menu + [ # 3rd item of the 2nd layer menu + [[...], [...], ..., [...]], # containing another menu layer + "Sub-Sub-Menu", # title for this container + undef + ] + ], # end of array/nested menu + "Sub-Menu", # title for the container item + undef + ] # end of 2nd item of the 1st layer menu +] + =item innerregister() @@ -285,6 +312,11 @@ sub primary_menu { undef,'tools'))); push(@primsub,$item); } + if ($title eq 'Personal' && $env{'user.name'} && $env{'user.domain'} ) { + $title = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}); + } else { + $title = &mt($title); + } if (@primsub > 0) { $menu{$position} .= &create_submenu($link,$target,$title,\@primsub,1); } elsif ($link) { @@ -536,51 +568,80 @@ sub create_submenu { if ($target ne '') { $disptarget = ' target="'.$target.'"'; } - my $name; - if ($title eq 'Personal') { - if ($env{'user.name'} && $env{'user.domain'}) { - $name = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'}); - } else { - $name = &mt($title); - } - } else { - $name = &mt($title); - } my $menu = '
  • '. ''. - ''.$name. + ''.$title. ''. ' ▼'. '
  • '; + + return $menu; +} + +# helper routine for create_submenu +# build the dropdown (and nested submenus) recursively +# see perldoc create_submenu documentation for further information +sub build_submenu { + my ($target, $submenu, $translate, $first_level) = @_; + unless (@{$submenu}) { + return ''; + } + + my $menu = ''; my $count = 0; my $numsub = scalar(@{$submenu}); foreach my $item (@{$submenu}) { $count ++; if (ref($item) eq 'ARRAY') { my $href = $item->[0]; - if ($href =~ /(aboutme|rss\.html)$/) { - next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')); - $href =~ s/\[domain\]/$env{'user.domain'}/g; - $href =~ s/\[user\]/$env{'user.name'}/g; - } + my $bordertop; my $borderbot; - if ($count == $numsub) { - $borderbot = 'border-bottom:1px solid black;'; - } - unless (($href eq '') || ($href =~ /^\#/)) { - $target = ' target="_top"'; - } - $menu .= '
  • '; + my $title; + if ($translate) { - $menu .= &mt($item->[1]); + $title = &mt($item->[1]); } else { - $menu .= $item->[1]; + $title = $item->[1]; + } + + if ($count == 1 && !$first_level) { + $bordertop = 'border-top: 1px solid black;'; + } + if ($count == $numsub) { + $borderbot = 'border-bottom: 1px solid black;'; + } + + # href is a reference to another submenu + if (ref($href) eq 'ARRAY') { + $menu .= '
  • '; + $menu .= '

    ' + . $title . '

    '; + $menu .= ''; + $menu .= '
  • '; + } else { # href is the actual hyperlink and does not represent another submenu + # for the current menu title + if ($href =~ /(aboutme|rss\.html)$/) { + next unless (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')); + $href =~ s/\[domain\]/$env{'user.domain'}/g; + $href =~ s/\[user\]/$env{'user.name'}/g; + } + unless (($href eq '') || ($href =~ /^\#/)) { + $target = ' target="_top"'; + } + + $menu .= '
  • '; + $menu .= '' . $title . ''; + $menu .= '
  • '; } - $menu .= ''; } } - $menu .= ''; return $menu; } @@ -2285,7 +2346,7 @@ sub roles_selector { my $now = time; my (%courseroles,%seccount,%courseprivs); my $is_cc; - my ($js,$form,$switcher,$switchtext); + my ($js,$form,$switcher); my $ccrole; if ($crstype eq 'Community') { $ccrole = 'co'; @@ -2373,7 +2434,6 @@ sub roles_selector { } } } - $switchtext = 'Switch role'; # do not translate here my @roles_order = ($ccrole,'in','ta','ep','ad','st'); my $numdiffsec; if (keys(%seccount) == 1) { @@ -2429,7 +2489,7 @@ sub roles_selector { } } if (@submenu > 0) { - $switcher = &create_submenu('','',$switchtext,\@submenu); + $switcher = &create_submenu('','',&mt('Switch role'),\@submenu); } } return ($js,$form,$switcher);