| 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.6 2008/03/26 02:43:07 gage Exp $ |
4 | # $CVSHeader: pg/macros/AppletObjects.pl,v 1.7 2008/04/26 21:19:14 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. |
| … | |
… | |
| 42 | </script> |
42 | </script> |
| 43 | <script src="/webwork2_files/js/Base64.js" language="javascript"> |
43 | <script src="/webwork2_files/js/Base64.js" language="javascript"> |
| 44 | </script> |
44 | </script> |
| 45 | |
45 | |
| 46 | <script language="JavaScript"> |
46 | <script language="JavaScript"> |
| 47 | var applet_initializeAction_list = new Object; |
|
|
| 48 | var applet_submitAction_list = new Object; |
|
|
| 49 | var applet_setState_list = new Object; |
|
|
| 50 | var applet_getState_list = new Object; |
|
|
| 51 | var applet_config_list = new Object; |
|
|
| 52 | |
47 | |
|
|
48 | ////////////////////////////////////////////////////////// |
|
|
49 | // applet lists |
|
|
50 | ////////////////////////////////////////////////////////// |
|
|
51 | |
|
|
52 | var applet_initializeAction_list = new Object; // functions for initializing question with an applet |
|
|
53 | var applet_submitAction_list = new Object; // functions for submitting question with applet |
|
|
54 | var applet_setState_list = new Object; // functions for setting state (XML) from applets |
|
|
55 | var applet_getState_list = new Object; // functions for getting state (XML) from applets |
|
|
56 | var applet_config_list = new Object; // functions for configuring on applets |
|
|
57 | var applet_checkLoaded_list = new Object; // functions for probing the applet to see if it is loaded |
|
|
58 | var applet_reportsLoaded_list = new Object; // flag set by applet |
|
|
59 | var applet_isReady_list = new Object; // flag set by javaScript in checkLoaded |
| 53 | |
60 | |
| 54 | function base64Q(str) { |
61 | ////////////////////////////////////////////////////////// |
| 55 | return ( !str.match(/<XML/i) && !str.match(/<?xml/i)); |
62 | // DEBUGGING tools |
| 56 | } |
63 | ////////////////////////////////////////////////////////// |
| 57 | |
64 | var debug = $debugMode; |
|
|
65 | var debugText = ""; |
|
|
66 | function debug_add(str) { |
|
|
67 | if (debug) { |
|
|
68 | debugText = debugText + "\n\n" +str; |
|
|
69 | } |
|
|
70 | } |
|
|
71 | |
|
|
72 | ////////////////////////////////////////////////////////// |
|
|
73 | // INITIALIZE and SUBMIT actions |
|
|
74 | ////////////////////////////////////////////////////////// |
|
|
75 | |
| 58 | function submitAction() { |
76 | function submitAction() { |
| 59 | //alert("submit Action" ); |
77 | //alert("submit Action" ); |
| 60 | for (var applet in applet_submitAction_list) { |
78 | for (var applet in applet_submitAction_list) { |
| 61 | //alert(applet); |
79 | //alert(applet); |
| 62 | applet_submitAction_list[applet](); |
80 | applet_submitAction_list[applet](); |
| 63 | } |
81 | } |
| 64 | |
82 | |
| 65 | } |
83 | } |
| 66 | // Give some time delay before initializing |
|
|
| 67 | function initializeAction() { |
84 | function initializeAction() { |
| 68 | //alert("ready to initialize"); |
85 | var iMax = 10; |
| 69 | // give some delay to allow flash applet to load. FIXME |
86 | debugText="start intializeAction() with up to " +iMax + " attempts\n"; |
| 70 | window.setTimeout("initializeAction1()",200); |
87 | for (var appletName in applet_initializeAction_list) { |
| 71 | //initializeAction1(); |
88 | safe_applet_initialize(appletName, iMax); |
|
|
89 | } |
|
|
90 | |
| 72 | } |
91 | } |
| 73 | function initializeAction1() { |
92 | |
| 74 | |
93 | // applet can set isReady flag by calling applet_loaded(appletName, loaded); |
| 75 | for (var appletName in applet_initializeAction_list) { |
94 | function applet_loaded(appletName,loaded) { |
| 76 | //alert("initialize: " + appletName); |
95 | applet_reportsLoaded_list[appletName] = loaded; // 0 means not loaded |
| 77 | try{ |
96 | debug_add("applet reporting that it has been loaded = " + loaded ); |
| 78 | applet_config_list[appletName](); |
|
|
| 79 | } catch(e) { |
|
|
| 80 | alert("unable to configure " + appletName + " It may have been slow to load. " +e ); |
|
|
| 81 | } |
|
|
| 82 | try{ |
|
|
| 83 | applet_initializeAction_list[appletName](); |
|
|
| 84 | } catch(e) { |
|
|
| 85 | alert("unable to initialize " + appletName + " It may have been slow to load. " +e ); |
|
|
| 86 | } |
|
|
| 87 | } |
|
|
| 88 | } |
97 | } |
| 89 | |
98 | |
|
|
99 | // insures that applet is loaded before initializing it |
|
|
100 | function safe_applet_initialize(appletName, i) { |
|
|
101 | debug_add("Iteration " + i + " of safe_applet_initialize with applet " + appletName ); |
|
|
102 | |
|
|
103 | i--; |
|
|
104 | var applet_loaded = applet_checkLoaded_list[appletName](); |
|
|
105 | debug_add("applet is ready = " + applet_loaded ); |
|
|
106 | |
|
|
107 | if ( 0 < i && !applet_loaded ) { // wait until applet is loaded |
|
|
108 | debug_add("applet " + appletName + "not ready try again"); |
|
|
109 | window.setTimeout( "safe_applet_initialize(\"" + appletName + "\"," + i + ")",1); |
|
|
110 | } else if( 0 < i ){ // now that applet is loaded configure it and initialize it with saved data. |
|
|
111 | debug_add(" Ready to initialize applet " + appletName + " with " + i + " iterations left. "); |
|
|
112 | |
|
|
113 | // in-line handler -- configure and initialize |
|
|
114 | try{ |
|
|
115 | if (debug && typeof(getApplet(appletName).debug) == "function" ) { |
|
|
116 | getApplet(appletName).debug(1); |
|
|
117 | } |
|
|
118 | } catch(e) { |
|
|
119 | alert("Unable to set debug mode for applet " + appletName); |
|
|
120 | } |
|
|
121 | try{ |
|
|
122 | applet_config_list[appletName](); |
|
|
123 | } catch(e) { |
|
|
124 | alert("Unable to configure " + appletName + " \n " +e ); |
|
|
125 | } |
|
|
126 | try{ |
|
|
127 | applet_initializeAction_list[appletName](); |
|
|
128 | } catch(e) { |
|
|
129 | alert("unable to initialize " + appletName + " \n " +e ); |
|
|
130 | } |
|
|
131 | |
|
|
132 | } else { |
|
|
133 | if (debug) {alert("Error: timed out waiting for applet " +appletName + " to load");} |
|
|
134 | } |
|
|
135 | if (debug) {alert(debugText); debugText="";}; |
|
|
136 | } |
|
|
137 | |
|
|
138 | /////////////////////////////////////////////////////// |
|
|
139 | // Utility functions |
|
|
140 | /////////////////////////////////////////////////////// |
| 90 | |
141 | |
| 91 | var flash; |
142 | |
| 92 | function getApplet(appletName) { |
143 | function getApplet(appletName) { |
| 93 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
144 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
| 94 | var obj = (isIE) ? window[appletName] : window.document[appletName]; |
145 | var obj = (isIE) ? window[appletName] : window.document[appletName]; |
| 95 | //return window.document[appletName]; |
146 | //return window.document[appletName]; |
| 96 | if (obj && (obj.name = appletName)) { |
147 | if (obj && (obj.name = appletName)) { |
| 97 | return( obj ); |
148 | return( obj ); |
| 98 | } else { |
149 | } else { |
| 99 | alert ("can't find applet " + appletName); |
150 | // alert ("can't find applet " + appletName); |
| 100 | } |
151 | } |
| 101 | } |
152 | } |
| 102 | |
153 | |
| 103 | function listQuestionElements() { // list all HTML input and textarea elements in main problem form |
154 | function listQuestionElements() { // list all HTML input and textarea elements in main problem form |
| 104 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
155 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
| 105 | var elementList = (isIE) ? document.getElementsByTagName("input") : document.problemMainForm.getElementsByTagName("input"); |
156 | var elementList = (isIE) ? document.getElementsByTagName("input") : document.problemMainForm.getElementsByTagName("input"); |
| 106 | var str=elementList.length +" Question Elements\n type | name = value < id > \n"; |
157 | var str=elementList.length +" Question Elements\n type | name = value < id > \n"; |
| 107 | for( var i=0; i< elementList.length; i++) { |
158 | for( var i=0; i< elementList.length; i++) { |
| 108 | str = str + " "+i+" " + elementList[i].type |
159 | str = str + " "+i+" " + elementList[i].type |
| 109 | + " | " + elementList[i].name |
160 | + " | " + elementList[i].name |
| 110 | + "= " + elementList[i].value + |
161 | + "= " + elementList[i].value + |
| 111 | " <" + elementList[i].id + ">\n"; |
162 | " <" + elementList[i].id + ">\n"; |
| 112 | } |
163 | } |
| 113 | elementList = (isIE) ? document.getElementsByTagName("textarea") : document.problemMainForm.getElementsByTagName("textarea"); |
164 | elementList = (isIE) ? document.getElementsByTagName("textarea") : document.problemMainForm.getElementsByTagName("textarea"); |
| 114 | for( var i=0; i< elementList.length; i++) { |
165 | for( var i=0; i< elementList.length; i++) { |
| 115 | str = str + " "+i+" " + elementList[i].type |
166 | str = str + " "+i+" " + elementList[i].type |
| 116 | + " | " + elementList[i].name |
167 | + " | " + elementList[i].name |
| 117 | + "= " + elementList[i].value + |
168 | + "= " + elementList[i].value + |
| 118 | " <" + elementList[i].id + ">\n"; |
169 | " <" + elementList[i].id + ">\n"; |
| 119 | } |
170 | } |
| 120 | alert(str +"\n Place listQuestionElements() at end of document in order to get all form elements!"); |
171 | alert(str +"\n Place listQuestionElements() at end of document in order to get all form elements!"); |
| 121 | } |
172 | } |
| 122 | |
173 | |
|
|
174 | function base64Q(str) { |
|
|
175 | return ( !str.match(/<XML/i) && !str.match(/<?xml/i)); |
|
|
176 | } |
|
|
177 | function setEmptyState(appletName){ |
|
|
178 | var newState = "<xml></xml>"; |
|
|
179 | applet_setState_list[appletName](newState); |
|
|
180 | var applet = getApplet(appletName); |
|
|
181 | getQE(appletName+"_state").value = newState; |
|
|
182 | getQE("previous_" + appletName + "_state").value = newState |
|
|
183 | } |
|
|
184 | |
| 123 | function getQE(name1) { // get Question Element in problemMainForm by name |
185 | function getQE(name1) { // get Question Element in problemMainForm by name |
| 124 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
186 | var isIE = navigator.appName.indexOf("Microsoft") != -1; |
| 125 | var obj = (isIE) ? document.getElementById(name1) |
187 | var obj = (isIE) ? document.getElementById(name1) |
| 126 | :document.problemMainForm[name1]; |
188 | :document.problemMainForm[name1]; |
| 127 | // needed for IE -- searches id and name space so it can be unreliable if names are not unique |
189 | // needed for IE -- searches id and name space so it can be unreliable if names are not unique |
| 128 | if (!obj || obj.name != name1) { |
190 | if (!obj || obj.name != name1) { |
| 129 | alert("Can't find element " + name1); |
191 | alert("Can't find element " + name1); |
| 130 | listQuestionElements(); |
192 | listQuestionElements(); |
| 131 | } else { |
193 | } else { |
| 132 | return( obj ); |
194 | return( obj ); |
| 133 | } |
195 | } |
| 134 | |
196 | |
| 135 | } |
197 | } |
| 136 | function getQuestionElement(name1) { |
198 | function getQuestionElement(name1) { |
| 137 | return getQE(name1); |
199 | return getQE(name1); |
| 138 | } |
200 | } |
| 139 | |
201 | |
| 140 | </script> |
202 | </script> |
| 141 | |
203 | |
| 142 | END_HEADER_TEXT |
204 | END_HEADER_TEXT |
| 143 | |
205 | |
| … | |
… | |
| 232 | > |
294 | > |
| 233 | !; |
295 | !; |
| 234 | my $state_input_element = ($self->debug == 1) ? $debug_input_element : |
296 | my $state_input_element = ($self->debug == 1) ? $debug_input_element : |
| 235 | qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!; |
297 | qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!; |
| 236 | my $reset_button_str = ($reset_button) ? |
298 | my $reset_button_str = ($reset_button) ? |
| 237 | qq!<br/><input type='button' value='set applet state empty' onClick="applet_setState_list['$appletName']('<xml></xml>')"> |
299 | qq!<br/><input type='button' value='set applet state empty' onClick="setEmptyState('$appletName')"> |
| 238 | <input type="button" value="reinitialize applet" onClick="initializeAction()"/>! |
300 | <input type="button" value="reinitialize applet" onClick="getQE('$appletStateName').value='$base64_initialState'"/>! |
| 239 | : '' |
301 | : '' |
| 240 | ; |
302 | ; |
| 241 | # always base64 encode the hidden answer value to prevent problems with quotes. |
303 | # always base64 encode the hidden answer value to prevent problems with quotes. |
| 242 | # |
304 | # |
| 243 | $state_storage_html_code = |
305 | $state_storage_html_code = |