[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 5944 Revision 5994
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.12 2008/05/22 19:15:59 gage Exp $ 4# $CVSHeader: pg/lib/Applet.pm,v 1.14 2009/01/28 17:07:08 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.
108 108
109 getApplet(appletName) -- finds the applet path in the DOM 109 getApplet(appletName) -- finds the applet path in the DOM
110 110
111 submitAction() -- calls the submit action of the applets 111 submitAction() -- calls the submit action of the applets
112 112
113 initializeAction() -- calls the initialize action of the applets 113 initializeWWquestion() -- calls the initialize action of the applets
114 114
115 getQE(name) -- gets an HTML element of the question by name 115 getQE(name) -- gets an HTML element of the question by name
116 or by id. Be sure to keep all names and ids 116 or by id. Be sure to keep all names and ids
117 unique within a given PG question. 117 unique within a given PG question.
118 118
177 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.
178 178
179 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
180 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
181 .pg question is pressed. 181 .pg question is pressed.
182 182 answerBox -- name of answer box to return answer to: default defaultAnswerBox
183 returnFieldName 183 getAnswer -- (formerly sendData) get student answer from applet and place in answerBox
184 returnFieldName -- (deprecated) synonmym for answerBox
184 185
185 186
186=cut 187=cut
187 188
188 189
203 getStateAlias => 'getXML', 204 getStateAlias => 'getXML',
204 setStateAlias => 'setXML', 205 setStateAlias => 'setXML',
205 configAlias => 'config', 206 configAlias => 'config',
206 initializeActionAlias => 'setXML', 207 initializeActionAlias => 'setXML',
207 submitActionAlias => 'getXML', 208 submitActionAlias => 'getXML',
208 returnFieldName => 'receivedField', 209 submitActionScript => '', # script executed on submitting the WW question
210 answerBox => 'answerBox',
209 headerText => DEFAULT_HEADER_TEXT(), 211 headerText => DEFAULT_HEADER_TEXT(),
210 objectText => '', 212 objectText => '',
211 debug => 0, 213 debug => 0,
212 @_, 214 @_,
213 }; 215 };
256sub submitActionAlias { 258sub submitActionAlias {
257 my $self = shift; 259 my $self = shift;
258 $self->{submitActionAlias} = shift ||$self->{submitActionAlias}; # replace the current contents if non-empty 260 $self->{submitActionAlias} = shift ||$self->{submitActionAlias}; # replace the current contents if non-empty
259 $self->{submitActionAlias}; 261 $self->{submitActionAlias};
260} 262}
263sub submitActionScript {
264 my $self = shift;
265 $self->{submitActionScript} = shift ||$self->{submitActionScript}; # replace the current contents if non-empty
266 $self->{submitActionScript};
267}
261sub getStateAlias { 268sub getStateAlias {
262 my $self = shift; 269 my $self = shift;
263 $self->{getStateAlias} = shift ||$self->{getStateAlias}; # replace the current contents if non-empty 270 $self->{getStateAlias} = shift ||$self->{getStateAlias}; # replace the current contents if non-empty
264 $self->{getStateAlias}; 271 $self->{getStateAlias};
265} 272}
274 $self->{configAlias} = shift ||$self->{configAlias}; # replace the current contents if non-empty 281 $self->{configAlias} = shift ||$self->{configAlias}; # replace the current contents if non-empty
275 $self->{configAlias}; 282 $self->{configAlias};
276} 283}
277sub returnFieldName { 284sub returnFieldName {
278 my $self = shift; 285 my $self = shift;
279 $self->{returnFieldName} = shift ||$self->{returnFieldName}; # replace the current contents if non-empty 286 $self->{answerBox} = shift ||$self->{answerBox}; # replace the current contents if non-empty
280 $self->{returnFieldName}; 287 $self->{answerBox};
288}
289sub answerBox {
290 my $self = shift;
291 $self->{answerBox} = shift ||$self->{answerBox}; # replace the current contents if non-empty
292 $self->{answerBox};
281} 293}
282sub codebase { 294sub codebase {
283 my $self = shift; 295 my $self = shift;
284 $self->{codebase} = shift ||$self->{codebase}; # replace the current codebase if non-empty 296 $self->{codebase} = shift ||$self->{codebase}; # replace the current codebase if non-empty
285 $self->{codebase}; 297 $self->{codebase};
353 my $appletId = $self->appletId; 365 my $appletId = $self->appletId;
354 my $appletName = $self->appletName; 366 my $appletName = $self->appletName;
355 my $base64_initialState = $self->base64_state; 367 my $base64_initialState = $self->base64_state;
356 my $initializeAction = $self->initializeActionAlias; 368 my $initializeAction = $self->initializeActionAlias;
357 my $submitAction = $self->submitActionAlias; 369 my $submitAction = $self->submitActionAlias;
370 my $submitActionScript = $self->submitActionScript;
358 my $setState = $self->setStateAlias; 371 my $setState = $self->setStateAlias;
359 my $getState = $self->getStateAlias; 372 my $getState = $self->getStateAlias;
360 my $config = $self->configAlias; 373 my $config = $self->configAlias;
361 my $base64_config = $self->base64_config; 374 my $base64_config = $self->base64_config;
362 my $debugMode = ($self->debug) ? "1": "0"; 375 my $debugMode = ($self->debug) ? "1": "0";
363 my $returnFieldName = $self->{returnFieldName}; 376 my $returnFieldName = $self->{returnFieldName};
364# my $encodeStateQ = ($self->debug)?'' : "state = Base64.encode(state);"; # in debug mode base64 encoding is not used. 377 my $answerBox = $self->{answerBox};
365# my $decodeStateQ = "if (!state.match(/<XML>*/i) ) {state = Base64.decode(state)}"; # decode if <XML> is not present
366 my $headerText = $self->header(); 378 my $headerText = $self->header();
367 379
368 $headerText =~ s/(\$\w+)/$1/gee; # interpolate variables p17 of Cookbook 380 $headerText =~ s/(\$\w+)/$1/gee; # interpolate variables p17 of Cookbook
369 381
370 return $headerText; 382 return $headerText;
393 405
394 $objectText = $self->{objectText}; 406 $objectText = $self->{objectText};
395 $objectText =~ s/(\$\w+)/$1/gee; 407 $objectText =~ s/(\$\w+)/$1/gee;
396 return $objectText; 408 return $objectText;
397} 409}
398sub initialize { 410# sub initialize {
399 my $self = shift; 411# my $self = shift;
400 return q{ 412# return q{
401 <script> 413# <script>
402 initializeAction(); 414# initializeAllApplets();
403 // this should really be done in the <body> tag 415# // this should really be done in the <body> tag
404 </script> 416# </script>
405 }; 417# };
406 418#
407} 419# }
408######################################################## 420########################################################
409# HEADER material for one flash or java applet 421# HEADER material for one flash or java applet
410######################################################## 422########################################################
411 423
412use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT'; 424use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT';
413 425 <script src="/webwork2_files/js/Base64.js" language="javascript">
426 </script>
427 <script src="/webwork2_files/js/ww_applet_support.js">
428 //upload functions stored in /opt/webwork/webwork2/htdocs/js ...
429 </script>
414 <script language="JavaScript"> 430 <script language="JavaScript">
415 431
416 // set debug mode for this applet 432 // set debug mode for this applet
417 set_debug($debugMode); 433 set_debug($debugMode);
434
435 //////////////////////////////////////////////////////////
436 //TEST code
437 //
438 //
439 //////////////////////////////////////////////////////////
440
441 ww_applet_list["$appletName"] = new ww_applet("$appletName");
442
443
444 ww_applet_list["$appletName"].code = "$code";
445 ww_applet_list["$appletName"].codebase = "$codebase";
446 ww_applet_list["$appletName"].appletID = "$appletID";
447 ww_applet_list["$appletName"].base64_state = "$base64_initializationState";
448 ww_applet_list["$appletName"].base64_config = "$base64_config";
449 ww_applet_list["$appletName"].getStateAlias = "$getState";
450 ww_applet_list["$appletName"].setStateAlias = "$setState";
451 ww_applet_list["$appletName"].configAlias = "$config";
452 ww_applet_list["$appletName"].initializeActionAlias = "$initializeAction";
453 ww_applet_list["$appletName"].submitActionAlias = "$submitAction";
454 ww_applet_list["$appletName"].submitActionScript = "$submitActionScript";
455 ww_applet_list["$appletName"].answerBox = "$answerBox";
456 ww_applet_list["$appletName"].debug = "$debugMode";
457
418 458
419 ////////////////////////////////////////////////////////// 459 //////////////////////////////////////////////////////////
420 //CONFIGURATIONS 460 //CONFIGURATIONS
421 // 461 //
422 // configurations are "permanent" 462 // configurations are "permanent"
423 ////////////////////////////////////////////////////////// 463 //////////////////////////////////////////////////////////
424 464
425 applet_config_list["$appletName"] = function() { 465// applet_config_list["$appletName"] = function() {
426 debug_add("applet_config_list:\n attempt to configure $appletName . $config ( $base64_config ) if config function is defined: " 466// debug_add("applet_config_list:\n attempt to configure $appletName . $config ( $base64_config ) if config function is defined: "
427 ); 467// );
428 try { 468// try {
429 if (( typeof(getApplet("$appletName").$config) == "function" ) ) { 469// if (( typeof(getApplet("$appletName").$config) == "function" ) ) {
430 debug_add("CONFIGURE $appletName"); 470// debug_add("CONFIGURE $appletName");
431 getApplet("$appletName").$config(Base64.decode("$base64_config")); 471// getApplet("$appletName").$config(Base64.decode("$base64_config"));
432 } 472// }
433 } catch(e) { 473// } catch(e) {
434 alert("Error executing configuration command $config for $appletName: " + e ); 474// alert("Error executing configuration command $config for $appletName: " + e );
435 } 475// }
436 } 476// }
437 //////////////////////////////////////////////////////////// 477// ////////////////////////////////////////////////////////////
438 // 478// //
439 //STATE: 479// //STATE:
440 // state can vary as the applet is manipulated -- it is reset from the questions _state values 480// // state can vary as the applet is manipulated -- it is reset from the questions _state values
441 // 481// //
442 ////////////////////////////////////////////////////////// 482// //////////////////////////////////////////////////////////
443 483//
444 applet_setState_list["$appletName"] = function(state) { 484// applet_setState_list["$appletName"] = function(state) {
445 debug_add("Begin setState for applet $appletName"); 485// debug_add("Begin setState for applet $appletName");
446 debug_add("Obtain state from $appletName"+"_state"); 486// debug_add("Obtain state from $appletName"+"_state");
447 state = state || getQE("$appletName"+"_state").value; 487// state = state || getQE("$appletName"+"_state").value;
448 if ( base64Q(state) ) { 488// if ( base64Q(state) ) {
449 state=Base64.decode(state); 489// state=Base64.decode(state);
450 } 490// }
451 if (state.match(/<xml/i) || state.match(/<?xml/i) ) { // if state starts with <?xml 491// if (state.match(/<xml/i) || state.match(/<?xml/i) ) { // if state starts with <?xml
452 492//
453 debug_add("applet_setState_list: \n set (decoded) state for $appletName to " + 493// debug_add("applet_setState_list: \n set (decoded) state for $appletName to " +
454 state +"\nfunction type is " +typeof(getApplet("$appletName").$setState) 494// state +"\nfunction type is " +typeof(getApplet("$appletName").$setState)
455 ); 495// );
456 try { 496// try {
457 if (( typeof(getApplet("$appletName").$setState) =="function" ) ) { 497// if (( typeof(getApplet("$appletName").$setState) =="function" ) ) {
458 debug_add("setState for $appletName"); 498// debug_add("setState for $appletName");
459 getApplet("$appletName").$setState( state ); 499// getApplet("$appletName").$setState( state );
460 } 500// }
461 } catch(e) { 501// } catch(e) {
462 alert("Error in setting state of $appletName using command $setState : " + e ); 502// alert("Error in setting state of $appletName using command $setState : " + e );
463 } 503// }
464 } else if (debug) { 504// } else if (debug) {
465 alert("new state was empty string or did not begin with <xml-- state was not reset"); 505// alert("new state was empty string or did not begin with <xml-- state was not reset");
466 } 506// }
467 }; 507// };
468 applet_getState_list["$appletName"] = function () { 508// applet_getState_list["$appletName"] = function () {
469 debug_add("get current state for applet $appletName and store it in $appletName"+"_state"); 509// debug_add("get current state for applet $appletName and store it in $appletName"+"_state");
470 var applet = getApplet("$appletName"); 510// var applet = getApplet("$appletName");
471 try { 511// try {
472 if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function 512// if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function
473 state = applet.$getState(); // get state in xml format 513// state = applet.$getState(); // get state in xml format
474 debug_add("state has type " + typeof(state)); 514// debug_add("state has type " + typeof(state));
475 state = String(state); // geogebra returned an object type instead of a string type 515// state = String(state); // geogebra returned an object type instead of a string type
476 debug_add("state converted to type " + typeof(state)); 516// debug_add("state converted to type " + typeof(state));
477 } 517// }
478 518//
479 if (!debug) { 519// if (!debug) {
480 state = Base64.encode(state); 520// state = Base64.encode(state);
481 }; // replace state by encoded version unless in debug mode 521// }; // replace state by encoded version unless in debug mode
482 522//
483 debug_add("state is "+state); // this should still be in plain text 523// debug_add("state is "+state); // this should still be in plain text
484 getQE("$appletName"+"_state").value = state; //place state in input item (debug: textarea, otherwise: hidden) 524// getQE("$appletName"+"_state").value = state; //place state in input item (debug: textarea, otherwise: hidden)
485 } catch (e) { 525// } catch (e) {
486 alert("Error in getting state for $appletName " + e ); 526// alert("Error in getting state for $appletName " + e );
487 } 527// }
488 }; 528// };
489 529//
490 //////////////////////////////////////////////////////////// 530// ////////////////////////////////////////////////////////////
491 // 531// //
492 //INITIALIZE 532// //INITIALIZE
493 // 533// //
494 //////////////////////////////////////////////////////////// 534// ////////////////////////////////////////////////////////////
495 535//
496 536//
497 applet_checkLoaded_list["$appletName"] = function() { // this function returns 0 unless: 537// applet_checkLoaded_list["$appletName"] = function() { // this function returns 0 unless:
498 // applet has already been flagged as ready in applet_isReady_list 538// // applet has already been flagged as ready in applet_isReady_list
499 // applet.config is defined (or alias for .config) 539// // applet.config is defined (or alias for .config)
500 // applet.setState is defined 540// // applet.setState is defined
501 // applet.isActive is defined 541// // applet.isActive is defined
502 // applet reported that it is loaded by calling loadQ() 542// // applet reported that it is loaded by calling loadQ()
503 var ready = 0; 543// var ready = 0;
504 var applet = getApplet("$appletName"); 544// var applet = getApplet("$appletName");
505 if (!debug && applet_isReady_list["$appletName"]) {return(1)}; // memorize readiness in non-debug mode 545// if (!debug && applet_isReady_list["$appletName"]) {return(1)}; // memorize readiness in non-debug mode
506 if ( typeof(applet.$config) == "function") { 546// if ( typeof(applet.$config) == "function") {
507 debug_add( "applet.config is " + typeof(applet.$config) ); 547// debug_add( "applet.config is " + typeof(applet.$config) );
508 ready = 1; 548// ready = 1;
509 } 549// }
510 if( typeof(applet.$getState) == "function") { 550// if( typeof(applet.$getState) == "function") {
511 debug_add( "applet.getState is " + typeof(applet.$getState) ); 551// debug_add( "applet.getState is " + typeof(applet.$getState) );
512 ready =1; 552// ready =1;
513 } 553// }
514 if (typeof(applet.isActive) == "function" && applet.isActive ) { 554// if (typeof(applet.isActive) == "function" && applet.isActive ) {
515 debug_add( "applet.isActive is " + typeof(applet.isActive) ); 555// debug_add( "applet.isActive is " + typeof(applet.isActive) );
516 ready =1; 556// ready =1;
517 } 557// }
518 if (typeof(applet_reportsLoaded_list["$appletName"]) !="undefined" && applet_reportsLoaded_list["$appletName"] != 0 ) { 558// if (typeof(applet_reportsLoaded_list["$appletName"]) !="undefined" && applet_reportsLoaded_list["$appletName"] != 0 ) {
519 debug_add( "applet reports that it is loaded " + applet_reportsLoaded_list["$appletName"] ); 559// debug_add( "applet reports that it is loaded " + applet_reportsLoaded_list["$appletName"] );
520 ready =1; 560// ready =1;
521 } 561// }
522 applet_isReady_list["$appletName"]= ready; 562// applet_isReady_list["$appletName"]= ready;
523 return(ready); 563// return(ready);
524 } 564// }
525 565//
526 applet_initializeAction_list["$appletName"] = function (state) { 566// applet_initializeAction_list["$appletName"] = function (state) {
527 applet_setState_list["$appletName"](state); 567// applet_setState_list["$appletName"](state);
528 }; 568// };
529 569//
530 applet_submitAction_list["$appletName"] = function () { 570// applet_submitAction_list["$appletName"] = function () {
531 if (! applet_isReady_list["$appletName"] ) { 571// if (! applet_isReady_list["$appletName"] ) {
532 alert("$appletName is not ready"); 572// alert("$appletName is not ready");
533 } 573// }
534 applet_getState_list["$appletName"](); 574// applet_getState_list["$appletName"]();
575// $submitActionScript
535 //getQE("$returnFieldName").value = getApplet("$appletName").sendData(); //FIXME -- not needed in general? 576// //getQE("$answerBox").value = getApplet("$appletName").getAnswer(); //FIXME -- not needed in general?
536 }; 577// };
537 </script> 578 </script>
538 579
539END_HEADER_SCRIPT 580END_HEADER_SCRIPT
540 581
541package FlashApplet; 582package FlashApplet;

Legend:
Removed from v.5944  
changed lines
  Added in v.5994

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9