From: Mike G. v. a. <we...@ma...> - 2009-03-10 12:17:52
|
Log Message: ----------- refactored code using methodDefined() converted configAlias to setConfigAlias replaced debug by jsDebugMode (for now -- eventually we'll have a debug mode for each applet) Modified Files: -------------- webwork2/htdocs/js: ww_applet_support.js Revision Data ------------- Index: ww_applet_support.js =================================================================== RCS file: /webwork/cvs/system/webwork2/htdocs/js/ww_applet_support.js,v retrieving revision 1.6 retrieving revision 1.7 diff -Lhtdocs/js/ww_applet_support.js -Lhtdocs/js/ww_applet_support.js -u -r1.6 -r1.7 --- htdocs/js/ww_applet_support.js +++ htdocs/js/ww_applet_support.js @@ -35,15 +35,15 @@ ////////////////////////////////////////////////////////// // DEBUGGING tools ////////////////////////////////////////////////////////// - var debug; + var jsDebugMode; var debugText = ""; function set_debug(num) { // setting debug for any applet sets it for all of them if (num) { - debug =1; + jsDebugMode =1; } } function debug_add(str) { - if (debug) { + if (jsDebugMode) { debugText = debugText + "\n" +str; } } @@ -54,7 +54,7 @@ function submitAction() { // called from the submit button defined in Problem.pm - if (debug) { + if (jsDebugMode) { debugText = "Call submitAction() function on each applet\n"; } @@ -62,7 +62,7 @@ ww_applet_list[appletName].submitAction(); } debug_add("\n Done calling submitAction() on each applet.\n"); - if (debug) { + if (jsDebugMode) { alert(debugText); debugText=""; }; } @@ -82,34 +82,46 @@ } // applet can set isReady flag by calling applet_loaded(appletName, loaded); -// function applet_loaded(appletName,loaded) { -// debug_add("applet reporting that it has been loaded = " + loaded ); -// ww_applet_list[appletName].reportsLoaded = loaded; // 0 means not loaded -// } +function applet_loaded(appletName,loaded) { + debug_add("applet reporting that it has been loaded = " + loaded ); + ww_applet_list[appletName].reportsLoaded = loaded; // 0 means not loaded +} // insures that applet is loaded before initializing it + + function safe_applet_initialize(appletName, i) { - debug_add(" Try to initialize applet " + appletName + " with " +i + " attempts remaining.\n" ); + i--; + debug_add(" Try to initialize applet " + appletName + ". Count down: " + i + ".\n" ); - i--; + var ww_applet = ww_applet_list[appletName]; var applet_loaded = ww_applet.checkLoaded(); + if (applet_loaded=="still_loading" && !(i> 0) ) { + // it's possible that the isActive() response of the applet is not working properly + alert("The isActive() method of applet " +appletName + " claims it is still loading! We'll ignore this."); + i=1; + applet_loaded=1; + } else if (applet_loaded=="still_loading") { + applet_loaded=0; // keep trying + } + debug_add(" applet is ready = " + applet_loaded ); if ( 0 < i && !applet_loaded ) { // wait until applet is loaded - debug_add(" applet " + appletName + "not ready try again"); + debug_add(" applet " + appletName + " is not yet ready try again"); window.setTimeout( "safe_applet_initialize(\"" + appletName + "\"," + i + ")",1); } else if( 0 < i ){ // now that applet is loaded configure it and initialize it with saved data. - debug_add(appletName + " initialization completed with " + i + " possible attempts remaining. "); + debug_add(appletName + " initialization completed with " + i + " possible attempts remaining. \n"); // in-line handler -- configure and initialize try{ - ww_applet.setDebug(debug); + ww_applet.setDebug(jsDebugMode); } catch(e) { var msg = "Unable set debug in " + appletName + " \n " +e; - if (debug) {debug_add(msg);} else {alert(msg)}; + if (jsDebugMode) {debug_add(msg);} else {alert(msg)}; } try{ @@ -117,7 +129,7 @@ } catch(e) { var msg = "Unable to configure " + appletName + " \n " +e; - if (debug) {debug_add(msg);} else {alert(msg)}; + if (jsDebugMode) {debug_add(msg);} else {alert(msg)}; } try{ @@ -125,13 +137,13 @@ } catch(e) { var msg = "unable to initialize " + appletName + " \n " +e; - if (debug) {debug_add(msg);} else {alert(msg)}; + if (jsDebugMode) {debug_add(msg);} else {alert(msg)}; } } else { - if (debug) {debug_add("Error: timed out waiting for applet " +appletName + " to load");} + if (jsDebugMode) {debug_add("Error: timed out waiting for applet " +appletName + " to load");} } - if (debug) {alert(debugText); debugText="";}; + if (jsDebugMode) {alert(debugText); debugText="";}; } /////////////////////////////////////////////////////// @@ -188,7 +200,7 @@ // needed for IE -- searches id and name space so it can be unreliable if names are not unique if (!obj || obj.name != name1) { var msg = "Can't find element " + name1; - if (debug) { + if (jsDebugMode) { debug_add(msg + "\n ( Place listQuestionElements() at end of document in order to get all form elements! )\n" ); } else { alert(msg); listQuestionElements(); @@ -199,6 +211,8 @@ } } + + function getQuestionElement(name1) { return getQE(name1); } @@ -228,6 +242,7 @@ this.reportsLoaded = 0 ; }; + ////////////////////////////////////////////////////////// //CONFIGURATIONS // @@ -238,15 +253,11 @@ var appletName = this.appletName; var applet = getApplet(appletName); var configAlias = this.configAlias; - debug_add(" Checking " + appletName +"."+ configAlias +"( " + this.base64_config + " ) to see if config function is defined. The data is" - + this.base64_config + " " + Base64.decode(this.base64_config) ); + debug_add(" Calling " + appletName +"."+ configAlias +"( " + Base64.decode(this.base64_config) + " ) " ); try { - if (( typeof(applet[configAlias]) == "function" ) ) { - debug_add(" CONFIGURING " + appletName); + if ( this.methodDefined(configAlias) ) { applet[configAlias](Base64.decode(this.base64_config)); - } else { - debug_add("Applet does not have config method named: " + configAlias + "."); - } + } } catch(e) { var msg = "Error in configuring " + appletName + " using command " + configAlias + " : " + e ; alert(msg); @@ -258,6 +269,24 @@ // state can vary as the applet is manipulated -- it is reset from the questions _state values // ////////////////////////////////////////////////////////// +ww_applet.prototype.methodDefined = function(methodName) { + var appletName = this.appletName; + var applet = getApplet(appletName); + try { + if (typeof(applet[methodName]) == "function" ) { + debug_add("Method " + methodName + " is defined in " + appletName ); + return(true); + } else { + debug_add("Method " + methodName + " is not defined in " + appletName); + return(false); + } + } catch(e) { + var msg = "Error in accessing " + methodName + " in applet " +appletName + "Error: " +e ; + alert(msg); + } + return(false); +} + ww_applet.prototype.setState = function(state) { var appletName = this.appletName; @@ -292,17 +321,14 @@ ); try { - if ( typeof(applet[setStateAlias]) == "function" ) { + if ( this.methodDefined(setStateAlias) ) { applet[setStateAlias]( state ); // change the applets current state - debug_add("Completed setting the state of applet: " + appletName + "."); - } else { - debug_add("Applet does not have setState method named: " + setStateAlias + "."); - } + } } catch(e) { msg = "Error in setting state of " + appletName + " using command " + setStateAlias + " : " + e ; alert(msg); } - } else if (debug) { + } else if (jsDebugMode) { debug_add(" new state was empty string or did not begin with <xml> -- Applet state was not reset"); } return(''); @@ -318,7 +344,7 @@ debug_add(" Begin getState for applet " + appletName ); try { - if ((typeof(applet[getStateAlias]) == "function" )) { // there may be no state function + if (this.methodDefined(getStateAlias)) { // there may be no state function state = applet[getStateAlias](); // get state in xml format debug_add(" state has type " + typeof(state)); state = String(state); // geogebra returned an object type instead of a string type @@ -334,13 +360,13 @@ alert(msg); } - if (!debug) { + if (!jsDebugMode) { state = Base64.encode(state); }; // replace state by encoded version unless in debug mode debug_add(" state is \n "+ state + "\n"); // state should still be in plain text - var ww_preserve_applet_state = getQE(appletName + "_state"); // answer box preserving applet state (debug: textarea, otherwise: hidden) - ww_preserve_applet_state.value = state; //place state in input item (debug: textarea, otherwise: hidden) + var ww_preserve_applet_state = getQE(appletName + "_state"); // answer box preserving applet state (jsDebugMode: textarea, otherwise: hidden) + ww_preserve_applet_state.value = state; //place state in input item (jsDebugMode: textarea, otherwise: hidden) debug_add("State stored in answer box "+ appletName + "_state and getState is finished."); }; @@ -352,7 +378,7 @@ debugMode = debugMode || this.debug; try{ - if (typeof(applet.debug) == "function" ) { + if (this.methodDefined("debug") ) { applet.debug(1); // turn the applet's debug functions on. } else { debug_add( " Unable to set debug state in applet " + appletName + "."); @@ -365,6 +391,25 @@ } +ww_applet.prototype.getConfig = function() { // used for debugging purposes -- gets the configuration from the applet + var config = "foobar"; + var appletName = this.appletName; + var applet = getApplet(appletName); + var getConfigAlias = this.getConfigAlias; + try { + if (this.methodDefined(getConfigAlias) ) { + alert( applet[getConfigAlias]() ); + } else { + alert(debugText); + } + } catch(e) { + var msg = " Error in getting configuration from applet " + appletName + " " + e; + alert(msg); + } +} + + + //////////////////////////////////////////////////////////// // //INITIALIZE @@ -382,7 +427,7 @@ var ww_preserve_applet_state = getQE(appletName + "_state"); // hidden answer box preserving applet state var saved_state = ww_preserve_applet_state.value; - if (debug) {debug_add("Begin submit action for applet " + appletName);} + if (jsDebugMode) {debug_add("Begin submit action for applet " + appletName);} var applet = getApplet(appletName); if (! this.isReady ) { alert(appletName + " is not ready"); @@ -391,44 +436,49 @@ // Check to see if we want to restart the applet if (saved_state.match(/^<xml>restart_applet<\/xml>/) ) { - if (debug) { debug_add("Restarting the applet "+appletName);} + if (jsDebugMode) { debug_add("Restarting the applet "+appletName);} setAppletStateToRestart(appletName); // erases all of the saved state return(''); } // if we are not restarting the applet save the state and submit this.getState(); // have ww_applet retrieve state from applet and store in answerbox - if (debug) {debug_add("Submit Action Script " + this.submitActionScript + "\n");} + if (jsDebugMode) {debug_add("Submit Action Script " + this.submitActionScript + "\n");} eval(this.submitActionScript); //getQE(this.answerBox).value = applet.[getAnswer](); //FIXME -- not needed in general? - if (debug) {debug_add("Completed submitAction() for applet " + appletName+ "\n");} + if (jsDebugMode) {debug_add("Completed submitAction() for applet " + appletName+ "\n");} }; + ww_applet.prototype.checkLoaded = function() { // this function returns 0 unless: // applet has already been flagged as ready in applet_isReady_list // applet.config is defined (or alias for .config) // applet.setState is defined - // applet.isActive is defined + // applet.isActive is defined and returns 1; // applet reported that it is loaded by calling applet_loaded() var ready = 0; var appletName = this.appletName; var applet = getApplet(appletName); - if (!debug && this.isReady) {return(1)}; // memorize readiness in non-debug mode - debug_add(" Test 4 methods to see if the applet " + appletName + " has been loaded. "); - if ( typeof(applet[this.configAlias]) == "function") { - debug_add( " applet.config method is defined as " + typeof(applet[this.configAlias]) ); + if (!jsDebugMode && this.isReady) {return(1)}; // memorize readiness in non-debug mode + debug_add("Test 4 methods to see if the applet " + appletName + " has been loaded: \n"); + if ( this.methodDefined(this.setConfigAlias) ) { ready = 1; } - if( typeof(applet[this.getStateAlias]) == "function") { - debug_add( " applet.getState method is defined as " + typeof(applet[this.getStateAlias]) ); + if ( this.methodDefined(this.setStateAlias) ) { ready =1; } - if (typeof(applet.isActive) == "function" && applet.isActive ) { - debug_add( " applet.isActive method is defined as " + typeof(applet.isActive) ); - ready =1; + + if ( this.methodDefined("isActive") ) { + if (applet.isActive()) { //this could be zero if applet is loaded, but it is loading auxiliary data. + debug_add( "Applet " +appletName + " signals it is active."); + ready =1; + } else { + debug_add( "Applet " + appletName + " signals it is not active. -- \n it may still be loading data."); + ready = "still_loading"; + } } if (typeof(this.reportsLoaded) !="undefined" && this.reportsLoaded != 0 ) { - debug_add( " " + appletName + " applet self reports that it is loaded " ); + debug_add( " " + appletName + " applet self reports that it has completed loading. " ); ready =1; } this.isReady = ready; @@ -437,5 +487,5 @@ function iamhere() { - return "functions still work"; + alert( "javaScript loaded. functions still work"); } \ No newline at end of file |