[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 5622 Revision 5667
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.5 2008/03/16 14:39:39 gage Exp $ 4# $CVSHeader: pg/lib/Applet.pm,v 1.10 2008/05/05 17:24: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.
35 ################################### 35 ###################################
36 # Configure applet 36 # Configure applet
37 ################################### 37 ###################################
38 38
39 #xml data to set up the problem-rac 39 #xml data to set up the problem-rac
40 $applet->state(qq{<XML> 40 $applet->config(qq{<XML>
41 <point xval='$xval_1' yval='$yval_1' /> 41 <point xval='$xval_1' yval='$yval_1' />
42 <point xval='$xval_2' yval='$yval_2' /> 42 <point xval='$xval_2' yval='$yval_2' />
43 </XML>}); 43 </XML>});
44 44
45 45
90 90
91 91
92 92
93package Applet; 93package Applet;
94 94
95 95use URI::Escape;
96 96
97 97
98 98
99use MIME::Base64 qw( encode_base64 decode_base64); 99use MIME::Base64 qw( encode_base64 decode_base64);
100 100
190 codebase=>'', 190 codebase=>'',
191# appletId =>'', #always use identical applet Id's and applet Names 191# appletId =>'', #always use identical applet Id's and applet Names
192 params =>undef, 192 params =>undef,
193 width => 550, 193 width => 550,
194 height => 400, 194 height => 400,
195 base64_state => '', 195 base64_state => undef, # this is an state to use for initializing the first occurence of the question.
196 base64_config => '', 196 base64_config => undef, # this is the initial (and final?) configuration
197 getStateAlias => 'getXML', 197 getStateAlias => 'getXML',
198 setStateAlias => 'setState', 198 setStateAlias => 'setXML',
199 configAlias => 'config', 199 configAlias => 'config',
200 initializeActionAlias => 'setXML', 200 initializeActionAlias => 'setXML',
201 submitActionAlias => 'getXML', 201 submitActionAlias => 'getXML',
202 returnFieldName => 'receivedField', 202 returnFieldName => 'receivedField',
203 headerText => DEFAULT_HEADER_TEXT(), 203 headerText => DEFAULT_HEADER_TEXT(),
204 objectText => '', 204 objectText => '',
205 debug => 0, 205 debug => 0,
206 @_, 206 @_,
207 }; 207 };
208 bless $self, $class; 208 bless $self, $class;
209 $self->state('<xml></xml>');
210 $self->config('<xml></xml>');
209 return $self; 211 return $self;
210} 212}
211 213
212sub header { 214sub header {
213 my $self = shift; 215 my $self = shift;
371 my $appletId = $self->{appletName}; 373 my $appletId = $self->{appletName};
372 my $appletName = $self->{appletName}; 374 my $appletName = $self->{appletName};
373 my $archive = $self->{archive}; 375 my $archive = $self->{archive};
374 my $width = $self->{width}; 376 my $width = $self->{width};
375 my $height = $self->{height}; 377 my $height = $self->{height};
376 my $parameters = ''; 378 my $javaParameters = '';
377 my $parameters = ''; 379 my $flashParameters = '';
378 my %param_hash = %{$self->params()}; 380 my %param_hash = %{$self->params()};
379 foreach my $key (keys %param_hash) { 381 foreach my $key (keys %param_hash) {
380 $parameters .= qq!<param name ="$key" value = "$param_hash{$key}">\n! 382 $javaParameters .= qq!<param name ="$key" value = "$param_hash{$key}">\n!;
383 $flashParameters .= uri_escape($key).'='.uri_escape($param_hash{$key}).'&';
381 } 384 }
385 $flashParameters =~ s/\&$//; # trim last &
382 386
387
383 $objectText = $self->{objectText}; 388 $objectText = $self->{objectText};
384 $objectText =~ s/(\$\w+)/$1/gee; 389 $objectText =~ s/(\$\w+)/$1/gee;
385 return $objectText; 390 return $objectText;
386} 391}
387sub initialize { 392sub initialize {
392 // this should really be done in the <body> tag 397 // this should really be done in the <body> tag
393 </script> 398 </script>
394 }; 399 };
395 400
396} 401}
397 402########################################################
403# HEADER material for one flash or java applet
404########################################################
398 405
399use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT'; 406use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT';
400 407
401 <script language="JavaScript"> 408 <script language="JavaScript">
402 var debug = $debugMode; 409
403 // 410 // set debug mode for this applet
411 set_debug($debugMode);
412
413 //////////////////////////////////////////////////////////
404 //CONFIGURATIONS 414 //CONFIGURATIONS
405 // 415 //
406 // configurations are "permanent" 416 // configurations are "permanent"
417 //////////////////////////////////////////////////////////
418
407 applet_config_list["$appletName"] = function() { 419 applet_config_list["$appletName"] = function() {
408 if (debug) { alert("configure $appletName . $config ( $base64_config )");} 420 debug_add("applet_config_list:\n attempt to configure $appletName . $config ( $base64_config ) if config function is defined: "
421 );
409 try { 422 try {
410 if (debug || !( typeof(getApplet("$appletName").$config) == "undefined" ) ) { 423 if (( typeof(getApplet("$appletName").$config) == "function" ) ) {
411 424 debug_add("CONFIGURE $appletName");
412 getApplet("$appletName").$config(Base64.decode("$base64_config")); 425 getApplet("$appletName").$config(Base64.decode("$base64_config"));
413 } 426 }
414 } catch(e) { 427 } catch(e) {
415 alert("error executing configuration command $config for $appletName: " + e ); 428 alert("Error executing configuration command $config for $appletName: " + e );
416 } 429 }
417 } 430 }
431 ////////////////////////////////////////////////////////////
418 // 432 //
419 //STATE 433 //STATE:
434 // state can vary as the applet is manipulated -- it is reset from the questions _state values
420 // 435 //
421 // state can vary as the applet is manipulated. 436 //////////////////////////////////////////////////////////
437
422 applet_setState_list["$appletName"] = function(state) { 438 applet_setState_list["$appletName"] = function(state) {
439 debug_add("Begin setState for applet $appletName");
440 debug_add("Obtain state from $appletName"+"_state");
423 state = state || getQE("$appletName"+"_state").value 441 state = state || getQE("$appletName"+"_state").value;
424 if (state.match("\S") ) { // if state is not all white space
425 if ( base64Q(state) ) { 442 if ( base64Q(state) ) {
426 state=Base64.decode(state); 443 state=Base64.decode(state);
427 } 444 }
428 if (debug) { alert("set state for $appletName to " + state);} 445 if (state.match(/<xml/i) || state.match(/<?xml/i) ) { // if state starts with <?xml
446
447 debug_add("applet_setState_list: \n set (decoded) state for $appletName to " +
448 state +"\nfunction type is " +typeof(getApplet("$appletName").$setState)
449 );
429 try { 450 try {
430 if (debug || !( typeof(getApplet("$appletName").$setState) =="undefined" ) ) { 451 if (( typeof(getApplet("$appletName").$setState) =="function" ) ) {
452 debug_add("setState for $appletName");
431 getApplet("$appletName").$setState( state ); 453 getApplet("$appletName").$setState( state );
432 } 454 }
433 } catch(e) { 455 } catch(e) {
434 alert("Error in setting state of $appletName using command $setState : " + e ); 456 alert("Error in setting state of $appletName using command $setState : " + e );
435 } 457 }
436 } 458 } else if (debug) {
459 alert("new state was empty string or did not begin with <xml-- state was not reset");
460 }
437 }; 461 };
438 applet_getState_list["$appletName"] = function () { 462 applet_getState_list["$appletName"] = function () {
439 if (debug) { alert("getState for applet $appletName");} 463 debug_add("get current state for applet $appletName and store it in $appletName"+"_state");
440 try {
441 var applet = getApplet("$appletName"); 464 var applet = getApplet("$appletName");
442 var state; 465 try {
443 if (!( typeof(getApplet("$appletName").$getState) =="undefined" ) ) { 466 if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function
444 state = applet.$getState(); // get state in xml format 467 state = applet.$getState(); // get state in xml format
445 } 468 }
469
446 if (!debug) {state = Base64.encode(state) }; // replace state by encoded version 470 if (!debug) {state = Base64.encode(state) }; // replace state by encoded version unless in debug mode
471 debug_add("state is "+state); // this should still be in plain text
447 getQE("$appletName"+"_state").value = state; //place in state htmlItem (debug: textarea, otherwise hidden) 472 getQE("$appletName"+"_state").value = state; //place state in input item (debug: textarea, otherwise: hidden)
448 } catch (e) { 473 } catch (e) {
449 alert("Error in getting state for $appletName " + e ); 474 alert("Error in getting state for $appletName " + e );
450 } 475 }
451 }; 476 };
477
478 ////////////////////////////////////////////////////////////
452 // 479 //
453 //INITIALIZE 480 //INITIALIZE
454 // 481 //
482 ////////////////////////////////////////////////////////////
483
484
485 applet_checkLoaded_list["$appletName"] = function() { // this function returns 0 unless:
486 // applet has already been flagged as ready in applet_isReady_list
487 // applet.config is defined (or alias for .config)
488 // applet.setState is defined
489 // applet.isActive is defined
490 // applet reported that it is loaded by calling loadQ()
491 var ready = 0;
492 var applet = getApplet("$appletName");
493 if (!debug && applet_isReady_list["$appletName"]) {return(1)}; // memorize readiness in non-debug mode
494 if ( typeof(applet.$config) == "function") {
495 debug_add( "applet.config is " + typeof(applet.$config) );
496 ready = 1;
497 }
498 if( typeof(applet.$getState) == "function") {
499 debug_add( "applet.getState is " + typeof(applet.$getState) );
500 ready =1;
501 }
502 if (typeof(applet.isActive) == "function" && applet.isActive ) {
503 debug_add( "applet.isActive is " + typeof(applet.isActive) );
504 ready =1;
505 }
506 if (typeof(applet_reportsLoaded_list["$appletName"]) !="undefined" && applet_reportsLoaded_list["$appletName"] != 0 ) {
507 debug_add( "applet reports that it is loaded " + applet_reportsLoaded_list["$appletName"] );
508 ready =1;
509 }
510 applet_isReady_list["$appletName"]= ready;
511 return(ready);
512 }
513
455 applet_initializeAction_list["$appletName"] = function () { 514 applet_initializeAction_list["$appletName"] = function (state) {
456 applet_setState_list["$appletName"](); 515 applet_setState_list["$appletName"](state);
457 }; 516 };
458 517
459 applet_submitAction_list["$appletName"] = function () { 518 applet_submitAction_list["$appletName"] = function () {
519 if (! applet_isReady_list["$appletName"] ) {
520 alert("$appletName is not ready");
521 }
460 applet_getState_list["$appletName"](); 522 applet_getState_list["$appletName"]();
461 getQE("$returnFieldName").value = getApplet("$appletName").sendData(); 523 //getQE("$returnFieldName").value = getApplet("$appletName").sendData(); //FIXME -- not needed in general?
462 }; 524 };
463 </script> 525 </script>
464 526
465END_HEADER_SCRIPT 527END_HEADER_SCRIPT
466 528
512 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 574 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
513 <param name="movie" value="$codebase/$appletName.swf" /> 575 <param name="movie" value="$codebase/$appletName.swf" />
514 <param name="quality" value="high" /> 576 <param name="quality" value="high" />
515 <param name="bgcolor" value="#869ca7" /> 577 <param name="bgcolor" value="#869ca7" />
516 <param name="allowScriptAccess" value="sameDomain" /> 578 <param name="allowScriptAccess" value="sameDomain" />
579 <param name="FlashVars" value="$flashParameters"/>
517 <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#869ca7" 580 <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#869ca7"
518 width="$width" height="$height" name="$appletName" align="middle" id="$appletName" 581 width="$width" height="$height" name="$appletName" align="middle" id="$appletName"
519 play="true" loop="false" quality="high" allowScriptAccess="sameDomain" 582 play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
520 type="application/x-shockwave-flash" 583 type="application/x-shockwave-flash"
521 pluginspage="http://www.macromedia.com/go/getflashplayer"> 584 pluginspage="http://www.macromedia.com/go/getflashplayer"
585 FlashVars="$flashParameters">
522 </embed> 586 </embed>
523 587
524 </object> 588 </object>
525END_OBJECT_TEXT 589END_OBJECT_TEXT
526 590
562 id = "$appletName" 626 id = "$appletName"
563 width = "$width" 627 width = "$width"
564 height = "$height" 628 height = "$height"
565 MAYSCRIPT 629 MAYSCRIPT
566 > 630 >
567 $parameters 631 $javaParameters
568 </applet> 632 </applet>
569 END_OBJECT_TEXT 633 END_OBJECT_TEXT
570 634
571=cut 635=cut
572 636
580 id = "$appletName" 644 id = "$appletName"
581 width = "$width" 645 width = "$width"
582 height = "$height" 646 height = "$height"
583 MAYSCRIPT 647 MAYSCRIPT
584 > 648 >
585 $parameters 649 $javaParameters
650
651 Sorry, the Applet could not be started. Please make sure that
652Java 1.4.2 (or later) is installed and activated.
653(<a href="http://java.sun.com/getjava">click here to install Java now</a>)
586 </applet> 654 </applet>
587END_OBJECT_TEXT 655END_OBJECT_TEXT
588 656
589sub new { 657sub new {
590 my $class = shift; 658 my $class = shift;

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9