From 58ee5ce033d8a1b6a3afc63f1398d0b28494e453 Mon Sep 17 00:00:00 2001 From: Carl Eberhard Date: Thu, 29 Nov 2012 13:32:44 -0500 Subject: [PATCH] LazyDataLoader.js: now handles NaN/Infinity/-Infinity as null, allows overriding jQuery's datatype converters; controllers/root: added echo_json for debugging/testing --- lib/galaxy/webapps/galaxy/controllers/root.py | 19 ++++++++++++++++++ static/scripts/packed/utils/LazyDataLoader.js | 2 +- static/scripts/utils/LazyDataLoader.js | 20 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/webapps/galaxy/controllers/root.py b/lib/galaxy/webapps/galaxy/controllers/root.py index 24a8582da2b..f7b54e5726f 100644 --- a/lib/galaxy/webapps/galaxy/controllers/root.py +++ b/lib/galaxy/webapps/galaxy/controllers/root.py @@ -531,6 +531,25 @@ class RootController( BaseUIController, UsesHistoryMixin, UsesAnnotations ): rval += "-> %s" % kwd[k].file.read() return rval + @web.json + def echo_json( self, trans, **kwd ): + """Echos parameters as JSON (debugging) + + Attempts to parse values passed as boolean, float, then int. Defaults + to string. Non-recursive (will not parse lists). + """ + rval = {} + for k in kwd: + rval[ k ] = kwd[k] + try: + if rval[ k ] in [ 'true', 'True', 'false', 'False' ]: + rval[ k ] = util.string_as_bool( rval[ k ] ) + rval[ k ] = float( rval[ k ] ) + rval[ k ] = int( rval[ k ] ) + except: + pass + return rval + @web.expose def generate_error( self, trans ): raise Exception( "Fake error!" ) diff --git a/static/scripts/packed/utils/LazyDataLoader.js b/static/scripts/packed/utils/LazyDataLoader.js index e5c07f8d28a..fb0c27cd8bf 100644 --- a/static/scripts/packed/utils/LazyDataLoader.js +++ b/static/scripts/packed/utils/LazyDataLoader.js @@ -1 +1 @@ -function LazyDataLoader(c){var a=this,d="loaded.new",b="complete";ERROR_EVENT="error";jQuery.extend(a,LoggableMixin);jQuery.extend(a,{total:undefined,url:undefined,currentIntervalId:undefined,data:[],delay:4000,start:0,size:4000,initialize:function(e){jQuery.extend(a,e);if(e.hasOwnProperty("initialize")){e.initialize.call(a,e)}this.log(this+" initialized:",a)},buildUrl:function(f,e){return this.url+"&"+jQuery.param({start_val:f,max_vals:e})},ajaxErrorFn:function(g,e,f){},load:function(h){this.log(this+".load");if(!a.url){throw (a+" requires a url")}if(this.total===null){this.log("\t total is null (will load all)")}else{this.log("\t total:",this.total)}var g=a.size;if((a.total!==null)&&(a.total0){a.currentIntervalId=setTimeout(function(){f(m,n)},a.delay);a.log("\t currentIntervalId:",a.currentIntervalId)}else{e()}}else{e()}}})}function e(){a.log(a+".loadHelper, has finished:",a.data);$(a).trigger(b,[a.data,a.total]);if(h){h(a.data)}}},toString:function(){return"LazyDataLoader"}});a.initialize(c);return a}; \ No newline at end of file +function LazyDataLoader(c){var a=this,d="loaded.new",b="complete";ERROR_EVENT="error";jQuery.extend(a,LoggableMixin);jQuery.extend(a,{total:undefined,url:undefined,currentIntervalId:undefined,data:[],delay:4000,start:0,size:4000,initialize:function(e){jQuery.extend(a,e);if(e.hasOwnProperty("initialize")){e.initialize.call(a,e)}this.log(this+" initialized:",a)},buildUrl:function(f,e){return this.url+"&"+jQuery.param({start_val:f,max_vals:e})},ajaxErrorFn:function(g,e,f){console.error("ERROR fetching data:",f)},converters:{"* text":window.String,"text html":true,"text xml":jQuery.parseXML,"text json":function(e){e=e.replace(/NaN/g,"null");e=e.replace(/-Infinity/g,"null");e=e.replace(/Infinity/g,"null");return jQuery.parseJSON(e)}},load:function(h){this.log(this+".load");if(!a.url){throw (a+" requires a url")}if(this.total===null){this.log("\t total is null (will load all)")}else{this.log("\t total:",this.total)}var g=a.size;if((a.total!==null)&&(a.total0){a.currentIntervalId=setTimeout(function(){f(m,n)},a.delay);a.log("\t currentIntervalId:",a.currentIntervalId)}else{e()}}else{e()}}})}function e(){a.log(a+".loadHelper, has finished:",a.data);$(a).trigger(b,[a.data,a.total]);if(h){h(a.data)}}},toString:function(){return"LazyDataLoader"}});a.initialize(c);return a}; \ No newline at end of file diff --git a/static/scripts/utils/LazyDataLoader.js b/static/scripts/utils/LazyDataLoader.js index 227b56eb929..374f0beedec 100644 --- a/static/scripts/utils/LazyDataLoader.js +++ b/static/scripts/utils/LazyDataLoader.js @@ -97,7 +97,6 @@ function LazyDataLoader( config ){ if( config.hasOwnProperty( 'initialize' ) ){ config.initialize.call( loader, config ); } - this.log( this + ' initialized:', loader ); }, @@ -115,6 +114,23 @@ function LazyDataLoader( config ){ //OVERRIDE: to handle ajax errors differently ajaxErrorFn : function( xhr, status, error ){ + console.error( 'ERROR fetching data:', error ); + }, + + // converters passed to the jQuery ajax call for data type parsing + //OVERRIDE: to provide custom parsing + converters : { + '* text' : window.String, + 'text html' : true, + 'text xml' : jQuery.parseXML, + + // add NaN, inf, -inf handling to jquery json parser (by default) + 'text json' : function( json ){ + json = json.replace( /NaN/g, 'null' ); + json = json.replace( /-Infinity/g, 'null' ); + json = json.replace( /Infinity/g, 'null' ); + return jQuery.parseJSON( json ); + } }, // interface to begin load (and first recursive call) @@ -149,7 +165,9 @@ function LazyDataLoader( config ){ jQuery.ajax({ url : loader.buildUrl( start, size ), + converters : loader.converters, dataType : 'json', + error : function( xhr, status, error ){ loader.log( '\t ajax error, status:', status, 'error:', error ); if( loader.currentIntervalId ){