[LON-CAPA-cvs] cvs: loncom(GCI_3) /interface lonhtmlcommon.pm

raeburn raeburn@source.lon-capa.org
Mon, 04 Oct 2010 19:19:15 -0000


This is a MIME encoded message

--raeburn1286219955
Content-Type: text/plain

raeburn		Mon Oct  4 19:19:15 2010 EDT

  Modified files:              (Branch: GCI_3)
    /loncom/interface	lonhtmlcommon.pm 
  Log:
  - Customization for GCI_3
     - Backport 1.261, 1.274. 
  
  
--raeburn1286219955
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20101004191915.txt"

Index: loncom/interface/lonhtmlcommon.pm
diff -u loncom/interface/lonhtmlcommon.pm:1.253.2.2 loncom/interface/lonhtmlcommon.pm:1.253.2.3
--- loncom/interface/lonhtmlcommon.pm:1.253.2.2	Tue Dec  8 05:19:08 2009
+++ loncom/interface/lonhtmlcommon.pm	Mon Oct  4 19:19:15 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.253.2.2 2009/12/08 05:19:08 raeburn Exp $
+# $Id: lonhtmlcommon.pm,v 1.253.2.3 2010/10/04 19:19:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1383,6 +1383,13 @@
             } else {
                 my $description = 'Menu';
                 my $no_mt_descr = 0;
+                if ((exists($env{'request.course.id'})) &&
+                    ($env{'request.course.id'} ne '') &&
+                    ($env{'course.'.$env{'request.course.id'}.'.description'} ne '')) {
+                    $description =
+                        $env{'course.'.$env{'request.course.id'}.'.description'};
+                    $no_mt_descr = 1;
+                }
                 $menulink =  {  href   =>'/adm/menu',
                                 title  =>'Go to main menu',
                                 target =>'_top',
@@ -1403,23 +1410,30 @@
                      $bug  = $_->{'bug'}  if (exists($_->{'bug'}));
                      $help = $_->{'help'} if (exists($_->{'help'}));
 
-                     my $result = htmltag( 'a', 
-                                           $_->{no_mt} ? 
-                                            $_->{text} : mt($_->{text}), 
+                     my $result = $_->{no_mt} ? $_->{text} : mt($_->{text});
+
+                     if ($_->{href}){
+                         $result = htmltag( 'a', $result, 
                                            { 
                                                href   => $_->{href},
                                                title  => $_->{no_mt} ? 
                                                 $_->{title} : mt($_->{title}),
                                                target => $_->{target},
                                            });
+                     }
+
                      $result = htmltag( 'li', "$result $crumbsymbol");
-                     } @Crumbs;
+                 } @Crumbs;
 
         #should the last Element be translated?
-        $links  .= htmltag( 'li', 
-                        htmltag( 'b', 
-                                 $last->{'no_mt'} ? 
-                                 $last->{'text'} : mt($last->{'text'}) ));
+
+        my $lasttext = $last->{'no_mt'} ? $last->{'text'}
+                     : mt( $last->{'text'} );
+
+        # last breadcrumb is the first order heading of a page
+        # for course breadcrumbs it's just bold
+        $links .= htmltag( 'li', htmltag($CourseBreadcrumbs ? 'b' : 'h1',
+                $lasttext), {title => $lasttext});
 
         my $icons = '';
         $faq  = $last->{'faq'}  if (exists($last->{'faq'}));
@@ -1454,12 +1468,10 @@
                              .$links;
         }
         
-        #SD START (work in progress!)
-        add_tools(\$links);
-        #SD END
+        render_tools(\$links);
         $links = htmltag('div', $links, 
                         { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ;
-        add_advtools(\$links);
+        render_advtools(\$links);
 
         # Return the @Crumbs stack to what we started with
         push(@Crumbs,$last);
@@ -1477,55 +1489,104 @@
         push(@Crumbs,@_);
     }
     
+=item add_breadcrumb_tool($category, $html)
+
+Adds $html to $category of the breadcrumb toolbar container.
+
+$html is usually a link to a page that invokes a function on the currently
+displayed data (e.g. print when viewing a problem)
+
+Currently there are 3 possible values for $category:
+
+=over
+
+=item navigation
+left of breadcrumbs line
+
+=item tools
+right of breadcrumbs line
+
+=item advtools
+advanced tools shown in a separate box below breadcrumbs line
+
+=back
+
+returns: nothing
+
+=cut
 
-    #SD START (work in progress!)
     sub add_breadcrumb_tool {
-        my ($category, $html) = @_;
-        return unless $html;
-        if (!defined(%tools)) { 
-            my %tools = ( A => [], B => [], C => []);
+        my ($category, @html) = @_;
+        return unless @html;
+        if (!defined(%tools)) {
+            %tools = ( navigation => [], tools => [], advtools => []);
+        }
+
+        #this cleans data received from lonmenu::innerregister
+        @html = grep {defined $_ && $_ ne ''} @html;
+        for (@html) {
+            s/align="(right|left)"//;
+            s/<span.*?\/span>// if $category ne 'advtools';
         }
-        push @{$tools{$category}}, $html;
+
+        push @{$tools{$category}}, @html;
     }
 
+=item clear_breadcrumb_tools()
+
+Clears the breadcrumb toolbar container.
+
+returns: nothing
+
+=cut
+
     sub clear_breadcrumb_tools {
         undef(%tools);
     }
 
-    sub add_tools {
-        my ($links) = @_;
+=item render_tools(\$breadcrumbs)
+
+Creates html for breadcrumb tools (categories navigation and tools) and inserts
+\$breadcrumbs at the correct position.
+
+input: \$breadcrumbs - a reference to the string containing prepared
+breadcrumbs.
+
+returns: nothing
+=cut
+
+#TODO might split this in separate functions for each category
+    sub render_tools {
+        my ($breadcrumbs) = @_;
         return unless defined %tools;
-        my $html = '<ul class="LC_bct">';
-        for my $category ('A','B') {
-            $html .= '<li class="LC_breadcrumb_tools">'
-                   . '<ul class="LC_breadcrumb_tools'
-                   . " LC_breadcrumb_tools_$category\">";
-            for my $item (@{$tools{$category}}){
-                #SD ugly! I'll fix that later on
-                $item =~ s/align="(right|left)"//;
-                $item =~ s/<span(.*?)\/span>//;
-                $html .= "<li>$item</li>";
-            }
-            $html .= '</ul></li>';
-            if ($category eq 'A') { $html .= "<li>$$links</li>"; }
-        }
-        $$links = $html.'</ul>';
-    }
-
-    sub add_advtools {
-        my ($links) = @_;
-        return unless (defined $tools{'C'}) and (scalar (@{$tools{'C'}}) > 0);
-        my $html = start_funclist();
-        for my $item (@{$tools{'C'}}){
-                next unless $item;
-                $item =~ s/align="(right|left)"//;
-                $html .= add_item_funclist($item);
-        }
-        $html   .= end_funclist();
-        $html    = Apache::loncommon::head_subbox($html);
-        $$links .= $html;
+
+        my $navigation = list_from_array($tools{navigation},
+                   { listattr => { class=>"LC_breadcrumb_tools_navigation" } });
+        my $tools = list_from_array($tools{tools},
+                   { listattr => { class=>"LC_breadcrumb_tools_tools" } });
+        $$breadcrumbs = list_from_array([$navigation, $tools, $$breadcrumbs],
+                   { listattr => { class=>'LC_breadcrumb_tools_outerlist' } });
+    }
+
+=item render_advtools(\$breadcrumbs)
+
+Creates html for advanced tools (category advtools) and inserts \$breadcrumbs
+at the correct position.
+
+input: \$breadcrumbs - a reference to the string containing prepared
+breadcrumbs (after render_tools call).
+
+returns: nothing
+=cut
+
+    sub render_advtools {
+        my ($breadcrumbs) = @_;
+        return unless     (defined $tools{'advtools'})
+                      and (scalar(@{$tools{'advtools'}}) > 0);
+
+        $$breadcrumbs .= Apache::loncommon::head_subbox(
+                            funclist_from_array($tools{'advtools'}) );
     }
-    #SD END
 
 } # End of scope for @Crumbs
 
@@ -1658,7 +1719,6 @@
 
 } # End: row_count block for pick_box
 
-
 sub role_select_row {
     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
     my $crstype = 'Course';
@@ -1905,7 +1965,6 @@
 
 ##############################################
 ##############################################
-                                                                             
 # echo_form_input
 #
 # Generates html markup to add form elements from the referrer page
@@ -1970,7 +2029,6 @@
 
 ##############################################
 ##############################################
-                                                                             
 # set_form_elements
 #
 # Generates javascript to set form elements to values based on
@@ -2188,7 +2246,7 @@
 # USAGE: inittags(@tags);
 #
 # EXAMPLES:
-#  - my ($h1, $h2, $h3) = initTags( qw( h1 h2 h3 ) )
+#  - my ($h1, $h2, $h3) = inittags( qw( h1 h2 h3 ) )
 #    $h1->("This is a headline") #Returns: <h1>This is a headline</h1>
 #
 # NOTES: See sub htmltag for further information.
@@ -2237,6 +2295,39 @@
 };
 
 
+=item list_from_array( \@array, { listattr =>{}, itemattr =>{} } )
+
+Constructs a XHTML list from \@array.
+
+input:
+
+=over
+
+=item \@array
+
+A reference to the array containing text that will be wrapped in <li></li> tags.
+
+=item { listattr => {}, itemattr =>{} }
+
+Attributes for <ul> and <li> passed in as hash references.
+See htmltag() for more details.
+
+=back
+
+returns: XHTML list as String.
+
+=cut
+
+# \@items, {listattr => { class => 'abc', id => 'xyx' }, itemattr => {class => 'abc', id => 'xyx'}}
+sub list_from_array {
+    my ($items, $args) = @_;
+    return unless scalar @$items;
+    my ($ul, $li) = inittags( qw(ul li) );
+    my $listitems = join '', map { $li->($_, $args->{itemattr}) } @$items;
+    return $ul->( $listitems, $args->{listattr} );
+}
+
+
 ##############################################
 ##############################################
 
@@ -2266,12 +2357,6 @@
 #
 # Outputs: A scalar containing the html markup for the menu.
 
-# ---- Remove when done ----
-# This routine is part of the redesign of LON-CAPA and it's 
-# subject to change during this project.
-# Don't rely on its current functionality as it might be 
-# changed or removed.
-# --------------------------
 sub generate_menu {
     my @menu = @_;
     # subs for specific html elements
@@ -2287,11 +2372,15 @@
             # create the markup for the current $link and push it into @links.
             # each entry consists of an image and a text optionally followed 
             # by a help link.
+            my $src;
+            if ($$link{icon} ne '') {
+                $src = '/res/adm/pages/'.$$link{icon};
+            }
             push(@links,$li->(
                         $a->(
                             $img->("", {
                                 class => "LC_noBorder LC_middle",
-                                src   => "/res/adm/pages/$$link{icon}",
+                                src   => $src,
                                 alt   => mt(defined($$link{alttext}) ?
                                 $$link{alttext} : $$link{linktext})
                             }), {
@@ -2394,9 +2483,6 @@
     return '<li>'.$content.'</li>'."\n";
 }
 
-##############################################
-##############################################
-
 =pod
 
 =item &end_funclist
@@ -2412,16 +2498,46 @@
 Inputs: ./.
 
 Returns: HTML code with function list end
-
 =cut
 
-##############################################
-##############################################
-
 sub end_funclist {
     return "</ul>\n";
 }
 
+=pod
+
+=item funclist_from_array( \@array, {legend => 'text for legend'} )
+
+Constructs a XHTML list from \@array with the first item being visually
+highlighted and set to the value of legend or 'Functions' if legend is
+empty.
+
+=over
+
+=item \@array
+
+A reference to the array containing text that will be wrapped in <li></li> tags.
+
+=item { legend => 'text' }
+
+A string that's used as visually highlighted first item. 'Functions' is used if
+it's value evaluates to false.
+
+=back
+
+returns: XHTML list as string.
+
+=back
+
+=cut
+
+sub funclist_from_array {
+    my ($items, $args) = @_;
+    $args->{legend} ||= mt('Functions');
+    return list_from_array( [$args->{legend}, @$items],
+               { listattr => {class => 'LC_funclist'} });
+}
+
 1;
 
 __END__

--raeburn1286219955--