@@ -74,6 +74,7 @@ var $frameworkCustomURL = $("#framework-url-custom");
74
74
var $codeConsole = $ ( "#code-console" ) ;
75
75
var $apiOptions = $ ( "#api-options" ) ;
76
76
var $apiKey = $ ( "#api-key" ) ;
77
+ var $apiAsync = $ ( "#api-async" ) ;
77
78
var $includeButtons = $ ( "#include-buttons" ) ;
78
79
79
80
// a few global-esque vars */
@@ -82,7 +83,6 @@ var saved = {}; // use this to hold cookies/preferences
82
83
var tinyURLs = [ ] ; // URLs generated by bit.ly (reduce server hits)
83
84
var isCustomApiKey = false ;
84
85
85
-
86
86
// create editor
87
87
var $codeMirror = $ ( "#code-console" ) ;
88
88
var consoleEditor = {
@@ -166,6 +166,7 @@ function str_split(string, delimiter, limit) {
166
166
* @param {Boolean } allowBadOnLoad should onLoad not be verified? (true when Run is executed)
167
167
*/
168
168
var parseOptions = function ( allowBadOnLoad ) {
169
+ var async = $apiAsync . is ( ":checked" ) ;
169
170
var options = $apiOptions . val ( ) ;
170
171
var newOptions = "" ;
171
172
var processed = [ ] ;
@@ -186,6 +187,7 @@ var parseOptions = function( allowBadOnLoad ) {
186
187
$ . each ( options , function ( key , value ) {
187
188
if ( value !== "" ) {
188
189
var temp = str_split ( value . replace ( / \s + / g, "" ) , ":" , 2 ) ; // removes whitespace and splits into key:value pairs
190
+ temp [ 1 ] = temp [ 1 ] . replace ( / " / g, '' ) ;
189
191
var skip = false ;
190
192
/*
191
193
* These rules might be a bit confusing, priority matters
@@ -240,12 +242,20 @@ var parseOptions = function( allowBadOnLoad ) {
240
242
else {
241
243
if ( temp [ 0 ] === "api_key" ) {
242
244
isCustomApiKey = true ;
245
+ if ( temp [ 1 ] === "none" ) {
246
+ skip = true ;
247
+ }
243
248
}
244
249
processed [ temp [ 0 ] ] = true ;
245
250
}
246
251
247
252
if ( ! skip ) {
248
- newOptions += temp [ 0 ] + ": " + temp [ 1 ] + "\n" ;
253
+ if ( async ) {
254
+ newOptions += temp [ 0 ] + ": \"" + temp [ 1 ] + "\"\n" ;
255
+ }
256
+ else {
257
+ newOptions += temp [ 0 ] + ": " + temp [ 1 ] + "\n" ;
258
+ }
249
259
}
250
260
}
251
261
} ) ;
@@ -262,21 +272,42 @@ var parseOptions = function( allowBadOnLoad ) {
262
272
*/
263
273
var cleanUpEnvironment = function ( dontHideTinyURL ) {
264
274
// remove generated sandbox
265
- $ ( "iframe" , "#sandbox" ) . remove ( ) ;
266
-
275
+ // $("iframe","#sandbox").remove();
276
+ $sandbox . empty ( ) ;
267
277
$tinyURLContainer . pulse ( { duration : "fast" } ) ;
268
278
269
279
// remove previous error messages
270
280
removeAllErrorMessages ( ) ;
271
281
} ;
272
282
273
283
284
+
285
+ /**
286
+ * helper function to prepend tabs to the parameters
287
+ * makes it look prettier
288
+ * @method formatParams
289
+ * @param {String } params
290
+ * @param {String } tabs
291
+ */
292
+ var formatParams = function ( params , tabs , jstring ) {
293
+ params = params . split ( '\n' ) ;
294
+ $ . each ( params , function ( i ) {
295
+ if ( params [ i ] !== '' ) {
296
+ params [ i ] = tabs + params [ i ] ;
297
+ }
298
+ } ) ;
299
+ return params . join ( jstring ) ;
300
+ } ;
301
+
302
+
303
+
274
304
/**
275
305
* helper function to execute the request
276
306
* @method executeCode
277
307
* @param {Boolean } allowBadOnLoad passed directly to parseOptions()
278
308
*/
279
309
var executeCode = function ( allowBadOnLoad ) {
310
+ var async = $apiAsync . is ( ":checked" ) ;
280
311
parseOptions ( allowBadOnLoad ) ; // ensure that option rules are enforced
281
312
282
313
// fix inconsistent loc.protocol (some browsers will return "http", others return "http:")
@@ -297,21 +328,32 @@ var executeCode = function( allowBadOnLoad ) {
297
328
+ '"framework":"' + $frameworkSelector . val ( ) + '",'
298
329
+ '"frameworkurl":"' + frameworkURL + '",'
299
330
+ '"apikey":"' + apiKey + '",'
300
- + '"apioptions":"' + apiOptions . replace ( / \n / g, "\\n" ) + '",' // we need to escape newlines for valid JSON
301
- + '"sessionbuttons":' + $includeButtons . is ( ":checked" ) // note: this is boolean, no quotes
331
+ + '"apioptions":"' + apiOptions . replace ( / \n / g, "\\n" ) . replace ( / " / g, '' ) + '",' // we need to escape newlines and remove quotes for valid JSON
332
+ + '"sessionbuttons":' + $includeButtons . is ( ":checked" ) + ',' // note: this is boolean, no quotes
333
+ + '"async":' + async // note: this is boolean, no quotes
302
334
+ '}' ;
303
335
// some API key magic
304
336
if ( ( connectURL !== "custom" || frameworkURL === "" ) && ! isCustomApiKey ) {
305
337
// default to the Connect API Key
306
- apiKey = "api_key: " + CONNECT_API_KEY + "\n" ;
338
+ if ( async ) {
339
+ apiKey = "api_key: \"" + CONNECT_API_KEY + "\"\n" ;
340
+ }
341
+ else {
342
+ apiKey = "api_key: " + CONNECT_API_KEY + "\n" ;
343
+ }
307
344
}
308
345
else if ( isCustomApiKey ) {
309
346
// API Key override (api_key was specified in params)
310
347
apiKey = "" ;
311
348
}
312
349
else {
313
350
//
314
- apiKey = "api_key: " + apiKey + "\n" ;
351
+ if ( async ) {
352
+ apiKey = "api_key: \"" + apiKey + "\"\n" ;
353
+ }
354
+ else {
355
+ apiKey = "api_key: " + apiKey + "\n" ;
356
+ }
315
357
}
316
358
317
359
var params = apiKey + apiOptions ;
@@ -384,26 +426,56 @@ var executeCode = function( allowBadOnLoad ) {
384
426
}
385
427
} ) ;
386
428
429
+
430
+ var theScript = 'testing' ;
431
+ var tabs = async ? ' ' : ' ' ;
432
+ var jstring = async ? ',\n' : '\n' ;
433
+ params = formatParams ( params , tabs , jstring ) ;
434
+ if ( async ) {
435
+ params = params . slice ( 0 , - 2 ) ; // remove last comma and linebreak
436
+ }
437
+ else {
438
+ if ( async ) {
439
+ params = params . slice ( 0 , - 1 ) ; // remove last comma
440
+ }
441
+ }
442
+ if ( $includeButtons . is ( ":checked" ) ) {
443
+ runCode += SESSION_BUTTON ;
444
+ }
445
+ // manipulate the code to load sync or async
446
+ if ( async ) {
447
+ theScript = [ '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>' ,
448
+ '<script type="text/javascript">' ,
449
+ '$(function() {' ,
450
+ ' $.getScript("' + connectURL + '?async=true", function success() {' ,
451
+ ' IN.init({' ,
452
+ params ,
453
+ ' });' ,
454
+ ' });' ,
455
+ '});' ,
456
+ '</script>' ] . join ( '\n' ) ;
457
+ }
458
+ else {
459
+ theScript = [ '<script type="text/javascript" src="' + connectURL + '">' ,
460
+ params ,
461
+ '</script>' ] . join ( '\n' ) ;
462
+ }
463
+
387
464
// build the sandbox iframe
388
465
try {
389
- if ( $includeButtons . is ( ":checked" ) ) {
390
- runCode += SESSION_BUTTON ;
391
- }
392
466
// set a window function that will give us the HTML for the sandbox
393
467
// it gets overwritten on each run
394
468
window . getSandboxHtml = function getSandboxHtml ( ) {
395
469
return [ '' ,
396
- '<script type="text/javascript" src="' + connectURL + '">' ,
397
- params ,
398
- '</script>' ,
470
+ theScript ,
399
471
'\n' ,
400
472
runCode ,
401
473
'' ]
402
474
. join ( "\n" ) ;
403
475
} ;
404
476
// append sandbox iframe
405
477
// the sandbox will actually run the code
406
- $sandbox . append ( '<iframe id="sandboxrunner" src="sandbox.html">' ) ;
478
+ $ ( "# sandbox" ) . append ( '<iframe id="sandboxrunner" src="sandbox.html"/ >' ) ;
407
479
}
408
480
catch ( e ) {
409
481
throwErrorMessage ( "error1003" , "Failed to inject Framework\n" + e ) ;
@@ -580,8 +652,7 @@ var throwErrorMessage = function( id, message, type ) {
580
652
if ( type === "highlight" ) {
581
653
errType = "Warning: " ;
582
654
iconType = "info" ;
583
- }
584
- //console.log(errType+message);
655
+ }
585
656
$errorContainer . show ( "fast" ) ;
586
657
if ( $errID . length > 0 ) {
587
658
$ ( ".error-message" , $errID ) . html ( message ) ;
@@ -695,6 +766,9 @@ var restorePreferences = function( saved ) {
695
766
if ( saved . sessionbuttons !== undef ) {
696
767
$includeButtons . attr ( "checked" , saved . sessionbuttons ) ;
697
768
}
769
+ if ( saved . async !== undef ) {
770
+ $apiAsync . attr ( "checked" , saved . async ) ;
771
+ }
698
772
} ;
699
773
700
774
// Actual Execution begins here
0 commit comments