[system] / trunk / pg / lib / Applet.pm Repository:
ViewVC logotype

Diff of /trunk/pg/lib/Applet.pm

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

Revision 5667 Revision 5984
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/lib/Applet.pm,v 1.10 2008/05/05 17:24:31 gage Exp $ 4# $CVSHeader: pg/lib/Applet.pm,v 1.13 2008/11/19 04:39:43 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.
99use MIME::Base64 qw( encode_base64 decode_base64); 99use MIME::Base64 qw( encode_base64 decode_base64);
100 100
101 101
102=head2 Default javaScript functions placed in header 102=head2 Default javaScript functions placed in header
103 103
104=pod
105
104These functions are automatically defined for use for 106These functions are automatically defined for use for
105any javaScript placed in the text of a PG question. 107any javaScript placed in the text of a PG question.
106 108
107 getApplet(appletName) -- finds the applet path in the DOM 109 getApplet(appletName) -- finds the applet path in the DOM
108 110
109 submitAction() -- calls the submit action of the applets 111 submitAction() -- calls the submit action of the applets
110
111 112
112 initializeAction() -- calls the initialize action of the applets 113 initializeAction() -- calls the initialize action of the applets
113 114
114 getQE(name) -- gets an HTML element of the question by name 115 getQE(name) -- gets an HTML element of the question by name
115 or by id. Be sure to keep all names and ids 116 or by id. Be sure to keep all names and ids
116 unique within a given PG question. 117 unique within a given PG question.
117 118
118 getQuestionElement(name) -- long form of getQE(name) 119 getQuestionElement(name) -- long form of getQE(name)
119 120
120 listQuestionElements() -- for discovering the names of inputs in the 121 listQuestionElements() -- for discovering the names of inputs in the
121 PG question. An alert dialog will list all 122 PG question. An alert dialog will list all
122 of the elements. 123 of the elements.
123 Usage: Place this at the END of the question, 124 Usage: Place this at the END of the question, just before END_DOCUMENT():
124 just before END_DOCUMENT():
125 125
126 TEXT(qq!<script> listQuestionElements() </script>!); 126 TEXT(qq!<script> listQuestionElements() </script>!);
127 ENDDOCUMENT(); 127 ENDDOCUMENT();
128 to obtain a list of all of the HTML elements in the question
129
130 List of accessor methods made available by the FlashApplet class:
131 Usage: $current_value = $applet->method(new_value or empty)
132 These can also be set when creating the class -- for exampe:
133 $applet = new FlashApplet(
134 # can be replaced by $applet =FlashApplet() when using AppletObjects.pl
135 codebase => findAppletCodebase("$appletName.swf"),
136 appletName => $appletName,
137 appletId => $appletName,
138 submitActionAlias => 'checkAnswer',
139 );
128 140
129 list of accessor methods format: current_value = $self->method(new_value or empty)
130 141
131 appletId for simplicity and reliability appletId and appletName are always the same 142 appletId for simplicity and reliability appletId and appletName are always the same
132 appletName 143 appletName
133
134 archive the name of the .jar file containing the applet code 144 archive the name of the .jar file containing the applet code
135 code the name of the applet code in the .jar archive 145 code the name of the applet code in the .jar archive
136 codebase a prefix url used to find the archive and the applet itself 146 codebase a prefix url used to find the archive and the applet itself
137 147
138 height rectangle alloted in the html page for displaying the applet 148 height rectangle alloted in the html page for displaying the applet
139 width 149
140
141 params an anonymous array containing name/value pairs 150 params an anonymous array containing name/value pairs
142 to configure the applet [name =>'value, ...] 151 to configure the applet [name =>'value, ...]
143 152
144 header stores the text to be added to the header section of the html page 153 header stores the text to be added to the header section of the html page
145 object stores the text which places the applet on the html page 154 object stores the text which places the applet on the html page
146 155
147 debug in debug mode several alerts mark progress through the procedure of calling the applet 156 debug in debug mode several alerts mark progress through the procedure of calling the applet
148 157
149 config configuration are those customizable attributes of the applet which don't 158 config configuration are those customizable attributes of the applet which don't
150 change as it is used. When stored in hidden answer fields 159 change as it is used. When stored in hidden answer fields
151 it is usually stored in base64 encoded format. 160 it is usually stored in base64 encoded format.
152 base64_config base64 encode version of the contents of config 161 base64_config base64 encode version of the contents of config
153 162
154 configAlias (default: config ) names the applet command called with the contents of $self->config 163 configAlias (default: config ) names the applet command called with the contents of $self->config
155 to configure the applet. The parameters are passed to the applet in plain text using <xml> 164 to configure the applet. The parameters are passed to the applet in plain text using <xml>
156 The outer tags must be <xml> ..... </xml> 165 The outer tags must be <xml> ..... </xml>
157 state state consists of those customizable attributes of the applet which change 166 state state consists of those customizable attributes of the applet which change
158 as the applet is used. It is stored by the calling .pg question so that 167 as the applet is used. It is stored by the calling .pg question so that
159 when revisiting the question the applet 168 when revisiting the question the applet
160 will be restored to the same state it was left in when the question was last 169 will be restored to the same state it was left in when the question was last
161 viewed. 170 viewed.
162 171
163 getStateAlias (default: getState) alias for command called to read the current state of the applet. 172 getStateAlias (default: getState) alias for command called to read the current state of the applet.
164 The state is passed in plain text xml format with outer tags: <xml>....</xml> 173 The state is passed in plain text xml format with outer tags: <xml>....</xml>
165 setStateAlias (default: setState) alias for the command called to reset the state of the applet. 174 setStateAlias (default: setState) alias for the command called to reset the state of the applet.
166 The state is passed in plain text in xml format with outer tags: <xml>....</xml> 175 The state is passed in plain text in xml format with outer tags: <xml>....</xml>
167 176
168 base64_state returns the base64 encoded version of the state stored in the applet object. 177 base64_state returns the base64 encoded version of the state stored in the applet object.
169 178
170 initializeActionAlias -- (default: initializeAction) the name of the javaScript subroutine called to initialize the applet (some overlap with config/ and setState 179 initializeActionAlias -- (default: initializeAction) the name of the javaScript subroutine called to initialize the applet (some overlap with config/ and setState
171 submitActionAlias -- (default: submitAction)the name of the javaScript subroutine called when the submit button of the 180 submitActionAlias -- (default: submitAction)the name of the javaScript subroutine called when the submit button of the
172 .pg question is pressed. 181 .pg question is pressed.
173 182
174 returnFieldName 183 returnFieldName
175
176
177 184
178
179 185
180=cut 186=cut
181 187
182 188
183 189
197 getStateAlias => 'getXML', 203 getStateAlias => 'getXML',
198 setStateAlias => 'setXML', 204 setStateAlias => 'setXML',
199 configAlias => 'config', 205 configAlias => 'config',
200 initializeActionAlias => 'setXML', 206 initializeActionAlias => 'setXML',
201 submitActionAlias => 'getXML', 207 submitActionAlias => 'getXML',
208 submitActionScript => '', # script executed on submitting the WW question
202 returnFieldName => 'receivedField', 209 returnFieldName => 'receivedField',
203 headerText => DEFAULT_HEADER_TEXT(), 210 headerText => DEFAULT_HEADER_TEXT(),
204 objectText => '', 211 objectText => '',
205 debug => 0, 212 debug => 0,
206 @_, 213 @_,
250sub submitActionAlias { 257sub submitActionAlias {
251 my $self = shift; 258 my $self = shift;
252 $self->{submitActionAlias} = shift ||$self->{submitActionAlias}; # replace the current contents if non-empty 259 $self->{submitActionAlias} = shift ||$self->{submitActionAlias}; # replace the current contents if non-empty
253 $self->{submitActionAlias}; 260 $self->{submitActionAlias};
254} 261}
262sub submitActionScript {
263 my $self = shift;
264 $self->{submitActionScript} = shift ||$self->{submitActionScript}; # replace the current contents if non-empty
265 $self->{submitActionScript};
266}
255sub getStateAlias { 267sub getStateAlias {
256 my $self = shift; 268 my $self = shift;
257 $self->{getStateAlias} = shift ||$self->{getStateAlias}; # replace the current contents if non-empty 269 $self->{getStateAlias} = shift ||$self->{getStateAlias}; # replace the current contents if non-empty
258 $self->{getStateAlias}; 270 $self->{getStateAlias};
259} 271}
347 my $appletId = $self->appletId; 359 my $appletId = $self->appletId;
348 my $appletName = $self->appletName; 360 my $appletName = $self->appletName;
349 my $base64_initialState = $self->base64_state; 361 my $base64_initialState = $self->base64_state;
350 my $initializeAction = $self->initializeActionAlias; 362 my $initializeAction = $self->initializeActionAlias;
351 my $submitAction = $self->submitActionAlias; 363 my $submitAction = $self->submitActionAlias;
364 my $submitActionScript = $self->submitActionScript;
352 my $setState = $self->setStateAlias; 365 my $setState = $self->setStateAlias;
353 my $getState = $self->getStateAlias; 366 my $getState = $self->getStateAlias;
354 my $config = $self->configAlias; 367 my $config = $self->configAlias;
355 my $base64_config = $self->base64_config; 368 my $base64_config = $self->base64_config;
356 my $debugMode = ($self->debug) ? "1": "0"; 369 my $debugMode = ($self->debug) ? "1": "0";
463 debug_add("get current state for applet $appletName and store it in $appletName"+"_state"); 476 debug_add("get current state for applet $appletName and store it in $appletName"+"_state");
464 var applet = getApplet("$appletName"); 477 var applet = getApplet("$appletName");
465 try { 478 try {
466 if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function 479 if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function
467 state = applet.$getState(); // get state in xml format 480 state = applet.$getState(); // get state in xml format
481 debug_add("state has type " + typeof(state));
482 state = String(state); // geogebra returned an object type instead of a string type
483 debug_add("state converted to type " + typeof(state));
468 } 484 }
469 485
486 if (!debug) {
487 state = Base64.encode(state);
470 if (!debug) {state = Base64.encode(state) }; // replace state by encoded version unless in debug mode 488 }; // replace state by encoded version unless in debug mode
489
471 debug_add("state is "+state); // this should still be in plain text 490 debug_add("state is "+state); // this should still be in plain text
472 getQE("$appletName"+"_state").value = state; //place state in input item (debug: textarea, otherwise: hidden) 491 getQE("$appletName"+"_state").value = state; //place state in input item (debug: textarea, otherwise: hidden)
473 } catch (e) { 492 } catch (e) {
474 alert("Error in getting state for $appletName " + e ); 493 alert("Error in getting state for $appletName " + e );
475 } 494 }
518 applet_submitAction_list["$appletName"] = function () { 537 applet_submitAction_list["$appletName"] = function () {
519 if (! applet_isReady_list["$appletName"] ) { 538 if (! applet_isReady_list["$appletName"] ) {
520 alert("$appletName is not ready"); 539 alert("$appletName is not ready");
521 } 540 }
522 applet_getState_list["$appletName"](); 541 applet_getState_list["$appletName"]();
542 $submitActionScript
523 //getQE("$returnFieldName").value = getApplet("$appletName").sendData(); //FIXME -- not needed in general? 543 //getQE("$returnFieldName").value = getApplet("$appletName").sendData(); //FIXME -- not needed in general?
524 }; 544 };
525 </script> 545 </script>
526 546
527END_HEADER_SCRIPT 547END_HEADER_SCRIPT
528 548
529package FlashApplet; 549package FlashApplet;
530@ISA = qw(Applet); 550@ISA = qw(Applet);
531 551
532 552
553=head2 Insertion HTML code for FlashApplet
533 554
534=pod 555=pod
535 556
536The secret to making this applet work with IE in addition to normal browsers 557The secret to making this applet work with IE in addition to normal browsers
537is the addition of the C(<form></form>) construct just before the object. 558is the addition of the C(<form></form>) construct just before the object.
598 619
599 620
600package JavaApplet; 621package JavaApplet;
601@ISA = qw(Applet); 622@ISA = qw(Applet);
602 623
603 624=head2 Insertion HTML code for JavaApplet
604 625
605=pod 626=pod
606 627
607The secret to making this applet work with IE in addition to normal browsers 628The secret to making this applet work with IE in addition to normal browsers
608is the addition of the C(<form></form>) construct just before the object. 629is the addition of the C(<form></form>) construct just before the object.

Legend:
Removed from v.5667  
changed lines
  Added in v.5984

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9