From 2d132d75479f958c7f3dda21494f335638539a18 Mon Sep 17 00:00:00 2001 From: Michael Dale Date: Sun, 18 Oct 2009 21:03:24 +0000 Subject: [PATCH] * some updates to the api_proxy examples and code * display:none on iframe for ~background~ requests * append to page example added to testApiProxy.html * We could make some optimization by thinning out the MediaWiki:ApiProxy page by replacing it with a simple page without styles & extra js * added a iframe status callback for quicker timeouts --- js2/apiProxyPage.js | 5 +- js2/editPage.js | 9 +- .../libAddMedia/mvBaseUploadInterface.js | 16 +-- js2/mwEmbed/libAddMedia/remoteSearchDriver.js | 12 +- js2/mwEmbed/libMwApi/mw.proxy.js | 120 +++++++++++------- js2/mwEmbed/mv_embed.js | 84 ++++++------ js2/mwEmbed/tests/testApiProxy.html | 84 ++++++++---- js2/remoteMwEmbed.js | 17 ++- js2/uploadPage.js | 9 +- 9 files changed, 216 insertions(+), 140 deletions(-) diff --git a/js2/apiProxyPage.js b/js2/apiProxyPage.js index 46ff0fe317..0619e4abc1 100644 --- a/js2/apiProxyPage.js +++ b/js2/apiProxyPage.js @@ -7,7 +7,6 @@ /* * since this is proxy server set a pre-append debug flag to know which debug msgs are coming from where */ - if( !mwApiProxyConfig ) var mwApiProxyConfig = {}; @@ -18,10 +17,8 @@ var mwApiProxyDefaultConfig = { 'master_blacklist' : [] }; //user_white_list should also be checked and configured at runtime. - js2AddOnloadHook( function() { //build our configuration from the default and mwApiProxyConfig vars - mwApiProxyConfig = $j.extend(true, mwApiProxyDefaultConfig, mwApiProxyConfig); - + mwApiProxyConfig = $j.extend(true, mwApiProxyDefaultConfig, mwApiProxyConfig); $j.apiProxy( 'server', mwApiProxyConfig ); }); diff --git a/js2/editPage.js b/js2/editPage.js index 7775dc0f94..ca393d665e 100644 --- a/js2/editPage.js +++ b/js2/editPage.js @@ -20,13 +20,14 @@ var defaultAddMediaConfig = { 'local_wiki_api_url': wgServer + wgScriptPath + '/api.php' }; - js2AddOnloadHook( function() { var amwConf = $j.extend( true, defaultAddMediaConfig, mwAddMediaConfig ); // kind of tricky, it would be nice to use run on ready "loader" call here + var didWikiEditorBind = false; if( typeof $j.wikiEditor != 'undefined' ) { $j( 'textarea#wpTextbox1' ).bind( 'wikiEditor-toolbar-buildSection-main', function( e, section ) { + didWikiEditorBind = true; if ( typeof section.groups.insert.tools.file !== 'undefined' ) { section.groups.insert.tools.file.action = { 'type': 'callback', @@ -47,6 +48,10 @@ js2AddOnloadHook( function() { $j( '#btn-add-media-wiz' ).addMediaWiz( amwConf ); + }else{ + //make sure the wikieditor got binded: + if( !didWikiEditorBind ) + $j(".tool [rel='file']").addMediaWiz( amwConf ); } - },100) + }, 120) }); diff --git a/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js b/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js index 3417e3de67..f7a89d7463 100644 --- a/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js +++ b/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js @@ -252,15 +252,9 @@ mvBaseUploadInterface.prototype = { 'url' : $j('#wpUploadFileURL').val(), 'filename' : $j('#wpDestFile').val(), 'comment' : $j('#wpUploadDescription').val(), - 'watch' : ($j('#wpWatchthis').is(':checked'))?'true':'false' - } - //set up ignore warnings and watch arguments: - if( $j('#wpIgnoreWarning').is(':checked') ){ - httpUpConf[ 'ignorewarnings'] = 'true'; - } - if( $j('#wpWatchthis').is(':checked') ){ - httpUpConf[ 'watch'] = 'true'; - } + 'watch' : ($j('#wpWatchthis').is(':checked'))?'true':'false', + 'ignorewarnings': ($j('#wpIgnoreWarning').is(':checked'))?'true':'false' + } //check for editToken _this.etoken = $j("#wpEditToken").val(); _this.doHttpUpload( httpUpConf ); @@ -316,9 +310,7 @@ mvBaseUploadInterface.prototype = { 'asyncdownload' : true //do async download }; //set config from options: - for(var i in opt){ - req[i]= opt[i]; - } + $j.extend(req, opt); //else try and get a token: if(!_this.etoken && _this.api_url){ diff --git a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js index 7e1f588ca6..49a3fe9c19 100644 --- a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js +++ b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js @@ -412,7 +412,7 @@ remoteSearchDriver.prototype = { } } //set the upload target name if unset - if( _this.upload_api_target == 'local' && ! _this.upload_api_name && wgSiteName) + if( _this.upload_api_target == 'local' && ! _this.upload_api_name && typeof wgSiteName != 'undefined') _this.upload_api_name = wgSiteName; @@ -737,7 +737,7 @@ remoteSearchDriver.prototype = { } }, //issue a api request & cache the result - //this check can be avoided by setting the this.import_url_mode = 'api' | 'form' | insted of 'autodetect' or 'none' + //this check can be avoided by setting the this.import_url_mode = 'api' | 'form' | instead of 'autodetect' or 'none' checkForCopyURLSupport:function ( callback ){ var _this = this; js_log('checkForCopyURLSupport:: '); @@ -961,7 +961,7 @@ remoteSearchDriver.prototype = { 'data':reqObj, 'url':this.local_wiki_api_url }, function(data){ - //@@todo propogate the rObj + //@@todo propagate the rObj var rObj = {}; } ); @@ -1240,7 +1240,7 @@ remoteSearchDriver.prototype = { }) }, - /*set-up the control actions for clipEdit with relevent callbacks */ + /*set-up the control actions for clipEdit with relevant callbacks */ getClipEditControlActions:function( cp ){ var _this = this; var cConf= {}; @@ -1494,7 +1494,7 @@ remoteSearchDriver.prototype = { js_log("do preview asset"); //load the preview text: _this.getParsedWikiText( $j('#rsd_import_ta').val(), _this.cFileNS +':'+ rObj.target_resource_title, function( o ){ - js_log('got updated preivew: '); + js_log('got updated preview: '); $j('#rsd_import_desc').html(o); }); }); @@ -1540,7 +1540,7 @@ remoteSearchDriver.prototype = { { 'server_frame' : _this.upload_api_proxy_frame, },function(){ - //now that the api is setup call actual import + //now that the api loaded call actual import debugger; } ); diff --git a/js2/mwEmbed/libMwApi/mw.proxy.js b/js2/mwEmbed/libMwApi/mw.proxy.js index 86f673e6c2..a71ae49993 100644 --- a/js2/mwEmbed/libMwApi/mw.proxy.js +++ b/js2/mwEmbed/libMwApi/mw.proxy.js @@ -36,7 +36,13 @@ */ loadGM({ - "mwe-setting-up-proxy": "Setting up proxy" + "mwe-setting-up-proxy" : "Setting up proxy", + "mwe-re-try" : "Retry Api request", + "mwe-re-trying": "Retrying api request", + "mwe-cancel" : "Cancel", + "mwe-proxy-not-ready": "Proxy is not configured", + "mwe-please-login" : "Please loggin and or enable mwEmbed and retry the request", + "mwe-remember-loging": "As a genneral sequirty reminder. Only login to web sites when your address bar displayes that sites address" }); (function( $ ) { @@ -47,11 +53,9 @@ loadGM({ $.proxy = {}; /** - * The clientApiProxy handles a request to external server - * - * This is (Domain A) in the above described setup + * The client setup function: */ - $.proxy.client = function( pConf, callback ){ + $.proxy.client = function( pConf, conf ){ var _this = this; //do setup: if( pConf.server_frame ) @@ -59,20 +63,15 @@ loadGM({ if( pConf.client_frame_path ) $.proxy.client_frame_path = pConf.client_frame_path; - - //setup a dialog to manage proxy setup: - $j.addLoaderDialog( gM('mwe-setting-up-proxy') ); - + if( parseUri( $.proxy.server_frame).host == parseUri( document.URL ).host ){ - js_log("Error: why are you trying to proxy yourself? " ); - return false; - } - //add an iframe to domain B with request for proxy just do the setup - $.proxy.doFrameProxy( { 'init' : 'echo' } ); - - //if we have a setup callback - $.proxy.callback = callback; + js_log("Error: why are you trying to proxy yourself? " ); + return false; + } + return true; } + //set the frameProxy Flag: + var frameProxyOk = false; /* setup a iframe request hash */ $.proxy.doFrameProxy = function( reqObj ){ var hashPack = { @@ -85,15 +84,47 @@ loadGM({ //we can't update src's so we have to remove and add all the time :( //@@todo we should support frame msg system $j('#frame_proxy').remove(); - $j('body').append('' ); + + //add an onLoad hook: + $j('#frame_proxy').get(0).onload = function(){ + //add a 5 second timeout for setting up the nested child callback (after page load) + setTimeout(function(){ + if( !frameProxyOk ){ + //we timmed out no api proxy (should make sure the user is "logged in") + //for now let the implementation call proxyNotReadyDialog + $.proxy.callback ( false ); + } + }, 5000); + } } - + $.proxy.proxyNotReadyDialog = function(){ + var btn = {}; + btn[ gM('mwe-re-try') ] = function(){ + $j.addLoaderDialog( gM('mwe-re-trying') ); + $.proxy.doFrameProxy( lastApiReq ); + } + btn[ gM('mwe-cancel') ] = function(){ + $j.closeLoaderDialog(); + } + var pUri = parseUri( $.proxy.server_frame); + //this is sort of a temporary hack if we change the MediaWiki:ApiProxy key + //we will have to deal with that here as well: + var login_url = pUri.protocol +'://'+ pUri.host + + pUri.path.replace( 'MediaWiki:ApiProxy', 'Special:UserLogin'); + $j.addDialog( gM('mwe-proxy-not-ready'), gM('mwe-please-login', login_url ) + + '

' + gM('mwe-remember-loging') + '

', + btn + ) + } + var lastApiReq = {}; /* the do_api_request with callback: */ $.proxy.doRequest = function( reqObj, callback){ js_log("doRequest:: " + JSON.stringify( reqObj ) ); + lastApiReq = reqObj; //setup the callback: $.proxy.callback = callback; //do the proxy req: @@ -103,16 +134,18 @@ loadGM({ * The nested iframe action that passes its msg back up to the top instance */ $.proxy.nested = function( hashResult ){ - js_log( 'got $.proxy.nested callback!! :: ' + hashResult ); + //js_log( '$.proxy.nested callback :: ' + hashResult ); + frameProxyOk = true; //try to parse the hash result: try{ var rObj = JSON.parse( unescape( hashResult ) ); }catch (e) { js_log("Error could not parse hashResult"); } - //hide the loader if the initial state = ready msg is fired: - if( rObj && rObj.state == 'ready') - $j.closeLoaderDialog(); + //special callback to frameProxyOk flag (not an api result, don't call callback) + if( rObj.state == 'ok') + return ; + //if all good pass it to the callback: $.proxy.callback( rObj ); } @@ -149,7 +182,7 @@ loadGM({ return false; } - var domain = parseUri( document.URL ).host; + var domain = aObj.cd; var nested_frame_src = 'http://' + aObj.cd + aObj.cfp; //check the master whitelist for(var i in pConf.master_whitelist){ @@ -174,30 +207,31 @@ loadGM({ });*/ //if still not found: - js_log("domain " + domain + " not approved"); + js_log("domain " + domain + " not approved"); + + //offer the user the ability to "approve" requested domain save to their user page function doNestedProxy( reqObj ){ js_log("doNestedProxy to: " + nested_frame_src); - var outputhash = escape( JSON.stringify( reqObj ) ); - //check if its just a "echo" request (no need to hit the api) - if( reqObj.init && reqObj.init == 'echo'){ - doNestedFrame( {'state': 'ready', "echo": true } ); - }else{ - //add some api stuff: - reqObj['format'] = 'json'; - //process the api request - $j.get(wgScriptPath + '/api' + wgScriptExtension, - reqObj, - function( data ){ - js_log("Proxy GOT Res: " + data ); - //put it into the nested frame hash string: - doNestedFrame( JSON.parse( data ) ); - } - ); - } + //output iframe prior to running api request + //(so we can fail faster / cache the nested page / simotaniusly load things) + doNestedFrame ( 'nested_ok' , {'state':'ok'}); + + var outputhash = escape( JSON.stringify( reqObj ) ); + //add some api stuff: + reqObj['format'] = 'json'; + //process the api request + $j.post(wgScriptPath + '/api' + wgScriptExtension, + reqObj, + function( data ){ + //js_log("Proxy GOT Res: " + data ); + //put it into the nested frame hash string: + doNestedFrame( 'nested_push', JSON.parse( data ) ); + } + ); } //add the doNestedFrame iframe: - function doNestedFrame( resultObj ){ + function doNestedFrame( nestname, resultObj ){ $j('#nested_push').remove(); //setup the nested proxy that points back to top domain: $j('body').append( '