--- loncom/interface/lonhtmlcommon.pm 2006/12/05 02:55:53 1.155 +++ loncom/interface/lonhtmlcommon.pm 2009/08/10 12:32:34 1.229 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common html routines # -# $Id: lonhtmlcommon.pm,v 1.155 2006/12/05 02:55:53 albertel Exp $ +# $Id: lonhtmlcommon.pm,v 1.229 2009/08/10 12:32:34 droeschl Exp $ # # Copyright Michigan State University Board of Trustees # @@ -67,6 +67,87 @@ use LONCAPA; =pod +=item confirm_success + +Successful completion of an operation message + +=cut + +sub confirm_success { + my ($message,$failure)=@_; + if ($failure) { + return ''."\n" + .''.&mt('Error').' '."\n" + .$message."\n" + .''."\n"; + } else { + return ''."\n" + .''.&mt('Ok').' '."\n" + .$message."\n" + .''."\n"; + } +} + +############################################## +############################################## + +=pod + +=item dragmath_button + +Creates a button that launches a dragmath popup-window, in which an +expression can be edited and pasted as LaTeX into a specified textarea. + + textarea - Name of the textarea to edit. + helpicon - If true, show a help icon to the right of the button. + +=cut + +sub dragmath_button { + my ($textarea,$helpicon) = @_; + my $help_text; + if ($helpicon) { + $help_text = &Apache::loncommon::help_open_topic('Authoring_Math_Editor'); + } + my $buttontext=&mt('Edit Math'); + return <$help_text +ENDDRAGMATH +} + +############################################## + +=pod + +=item dragmath_js + +Javascript used to open pop-up window containing dragmath applet which +can be used to paste LaTeX into a textarea. + +=cut + +sub dragmath_js { + my ($popup) = @_; + return < + // + + +ENDDRAGMATHJS +} + + +############################################## +############################################## + +=pod + =item authorbombs =cut @@ -82,7 +163,7 @@ sub authorbombs { foreach (keys %bombs) { if ($_=~/^$udom\/$uname\//) { return ''. + '">'.&mt('Bomb').''. &Apache::loncommon::help_open_topic('About_Bombs'); } } @@ -140,6 +221,7 @@ sub select_recent { foreach my $value (sort(keys(%recent))) { unless ($value =~/^error\:/) { my $escaped = &Apache::loncommon::escape_url($value); + &Apache::loncommon::inhibit_menu_check(\$escaped); $return.="\n'; @@ -230,7 +312,7 @@ sub checkbox { $Str .= 'value="'.$value.'"'; } if ($checked) { - $Str .= ' checked="1"'; + $Str .= ' checked="checked"'; } $Str .= ' />'; return $Str; @@ -252,7 +334,7 @@ sub radio { $Str .= 'value="'.$value.'"'; } if ($checked eq $value) { - $Str .= ' checked="1"'; + $Str .= ' checked="checked"'; } $Str .= ' />'; return $Str; @@ -310,6 +392,7 @@ The method used to restrict user input w sub date_setter { my ($formname,$dname,$currentvalue,$special,$includeempty,$state, $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_; + my $now = time; my $wasdefined=1; if (! defined($state) || $state ne 'disabled') { $state = ''; @@ -318,28 +401,25 @@ sub date_setter { $no_hh_mm_ss = 0; } if ($currentvalue eq 'now') { - $currentvalue=time; + $currentvalue = $now; } if ((!defined($currentvalue)) || ($currentvalue eq '')) { $wasdefined=0; if ($includeempty) { $currentvalue = 0; } else { - $currentvalue = time; + $currentvalue = $now; } } # other potentially useful values: wkday,yrday,is_daylight_savings + my $tzname; my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','',''); if ($currentvalue) { - ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = - localtime($currentvalue); - $year += 1900; + ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); } unless ($wasdefined) { + ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now); if (($defhour) || ($defmin) || ($defsec)) { - ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = - localtime(time); - $year += 1900; $sec=($defsec?$defsec:0); $min=($defmin?$defmin:0); $hour=($defhour?$defhour:0); @@ -352,6 +432,7 @@ sub date_setter { my $result = "\n\n"; $result .= < +// ENDJS - $result .= ' '; + $result .= ' '; my $monthselector = qq{'; # Day my $dayselector = qq{}; # Year - my $yearselector = qq{}; + my $yearselector = qq{}; # my $hourselector = qq{'. + '// $heading<\/h4>". - "
". + "

".&mt("$heading")."<\/h4>". + "". '<\\/form>'.$end_page. "\');". "popwin.document.close();}". - "\nwindow.setTimeout(openpopwin,0)"); + "\nwindow.setTimeout(openpopwin,0)". + '// ]]>'. + ''); $prog_state{'formname'}='popremain'; $prog_state{'inputname'}="remaining"; } elsif ($type eq 'inline') { $prog_state{'window'}='window'; if (!$formname) { $prog_state{'formname'}=&get_uniq_name(); - &r_print($r,''); + &r_print($r,''); } else { $prog_state{'formname'}=$formname; } if (!$inputname) { $prog_state{'inputname'}=&get_uniq_name(); - &r_print($r,$heading.' '); + &r_print($r,&mt("$heading [_1]",' ')); } else { $prog_state{'inputname'}=$inputname; @@ -891,10 +1025,14 @@ sub Create_PrgWin { # update progress sub Update_PrgWin { my ($r,$prog_state,$displayString)=@_; - &r_print($r,''); + $displayString.'";'. + '// ]]>'. + ''); $$prog_state{'laststart'}=&Time::HiRes::time(); } @@ -947,11 +1085,15 @@ sub Increment_PrgWin { if ($user_browser eq 'explorer' && $user_os =~ 'mac') { $lasttime = ''; } - &r_print($r,''); + ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'. + '// ]]>'. + ''); $$prog_state{'laststart'}=&Time::HiRes::time(); } @@ -959,7 +1101,11 @@ sub Increment_PrgWin { sub Close_PrgWin { my ($r,$prog_state)=@_; if ($$prog_state{'type'} eq 'popup') { - &r_print($r,''."\n"); + &r_print($r,''."\n"); } elsif ($$prog_state{'type'} eq 'inline') { &Update_PrgWin($r,$prog_state,&mt('Done')); } @@ -1002,13 +1148,15 @@ sub crumbs { } } else { $path.='/'; + } + my $href_path = &HTML::Entities::encode($path,'<>&"'); + &Apache::loncommon::inhibit_menu_check(\$href_path); + if ($form) { + my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();'; + $output.=qq{$dir/}; + } else { + $output.=qq{$dir/}; } - my $linkpath = &Apache::loncommon::escape_single($path); - if ($form) { - $linkpath= - qq{javascript:$form.action='$linkpath';$form.submit();}; - } - $output.=qq{$dir/}; } } else { foreach my $dir (split('/',$uri)) { @@ -1039,6 +1187,7 @@ sub spellheader { my $nothing=&javascript_nothing(); return (< +// +// ]]> ENDCHECK } @@ -1082,46 +1232,13 @@ ENDLINK } sub htmlareaheaders { - if (&htmlareablocked()) { return ''; } - unless (&htmlareabrowser()) { return ''; } - my $lang='en'; - if (&mt('htmlarea_lang') ne 'htmlarea_lang') { - $lang=&mt('htmlarea_lang'); - } + return if (&htmlareablocked()); + return if (!&htmlareabrowser()); return (< -_editor_url='/htmlarea/'; -_editor_lang='$lang'; - - - + ENDHEADERS } -# ------------------------------------------------- Activate additional buttons - -sub htmlareaaddbuttons { - if (&htmlareablocked()) { return ''; } - unless (&htmlareabrowser()) { return ''; } - return (<\$','\$ '); - } - ); - config.registerButton('ed_math_eqn','LaTeX Equation', - '/htmlarea/images/ed_math_eqn.gif',false, - function(editor,id) { - editor.surroundHTML( - ' \\n
\\\\[','\\\\]
\\n '); - } - ); - config.toolbar.push(['ed_math','ed_math_eqn']); -ENDADDBUTTON -} - # ----------------------------------------------------------------- Preferences sub disablelink { @@ -1140,18 +1257,38 @@ sub enablelink { return ''.&mt('Enable WYSIWYG Editor').''; } +# ------------------------------------------------- lang to use in html editor +sub htmlarea_lang { + my $lang='en'; + if (&mt('htmlarea_lang') ne 'htmlarea_lang') { + $lang=&mt('htmlarea_lang'); + } + return $lang; +} + # ----------------------------------------- Script to activate only some fields sub htmlareaselectactive { my @fields=@_; unless (&htmlareabrowser()) { return ''; } if (&htmlareablocked()) { return '
'.&enablelink(@fields); } - my $output='
". + my $output='
'. &disablelink(@fields); return $output; } @@ -1177,19 +1314,19 @@ 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) $css_class (optional name for the class to apply to the table for CSS) + $no_mt (optional flag, 1 if &mt() is _not_ to be applied to $component + when including the text on the right. Returns a string containing breadcrumbs for the current page. =item clear_breadcrumbs @@ -1202,7 +1339,8 @@ Pushes a breadcrumb on the stack of crum input: $breadcrumb, a hash reference. The keys 'href','title', and 'text' are required. If present the keys 'faq' and 'bug' will be used to provide -links to the FAQ and bug sites. +links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' +and 'text' values won't be sent through &mt() returns: nothing @@ -1214,55 +1352,70 @@ returns: nothing my @Crumbs; sub breadcrumbs { - my ($component,$component_help,$menulink,$helplink,$css_class) = @_; - # - $css_class ||= 'LC_breadcrumbs'; - my $Str = "\n".''; - # - if (defined($component)) { - $Str .= ''; + if ($faq ne '' || $component_help ne '' || $bug ne '') { + $icons .= &Apache::loncommon::help_open_menu($component, + $component_help, + $faq,$bug); } - $Str .= '
'; + my ($component,$component_help,$menulink,$helplink,$css_class,$no_mt, $CourseBreadcrumbs) = @_; # + $css_class ||= 'LC_breadcrumbs'; + # Make the faq and bug data cascade - my $faq = ''; - my $bug = ''; - my $help=''; + 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; if (exists($env{'request.course.id'}) && $env{'request.course.id'} ne '') { $description = $env{'course.'.$env{'request.course.id'}.'.description'}; + $no_mt_descr = 1; + } + $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; } - unshift(@Crumbs,{ - href =>'/adm/menu', - title =>'Go to main menu', - target =>'_top', - text =>$description, - }); } - my $links .= - join('->', + 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'}.'" '; - } - $result .='title="'.&mt($_->{'title'}).'">'. - &mt($_->{'text'}).''; - $result; - } @Crumbs - ); - $links .= '->' if ($links ne ''); - $links .= ''.&mt($last->{'text'}).''; - # + + 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 '') { @@ -1271,29 +1424,36 @@ 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); - } - # - $Str .= $links.''. - &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; + } + + $links = htmltag('div', $links, + { id => "LC_breadcrumbs" }) unless ($CourseBreadcrumbs) ; + # Return the @Crumbs stack to what we started with push(@Crumbs,$last); shift(@Crumbs); - # - return $Str; + # Return the breadcrumb's line + return "$links"; } sub clear_breadcrumbs { @@ -1320,8 +1480,8 @@ returns: nothing # row1 # row2 # row3 ... etc. -# &submit_row(0 -# &end_pickbox() +# &submit_row() +# &end_pick_box() # # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row, # &status_select_row and &email_default_row @@ -1362,6 +1522,9 @@ returns: nothing # routines, but can also be called directly to start and end rows which have # needs that are not accommodated by the *_select_row() routines. +{ # Start: row_count block for pick_box +my @row_count; + sub start_pick_box { my ($css_class) = @_; if (defined($css_class)) { @@ -1369,6 +1532,7 @@ sub start_pick_box { } else { $css_class= 'class="LC_pick_box"'; } + unshift(@row_count,0); my $output = <<"END"; END @@ -1376,26 +1540,38 @@ END } sub end_pick_box { + shift(@row_count); my $output = <<"END";
END return $output; } +sub row_headline { + my $output = <<"END"; + +END + return $output; +} + sub row_title { my ($title,$css_title_class,$css_value_class) = @_; + $row_count[0]++; + my $css_class = ($row_count[0] % 2)?'LC_odd_row':'LC_even_row'; $css_title_class ||= 'LC_pick_box_title'; $css_title_class = 'class="'.$css_title_class.'"'; $css_value_class ||= 'LC_pick_box_value'; - $css_value_class = 'class="'.$css_value_class.'"'; + if ($title ne '') { + $title .= ':'; + } my $output = <<"ENDONE"; - $title: + $title - + ENDONE return $output; } @@ -1417,6 +1593,9 @@ ENDTWO return $output; } +} # End: row_count block for pick_box + + sub role_select_row { my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_; my $output; @@ -1424,7 +1603,7 @@ sub role_select_row { $output = &row_title($title,$css_class); } $output .= qq| - \n|; foreach my $role (@$roles) { my $plrole; if ($role eq 'ow') { @@ -1460,8 +1639,16 @@ sub course_select_row { my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles, $css_class) = @_; my $output = &row_title($title,$css_class); - $output .= qq| + $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles); + $output .= &row_closure(); + return $output; +} + +sub course_selection { + my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_; + my $output = qq| |; my $courseform=''.&Apache::loncommon::selectcourse_link @@ -1533,8 +1721,7 @@ sub course_select_row { $output .= '
'; } } - $output .= ''.&mt('Pick specific course(s):').' '.$courseform.'  selected.
'."\n"; - $output .= &row_closure(); + $output .= ''.&mt('Pick specific course(s):').' '.$courseform.'  selected.
'."\n"; return $output; } @@ -1545,7 +1732,7 @@ sub status_select_row { $output = &row_title($title,$css_class,'LC_pick_box_select'); } $output .= qq| - \n|; foreach my $status_type (sort(keys(%{$types}))) { $output .= ' '; } @@ -1559,7 +1746,6 @@ sub status_select_row { sub email_default_row { my ($authtypes,$title,$descrip,$css_class) = @_; my $output = &row_title($title,$css_class); - my @rowcols = ('#eeeeee','#dddddd'); $output .= $descrip. &Apache::loncommon::start_data_table(). &Apache::loncommon::start_data_table_header_row(). @@ -1620,6 +1806,23 @@ 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 +# +sub topic_bar { + my ($imgnum,$title) = @_; + return '
' + .''.&mt('Step [_1]',$imgnum).'' + .' '.$title + .'
'; +} + +############################################## +############################################## # echo_form_input # @@ -1830,6 +2033,296 @@ sub set_form_elements { return $output; } +############################################## +############################################## + +# javascript_valid_email +# +# Generates javascript to validate an e-mail address. +# Returns a javascript function which accetps a form field as argumnent, and +# returns false if field.value does not satisfy two regular expression matches +# for a valid e-mail address. Backwards compatible with old browsers without +# support for javascript RegExp (just checks for @ in field.value in this case). + +sub javascript_valid_email { + my $scripttag .= <<'END'; +function validmail(field) { + var str = field.value; + if (window.RegExp) { + var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"; + var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //" + var reg1 = new RegExp(reg1str); + var reg2 = new RegExp(reg2str); + if (!reg1.test(str) && reg2.test(str)) { + return true; + } + return false; + } + else + { + if(str.indexOf("@") >= 0) { + return true; + } + return false; + } +} +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, true/false); +# +# EXAMPLES: +# - scripttag("alert('Hello World!')") +# +# NOTES: +# - works currently only for javascripts +# +# OUTPUT: Scriptcode properly enclosed in