Index: branches/js2-work/phase3/js/mwEmbed/modules/ApiProxy/mw.ApiProxy.js |
— | — | @@ -8,9 +8,10 @@ |
9 | 9 | * and a central blacklisting of domains controlled by the site |
10 | 10 | * |
11 | 11 | * if the browser supports it we should pass msgs with postMessage API |
12 | | -* http://ejohn.org/blog/cross-window-messaging/ |
| 12 | +* http://ejohn.org/blog/cross-window-messaging/ |
13 | 13 | * |
14 | | -* NOTE: it would be nice if this supported multiple proxy targets (ie to a bright widgets future) |
| 14 | +* NOTE: refactor efforts will include separating out "core" proxy functions and |
| 15 | +* having a separate class for "server" and "client" api usage |
15 | 16 | * |
16 | 17 | */ |
17 | 18 | |
— | — | @@ -24,8 +25,6 @@ |
25 | 26 | } ); |
26 | 27 | |
27 | 28 | |
28 | | - |
29 | | - |
30 | 29 | /** |
31 | 30 | * apiProxy jQuery binding |
32 | 31 | * |
— | — | @@ -76,6 +75,7 @@ |
77 | 76 | * @param {Function} callback Function called once the request is complete |
78 | 77 | */ |
79 | 78 | $.doRequest = function( apiUrl, requestQuery, callback ) { |
| 79 | + |
80 | 80 | // Reset local vars: |
81 | 81 | proxyCallback = false; |
82 | 82 | frameProxyOk = false; |
— | — | @@ -87,10 +87,16 @@ |
88 | 88 | } |
89 | 89 | |
90 | 90 | mw.log( "doRequest:: " + JSON.stringify( requestQuery ) ); |
| 91 | + |
| 92 | + // Set local scope current request |
| 93 | + // ( presently the api proxy only support sequential requests |
| 94 | + // for multiple simultaneous requests we will need to do some minor refactoring ) |
91 | 95 | currentApiReq = requestQuery; |
92 | 96 | currentApiUrl = apiUrl; |
| 97 | + |
93 | 98 | // Setup the callback: |
94 | 99 | proxyCallback = callback; |
| 100 | + |
95 | 101 | // Do the proxy req: |
96 | 102 | doFrameProxy( requestQuery ); |
97 | 103 | } |
— | — | @@ -105,6 +111,7 @@ |
106 | 112 | $.nested = function( hashResult ) { |
107 | 113 | // Close the loader if present: |
108 | 114 | mw.closeLoaderDialog(); |
| 115 | + |
109 | 116 | mw.log( '$.proxy.nested callback :: ' + unescape( hashResult ) ); |
110 | 117 | frameProxyOk = true; |
111 | 118 | |
— | — | @@ -193,7 +200,7 @@ |
194 | 201 | |
195 | 202 | // Setup the proxy callback to display the upload unhide the iframe upload form |
196 | 203 | proxyCallback = function( iframeData ){ |
197 | | - //proccess fileBrowse callbacks:: |
| 204 | + // proccess fileBrowse callbacks:: |
198 | 205 | |
199 | 206 | // check for basic status "ok" |
200 | 207 | if( iframeData['status'] == 'ok' ){ |
— | — | @@ -212,13 +219,9 @@ |
213 | 220 | break; |
214 | 221 | default: |
215 | 222 | mw.log(" Error unreconginzed event " + iframeData['event'] ); |
216 | | - } |
217 | | - |
218 | | - } |
219 | | - |
220 | | - |
221 | | - } |
222 | | - |
| 223 | + } |
| 224 | + } |
| 225 | + } |
223 | 226 | } |
224 | 227 | |
225 | 228 | /** |
— | — | @@ -238,15 +241,7 @@ |
239 | 242 | // Inform the client frame that we passed validation |
240 | 243 | sendClientMsg( { 'state':'ok' } ); |
241 | 244 | |
242 | | - // Process request type |
243 | | - if( clientRequest['browseFile'] ){ |
244 | | - mw.log( "DO BROWSE FILE" ); |
245 | | - serverBrowseFile( proxyConfig ); |
246 | | - return ; |
247 | | - } |
248 | | - // Else do a normal api request : |
249 | | - return doApiRequest(); |
250 | | - |
| 245 | + return serverHandleRequest(); |
251 | 246 | } |
252 | 247 | |
253 | 248 | /** |
— | — | @@ -325,7 +320,7 @@ |
326 | 321 | |
327 | 322 | setTimeout( function() { |
328 | 323 | if ( !frameProxyOk ) { |
329 | | - // we timmed out no api proxy (should make sure the user is "logged in") |
| 324 | + // We timmed out no api proxy (should make sure the user is "logged in") |
330 | 325 | mw.log( "Error:: api proxy timeout are we logged in? mwEmbed is on?" ); |
331 | 326 | proxyNotReadyDialog(); |
332 | 327 | } |
— | — | @@ -335,7 +330,7 @@ |
336 | 331 | |
337 | 332 | /** |
338 | 333 | * Validate an iframe request |
339 | | - * checks the url hash for required paramaters |
| 334 | + * checks the url hash for required parameters |
340 | 335 | * checks master_blacklist |
341 | 336 | * checks master_whitelist |
342 | 337 | */ |
— | — | @@ -365,8 +360,7 @@ |
366 | 361 | * HERE WE CHECK IF THE DOMAIN IS ALLOWED per the proxyConfig |
367 | 362 | */ |
368 | 363 | return isAllowedClientFrame( clientRequest.clientFrame ); |
369 | | - |
370 | | - |
| 364 | + |
371 | 365 | // Not a valid request return false |
372 | 366 | } |
373 | 367 | |
— | — | @@ -430,7 +424,8 @@ |
431 | 425 | var pUri = mw.parseUri( getServerFrame() ); |
432 | 426 | |
433 | 427 | // FIXME we should have a Hosted page once we deploy mwEmbed on the servers. |
434 | | - // A hosted page would be much faster since than a normal page view rewrite |
| 428 | + // A hosted page would be much faster since it would not have to load all the |
| 429 | + // normal page view assets prior to being rewrite for api proxy usage. |
435 | 430 | |
436 | 431 | var login_url = pUri.protocol + '://' + pUri.host; |
437 | 432 | login_url += pUri.path.replace( 'MediaWiki:ApiProxy', 'Special:UserLogin' ); |
— | — | @@ -454,18 +449,31 @@ |
455 | 450 | * Handles the server side proxy of requests |
456 | 451 | * it adds child frames pointing to the parent "blank" frames |
457 | 452 | */ |
458 | | - |
| 453 | + |
| 454 | + /** |
| 455 | + * serverHandleRequest handle a given request from the client |
| 456 | + * maps the request to serverBrowseFile or serverApiRequest |
| 457 | + */ |
| 458 | + function serverHandleRequest(){ |
| 459 | + var clientRequest = getClientRequest(); |
| 460 | + // Process request type |
| 461 | + if( clientRequest['browseFile'] ){ |
| 462 | + serverBrowseFile(); |
| 463 | + return true; |
| 464 | + } |
| 465 | + // Else do a normal api request : |
| 466 | + return serverApiRequest(); |
| 467 | + } |
459 | 468 | /** |
460 | 469 | * Api iFrame request: |
461 | | - * @param {Object} requestObj Api request object |
462 | 470 | */ |
463 | | - function doApiRequest( ) { |
| 471 | + function serverApiRequest( ) { |
464 | 472 | // Get the client request |
465 | 473 | var clientRequest = getClientRequest(); |
466 | 474 | // Make sure its a json format |
467 | 475 | clientRequest.request[ 'format' ] = 'json'; |
468 | 476 | |
469 | | - // Process the API request. We don't use mw.apiReq since we need to "post" |
| 477 | + // Process the API request. We don't use mw.getJSON since we need to "post" |
470 | 478 | $j.post( wgScriptPath + '/api' + wgScriptExtension, |
471 | 479 | clientRequest.request, |
472 | 480 | function( data ) { |
— | — | @@ -480,7 +488,9 @@ |
481 | 489 | * |
482 | 490 | * Sets the page content to browser file |
483 | 491 | */ |
484 | | - function serverBrowseFile( proxyConfig ){ |
| 492 | + function serverBrowseFile( ){ |
| 493 | + // Get the proxy config |
| 494 | + var proxyConfig = mw.getConfig( 'apiProxyConfig' ); |
485 | 495 | //check for fw ( file width ) |
486 | 496 | if( ! proxyConfig.fileWidth ){ |
487 | 497 | proxyConfig.fileWidth = 130; |
— | — | @@ -576,7 +586,7 @@ |
577 | 587 | |
578 | 588 | // After the nested frame is done loading schedule its removal |
579 | 589 | $j( '#' + nestName ).get( 0 ).onload = function() { |
580 | | - // use a settimeout to give time for client frame to propogate update. |
| 590 | + // Use a settimeout to give time for client frame to propagate update. |
581 | 591 | setTimeout( function(){ |
582 | 592 | $j('#' + nestName ).remove(); |
583 | 593 | }, 10 ); |