Skip to content

Commit e5a4a7c

Browse files
committed
update examples menu, added async loading option
1 parent c67bc39 commit e5a4a7c

File tree

4 files changed

+121
-37
lines changed

4 files changed

+121
-37
lines changed

examples/examples.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<li><a href="#examples/member/badge.html&amp;{&quot;extendapioptions&quot;:&quot;onLoad: onLinkedInLoad&quot;}">Login Badge</a></li>
1515
<li><a href="#examples/member/hover.html">Member Profile Popup</a></li>
1616
<li><a href="#examples/member/inline.html">Inline Profile</a></li>
17+
<li><a href="#examples/member/full_profile.html">Full Profile</a></li>
1718
<li><a href="#examples/member/public_profile.html">Member Data</a></li>
1819
<li><a href="#examples/member/connections.html&amp;{&quot;extendapioptions&quot;:&quot;onLoad: onLinkedInLoad&quot;}">Current User's Connections</a></li>
1920
<!--<li><a href="#examples/member/hoover_integration.html">Full Member Profile</a></li>-->
@@ -30,8 +31,9 @@
3031
<li>
3132
<a href="#example-cat5" class="category">Company Profile</a>
3233
<ul id="example-cat5" class="example-group">
33-
<li><a href="#examples/company_profile/inline.html">Inline</a></li>
34-
<li><a href="#examples/company_profile/hover.html">Popup</a></li>
34+
<li><a href="#examples/company_profile/company_insider.html">Company Insider</a></li>
35+
<li><a href="#examples/company_profile/inline.html">Inline Profile</a></li>
36+
<li><a href="#examples/company_profile/hover.html">Popup Profile</a></li>
3537
</ul>
3638
</li>
3739
<li>
@@ -46,13 +48,13 @@
4648
<li><a href="#examples/recommend/product.html">Recommend a Product</a></li>
4749
</ul>
4850
</li>
49-
<li>
51+
<!--li>
5052
<a href="#example-cat7" class="category">Company Insider</a>
5153
<ul id="example-cat7" class="example-group">
5254
<li><a href="#examples/company_insider/default.html">Default</a></li>
5355
<li><a href="#examples/company_insider/cnn.html">CNN Version</a></li>
5456
</ul>
55-
</li>
57+
</li-->
5658
<li>
5759
<a href="#example-cat9" class="category">People Search</a>
5860
<ul id="example-cat9" class="example-group">

index.html

+18-11
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ <h3><a href="#framework">Framework Options</a></h3>
8282
<li>
8383
<label for="include-buttons" class="inline">Session Buttons:</label>
8484
<div class="fieldgroup checkbox">
85-
<input type="checkbox" id="include-buttons" name="include-buttons" title="include Login/Logout buttons" checked/>
85+
<input type="checkbox" id="include-buttons" name="include-buttons" title="include Login/Logout buttons" checked="checked"/>
86+
</div>
87+
</li>
88+
<li>
89+
<label for="api-async" class="inline">Load Asynchronously:</label>
90+
<div class="fieldgroup checkbox">
91+
<input type="checkbox" id="api-async" name="api-async" title="Load the framework asynchronously"/>
8692
</div>
8793
</li>
8894
</ul>
@@ -118,23 +124,24 @@ <h2 id="title">Code</h2>
118124
<div id="result-container">
119125
<h2>Result</h2>
120126
<div id="sandbox"></div>
121-
<div id="iconbox"><ul id="icons">
122-
<li id="contract-sandbox" title="Contract Sandbox" class="ui-state-default ui-corner-all">
123-
<span class="ui-icon ui-icon-carat-1-n"></span>
124-
</li>
125-
<li id="expand-sandbox" title="Expand Sandbox" class="ui-state-default ui-corner-all">
126-
<span class="ui-icon ui-icon-carat-1-s"></span>
127-
</li>
128-
</ul>
127+
<div id="iconbox">
128+
<ul id="icons">
129+
<li id="contract-sandbox" title="Contract Sandbox" class="ui-state-default ui-corner-all">
130+
<span class="ui-icon ui-icon-carat-1-n"></span>
131+
</li>
132+
<li id="expand-sandbox" title="Expand Sandbox" class="ui-state-default ui-corner-all">
133+
<span class="ui-icon ui-icon-carat-1-s"></span>
134+
</li>
135+
</ul>
129136
</div>
130137
</div>
131138
</div>
132139
</div>
133140
</div>
134141
</div>
135142
<div id="codeview" title="Full Code View"><textarea></textarea></div>
136-
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
137-
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
143+
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
144+
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
138145
<script type="text/javascript" src="js/jquery.cookie.js"></script>
139146
<script type="text/javascript" src="js/jquery.qtip.js"></script>
140147
<script type="text/javascript" src="js/console.js"></script>

js/console.js

+91-17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var $frameworkCustomURL = $("#framework-url-custom");
7474
var $codeConsole = $("#code-console");
7575
var $apiOptions = $("#api-options");
7676
var $apiKey = $("#api-key");
77+
var $apiAsync = $("#api-async");
7778
var $includeButtons = $("#include-buttons");
7879

7980
// a few global-esque vars */
@@ -82,7 +83,6 @@ var saved = {}; // use this to hold cookies/preferences
8283
var tinyURLs = []; // URLs generated by bit.ly (reduce server hits)
8384
var isCustomApiKey = false;
8485

85-
8686
// create editor
8787
var $codeMirror = $("#code-console");
8888
var consoleEditor = {
@@ -166,6 +166,7 @@ function str_split(string, delimiter, limit) {
166166
* @param {Boolean} allowBadOnLoad should onLoad not be verified? (true when Run is executed)
167167
*/
168168
var parseOptions = function( allowBadOnLoad ) {
169+
var async = $apiAsync.is(":checked");
169170
var options = $apiOptions.val();
170171
var newOptions = "";
171172
var processed = [];
@@ -186,6 +187,7 @@ var parseOptions = function( allowBadOnLoad ) {
186187
$.each( options, function(key, value) {
187188
if( value !== "" ) {
188189
var temp = str_split(value.replace(/\s+/g,""), ":", 2); // removes whitespace and splits into key:value pairs
190+
temp[1] = temp[1].replace(/"/g,'');
189191
var skip = false;
190192
/*
191193
* These rules might be a bit confusing, priority matters
@@ -240,12 +242,20 @@ var parseOptions = function( allowBadOnLoad ) {
240242
else {
241243
if( temp[0] === "api_key" ) {
242244
isCustomApiKey = true;
245+
if(temp[1] === "none") {
246+
skip = true;
247+
}
243248
}
244249
processed[temp[0]] = true;
245250
}
246251

247252
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+
}
249259
}
250260
}
251261
});
@@ -262,21 +272,42 @@ var parseOptions = function( allowBadOnLoad ) {
262272
*/
263273
var cleanUpEnvironment = function( dontHideTinyURL ) {
264274
// remove generated sandbox
265-
$("iframe","#sandbox").remove();
266-
275+
//$("iframe","#sandbox").remove();
276+
$sandbox.empty();
267277
$tinyURLContainer.pulse( {duration: "fast"} );
268278

269279
// remove previous error messages
270280
removeAllErrorMessages();
271281
};
272282

273283

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+
274304
/**
275305
* helper function to execute the request
276306
* @method executeCode
277307
* @param {Boolean} allowBadOnLoad passed directly to parseOptions()
278308
*/
279309
var executeCode = function( allowBadOnLoad ) {
310+
var async = $apiAsync.is(":checked");
280311
parseOptions( allowBadOnLoad ); // ensure that option rules are enforced
281312

282313
// fix inconsistent loc.protocol (some browsers will return "http", others return "http:")
@@ -297,21 +328,32 @@ var executeCode = function( allowBadOnLoad ) {
297328
+'"framework":"'+$frameworkSelector.val()+'",'
298329
+'"frameworkurl":"'+frameworkURL+'",'
299330
+'"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
302334
+'}';
303335
// some API key magic
304336
if( (connectURL !== "custom" || frameworkURL === "") && !isCustomApiKey) {
305337
// 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+
}
307344
}
308345
else if( isCustomApiKey ) {
309346
// API Key override (api_key was specified in params)
310347
apiKey = "";
311348
}
312349
else {
313350
//
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+
}
315357
}
316358

317359
var params = apiKey+apiOptions;
@@ -384,26 +426,56 @@ var executeCode = function( allowBadOnLoad ) {
384426
}
385427
});
386428

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+
387464
// build the sandbox iframe
388465
try {
389-
if($includeButtons.is(":checked")) {
390-
runCode += SESSION_BUTTON;
391-
}
392466
// set a window function that will give us the HTML for the sandbox
393467
// it gets overwritten on each run
394468
window.getSandboxHtml = function getSandboxHtml() {
395469
return ['',
396-
'<script type="text/javascript" src="'+connectURL+'">',
397-
params,
398-
'</script>',
470+
theScript,
399471
'\n',
400472
runCode,
401473
'']
402474
.join("\n");
403475
};
404476
// append sandbox iframe
405477
// 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"/>');
407479
}
408480
catch(e) {
409481
throwErrorMessage("error1003","Failed to inject Framework\n"+e);
@@ -580,8 +652,7 @@ var throwErrorMessage = function( id, message, type ) {
580652
if( type === "highlight" ) {
581653
errType = "Warning: ";
582654
iconType = "info";
583-
}
584-
//console.log(errType+message);
655+
}
585656
$errorContainer.show("fast");
586657
if( $errID.length > 0 ) {
587658
$(".error-message", $errID).html(message);
@@ -695,6 +766,9 @@ var restorePreferences = function( saved ) {
695766
if( saved.sessionbuttons !== undef ) {
696767
$includeButtons.attr("checked", saved.sessionbuttons);
697768
}
769+
if( saved.async !== undef ) {
770+
$apiAsync.attr("checked", saved.async);
771+
}
698772
};
699773

700774
// Actual Execution begins here

sandbox.html

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
<head><title>Sandbox</title></head>
44
<body>
55
<script type="text/javascript">
6-
// get the html to write from the parent, and document.write it
7-
// this might well be the only time document.write is acceptable
8-
if(parent.getSandboxHtml) {
9-
document.write(parent.getSandboxHtml());
10-
}
6+
// get the html to write from the parent, and document.write it
7+
// this might well be the only time document.write is acceptable
8+
if(parent.getSandboxHtml) {
9+
var html = parent.getSandboxHtml();
10+
document.write(html);
11+
}
1112
</script>
1213
</body>
1314
</html>

0 commit comments

Comments
 (0)