[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 5619 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.4 2007/12/20 00:04: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;
339#FIXME 341#FIXME
340# need to be able to adjust header material 342# need to be able to adjust header material
341 343
342sub insertHeader { 344sub insertHeader {
343 my $self = shift; 345 my $self = shift;
344 my $codebase = $self->codebase; 346 my $codebase = $self->codebase;
345 my $appletId = $self->appletId; 347 my $appletId = $self->appletId;
346 my $appletName = $self->appletName; 348 my $appletName = $self->appletName;
347 my $base64_initialState = $self->base64_state; 349 my $base64_initialState = $self->base64_state;
348 my $initializeAction = $self->initializeActionAlias; 350 my $initializeAction = $self->initializeActionAlias;
349 my $submitAction = $self->submitActionAlias; 351 my $submitAction = $self->submitActionAlias;
350 my $setState = $self->setStateAlias; 352 my $setState = $self->setStateAlias;
351 my $getState = $self->getStateAlias; 353 my $getState = $self->getStateAlias;
352 my $config = $self->configAlias; 354 my $config = $self->configAlias;
353 my $base64_config = $self->base64_config; 355 my $base64_config = $self->base64_config;
354 my $debugMode = ($self->debug) ? "1": "0"; 356 my $debugMode = ($self->debug) ? "1": "0";
355 my $returnFieldName = $self->{returnFieldName}; 357 my $returnFieldName = $self->{returnFieldName};
356# my $encodeStateQ = ($self->debug)?'' : "state = Base64.encode(state);"; # in debug mode base64 encoding is not used. 358# my $encodeStateQ = ($self->debug)?'' : "state = Base64.encode(state);"; # in debug mode base64 encoding is not used.
357# my $decodeStateQ = "if (!state.match(/<XML>*/i) ) {state = Base64.decode(state)}"; # decode if <XML> is not present 359# my $decodeStateQ = "if (!state.match(/<XML>*/i) ) {state = Base64.decode(state)}"; # decode if <XML> is not present
358 my $headerText = $self->header(); 360 my $headerText = $self->header();
361
359 $headerText =~ s/(\$\w+)/$1/gee; # interpolate variables p17 of Cookbook 362 $headerText =~ s/(\$\w+)/$1/gee; # interpolate variables p17 of Cookbook
360 363
361 return $headerText; 364 return $headerText;
362 365
363 366
370 my $appletId = $self->{appletName}; 373 my $appletId = $self->{appletName};
371 my $appletName = $self->{appletName}; 374 my $appletName = $self->{appletName};
372 my $archive = $self->{archive}; 375 my $archive = $self->{archive};
373 my $width = $self->{width}; 376 my $width = $self->{width};
374 my $height = $self->{height}; 377 my $height = $self->{height};
375 my $parameters = ''; 378 my $javaParameters = '';
376 my $parameters = ''; 379 my $flashParameters = '';
377 my %param_hash = %{$self->params()}; 380 my %param_hash = %{$self->params()};
378 foreach my $key (keys %param_hash) { 381 foreach my $key (keys %param_hash) {
379 $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}).'&';
380 } 384 }
385 $flashParameters =~ s/\&$//; # trim last &
381 386
387
382 $objectText = $self->{objectText}; 388 $objectText = $self->{objectText};
383 $objectText =~ s/(\$\w+)/$1/gee; 389 $objectText =~ s/(\$\w+)/$1/gee;
384 return $objectText; 390 return $objectText;
385} 391}
386sub initialize { 392sub initialize {
391 // this should really be done in the <body> tag 397 // this should really be done in the <body> tag
392 </script> 398 </script>
393 }; 399 };
394 400
395} 401}
396 402########################################################
403# HEADER material for one flash or java applet
404########################################################
397 405
398use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT'; 406use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT';
399 407
400 <script language="JavaScript"> 408 <script language="JavaScript">
401 var debug = $debugMode; 409
402 // 410 // set debug mode for this applet
411 set_debug($debugMode);
412
413 //////////////////////////////////////////////////////////
403 //CONFIGURATIONS 414 //CONFIGURATIONS
404 // 415 //
405 // configurations are "permanent" 416 // configurations are "permanent"
417 //////////////////////////////////////////////////////////
418
406 applet_config_list["$appletName"] = function() { 419 applet_config_list["$appletName"] = function() {
407 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 );
408 try { 422 try {
409 if (debug || !( typeof(getApplet("$appletName").$config) == "undefined" ) ) { 423 if (( typeof(getApplet("$appletName").$config) == "function" ) ) {
410 424 debug_add("CONFIGURE $appletName");
411 getApplet("$appletName").$config(Base64.decode("$base64_config")); 425 getApplet("$appletName").$config(Base64.decode("$base64_config"));
412 } 426 }
413 } catch(e) { 427 } catch(e) {
414 alert("error executing configuration command $config for $appletName: " + e ); 428 alert("Error executing configuration command $config for $appletName: " + e );
415 } 429 }
416 } 430 }
431 ////////////////////////////////////////////////////////////
417 // 432 //
418 //STATE 433 //STATE:
434 // state can vary as the applet is manipulated -- it is reset from the questions _state values
419 // 435 //
420 // state can vary as the applet is manipulated. 436 //////////////////////////////////////////////////////////
437
421 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");
422 state = state || getQE("$appletName"+"_state").value || "<xml></xml>"; 441 state = state || getQE("$appletName"+"_state").value;
423 if ( base64Q(state) ) { state=Base64.decode(state);} 442 if ( base64Q(state) ) {
424 if (debug) { alert("set state for $appletName to " + state);} 443 state=Base64.decode(state);
444 }
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 );
425 try { 450 try {
426 if (debug || !( typeof(getApplet("$appletName").$setState) =="undefined" ) ) { 451 if (( typeof(getApplet("$appletName").$setState) =="function" ) ) {
452 debug_add("setState for $appletName");
427 getApplet("$appletName").$setState( state ); 453 getApplet("$appletName").$setState( state );
428 } 454 }
429 } catch(e) { 455 } catch(e) {
430 alert("Error in setting state of $appletName: " + e ); 456 alert("Error in setting state of $appletName using command $setState : " + e );
457 }
458 } else if (debug) {
459 alert("new state was empty string or did not begin with <xml-- state was not reset");
431 } 460 }
432 }; 461 };
433 applet_getState_list["$appletName"] = function () { 462 applet_getState_list["$appletName"] = function () {
434 if (debug) { alert("getState for applet $appletName");} 463 debug_add("get current state for applet $appletName and store it in $appletName"+"_state");
435 try {
436 var applet = getApplet("$appletName"); 464 var applet = getApplet("$appletName");
437 var state; 465 try {
438 if (!( typeof(getApplet("$appletName").$getState) =="undefined" ) ) { 466 if (( typeof(applet.$getState) == "function" ) ) { // there may be no state function
439 state = applet.$getState(); // get state in xml format 467 state = applet.$getState(); // get state in xml format
440 } 468 }
469
441 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
442 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)
443 } catch (e) { 473 } catch (e) {
444 alert("Error in getting state for $appletName " + e ); 474 alert("Error in getting state for $appletName " + e );
445 } 475 }
446 }; 476 };
477
478 ////////////////////////////////////////////////////////////
447 // 479 //
448 //INITIALIZE 480 //INITIALIZE
449 // 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
450 applet_initializeAction_list["$appletName"] = function () { 514 applet_initializeAction_list["$appletName"] = function (state) {
451 applet_setState_list["$appletName"](); 515 applet_setState_list["$appletName"](state);
452 }; 516 };
453 517
454 applet_submitAction_list["$appletName"] = function () { 518 applet_submitAction_list["$appletName"] = function () {
519 if (! applet_isReady_list["$appletName"] ) {
520 alert("$appletName is not ready");
521 }
455 applet_getState_list["$appletName"](); 522 applet_getState_list["$appletName"]();
456 getQE("$returnFieldName").value = getApplet("$appletName").sendData(); 523 //getQE("$returnFieldName").value = getApplet("$appletName").sendData(); //FIXME -- not needed in general?
457 }; 524 };
458 </script> 525 </script>
459 526
460END_HEADER_SCRIPT 527END_HEADER_SCRIPT
461 528
507 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 574 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
508 <param name="movie" value="$codebase/$appletName.swf" /> 575 <param name="movie" value="$codebase/$appletName.swf" />
509 <param name="quality" value="high" /> 576 <param name="quality" value="high" />
510 <param name="bgcolor" value="#869ca7" /> 577 <param name="bgcolor" value="#869ca7" />
511 <param name="allowScriptAccess" value="sameDomain" /> 578 <param name="allowScriptAccess" value="sameDomain" />
579 <param name="FlashVars" value="$flashParameters"/>
512 <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#869ca7" 580 <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#869ca7"
513 width="$width" height="$height" name="$appletName" align="middle" id="$appletName" 581 width="$width" height="$height" name="$appletName" align="middle" id="$appletName"
514 play="true" loop="false" quality="high" allowScriptAccess="sameDomain" 582 play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
515 type="application/x-shockwave-flash" 583 type="application/x-shockwave-flash"
516 pluginspage="http://www.macromedia.com/go/getflashplayer"> 584 pluginspage="http://www.macromedia.com/go/getflashplayer"
585 FlashVars="$flashParameters">
517 </embed> 586 </embed>
518 587
519 </object> 588 </object>
520END_OBJECT_TEXT 589END_OBJECT_TEXT
521 590
557 id = "$appletName" 626 id = "$appletName"
558 width = "$width" 627 width = "$width"
559 height = "$height" 628 height = "$height"
560 MAYSCRIPT 629 MAYSCRIPT
561 > 630 >
562 $parameters 631 $javaParameters
563 </applet> 632 </applet>
564 END_OBJECT_TEXT 633 END_OBJECT_TEXT
565 634
566=cut 635=cut
567 636
575 id = "$appletName" 644 id = "$appletName"
576 width = "$width" 645 width = "$width"
577 height = "$height" 646 height = "$height"
578 MAYSCRIPT 647 MAYSCRIPT
579 > 648 >
580 $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>)
581 </applet> 654 </applet>
582END_OBJECT_TEXT 655END_OBJECT_TEXT
583 656
584sub new { 657sub new {
585 my $class = shift; 658 my $class = shift;

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9