[LON-CAPA-cvs] cvs: loncom /homework edit.pm lonhomework.pm structuretags.pm

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 15 Dec 2005 23:20:56 -0000


albertel		Thu Dec 15 18:20:56 2005 EDT

  Modified files:              
    /loncom/homework	lonhomework.pm structuretags.pm edit.pm 
  Log:
  - detect in Edit and EditXML screns if there are changes and throw up a 'unsaved changes warning' BUG#4469
  
  
Index: loncom/homework/lonhomework.pm
diff -u loncom/homework/lonhomework.pm:1.226 loncom/homework/lonhomework.pm:1.227
--- loncom/homework/lonhomework.pm:1.226	Mon Nov 21 16:51:29 2005
+++ loncom/homework/lonhomework.pm	Thu Dec 15 18:20:55 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Homework handler
 #
-# $Id: lonhomework.pm,v 1.226 2005/11/21 21:51:29 albertel Exp $
+# $Id: lonhomework.pm,v 1.227 2005/12/15 23:20:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -753,10 +753,13 @@
 	my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,
 					($env{'environment.remote'} ne 'off'));
 	my $html=&Apache::lonxml::xmlbegin();
-	$result.=$html.$bodytag.
+	my $head="<head><title>EditXML $file</title>".
+	    &Apache::edit::js_change_detection()."</head>";
+
+	$result.=$html.$head.$bodytag.
 	    &renderpage($request,$file,['no_output_web'],1).
 	    &Apache::lonxml::message_location().'
-            <form name="lonhomework" method="POST" action="'.
+            <form '.&Apache::edit::form_change_detection().' name="lonhomework" method="POST" action="'.
 	    &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
 	    &Apache::structuretags::remember_problem_state().'
             <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
@@ -768,7 +771,7 @@
             <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
             <hr />
             ' . $xml_help . '
-            <textarea style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
+            <textarea '.&Apache::edit::element_change_detection().' style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
 	    &HTML::Entities::encode($problem,'<>&"').'</textarea>
             </form></body></html>';
 	&Apache::lonxml::add_messages(\$result);
Index: loncom/homework/structuretags.pm
diff -u loncom/homework/structuretags.pm:1.326 loncom/homework/structuretags.pm:1.327
--- loncom/homework/structuretags.pm:1.326	Thu Dec  1 13:46:31 2005
+++ loncom/homework/structuretags.pm	Thu Dec 15 18:20:55 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # definition of tags that give a structure to a document
 #
-# $Id: structuretags.pm,v 1.326 2005/12/01 18:46:31 albertel Exp $
+# $Id: structuretags.pm,v 1.327 2005/12/15 23:20:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -113,6 +113,9 @@
 	    &Apache::lonhtmlcommon::htmlareaheaders().
 	    &Apache::lonhtmlcommon::spellheader().
 	    &Apache::lonxml::fontsettings();     
+	if ($target eq 'edit') {
+	    $head_tag_start.=&Apache::edit::js_change_detection();
+	}
     }
     my $body_tag_start;
     if (!defined($found{'body'}) && $env{'request.state'} eq 'construct') {
@@ -150,7 +153,11 @@
 	$form_tag_start='<form name="lonhomework" enctype="multipart/form-data" method="POST" action="';
 	my $uri=$env{'request.uri'};
 	if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
-	$form_tag_start.=$uri.'">';
+	$form_tag_start.=$uri.'" ';
+	if ($target eq 'edit') {
+	    $form_tag_start.=&Apache::edit::form_change_detection();
+	}
+	$form_tag_start.='>';
     }
     return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
 }
Index: loncom/homework/edit.pm
diff -u loncom/homework/edit.pm:1.97 loncom/homework/edit.pm:1.98
--- loncom/homework/edit.pm:1.97	Thu Dec  1 13:46:31 2005
+++ loncom/homework/edit.pm	Thu Dec 15 18:20:55 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # edit mode helpers
 #
-# $Id: edit.pm,v 1.97 2005/12/01 18:46:31 albertel Exp $
+# $Id: edit.pm,v 1.98 2005/12/15 23:20:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -498,6 +498,37 @@
     return "\n<script type=\"loncapa/perl\"></script>";
 }
 
+sub js_change_detection {
+    my $unsaved=&mt("There are unsaved changes");
+    return (<<SCRIPT);
+<script type="text/javascript">
+var clean = true;
+var is_submit = false;
+function compareForm(event_) {
+        if (!event_ && window.event) {
+          event_ = window.event;
+        }
+	if (!is_submit && !clean) {
+            event_.returnValue = "$unsaved";
+            return "$unsaved";
+        }
+}
+function unClean() {
+     clean=false;
+}
+window.onbeforeunload = compareForm;
+</script>
+SCRIPT
+}
+
+sub form_change_detection {
+    return ' onsubmit="is_submit=true;" ';
+}
+
+sub element_change_detection {
+    return ' onchange="unClean();" ';
+}
+
 sub textarea_sizes {
     my ($data)=@_;
     my $count=0;
@@ -516,10 +547,11 @@
     my ($tag,$data,$description,$size)=@_;
     $data=&HTML::Entities::encode($data,'<>&"');
     if ($description) { $description="<br />".$description."<br />"; }
+    my $change_code = &element_change_detection();
     my $result = <<"END";
 $description
 <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
-       value="$data" size="$size" />
+       value="$data" size="$size" $change_code />
 END
     return $result;
 }
@@ -544,7 +576,7 @@
     return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea style="width:100%" rows="'.$rows.
 	'" cols="'.$cols.'" name="homework_edit_'.
 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
-	$Apache::lonxml::curdepth.'">'.
+	$Apache::lonxml::curdepth.'" '.&element_change_detection().'>'.
 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
@@ -652,7 +684,7 @@
 		last;
 	    }
 	}
-	$result.=" />$text</nobr>\n";
+	$result.=&element_change_detection()." />$text</nobr>\n";
     }
     return $result;
 }
@@ -663,7 +695,8 @@
     if (!defined $size) { $size=20; }
     my $arg=$token->[2]{$name};
     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
-	'" type="text" value="'.$arg.'" size="'.$size.'" />';
+	'" type="text" value="'.$arg.'" size="'.$size.'" '.
+	&element_change_detection().'/>';
     return '<nobr>'.$result.'</nobr>';
 }
 
@@ -689,7 +722,7 @@
 	}
     }
     $result.='<nobr>'.$description.'&nbsp;<select name="'.
-	&html_element_name($name).'">
+	&html_element_name($name).'" '.&element_change_detection().' >
        '.$optionlist.'
       </select></nobr>';
     return $result;
@@ -722,6 +755,7 @@
  	((!$found)?' selected="selected"':'').
  	">".&mt('Type-in value')."</option>\n";
 #
+    my $change_code=&element_change_detection();
     my $element=&html_element_name($name);
     my $selectelement='select_list_'.$element;
     my $typeinelement='type_in_'.$element;
@@ -746,7 +780,7 @@
        value="$typeinvalue" 
 onChange="$hiddenvalue=$typedinvalue;"
 onFocus="$selectedindex=$selecttypeinindex-1;" />
-<input type="hidden" name="$element" value="$selected" />
+<input type="hidden" name="$element" value="$selected" $change_code />
 </nobr>
 ENDSELECTORTYPE
 }