[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 5597 Revision 5618
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.2 2007/12/03 22:32:01 gage Exp $ 4# $CVSHeader: pg/macros/AppletObjects.pl,v 1.3 2008/01/03 15:17:40 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.
14# Artistic License for more details. 14# Artistic License for more details.
15################################################################################ 15################################################################################
16 16
17=head1 NAME 17=head1 NAME
18 18
19AppletOjects.pl - Macro-based front end for the Applet.pm module. 19AppletObjects.pl - Macro-based front end for the Applet.pm module.
20 20
21 21
22=head1 DESCRIPTION 22=head1 DESCRIPTION
23 23
24This subroutines in this 24This subroutines in this
30 30
31L<Applets.pm>. 31L<Applets.pm>.
32 32
33=cut 33=cut
34 34
35
35sub _PGapplets_init {}; # don't reload this file 36sub _AppletObjects_init{}; # don't reload this file
37
38
39main::HEADER_TEXT(<<'END_HEADER_TEXT');
40 <script language="javascript">AC_FL_RunContent = 0;</script>
41 <script src="/webwork2_files/applets/AC_RunActiveContent.js" language="javascript">
42 </script>
43 <script src="/webwork2_files/js/Base64.js" language="javascript">
44 </script>
45
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
53
54 function base64Q(str) {
55 return !str.match(/<XML>*/i);
56 }
57
58 function submitAction() {
59 //alert("submit Action" );
60 for (var applet in applet_submitAction_list) {
61 //alert(applet);
62 applet_submitAction_list[applet]();
63 }
64
65 }
66 // Give some time delay before initializing
67 function initializeAction() {
68 //alert("ready to initialize");
69 // give some delay to allow flash applet to load. FIXME
70 window.setTimeout("initializeAction1()",200);
71 //initializeAction1();
72 }
73 function initializeAction1() {
74
75 for (var appletName in applet_initializeAction_list) {
76 //alert("initialize: " + appletName);
77 try{
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 }
89
90
91 var flash;
92 function getApplet(appletName) {
93 var isIE = navigator.appName.indexOf("Microsoft") != -1;
94 var obj = (isIE) ? window[appletName] : window.document[appletName];
95 //return window.document[appletName];
96 if (obj && (obj.name = appletName)) {
97 return( obj );
98 } else {
99 alert ("can't find applet " + appletName);
100 }
101 }
102
103 function listQuestionElements() { // list all HTML input and textarea elements in main problem form
104 var isIE = navigator.appName.indexOf("Microsoft") != -1;
105 var elementList = (isIE) ? document.getElementsByTagName("input") : document.problemMainForm.getElementsByTagName("input");
106 var str=elementList.length +" Question Elements\n type | name = value < id > \n";
107 for( var i=0; i< elementList.length; i++) {
108 str = str + " "+i+" " + elementList[i].type
109 + " | " + elementList[i].name
110 + "= " + elementList[i].value +
111 " <" + elementList[i].id + ">\n";
112 }
113 elementList = (isIE) ? document.getElementsByTagName("textarea") : document.problemMainForm.getElementsByTagName("textarea");
114 for( var i=0; i< elementList.length; i++) {
115 str = str + " "+i+" " + elementList[i].type
116 + " | " + elementList[i].name
117 + "= " + elementList[i].value +
118 " <" + elementList[i].id + ">\n";
119 }
120 alert(str +"\n Place listQuestionElements() at end of document in order to get all form elements!");
121 }
122
123 function getQE(name1) { // get Question Element in problemMainForm by name
124 var isIE = navigator.appName.indexOf("Microsoft") != -1;
125 var obj = (isIE) ? document.getElementById(name1)
126 :document.problemMainForm[name1];
127 // needed for IE -- searches id and name space so it can be unreliable if names are not unique
128 if (!obj || obj.name != name1) {
129 alert("Can't find element " + name1);
130 listQuestionElements();
131 } else {
132 return( obj );
133 }
134
135 }
136 function getQuestionElement(name1) {
137 return getQE(name1);
138 }
139
140 </script>
141
142END_HEADER_TEXT
143
144
36 145
37=head3 146=head3
38 FlashApplet 147 FlashApplet
39 148
40 Useage: $applet = FlashApplet(); 149 Useage: $applet = FlashApplet();
44sub FlashApplet { 153sub FlashApplet {
45 return new FlashApplet(@_); 154 return new FlashApplet(@_);
46 155
47} 156}
48 157
158sub JavaApplet {
159 return new JavaApplet(@_);
160
161}
162
49package FlashApplet; 163package Applet;
50 164
165
166
51=head2 Methods 167=head2 Methods
52 168
53=cut 169=cut
54 170
55## this method is defined in this file 171## this method is defined in this file
56## because the main subroutines HEADER_TEXT and MODES are 172## because the main subroutines HEADER_TEXT and MODES are
57## not available to the module FlashApplet when that file 173## not available to the module FlashApplet when that file
58## is compiled (at the time the apache child process is first initialized) 174## is compiled (at the time the apache child process is first initialized)
59 175
60=head3 insert 176=head3 insertAll
61 177
62 Useage: TEXT( $applet->insert() ); 178 Useage: TEXT( $applet->insertAll() );
63 \{ $applet->insert \} (used within BEGIN_TEXT/END_TEXT blocks) 179 \{ $applet->insertAll() \} (used within BEGIN_TEXT/END_TEXT blocks)
64 180
65=cut 181=cut
66 182
67=pod 183=pod
68 184
69Inserts applet at this point in the HTML code. (In TeX mode a message "Applet" is written.) This method 185Inserts applet at this point in the HTML code. (In TeX mode a message "Applet" is written.) This method
70also adds the applets header material into the header portion of the HTML page. It effectively inserts 186also adds the applets header material into the header portion of the HTML page. It effectively inserts
71the outputs of both C<$applet-E<gt>insertHeader> and C<$applet-E<gt>insertObject> (defined in L<Applet.pm> ) in the appropriate places. 187the outputs of both C<$applet-E<gt>insertHeader> and C<$applet-E<gt>insertObject> (defined in L<Applet.pm> )
188in the appropriate places.
72 189
73=cut 190Note: This method is defined here rather than in Applet.pl because it
191 requires access to the RECORD_FORM_LABEL subroutine
192 and to the routine accessing the stored values of the answers. These are defined in main::.
74 193
194=cut
195
75sub insert { ## inserts both header text and object text 196sub insertAll { ## inserts both header text and object text
76 my $self = shift; 197 my $self = shift;
198 my %options = @_;
199 $self->debug( (defined($options{debug}) and $options{debug}==1) ? 1 : 0 );
200 my $reset_button = $options{reset_button} || 0;
201 # prepare html code for storing state
202 my $appletName = $self->appletName;
203 my $appletStateName = "${appletName}_state";
204 my $getState = $self->getStateAlias;
205 my $setState = $self->setStateAlias;
206 my $base64_initialState = $self->base64_state;
207 main::RECORD_FORM_LABEL($appletStateName); #this insures that they'll be saved from one invocation to the next
208 #main::RECORD_FORM_LABEL("previous_$appletStateName");
209 my $answer_value = '';
210 $answer_value = ${$main::inputs_ref}{$appletStateName} if defined(${$main::inputs_ref}{$appletStateName});
211
212 if ( defined( $main::rh_sticky_answers->{$appletStateName} ) ) {
213 $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} });
214 $answer_value = '' unless defined($answer_value);
215 }
216 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
217 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
218
219 #######
220 # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons)
221 #######
222 my $base_64_encoded_answer_value = ($answer_value =~/<XML>/i)? encode_base64($answer_value) : $answer_value;
223 my $decoded_answer_value = ($answer_value =~/<XML>/i) ? $answer_value : decode_base64($answer_value);
224 my $debug_input_element = qq!\n<textarea rows="4" cols="80"
225 name = "$appletStateName">$decoded_answer_value</textarea><br/>
226 <input type="button" value="getState"
227 onClick="applet_getState_list['$appletName']()">
228 <input type="button" value="setState"
229 onClick="applet_setState_list['$appletName']();
230 var tmp = getQE('$appletStateName').value;">
231 !;
232 my $state_input_element = ($self->debug == 1) ? $debug_input_element :
233 qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!;
234 my $reset_button_str = ($reset_button) ?
235 qq!<br/><input type='button' value='reset applet' onClick="applet_setState_list['$appletName']('<xml></xml>')">!
236 : ''
237 ;
238 # always base64 encode the hidden answer value to prevent problems with quotes.
239 #
240 $state_storage_html_code =
241 $reset_button_str.
242 $state_input_element.
243 qq!<input type="hidden" name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!;
244 #######
245 # insert header material
246 #######
77 main::HEADER_TEXT($self->insertHeader()); 247 main::HEADER_TEXT($self->insertHeader());
78 return main::MODES(TeX=>' {\bf flash applet } ', HTML=>$self->insertObject); 248 return main::MODES(TeX=>' {\bf applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code);
79} 249}
80
81 250
82=head3 Example problem 251=head3 Example problem
83 252
84 253
85=cut 254=cut
118 287
119 # findAppletCodebase looks for the applet in a list 288 # findAppletCodebase looks for the applet in a list
120 # of locations specified in global.conf 289 # of locations specified in global.conf
121 290
122 ################################### 291 ###################################
123 # Add java script functions to header section of HTML to 292 # Add additional javaScript functions to header section of HTML to
124 # communicate with the "ExternalInterface" applet. 293 # communicate with the "ExternalInterface" applet.
125 ################################### 294 ###################################
126 295
127 $applet->header(<<'END_HEADER'); 296 $applet->header(<<'END_HEADER');
128 <script type="text/javascript" src="https://devel.webwork.rochester.edu:8002/webwork2_files/js/BrowserSniffer.js"> 297 <script type="text/javascript" src="https://devel.webwork.rochester.edu:8002/webwork2_files/js/BrowserSniffer.js">
136 //alert("found sniffer" +sniffer); 305 //alert("found sniffer" +sniffer);
137 return sniffer; 306 return sniffer;
138 } 307 }
139 308
140 function updateStatus(sMessage) { 309 function updateStatus(sMessage) {
141 //alert("update form with " + sMessage);
142 //window.document.problemMainForm.playbackStatus.value = sMessage;
143 //document.problemMainForm.playbackStatus.value = sMessage;
144 document.getElementById("playbackStatus").value = sMessage; 310 getQE("playbackStatus").value = sMessage;
145 } 311 }
146 312
147 function newColor() { 313 function newColor() {
148 //var app = getFlashMovie("ExternalInterface").getElementById("movie1"); 314
149 // alert(app);
150 // The difficult issue in adapting Barbara's original file was locating
151 // the object. It took several tries to find a method that worked
152 // and it is quite likely that it will not work in all browsers.
153
154 //alert("update color");
155 //alert(getFlashMovie("ExternalInterface"));
156 getFlashMovie("ExternalInterface").updateColor(Math.round(Math.random() * 0xFFFFFF)); 315 getApplet("ExternalInterface").updateColor(Math.round(Math.random() * 0xFFFFFF));
157 } 316 }
158 317
159 function submitAction() { newColor()
160 }
161
162 </script> 318 </script>
163 END_HEADER 319 END_HEADER
164 320
165 ################################### 321 ###################################
166 # Configure applet 322 # Configure applet
176 ################################### 332 ###################################
177 333
178 TEXT(beginproblem()); 334 TEXT(beginproblem());
179 335
180 336
181 TEXT( MODES(TeX=>'object code', HTML=><<END_SCRIPT ) );
182 <script>
183 //alert("foobar");
184 initialize();
185 // this should really be done in the <body> tag -- can we make that happen?
186 </script>
187 END_SCRIPT
188
189 337
190 BEGIN_TEXT 338 BEGIN_TEXT
191 \{ $applet->insert \} 339 \{ $applet->insertAll() \}
192 $PAR 340 $PAR
193 341
194 The Flash object operates above this line. The box and button below this line are part of 342 The Flash object operates above this line. The box and button below this line are part of
195 the WeBWorK problem. They communicate with the Flash object. 343 the WeBWorK problem. They communicate with the Flash object.
196 $HR 344 $HR

Legend:
Removed from v.5597  
changed lines
  Added in v.5618

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9