File:  [LON-CAPA] / loncom / homework / edit.pm
Revision 1.70: download - view: text, annotated - select for diffs
Thu Oct 30 20:52:54 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#1974, <textarea>s delete the initial linefeeds if they exist, now checks old value and preserves the \n from the old value if it had been there.

    1: # The LearningOnline Network with CAPA 
    2: # edit mode helpers
    3: #
    4: # $Id: edit.pm,v 1.70 2003/10/30 20:52:54 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 3/20 Guy
   29: # 01/10/02 Matthew
   30: # 03/06/02 Matthew
   31: package Apache::edit; 
   32: 
   33: use strict;
   34: use Apache::lonnet();
   35: use HTML::Entities();
   36: 
   37: # Global Vars
   38: # default list of colors to use in editing
   39: @Apache::edit::colorlist=('#ffffff','#ff0000','#00ff00','#0000ff','#0ff000','#000ff0','#f0000f');
   40: # depth of nesting of edit
   41: $Apache::edit::colordepth=0;
   42: @Apache::edit::inserttag=();
   43: # image-type responses: active background image and curdepth at definition
   44: $Apache::edit::bgimgsrc='';
   45: $Apache::edit::bgimgsrccurdepth='';
   46: 
   47: sub initialize_edit {
   48:     $Apache::edit::colordepth=0;
   49:     @Apache::edit::inserttag=();
   50: }
   51: 
   52: sub tag_start {
   53:     my ($target,$token,$description) = @_;
   54:     my $result='';
   55:     if ($target eq "edit") {
   56: 	my $tag=$token->[1];
   57: 	if (!$description) {
   58: 	    $description=&Apache::lonxml::description($token);
   59: 	    if (!$description) { $description="&lt;$tag&gt;"; }
   60: 	}
   61: 	$result.= &start_table($token)."<tr><td>$description</td>
   62:                       <td>Delete".
   63: 		      &deletelist($target,$token)
   64: 		      ."</td>
   65:                        <td>".
   66: 		       &insertlist($target,$token);
   67: #<td>". 
   68: #  &movebuttons($target,$token).
   69: #    "</tr><tr><td colspan=\"3\">\n";
   70: 	my @help = Apache::lonxml::helpinfo($token);
   71: 	if ($help[0]) {
   72: 	    $result .= '<td align="right" valign="top">' .
   73: 		Apache::loncommon::help_open_topic(@help) .
   74: 		'</td>';
   75: 	} else { $result .= "<td>&nbsp;</td>"; }
   76: 	$result .= &end_row().&start_spanning_row();
   77:     }
   78:     return $result;
   79: }
   80: 
   81: sub tag_end {
   82:     my ($target,$token,$description) = @_;
   83:     my $result='';
   84:     if ($target eq 'edit') {
   85: 	$result.="</td></tr>".&end_table()."\n";
   86:     }
   87:     return $result;
   88: }
   89: 
   90: sub start_table {
   91:     my ($token)=@_;
   92:     my $tag = $token->[1];
   93:     my $tagnum;
   94:     foreach my $namespace (reverse @Apache::lonxml::namespace) {
   95: 	my $testtag=$namespace.'::'.$tag;
   96: 	$tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
   97: 	if (defined($tagnum)) { last; }
   98:     }
   99:     if (!defined ($tagnum)) {$tagnum=$Apache::lonxml::insertlist{"$tag.num"};}
  100:     my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
  101:     if (!defined($color)) {
  102: 	$color = $Apache::edit::colorlist[$Apache::edit::colordepth];
  103:     }
  104:     $Apache::edit::colordepth++;
  105:     push(@Apache::edit::inserttag,$token->[1]);
  106:     my $result='<div align="right">';
  107:     $result.='<table bgcolor="'.$color.'" width="97%" border="0" cellspacing="5" cellpadding="3">';
  108:     return $result;
  109: }
  110: 
  111: sub end_table {
  112:     $Apache::edit::colordepth--;
  113:     my $result='</table></div>';
  114:     $result.="<table><tr><td>";
  115: 
  116:     my ($tagname,$closingtag);
  117:     if (defined($Apache::edit::inserttag[-2])) {
  118: 	$tagname=$Apache::edit::inserttag[-2];
  119:     } else {$tagname='problem';}
  120:     if (defined($Apache::edit::inserttag[-1])) {
  121: 	$closingtag=$Apache::edit::inserttag[-1];
  122:     }
  123:     $result.=&innerinsertlist('edit',$tagname,$closingtag).
  124: 	"</td></tr></table>";
  125:     pop(@Apache::edit::inserttag);
  126:     return $result;
  127: }
  128: 
  129: sub start_spanning_row { return '<tr><td colspan="4" bgcolor="#DDDDDD">';}
  130: sub start_row          { return '<tr><td bgcolor="#DDDDDD">';            }
  131: sub end_row            { return '</td></tr>';          }
  132: 
  133: sub movebuttons {
  134:     my ($target,$token) = @_;
  135:     my $result='<input type="submit" name="moveup.'.
  136: 	$Apache::lonxml::curdepth.'" value="Move Up" />';
  137:     $result.='<input type="submit" name="movedown.'.
  138: 	$Apache::lonxml::curdepth.'" value="Move Down" />';
  139:     return $result;
  140: }
  141: 
  142: sub deletelist {
  143:     my ($target,$token) = @_;
  144:     my $result = "<select name=\"delete_$Apache::lonxml::curdepth\">
  145: <option></option>
  146: <option>Yes</option>
  147: </select>";
  148:     return $result;
  149: }
  150: 
  151: sub handle_delete {
  152:     if (!$ENV{"form.delete_$Apache::lonxml::curdepth"}) { return ''; }
  153:     my ($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  154:     my $result=0;
  155:     if ($space) {
  156: 	my $sub1="$space\:\:delete_$token->[1]";
  157: 	{
  158: 	    no strict 'refs';
  159: 	    if (defined &$sub1) {
  160: 		$result=&$sub1($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
  161: 	    }
  162: 	}
  163:     }
  164:     if (!$result) {
  165: 	my $endtag='/'.$token->[1];
  166: 	my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
  167: 	$$parser['-1']->get_token();
  168: 	&Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
  169: 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
  170:     }
  171:     return 1;
  172: }
  173: 
  174: sub get_insert_list {
  175:     my ($tagname) = @_;
  176:     my $result='';
  177:     my @tagnums= ();
  178:     #&Apache::lonxml::debug("keys ".join("\n",sort(keys(%Apache::lonxml::insertlist))));
  179:     if ($Apache::lonxml::insertlist{"$tagname.which"}) {
  180: 	push (@tagnums, @{ $Apache::lonxml::insertlist{"$tagname.which"} });
  181:     }
  182:     foreach my $namespace (@Apache::lonxml::namespace) {
  183: 	if ($Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"}) {
  184: 	    push (@tagnums, @{ $Apache::lonxml::insertlist{"$namespace".'::'."$tagname.which"} });
  185: 	}
  186:     }
  187:     if (@tagnums) {
  188: 	my %options;
  189: 	foreach my $tagnum (@tagnums) {
  190: 	    my $descrip=$Apache::lonxml::insertlist{"$tagnum.description"};
  191: 	    $options{$descrip} ="<option value=\"$tagnum\">".
  192: 		$descrip."</option>\n";
  193: 	}
  194: 	foreach my $option (sort(keys(%options))) {$result.=$options{$option};}
  195: 	if ($result) { $result='<option selected="on"></option>'.$result; }
  196:     }
  197:     return $result;
  198: }
  199: 
  200: sub insertlist {
  201:     my ($target,$token) = @_;
  202:     return &innerinsertlist($target,$token->[1]);
  203: }
  204: 
  205: sub innerinsertlist {
  206:     my ($target,$tagname,$closingtag) = @_;
  207:     my $result;
  208:     my $after='';
  209:     if ($closingtag) {
  210: 	$after='_after_'.$closingtag; 
  211:     }
  212:     if ($target eq 'edit') {
  213: 	my $optionlist= &get_insert_list($tagname);
  214: 	if ($optionlist) {
  215: 	    $result = "Insert:
  216:             <select name=\"insert$after\_$Apache::lonxml::curdepth\">
  217:                   $optionlist
  218:             </select>"
  219: 	} else {
  220: 	    $result="&nbsp;";
  221: 	}
  222:     }
  223:     return $result;
  224: }
  225: 
  226: sub handle_insert {
  227:     if ($ENV{"form.insert_$Apache::lonxml::curdepth"} eq '') { return ''; }
  228:     my $result;
  229:     my $tagnum = $ENV{"form.insert_$Apache::lonxml::curdepth"};
  230:     my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
  231:     if ($func eq 'default') {
  232: 	my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  233: 	my $namespace;
  234: 	if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
  235: 	$result.="\n<$newtag>\n</$newtag>";
  236:     } else {
  237: 	if (defined(&$func)) {
  238: 	    {
  239: 		no strict 'refs';
  240: 		$result.=&$func();
  241: 	    }
  242: 	} else {
  243: 	    my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  244: 	    &Apache::lonxml::error("Unable to insert tag $newtag, $func was not defined.");
  245: 	}
  246:     }
  247:     return $result;
  248: }
  249: 
  250: sub handle_insertafter {
  251:     my $tagname=shift;
  252:     if ($ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"} eq '')
  253:     { return ''; }
  254:     my $result;
  255:     my $tagnum =$ENV{"form.insert_after_$tagname\_$Apache::lonxml::curdepth"};
  256:     my $func=$Apache::lonxml::insertlist{"$tagnum.function"};
  257:     if ($func eq 'default') {
  258: 	my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  259: 	my $namespace;
  260: 	if ($newtag =~ /::/) { ($namespace,$newtag) = split(/::/,$newtag); }
  261: 	$result.="\n<$newtag>\n</$newtag>";
  262:     } else {
  263: 	if (defined(&$func)) {
  264: 	    {
  265: 		no strict 'refs';
  266: 		$result.=&$func();
  267: 	    }
  268: 	} else {
  269: 	    my $newtag=$Apache::lonxml::insertlist{"$tagnum.tag"};
  270: 	    &Apache::lonxml::error("Unable to insert (after) tag $newtag, $func was not defined. ($tagname $tagnum)");
  271: 	}
  272:     }
  273:     return $result;
  274: }
  275: 
  276: sub insert_img {
  277:     return '
  278:     <img />';
  279: }
  280: 
  281: sub insert_responseparam {
  282:     return '
  283:     <responseparam />';
  284: }
  285: 
  286: sub insert_formularesponse {
  287:     return '
  288: <formularesponse answer="" samples="">
  289:     <textline />
  290:     <hintgroup>
  291:     <startouttext/>
  292:     <endouttext />
  293:     </hintgroup>
  294: </formularesponse>';
  295: }
  296: 
  297: sub insert_numericalresponse {
  298:     return '
  299: <numericalresponse answer="">
  300:     <textline />
  301:     <hintgroup>
  302:     <startouttext/>
  303:     <endouttext />
  304:     </hintgroup>
  305: </numericalresponse>';
  306: }
  307: 
  308: sub insert_stringresponse {
  309:     return '
  310: <stringresponse answer="" type="">
  311:     <textline />
  312:     <hintgroup>
  313:     <startouttext/>
  314:     <endouttext />
  315:     </hintgroup>
  316: </stringresponse>';
  317: }
  318: 
  319: sub insert_essayresponse {
  320:     return '
  321: <essayresponse>
  322:     <textfield></textfield>
  323: </essayresponse>';
  324: }
  325: 
  326: sub insert_imageresponse {
  327:     return '
  328: <imageresponse max="1">
  329:     <foilgroup>
  330:     </foilgroup>
  331:     <hintgroup>
  332:     <startouttext/>
  333:     <endouttext />
  334:     </hintgroup>
  335: </imageresponse>';
  336: }
  337: 
  338: sub insert_optionresponse {
  339:     return '
  340: <optionresponse max="10">
  341:     <foilgroup options="">
  342:     </foilgroup>
  343:     <hintgroup>
  344:     <startouttext/>
  345:     <endouttext />
  346:     </hintgroup>
  347: </optionresponse>';
  348: }
  349: 
  350: sub insert_radiobuttonresponse {
  351:     return '
  352: <radiobuttonresponse max="10">
  353:     <foilgroup>
  354:     </foilgroup>
  355:     <hintgroup>
  356:     <startouttext/>
  357:     <endouttext />
  358:     </hintgroup>
  359: </radiobuttonresponse>';
  360: }
  361: 
  362: sub insert_rankresponse {
  363:     return '
  364: <rankresponse max="10">
  365:     <foilgroup options="">
  366:     </foilgroup>
  367:     <hintgroup>
  368:     <startouttext/>
  369:     <endouttext />
  370:     </hintgroup>
  371: </rankresponse>';
  372: }
  373: 
  374: sub insert_matchresponse {
  375:     return '
  376: <matchresponse max="10">
  377:     <foilgroup options="">
  378:       <itemgroup>
  379:       </itemgroup>
  380:     </foilgroup>
  381:     <hintgroup>
  382:     <startouttext/>
  383:     <endouttext />
  384:     </hintgroup>
  385: </matchresponse>';
  386: }
  387: 
  388: sub insert_displayduedate { return '<displayduedate />'; }
  389: sub insert_displaytitle   { return '<displaytitle />'; }
  390: sub insert_hintpart {
  391:     return '
  392: <hintpart on="default">
  393:     <startouttext/>
  394:     <endouttext />
  395: </hintpart>';
  396: }
  397: 
  398: sub insert_hintgroup {
  399:   return '
  400: <hintgroup>
  401:     <startouttext/>
  402:     <endouttext />
  403: </hintgroup>';
  404: }
  405: 
  406: sub insert_numericalhint {
  407:     return '
  408: <numericalhint>
  409: </numericalhint>';
  410: }
  411: 
  412: sub insert_stringhint {
  413:     return '
  414: <stringhint>
  415: </stringhint>';
  416: }
  417: 
  418: sub insert_formulahint {
  419:     return '
  420: <formulahint>
  421: </formulahint>';
  422: }
  423: 
  424: sub insert_radiobuttonhint {
  425:     return '
  426: <radiobuttonhint>
  427: </radiobuttonhint>';
  428: }
  429: 
  430: sub insert_optionhint {
  431:     return '
  432: <optionhint>
  433: </optionhint>';
  434: }
  435: 
  436: sub insert_startouttext {
  437:     return "<startouttext />\n<endouttext />";
  438: }
  439: 
  440: sub insert_script {
  441:     return "\n<script type=\"loncapa/perl\">\n</script>";
  442: }
  443: 
  444: sub textarea_sizes {
  445:     my ($data)=@_;
  446:     my $count=0;
  447:     my $maxlength=-1;
  448:     foreach (split ("\n", $$data)) {
  449: 	$count+=int(length($_)/79);
  450: 	$count++;
  451: 	if (length($_) > $maxlength) { $maxlength = length($_); }
  452:     }
  453:     my $rows = $count;
  454:     my $cols = $maxlength;
  455:     return ($rows,$cols);
  456: }
  457: 
  458: sub editline {
  459:     my ($tag,$data,$description,$size)=@_;
  460:     $data=&HTML::Entities::encode($data);
  461:     if ($description) { $description="<br />".$description."<br />"; }
  462:     my $result = <<"END";
  463: $description
  464: <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
  465:        value="$data" size="$size" />
  466: END
  467:     return $result;
  468: }
  469: 
  470: sub editfield {
  471:     my ($tag,$data,$description,$minwidth,$minheight)=@_;
  472: 
  473:     my ($rows,$cols)=&textarea_sizes(\$data);
  474:     if ($cols > 80) { $cols = 80; }
  475:     if ($cols < $minwidth ) { $cols = $minwidth; }
  476:     if ($rows < $minheight) { $rows = $minheight; }
  477:     if ($description) { $description="<br />".$description."<br />"; }
  478:     return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea rows="'.$rows.
  479: 	'" cols="'.$cols.'" name="homework_edit_'.
  480: 	$Apache::lonxml::curdepth.'">'.
  481: 	&HTML::Entities::encode($data).'</textarea>'."\n";
  482: }
  483: 
  484: sub modifiedfield {
  485:     my ($endtag,$parser) = @_;
  486:     my $result;
  487: #  foreach my $envkey (sort keys %ENV) {
  488: #    &Apache::lonxml::debug("$envkey ---- $ENV{$envkey}");
  489: #  }
  490: #  &Apache::lonxml::debug("I want homework_edit_$Apache::lonxml::curdepth");
  491: #  &Apache::lonxml::debug($ENV{"form.homework_edit_$Apache::lonxml::curdepth"});
  492:     $result=$ENV{"form.homework_edit_$Apache::lonxml::curdepth"};
  493:     my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
  494:     # textareas throw away intial \n 
  495:     if ($bodytext=~/^\n/) { $result="\n".$result; }
  496:     return $result;
  497: }
  498: 
  499: # Returns a 1 if the token has been modified and you should rebuild the tag
  500: # side-effects, will modify the $token if new values are found
  501: sub get_new_args {
  502:     my ($token,$parstack,$safeeval,@args)=@_;
  503:     my $rebuild=0;
  504:     foreach my $arg (@args) {
  505: 	#just want the string that it was set to
  506: 	my $value=$token->[2]->{$arg};
  507: 	my $element=&html_element_name($arg);
  508: 	my $newvalue=$ENV{"form.$element"};
  509: 	&Apache::lonxml::debug("for:$arg: cur is :$value: new is :$newvalue:");
  510: 	if (defined($newvalue) && $value ne $newvalue) {
  511: 	    if (ref($newvalue) eq 'ARRAY') {
  512: 		$token->[2]->{$arg}=join(',',@$newvalue);
  513: 	    } else {
  514: 		$token->[2]->{$arg}=$newvalue;
  515: 	    }
  516: 	    $rebuild=1;
  517: 	}
  518:     }
  519:     return $rebuild;
  520: }
  521: 
  522: # looks for /> on start tags
  523: sub rebuild_tag {
  524:     my ($token) = @_;
  525:     my $result;
  526:     if ($token->[0] eq 'S') {
  527: 	$result = '<'.$token->[1];
  528: 	while (my ($key,$val)= each(%{$token->[2]})) {
  529: 	    $val=~s:^\s+|\s+$::g;
  530: 	    $val=~s:"::g; #"
  531: 	    &Apache::lonxml::debug("setting :$key: to  :$val:");
  532: 	    $result.=' '.$key.'="'.$val.'"';
  533: 	}
  534: 	if ($token->[4] =~ m:/>$:) {
  535: 	    $result.=' />';
  536: 	} else {
  537: 	    $result.='>';
  538: 	}
  539:     } elsif ( $token->[0] eq 'E' ) {
  540: 	$result = '</'.$token->[1].'>';
  541:     }
  542:     return $result;
  543: }
  544: 
  545: sub html_element_name {
  546:     my ($name) = @_;
  547:     return $name.'_'.$Apache::lonxml::curdepth;
  548: }
  549: 
  550: sub hidden_arg {
  551:     my ($name,$token) = @_;
  552:     my $result;
  553:     my $arg=$token->[2]{$name};
  554:     $result='<input name="'.&html_element_name($name).
  555: 	'" type="hidden" value="'.$arg.'" />';
  556:     return $result;
  557: }
  558: 
  559: sub checked_arg {
  560:     my ($description,$name,$list,$token) = @_;
  561:     my $result;
  562:     my $optionlist="";
  563:     my $allselected=$token->[2]{$name};
  564:     $result=$description;
  565:     foreach my $option (@$list) {
  566: 	my ($value,$text);
  567: 	if ( ref($option) eq 'ARRAY') {
  568: 	    $value='value="'.$$option[0].'"';
  569: 	    $text=$$option[1];
  570: 	    $option=$$option[0];
  571: 	} else {
  572: 	    $text=$option;
  573: 	    $value='value="'.$option.'"';
  574: 	}
  575: 	$result.="<nobr><input type='checkbox' $value name='".
  576: 	    &html_element_name($name)."'";
  577: 	foreach my $selected (split(/,/,$allselected)) {
  578: 	    if ( $selected eq $option ) {
  579: 		$result.=" checked='on' ";
  580: 		last;
  581: 	    }
  582: 	}
  583: 	$result.=" />$text</nobr>\n";
  584:     }
  585:     return $result;
  586: }
  587: 
  588: sub text_arg {
  589:     my ($description,$name,$token,$size) = @_;
  590:     my $result;
  591:     if (!defined $size) { $size=20; }
  592:     my $arg=$token->[2]{$name};
  593:     $result=$description.'&nbsp;<input name="'.&html_element_name($name).
  594: 	'" type="text" value="'.$arg.'" size="'.$size.'" />';
  595:     return '<nobr>'.$result.'</nobr>';
  596: }
  597: 
  598: sub select_arg {
  599:     my ($description,$name,$list,$token) = @_;
  600:     my $result;
  601:     my $optionlist="";
  602:     my $selected=$token->[2]{$name};
  603:     foreach my $option (@$list) {
  604: 	my ($text,$value);
  605: 	if ( ref($option) eq 'ARRAY') {
  606: 	    $value='value="'.$$option[0].'"';
  607: 	    $text=$$option[1];
  608: 	    $option=$$option[0];
  609: 	} else {
  610: 	    $text=$option;
  611: 	    $value='value="'.$option.'"';
  612: 	}
  613: 	if ( $selected eq $option ) {
  614: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
  615: 	} else {
  616: 	    $optionlist.="<option $value >$text</option>\n";
  617: 	}
  618:     }
  619:     $result.='<nobr>'.$description.'&nbsp;<select name="'.
  620: 	&html_element_name($name).'">
  621:        '.$optionlist.'
  622:       </select></nobr>';
  623:     return $result;
  624: }
  625: 
  626: sub select_or_text_arg {
  627:     my ($description,$name,$list,$token,$size) = @_;
  628:     my $result;
  629:     my $optionlist="";
  630:     my $found=0;
  631:     my $selected=$token->[2]{$name};
  632:     foreach my $option (@$list) {
  633: 	my ($text,$value);
  634: 	if ( ref($option) eq 'ARRAY') {
  635: 	    $value='value="'.$$option[0].'"';
  636: 	    $text=$$option[1];
  637: 	    $option=$$option[0];
  638: 	} else {
  639: 	    $text=$option;
  640: 	    $value='value="'.$option.'"';
  641: 	}
  642: 	if ( $selected eq $option ) {
  643: 	    $optionlist.="<option $value selected=\"on\">$text</option>\n";
  644: 	    $found=1;
  645: 	} else {
  646: 	    $optionlist.="<option $value>$text</option>\n";
  647: 	}
  648:     }
  649:     $optionlist.="<option value=\"TYPEDINVALUE\"".
  650:  	((!$found)?' selected="on"':'').
  651:  	">Type in value</option>\n";
  652: #
  653:     my $element=&html_element_name($name);
  654:     my $selectelement='select_list_'.$element;
  655:     my $typeinelement='type_in_'.$element;
  656:     my $typeinvalue=($found?'':$selected);
  657: #
  658:     my $hiddenvalue='this.form.'.$element.'.value';
  659:     my $selectedindex='this.form.'.$selectelement.'.selectedIndex';
  660:     my $selectedvalue='this.form.'.$selectelement.
  661: 	     '.options['.$selectedindex.'].value';
  662:     my $typedinvalue='this.form.'.$typeinelement.'.value';
  663:     my $selecttypeinindex='this.form.'.$selectelement.'.options.length';
  664: #
  665:     return (<<ENDSELECTORTYPE);
  666: <nobr>
  667: $description
  668: &nbsp;<select name="$selectelement"
  669: onChange="if ($selectedvalue!='TYPEDINVALUE') { $hiddenvalue=$selectedvalue; $typedinvalue=''; }"
  670: $optionlist
  671: </select>
  672: <input type="text" size="$size" name="$typeinelement"
  673:        value="$typeinvalue" 
  674: onChange="$hiddenvalue=$typedinvalue;"
  675: onFocus="$selectedindex=$selecttypeinindex-1;" />
  676: <input type="hidden" name="$element" value="$selected" />
  677: </nobr>
  678: ENDSELECTORTYPE
  679: }
  680: 
  681: #----------------------------------------------------- image coordinates
  682: # single image coordinates, x, y 
  683: sub entercoords {
  684:     my ($idx,,$idy,$mode,$width,$height) = @_;
  685:     unless ($Apache::edit::bgimgsrc) { return ''; }
  686:     if ($idx) { $idx.='_'; }
  687:     if ($idy) { $idy.='_'; }
  688:     my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc);
  689:     my $form    = 'lonhomework';
  690:     my $element;
  691:     if (! defined($mode) || $mode eq 'attribute') {
  692:         $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth");
  693:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  694:         $element = &Apache::lonnet::escape('homework_edit_'.
  695:                                            $Apache::lonxml::curdepth);
  696:     }
  697:     my $formheight='';
  698:     if ($height) {
  699: 	$formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth;
  700:     }
  701:     my $formwidth='';
  702:     if ($width) {
  703: 	$formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth;
  704:     }
  705:     my $result = <<"ENDBUTTON";
  706: <a href="/cgi-bin/imagechoice.pl?formname=$form&file=$bgfile&formx=$idx$element&formy=$idy$element$formheight$formwidth"
  707: target="imagechoice">Click Coordinates</a>
  708: ENDBUTTON
  709:     return $result;
  710: }
  711: 
  712: # coordinate pair (x1,y1)-(x2,y2)
  713: sub entercoordpair {
  714:     my ($id,$mode,$width,$height) = @_;
  715:     unless ($Apache::edit::bgimgsrc) { return ''; }
  716:     my $bgfile=&Apache::lonnet::escape($Apache::edit::bgimgsrc);
  717:     my $form    = 'lonhomework';
  718:     my $element;
  719:     if (! defined($mode) || $mode eq 'attribute') {
  720:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
  721:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  722:         $element = &Apache::lonnet::escape('homework_edit_'.
  723:                                            $Apache::lonxml::curdepth);
  724:     }
  725:     my $formheight='';
  726:     if ($height) {
  727: 	$formheight='&formheight='.$height.'_'.$Apache::edit::bgimgsrccurdepth;
  728:     }
  729:     my $formwidth='';
  730:     if ($width) {
  731: 	$formwidth='&formwidth='.$width.'_'.$Apache::edit::bgimgsrccurdepth;
  732:     }
  733:     my $result = <<"ENDBUTTON";
  734: <a href="/cgi-bin/imagechoice.pl?mode=pair&formname=$form&file=$bgfile$formheight$formwidth&formcoord=$element"
  735: target="imagechoice">Click Coordinate Pair</a>
  736: ENDBUTTON
  737:     return $result;
  738: }
  739: #----------------------------------------------------- browse
  740: sub browse {
  741:     # insert a link to call up the filesystem browser (lonindexer)
  742:     my ($id, $mode, $titleid) = @_;
  743:     my $form    = 'lonhomework';
  744:     my $element;
  745:     if (! defined($mode) || $mode eq 'attribute') {
  746:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
  747:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  748:         $element = &Apache::lonnet::escape('homework_edit_'.
  749:                                            $Apache::lonxml::curdepth);	
  750:     }
  751:     my $titleelement;
  752:     if ($titleid) {
  753: 	$titleelement=",'','','".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
  754:     }
  755:     my $result = <<"ENDBUTTON";
  756: <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
  757: ENDBUTTON
  758:     return $result;
  759: }
  760: 
  761: #----------------------------------------------------- browse
  762: sub search {
  763:     # insert a link to call up the filesystem browser (lonindexer)
  764:     my ($id, $mode, $titleid) = @_;
  765:     my $form    = 'lonhomework';
  766:     my $element;
  767:     if (! defined($mode) || $mode eq 'attribute') {
  768:         $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
  769:     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
  770:         $element = &Apache::lonnet::escape('homework_edit_'.
  771:                                            $Apache::lonxml::curdepth);
  772:     }
  773:     my $titleelement;
  774:     if ($titleid) {
  775: 	$titleelement=",'".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
  776:     }
  777:     my $result = <<"ENDBUTTON";
  778: <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>
  779: ENDBUTTON
  780:     return $result;
  781: }
  782: 
  783: 
  784: 1;
  785: __END__
  786: 
  787: =head1 NAME
  788: 
  789: Apache::edit - edit mode helpers
  790: 
  791: =head1 SYNOPSIS
  792: 
  793: Invoked by many homework and xml related modules.
  794: 
  795:  &Apache::edit::SUBROUTINENAME(ARGUMENTS);
  796: 
  797: =head1 INTRODUCTION
  798: 
  799: This module outputs HTML syntax helpful for the rendering of edit
  800: mode interfaces.
  801: 
  802: This is part of the LearningOnline Network with CAPA project
  803: described at http://www.lon-capa.org.
  804: 
  805: =head1 HANDLER SUBROUTINE
  806: 
  807: There is no handler subroutine.
  808: 
  809: =head1 OTHER SUBROUTINES
  810: 
  811: =over 4
  812: 
  813: =item *
  814: 
  815: initialize_edit() : initialize edit (set colordepth to zero)
  816: 
  817: =item *
  818: 
  819: tag_start($target,$token,$description) : provide deletion and insertion lists
  820: for the manipulation of a start tag; return a scalar string
  821: 
  822: =item *
  823: 
  824: tag_end($target,$token,$description) : ending syntax corresponding to
  825: &tag_start. return a scalar string.
  826: 
  827: =item *
  828: 
  829: start_table($token) : start table; update colordepth; return scalar string.
  830: 
  831: =item *
  832: 
  833: end_table() : reduce color depth; end table; return scalar string
  834: 
  835: =item *
  836: 
  837: start_spanning_row() : start a new table row spanning the 'edit' environment.
  838: 
  839: =item *
  840: 
  841: start_row() : start a new table row and element. 
  842: 
  843: =item *
  844: 
  845: end_row() : end current table element and row.
  846: 
  847: =item *
  848: 
  849: movebuttons($target,$token) : move-up and move-down buttons; return scalar
  850: string
  851: 
  852: =item *
  853: 
  854: deletelist($target,$token) : provide a yes option in an HTML select element;
  855: return scalar string
  856: 
  857: =item *
  858: 
  859: handle_delete($space,$target,$token,$tagstack,$parstack,$parser,$safeeval,
  860: $style) : respond to a user delete request by passing relevant stack
  861: and array information to various rendering functions; return a scalar string
  862: 
  863: =item *
  864: 
  865: get_insert_list($token) : provide an insertion list based on possibilities
  866: from lonxml; return a scalar string
  867: 
  868: =item *
  869: 
  870: insertlist($target,$token) : api that uses get_insert_list;
  871: return a scalar string
  872: 
  873: =item *
  874: 
  875: handleinsert($token) : provide an insertion list based on possibilities
  876: from lonxml; return a scalar string
  877: 
  878: =item *
  879: 
  880: get_insert_list($token) : provide an insertion list based on possibilities
  881: from lonxml; return a scalar string
  882: 
  883: =item *
  884: browse($elementname) : provide a link which will open up the filesystem
  885: browser (lonindexer) and, once a file is selected, place the result in
  886: the form element $elementname.
  887: 
  888: =item *
  889: search($elementname) : provide a link which will open up the filesystem
  890: searcher (lonsearchcat) and, once a file is selected, place the result in
  891: the form element $elementname.
  892: 
  893: =item *
  894: editline(tag,data,description,size): Provide a <input type="text" ../> for
  895: single-line text entry.  This is to be used for text enclosed by tags, not
  896: arguements/parameters associated with a tag.
  897: 
  898: =back
  899: 
  900: incomplete...
  901: 
  902: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>