[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface lonmenu.pm

raeburn raeburn at source.lon-capa.org
Wed Sep 14 22:25:12 EDT 2016


raeburn		Thu Sep 15 02:25:12 2016 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	lonmenu.pm 
  Log:
  - For 2.11
    - Backport 1.431, 1.432.
  
  
-------------- next part --------------
Index: loncom/interface/lonmenu.pm
diff -u loncom/interface/lonmenu.pm:1.369.2.57 loncom/interface/lonmenu.pm:1.369.2.58
--- loncom/interface/lonmenu.pm:1.369.2.57	Sat Aug 13 21:14:50 2016
+++ loncom/interface/lonmenu.pm	Thu Sep 15 02:25:12 2016
@@ -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 @@
             (f) optional class for <li> 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 @@
                                                            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 @@
     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 = '<li class="LC_hoverable '.$addclass.'">'.
                '<a href="'.$link.'"'.$disptarget.'>'.
-               '<span class="LC_nobreak">'.$name.
+               '<span class="LC_nobreak">'.$title.
                '<span class="LC_fontsize_small" style="font-weight:normal;">'.
                ' ▼</span></span></a>'.
                '<ul>';
+
+    # $link and $title are only used in the initial string written in $menu
+    # as seen above, not needed for nested submenus
+    $menu .= &build_submenu($target, $submenu, $translate, '1');
+    $menu .= '</ul></li>';
+
+    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 .= '<li style="margin:0;padding:0;'.
-                     $borderbot.'"><a href="'.$href.'"'.$target.'>';
+            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 .= '<li style="margin:0;padding:0;'.$bordertop . $borderbot . '">';
+                $menu .= '<p><span class="LC_primary_menu_innertitle">'
+                                        . $title . '</span><span class="LC_primary_menu_innerarrow">▶</span></p>';
+                $menu .= '<ul>';
+                $menu .= &build_submenu($target, $href, $translate);
+                $menu .= '</ul>';
+                $menu .= '</li>';
+            } 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 .= '<li style="margin:0;padding:0;'. $bordertop . $borderbot .'">';
+                $menu .= '<a href="'.$href.'"'.$target.'>' .  $title . '</a>';
+                $menu .= '</li>';
             }
-            $menu .= '</a></li>';
         }
     }
-    $menu .= '</ul></li>';
     return $menu;
 }
 
@@ -2285,7 +2346,7 @@
     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 @@
             }
         }
     }
-    $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 @@
             }
         }
         if (@submenu > 0) {
-            $switcher = &create_submenu('','',$switchtext,\@submenu);
+            $switcher = &create_submenu('','',&mt('Switch role'),\@submenu);
         }
     }
     return ($js,$form,$switcher);


More information about the LON-CAPA-cvs mailing list