[system] / trunk / pg / macros / AppletObjects.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/AppletObjects.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5971 Revision 5993
1################################################################################ 1################################################################################
2# WeBWorK Online Homework Delivery System 2# WeBWorK Online Homework Delivery System
3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ 3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: pg/macros/AppletObjects.pl,v 1.11 2008/05/22 19:17:10 gage Exp $ 4# $CVSHeader: pg/macros/AppletObjects.pl,v 1.12 2009/01/17 03:25:31 gage Exp $
5# 5#
6# This program is free software; you can redistribute it and/or modify it under 6# This program is free software; you can redistribute it and/or modify it under
7# the terms of either: (a) the GNU General Public License as published by the 7# the terms of either: (a) the GNU General Public License as published by the
8# Free Software Foundation; either version 2, or (at your option) any later 8# Free Software Foundation; either version 2, or (at your option) any later
9# version, or (b) the "Artistic License" which comes with this package. 9# version, or (b) the "Artistic License" which comes with this package.
45main::HEADER_TEXT(<<'END_HEADER_TEXT'); 45main::HEADER_TEXT(<<'END_HEADER_TEXT');
46 <script language="javascript">AC_FL_RunContent = 0;</script> 46 <script language="javascript">AC_FL_RunContent = 0;</script>
47 <script src="/webwork2_files/applets/AC_RunActiveContent.js" language="javascript"> 47 <script src="/webwork2_files/applets/AC_RunActiveContent.js" language="javascript">
48 </script> 48 </script>
49 <script src="/webwork2_files/js/Base64.js" language="javascript"> 49 <script src="/webwork2_files/js/Base64.js" language="javascript">
50 </script>
51 <script src="/webwork2_files/js/ww_applet_support.js">
52 //upload functions stored in /opt/webwork/webwork2/htdocs/js ...
50 </script> 53 </script>
51
52<script language="JavaScript">
53
54//////////////////////////////////////////////////////////
55// applet lists
56//////////////////////////////////////////////////////////
57
58 var applet_initializeAction_list = new Object; // functions for initializing question with an applet
59 var applet_submitAction_list = new Object; // functions for submitting question with applet
60 var applet_setState_list = new Object; // functions for setting state (XML) from applets
61 var applet_getState_list = new Object; // functions for getting state (XML) from applets
62 var applet_config_list = new Object; // functions for configuring on applets
63 var applet_checkLoaded_list = new Object; // functions for probing the applet to see if it is loaded
64 var applet_reportsLoaded_list = new Object; // flag set by applet
65 var applet_isReady_list = new Object; // flag set by javaScript in checkLoaded
66
67//////////////////////////////////////////////////////////
68// DEBUGGING tools
69//////////////////////////////////////////////////////////
70 var debug;
71 var debugText = "";
72 function set_debug(num) { // setting debug for any applet sets it for all of them
73 if (num) {
74 debug =1;
75 }
76 }
77 function debug_add(str) {
78 if (debug) {
79 debugText = debugText + "\n\n" +str;
80 }
81 }
82
83//////////////////////////////////////////////////////////
84// INITIALIZE and SUBMIT actions
85//////////////////////////////////////////////////////////
86
87 function submitAction() {
88
89 if (debug) {
90 debugText = " Begin looping through applet_submitAction_list\n";
91 }
92 for (var applet in applet_submitAction_list) {
93
94 applet_submitAction_list[applet]();
95 }
96 if (debug) {
97 alert(debugText); debugText="";
98 };
99 }
100 function initializeAction() {
101 var iMax = 10;
102 debugText="start intializeAction() with up to " +iMax + " attempts\n";
103 for (var appletName in applet_initializeAction_list) {
104 safe_applet_initialize(appletName, iMax);
105 }
106
107 }
108
109 // applet can set isReady flag by calling applet_loaded(appletName, loaded);
110 function applet_loaded(appletName,loaded) {
111 applet_reportsLoaded_list[appletName] = loaded; // 0 means not loaded
112 debug_add("applet reporting that it has been loaded = " + loaded );
113 }
114
115 // insures that applet is loaded before initializing it
116 function safe_applet_initialize(appletName, i) {
117 debug_add("Iteration " + i + " of safe_applet_initialize with applet " + appletName );
118
119 i--;
120 var applet_loaded = applet_checkLoaded_list[appletName]();
121 debug_add("applet is ready = " + applet_loaded );
122
123 if ( 0 < i && !applet_loaded ) { // wait until applet is loaded
124 debug_add("applet " + appletName + "not ready try again");
125 window.setTimeout( "safe_applet_initialize(\"" + appletName + "\"," + i + ")",1);
126 } else if( 0 < i ){ // now that applet is loaded configure it and initialize it with saved data.
127 debug_add(" Ready to initialize applet " + appletName + " with " + i + " iterations left. ");
128
129 // in-line handler -- configure and initialize
130 try{
131 if (debug && typeof(getApplet(appletName).debug) == "function" ) {
132 getApplet(appletName).debug(1); // turn the applet's debug functions on.
133 }
134 } catch(e) {
135 alert("Unable to set debug mode for applet " + appletName);
136 }
137 try{
138 applet_config_list[appletName]();
139 } catch(e) {
140 alert("Unable to configure " + appletName + " \n " +e );
141 }
142 try{
143 applet_initializeAction_list[appletName]();
144 } catch(e) {
145 alert("unable to initialize " + appletName + " \n " +e );
146 }
147
148 } else {
149 if (debug) {alert("Error: timed out waiting for applet " +appletName + " to load");}
150 }
151 if (debug) {alert(debugText); debugText="";};
152 }
153
154///////////////////////////////////////////////////////
155// Utility functions
156///////////////////////////////////////////////////////
157
158
159 function getApplet(appletName) {
160 var isIE = navigator.appName.indexOf("Microsoft") != -1;
161 var obj = (isIE) ? window[appletName] : window.document[appletName];
162 //return window.document[appletName];
163 if (obj && (obj.name = appletName)) {
164 return( obj );
165 } else {
166 // alert ("can't find applet " + appletName);
167 }
168 }
169
170 function listQuestionElements() { // list all HTML input and textarea elements in main problem form
171 var isIE = navigator.appName.indexOf("Microsoft") != -1;
172 var elementList = (isIE) ? document.getElementsByTagName("input") : document.problemMainForm.getElementsByTagName("input");
173 var str=elementList.length +" Question Elements\n type | name = value < id > \n";
174 for( var i=0; i< elementList.length; i++) {
175 str = str + " "+i+" " + elementList[i].type
176 + " | " + elementList[i].name
177 + "= " + elementList[i].value +
178 " <" + elementList[i].id + ">\n";
179 }
180 elementList = (isIE) ? document.getElementsByTagName("textarea") : document.problemMainForm.getElementsByTagName("textarea");
181 for( var i=0; i< elementList.length; i++) {
182 str = str + " "+i+" " + elementList[i].type
183 + " | " + elementList[i].name
184 + "= " + elementList[i].value +
185 " <" + elementList[i].id + ">\n";
186 }
187 alert(str +"\n Place listQuestionElements() at end of document in order to get all form elements!");
188 }
189
190 function base64Q(str) {
191 return ( !str.match(/<XML/i) && !str.match(/<?xml/i));
192 }
193 function setEmptyState(appletName){
194 var newState = "<xml></xml>";
195 applet_setState_list[appletName](newState);
196 var applet = getApplet(appletName);
197 getQE(appletName+"_state").value = newState;
198 getQE("previous_" + appletName + "_state").value = newState
199 }
200
201 function getQE(name1) { // get Question Element in problemMainForm by name
202 var isIE = navigator.appName.indexOf("Microsoft") != -1;
203 var obj = (isIE) ? document.getElementById(name1)
204 :document.problemMainForm[name1];
205 // needed for IE -- searches id and name space so it can be unreliable if names are not unique
206 if (!obj || obj.name != name1) {
207 alert("Can't find element " + name1);
208 listQuestionElements();
209 } else {
210 return( obj );
211 }
212
213 }
214 function getQuestionElement(name1) {
215 return getQE(name1);
216 }
217
218 </script>
219 54
220END_HEADER_TEXT 55END_HEADER_TEXT
221 56
222}; 57};
223 58
283 my $setState = $self->setStateAlias; 118 my $setState = $self->setStateAlias;
284 my $base64_initialState = $self->base64_state; 119 my $base64_initialState = $self->base64_state;
285 main::RECORD_FORM_LABEL($appletStateName); #this insures that they'll be saved from one invocation to the next 120 main::RECORD_FORM_LABEL($appletStateName); #this insures that they'll be saved from one invocation to the next
286 #main::RECORD_FORM_LABEL("previous_$appletStateName"); 121 #main::RECORD_FORM_LABEL("previous_$appletStateName");
287 my $answer_value = ''; 122 my $answer_value = '';
288 $answer_value = ${$main::inputs_ref}{$appletStateName} if defined(${$main::inputs_ref}{$appletStateName}); 123
289 124 if ( defined( ${$main::inputs_ref}{$appletStateName} ) and ${$main::inputs_ref}{$appletStateName} =~ /\S/ ) {
125 $answer_value = ${$main::inputs_ref}{$appletStateName};
290 if ( defined( $main::rh_sticky_answers->{$appletStateName} ) ) { 126 } elsif ( defined( $main::rh_sticky_answers->{$appletStateName} ) ) {
127 warn "type of sticky answers is ", ref( $main::rh_sticky_answers->{$appletStateName} );
291 $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} }); 128 $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} });
292 $answer_value = '' unless defined($answer_value);
293 } 129 }
294 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3 130 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
295 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer 131 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
296
297 ####### 132 #######
298 # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons) 133 # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons)
299 ####### 134 #######
300 my $base_64_encoded_answer_value = ($answer_value =~/<XML|<?xml/i)? encode_base64($answer_value) : $answer_value; 135 my $base_64_encoded_answer_value;
136 my $decoded_answer_value;
137 if ( $answer_value =~/<XML|<?xml/i) {
138 $base_64_encoded_answer_value = encode_base64($answer_value);
139 $decoded_answer_value = $answer_value;
140 } else {
141 $decoded_answer_value = decode_base64($answer_value);
142 if ( $decoded_answer_value =~/<XML|<?xml/i) { # great, we've decoded the answer to obtain an xml string
143 $base_64_encoded_answer_value = $answer_value;
144 } else { #WTF?? apparently we don't have XML tags
145 $answer_value = "<xml>$answer_value</xml>";
146 $base_64_encoded_answer_value = encode_base64($answer_value);
147 $decoded_answer_value = $answer_value;
148 }
149 }
301 $base_64_encoded_answer_value =~ s/\r|\n//g; # get rid of line returns 150 $base_64_encoded_answer_value =~ s/\r|\n//g; # get rid of line returns
302 my $decoded_answer_value = ($answer_value =~/<XML|<?xml/i) ? $answer_value : decode_base64($answer_value); 151 # debug version of the applet state answerBox and controls
303 my $debug_input_element = qq!\n<textarea rows="4" cols="80" 152 my $debug_input_element = qq!\n<textarea rows="4" cols="80"
304 name = "$appletStateName">$decoded_answer_value</textarea><br/> 153 name = "$appletStateName">$decoded_answer_value</textarea><br/>
305 <input type="button" value="$getState" 154 <input type="button" value="$getState"
306 onClick="applet_getState_list['$appletName']()" 155 onClick="debugText='';
156 ww_applet_list['$appletName'].getState();
157 alert(debugText);"
307 > 158 >
308 <input type="button" value="$setState" 159 <input type="button" value="$setState"
309 onClick="var tmp = getQE('$appletStateName').value; 160 onClick="debugText='';
310 applet_setState_list['$appletName'](tmp);" 161 ww_applet_list['$appletName'].setState();
162 alert(debugText);"
311 > 163 >
312 !; 164 !;
313 my $state_input_element = ($self->debug == 1) ? $debug_input_element : 165 my $state_input_element = ($self->debug == 1) ? $debug_input_element :
314 qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!; 166 qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!;
315 my $reset_button_str = ($reset_button) ? 167 my $reset_button_str = ($reset_button) ?
321 # 173 #
322 $state_storage_html_code = 174 $state_storage_html_code =
323 $reset_button_str. 175 $reset_button_str.
324 $state_input_element. 176 $state_input_element.
325 qq!<input type="hidden" name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!; 177 qq!<input type="hidden" name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!;
178 $state_storage_html_code = qq!<input type="hidden" name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!
179 . $reset_button_str
180 . $state_input_element
181 ;
326 ####### 182 #######
327 # insert header material 183 # insert header material
328 ####### 184 #######
329 main::HEADER_TEXT($self->insertHeader()); 185 main::HEADER_TEXT($self->insertHeader());
330 return main::MODES(TeX=>' {\bf applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code); 186 return main::MODES(TeX=>' {\bf applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code);

Legend:
Removed from v.5971  
changed lines
  Added in v.5993

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9