--- loncom/interface/lonhtmlcommon.pm 2009/04/25 20:22:07 1.209 +++ loncom/interface/lonhtmlcommon.pm 2009/12/22 05:33:36 1.255 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common html routines # -# $Id: lonhtmlcommon.pm,v 1.209 2009/04/25 20:22:07 www Exp $ +# $Id: lonhtmlcommon.pm,v 1.255 2009/12/22 05:33:36 faziophi Exp $ # # Copyright Michigan State University Board of Trustees # @@ -62,6 +62,22 @@ use Apache::lonlocal; use Apache::lonnet; use LONCAPA; + +sub coursepreflink { + my ($text,$category)=@_; + if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) { + return ''.$text.''; + } else { + return ''; + } +} + +sub raw_href_to_link { + my ($message)=@_; + $message=~s/(https?\:\/\/[^\s\'\"]+)(\s|$)/$1<\/tt><\/a>$2/gi; + return $message; +} + ############################################## ############################################## @@ -76,13 +92,15 @@ Successful completion of an operation me sub confirm_success { my ($message,$failure)=@_; if ($failure) { - return ''. - $message. - ' '; + return ''."\n" + .''.&mt('Error').' '."\n" + .$message."\n" + .''."\n"; } else { - return ''. - $message. - ' '; + return ''."\n" + .''.&mt('OK').' '."\n" + .$message."\n" + .''."\n"; } } @@ -109,7 +127,7 @@ sub dragmath_button { } my $buttontext=&mt('Edit Math'); return <$help_text + $help_text ENDDRAGMATH } @@ -128,11 +146,13 @@ sub dragmath_js { my ($popup) = @_; return < + // ENDDRAGMATHJS @@ -156,10 +176,10 @@ sub authorbombs { $url=&Apache::lonnet::declutter($url); my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/}); my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom); - foreach (keys %bombs) { - if ($_=~/^$udom\/$uname\//) { + foreach my $bomb (keys(%bombs)) { + if ($bomb =~ /^$udom\/$uname\//) { return ''. + '">'.&mt('Bomb').''. &Apache::loncommon::help_open_topic('About_Bombs'); } } @@ -218,6 +238,9 @@ sub select_recent { unless ($value =~/^error\:/) { my $escaped = &Apache::loncommon::escape_url($value); &Apache::loncommon::inhibit_menu_check(\$escaped); + if ($area eq 'residx') { + next if ((!&Apache::lonnet::allowed('bre',$value)) && (!&Apache::lonnet::allowed('bro',$value))); + } $return.="\n'; @@ -235,7 +258,7 @@ sub get_recent { # Begin filling return_hash with any 'always_include' option my %time_hash = (); my %return_hash = (); - foreach my $item (keys %recent) { + foreach my $item (keys(%recent)) { my ($thistime,$thisvalue)=(split(/\&/,$recent{$item})); if ($thistime eq 'always_include') { $return_hash{$item} = &unescape($thisvalue); @@ -428,6 +451,7 @@ sub date_setter { my $result = "\n\n"; $result .= < +// ENDJS $result .= ' '; @@ -499,23 +524,23 @@ ENDJS unshift(@Months,'If you can read this an error occurred'); if ($includeempty) { $monthselector.=""; } for(my $m = 1;$m <=$#Months;$m++) { - $monthselector .= qq{ '; + + return $output; } # --------------------- A function that generates a window for the spellchecker @@ -1166,6 +1196,7 @@ sub spellheader { my $nothing=&javascript_nothing(); return (< +// +// ]]> ENDCHECK } @@ -1209,11 +1241,18 @@ ENDLINK } sub htmlareaheaders { - return if (&htmlareablocked()); - return if (!&htmlareabrowser()); - return (< -ENDHEADERS + +ENDEDITOR + } + $s.=(< + +ENDJQUERY + return $s; } # ----------------------------------------------------------------- Preferences @@ -1249,20 +1288,80 @@ sub htmlareaselectactive { my @fields=@_; unless (&htmlareabrowser()) { return ''; } if (&htmlareablocked()) { return '
'.&enablelink(@fields); } - my $output='
". + + function destroyRichEditor(id) { + CKEDITOR.instances[id].destroy(); + } + + function editorHandler(event) { + var rawid = $(this).attr("id"); + var id = new RegExp("LC_rt_(.*)").exec(rawid)[1] + event.preventDefault(); + if ($(this).hasClass("LC_enable_rt")) { + startRichEditor(id); + $("#LC_rt_"+id).html("« Plain text"); + $("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text"); + $("#LC_rt_"+id).addClass("LC_disable_rt"); + $("#LC_rt_"+id).removeClass("LC_enable_rt"); + } else { + destroyRichEditor(id); + $("#LC_rt_"+id).html("Rich formatting »"); + $("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)"); + $("#LC_rt_"+id).addClass("LC_enable_rt"); + $("#LC_rt_"+id).removeClass("LC_disable_rt"); + } + } + + $(document).ready(function(){ + $(".LC_richAlwaysOn").each(function() { + startRichEditor($(this).attr("id")); + }); + $(".LC_richDetectHtml").each(function() { + var id = $(this).attr("id"); + if(containsBlockHtml(id)) { + $(this).before(""); + startRichEditor(id); + $("#LC_rt_"+id).click(editorHandler); + } + else { + $(this).before(""); + $("#LC_rt_"+id).click(editorHandler); + } + }); + $(".LC_richDefaultOn").each(function() { + var id = $(this).attr("id"); + $(this).before(""); + startRichEditor(id); + $("#LC_rt_"+id).click(editorHandler); + }); + $(".LC_richDefaultOff").each(function() { + var id = $(this).attr("id"); + $(this).before(""); + $("#LC_rt_"+id).click(editorHandler); + }); + }); +'; + + $output.="\nwindow.status='Activated Editfields';\n" + .'// ]]>'."\n" + .'
'. &disablelink(@fields); return $output; } @@ -1288,15 +1387,13 @@ sub htmlareabrowser { =item breadcrumbs Compiles the previously registered breadcrumbs into an series of links. -FAQ and BUG links will be placed on the left side of the table if they -are defined for the last registered breadcrumb. Additionally supports a 'component', which will be displayed on the -right side of the table (without a link). +right side of the breadcrumbs enclosing div (without a link). A link to help for the component will be included if one is specified. All inputs can be undef without problems. -Inputs: $component (the large text on the right side of the table), +Inputs: $component (the text on the right side of the breadcrumbs trail), $component_help $menulink (boolean, controls whether to include a link to /adm/menu) $helplink (if 'nohelp' don't include the orange help link) @@ -1326,30 +1423,24 @@ returns: nothing ############################################################ { my @Crumbs; + my %tools = (); sub breadcrumbs { - my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $no_realBreadcrumb) = @_; + my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_; # - $css_class ||= 'LC_breadcrumbs'; - my $Str1 = '
    '; - - if($no_realBreadcrumb){ - $Str1 = '
      '; - } + $css_class ||= 'LC_breadcrumbs'; - my $Str = ''; - # # Make the faq and bug data cascade - my $faq = ''; - my $bug = ''; - my $help=''; - # Crumb Symbol - my $crumbsymbol = '» '; + my $faq = ''; + my $bug = ''; + my $help = ''; + # Crumb Symbol + my $crumbsymbol = '»'; # The last breadcrumb does not have a link, so handle it separately. my $last = pop(@Crumbs); # # The first one should be the course or a menu link - if (!defined($menulink)) { $menulink=1; } + if (!defined($menulink)) { $menulink=1; } if ($menulink) { my $description = 'Menu'; my $no_mt_descr = 0; @@ -1359,51 +1450,46 @@ returns: nothing $env{'course.'.$env{'request.course.id'}.'.description'}; $no_mt_descr = 1; } - unshift(@Crumbs,{ - href =>'/adm/menu', - title =>'Go to main menu', - target =>'_top', - text =>$description, - no_mt =>$no_mt_descr, - }); + $menulink = { href =>'/adm/menu', + title =>'Go to main menu', + target =>'_top', + text =>$description, + no_mt =>$no_mt_descr, }; + if($last) { + #$last set, so we have some crumbs + unshift(@Crumbs,$menulink); + } else { + #only menulink crumb present + $last = $menulink; + } } - my $links .= '
    • '. - join('
    • '.$crumbsymbol, + my $links = join "", map { - $faq = $_->{'faq'} if (exists($_->{'faq'})); - $bug = $_->{'bug'} if (exists($_->{'bug'})); + $faq = $_->{'faq'} if (exists($_->{'faq'})); + $bug = $_->{'bug'} if (exists($_->{'bug'})); $help = $_->{'help'} if (exists($_->{'help'})); - my $result = '{'target'}) && $_->{'target'} ne '') { - $result .= 'target="'.$_->{'target'}.'" '; - } - # set the possible translation for title - if ($_->{'no_mt'}) { - $result .='title="'.$_->{'title'}.'">'. - $_->{'text'}.''; - } else { - $result .='title="'.&mt($_->{'title'}).'">'. - &mt($_->{'text'}).''; - } - $result; - } @Crumbs - ).'
    • '; -#Workaround for edit course. - if(@Crumbs == 0 ){ - $links .= '
    • ' if ($links ne ''); - } else { - $links .= '
    • '.$crumbsymbol if ($links ne ''); - } -#should the last Element be translated? - if ($last->{'no_mt'}) { - $links .= ''.$last->{'text'}.''; - } else { - $links .= ''.&mt($last->{'text'}).''; - } - $links .= '
    • '; + + my $result = htmltag( 'a', + $_->{no_mt} ? + $_->{text} : mt($_->{text}), + { + href => $_->{href}, + title => $_->{no_mt} ? + $_->{title} : mt($_->{title}), + target => $_->{target}, + }); + $result = htmltag( 'li', "$result $crumbsymbol"); + } @Crumbs; + + #should the last Element be translated? + $links .= htmltag( 'li', + htmltag( 'b', + $last->{'no_mt'} ? + $last->{'text'} : mt($last->{'text'}) )); + my $icons = ''; - $faq = $last->{'faq'} if (exists($last->{'faq'})); - $bug = $last->{'bug'} if (exists($last->{'bug'})); + $faq = $last->{'faq'} if (exists($last->{'faq'})); + $bug = $last->{'bug'} if (exists($last->{'bug'})); $help = $last->{'help'} if (exists($last->{'help'})); $component_help=($component_help?$component_help:$help); # if ($faq ne '') { @@ -1412,48 +1498,100 @@ returns: nothing # if ($bug ne '') { # $icons .= &Apache::loncommon::help_open_bug($bug); # } - if ($faq ne '' || $component_help ne '' || $bug ne '') { - $icons .= &Apache::loncommon::help_open_menu($component, - $component_help, - $faq,$bug); - } + if ($faq ne '' || $component_help ne '' || $bug ne '') { + $icons .= &Apache::loncommon::help_open_menu($component, + $component_help, + $faq,$bug); + } # - if($no_realBreadcrumb){ - $Str1 .= $links.'
    '; - } else { - $Str1 .= $links.'
'; - } - # - if (defined($component)) { - $Str .= "\n".'' - .'
'; - if ($no_mt) { - $Str .= $component; - } else { - $Str .= &mt($component); - } - if ($icons ne '') { - $Str .= ' '.$icons; - } - $Str .= '
'."\n"; + unless ($CourseBreadcrumbs) { + $links = htmltag('ol', $links, { id => "LC_MenuBreadcrumbs" }); + } else { + $links = htmltag('ul', $links, { class => "LC_CourseBreadcrumbs" }); } - # + + if ($component) { + $links = htmltag('span', + ( $no_mt ? $component : mt($component) ). + ( $icons ? $icons : '' ), + { class => 'LC_breadcrumbs_component' } ) + .$links; + } + + #SD START (work in progress!) + add_tools(\$links); + #SD END + $links = htmltag('div', $links, + { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ; + add_advtools(\$links); + # Return the @Crumbs stack to what we started with push(@Crumbs,$last); shift(@Crumbs); - # Return a table and after that the breadcrumb's line - return "$Str\n$Str1"; + # Return the breadcrumb's line + return "$links"; } sub clear_breadcrumbs { undef(@Crumbs); + undef(%tools); } sub add_breadcrumb { - push (@Crumbs,@_); + push(@Crumbs,@_); + } + + + #SD START (work in progress!) + sub add_breadcrumb_tool { + my ($category, $html) = @_; + return unless $html; + if (!defined(%tools)) { + my %tools = ( A => [], B => [], C => []); + } + push @{$tools{$category}}, $html; + } + + sub clear_breadcrumb_tools { + undef(%tools); + } + + sub add_tools { + my ($links) = @_; + return unless defined %tools; + my $html = '
    '; + for my $category ('A','B') { + $html .= '
  • ' + . '
      "; + for my $item (@{$tools{$category}}){ + #SD ugly! I'll fix that later on + $item =~ s/align="(right|left)"//; + $item =~ s///; + $html .= "
    • $item
    • "; + } + $html .= '
  • '; + if ($category eq 'A') { $html .= "
  • $$links
  • "; } + } + $$links = $html.'
'; + } + + 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; } + #SD END } # End of scope for @Crumbs @@ -1546,9 +1684,9 @@ END } sub row_title { + my ($title,$css_title_class,$css_value_class, $css_value_furtherAttributes) = @_; $row_count[0]++; my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row'; - my ($title,$css_title_class,$css_value_class) = @_; $css_title_class ||= 'LC_pick_box_title'; $css_title_class = 'class="'.$css_title_class.'"'; @@ -1558,7 +1696,7 @@ sub row_title { $title .= ':'; } my $output = <<"ENDONE"; - + $title @@ -1589,6 +1727,10 @@ ENDTWO sub role_select_row { my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_; + my $crstype = 'Course'; + if ($cdom ne '' && $cnum ne '') { + $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum); + } my $output; if (defined($title)) { $output = &row_title($title,$css_class); @@ -1613,7 +1755,7 @@ sub role_select_row { $plrole = &mt('Custom Role'); } } else { - $plrole=&Apache::lonnet::plaintext($role); + $plrole=&Apache::lonnet::plaintext($role,$crstype); } if (($role ne 'cr') || (!$show_separate_custom)) { $output .= ' '; @@ -1628,17 +1770,18 @@ sub role_select_row { sub course_select_row { my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles, - $css_class) = @_; + $css_class,$crstype) = @_; my $output = &row_title($title,$css_class); - $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles); + $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype); $output .= &row_closure(); return $output; } sub course_selection { - my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_; + my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles,$crstype) = @_; my $output = qq| |; + + my ($allcrs,$pickspec); + if ($crstype eq 'Community') { + $allcrs = &mt('All communities'); + $pickspec = &mt('Pick specific communities:'); + } else { + $allcrs = &mt('All courses'); + $pickspec = &mt('Pick specific course(s):'); + } + my $courseform=''.&Apache::loncommon::selectcourse_link - ($formname,'pickcourse','pickdomain','coursedesc','',1).''; - $output .= ''.&mt('All courses').'
'; + ($formname,'pickcourse','pickdomain','coursedesc','',1,$crstype).''; + $output .= ''.$allcrs.'
'; if ($totcodes > 0) { my $numtitles = @$codetitles; if ($numtitles > 0) { @@ -1710,7 +1864,7 @@ sub course_selection { $output .= '
'; } } - $output .= ''.&mt('Pick specific course(s):').' '.$courseform.'  selected.
'."\n"; + $output .= ''.$pickspec.' '.$courseform.'  selected.
'."\n"; return $output; } @@ -1765,7 +1919,6 @@ sub email_default_row { sub submit_row { my ($title,$cmd,$submit_text,$css_class) = @_; - $submit_text = &mt($submit_text); my $output = &row_title($title,$css_class,'LC_pick_box_submit'); $output .= qq|
@@ -1799,18 +1952,21 @@ sub course_custom_roles { # topic_bar # -# Generates a div containing a numbered (static image) followed by a title -# with a background color defined in the corresponding CSS: LC_topic_bar -# +# Generates a div containing an (optional) number with a white background followed by a +# title with a background color defined in the corresponding CSS: LC_topic_bar +# Inputs: +# 1. number to display. +# If input for number is empty only the title will be displayed. +# 2. title text to display. +# Outputs - a scalar containing html mark-up for the div. + sub topic_bar { - my ($imgnum,$title) = @_; - return ' -
- '.&mt('Step [_1]',$imgnum).
-              '  - '.$title.' -
-'; + my ($num,$title) = @_; + my $number = ''; + if ($num ne '') { + $number = ''.$num.''; + } + return '
'.$number.$title.'
'; } ############################################## @@ -1945,7 +2101,7 @@ sub set_form_elements { $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g; $values{$name}[$i] =~ s/"/\\"/g; } - if ($$elements{$name} eq 'text') { + if (($$elements{$name} eq 'text') || ($$elements{$name} eq 'hidden')) { my $numvalues = @{$values{$name}}; if ($numvalues > 1) { my $valuestring = join('","',@{$values{$name}}); @@ -1980,6 +2136,8 @@ sub set_form_elements { $output .= qq| if (courseForm.elements['$name'].value == "$value") { courseForm.elements['$name'].checked = true; + } else { + courseForm.elements['$name'].checked = false; }|; } } @@ -2021,6 +2179,7 @@ sub set_form_elements { } } $output .= " + return; }\n"; return $output; } @@ -2062,6 +2221,88 @@ END return $scripttag; } + +# USAGE: htmltag(element, content, {attribute => value,...}); +# +# EXAMPLES: +# - htmltag('a', 'this is an anchor', {href => 'www.example.com', +# title => 'this is a title'}) +# +# - You might want to set up needed tags like: +# +# my $h3 = sub { return htmltag( "h3", @_ ) }; +# +# ... and use them: $h3->("This is a headline") +# +# - To set up a couple of tags, see sub inittags +# +# NOTES: +# - Empty elements, such as
are correctly terminated, +# i.e. htmltag('br') returns
+# - Empty attributes (title="") are filtered out. +# - The function will not check for deprecated attributes. +# +# OUTPUT: content enclosed in xhtml conform tags +sub htmltag{ + return + qq|<$_[0]| + . join( '', map { qq| $_="${$_[2]}{$_}"| if ${$_[2]}{$_} } keys %{ $_[2] } ) + . ($_[1] ? qq|>$_[1]| : qq|/>|). "\n"; +}; + + +# USAGE: inittags(@tags); +# +# EXAMPLES: +# - my ($h1, $h2, $h3) = initTags( qw( h1 h2 h3 ) ) +# $h1->("This is a headline") #Returns:

This is a headline

+# +# NOTES: See sub htmltag for further information. +# +# OUTPUT: List of subroutines. +sub inittags { + my @tags = @_; + return map { my $tag = $_; + sub { return htmltag( $tag, @_ ) } + } @tags; +} + + +# USAGE: scripttag(scriptcode, [start|end|both]); +# +# EXAMPLES: +# - scripttag("alert('Hello World!')", 'both') +# returns: +# +# +# NOTES: +# - works currently only for javascripts +# +# OUTPUT: +# Scriptcode properly enclosed in