From d920d8d8493635ff91194a28332b904dbf819214 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Tue, 30 Jul 2013 14:22:14 +1000 Subject: [PATCH] doc: don't block page load on JavaScript fetch R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/12050045 --- doc/codewalk/codewalk.js | 2 +- doc/godocs.js | 63 ++++++++++++++++++++++++++++++++++++++++ doc/root.html | 2 +- lib/godoc/godoc.html | 19 +++++------- lib/godoc/package.html | 50 ------------------------------- 5 files changed, 72 insertions(+), 64 deletions(-) diff --git a/doc/codewalk/codewalk.js b/doc/codewalk/codewalk.js index eb4667ac0c0..7bfcd393841 100644 --- a/doc/codewalk/codewalk.js +++ b/doc/codewalk/codewalk.js @@ -296,7 +296,7 @@ CodewalkViewer.prototype.updateHeight = function() { this.sizer.height(codeHeight); }; -jQuery(document).ready(function() { +window.initFuncs.push(function() { var viewer = new CodewalkViewer(jQuery('#codewalk-main')); viewer.selectFirstComment(); viewer.targetCommentLinksAtBlank(); diff --git a/doc/godocs.js b/doc/godocs.js index d8245104766..8003a1fe8cc 100644 --- a/doc/godocs.js +++ b/doc/godocs.js @@ -163,6 +163,52 @@ function setupDropdownPlayground() { $('#menu').css('min-width', '+=60'); } +function setupInlinePlayground() { + 'use strict'; + // Set up playground when each element is toggled. + $('div.play').each(function (i, el) { + // Set up playground for this example. + var setup = function() { + var code = $('.code', el); + playground({ + 'codeEl': code, + 'outputEl': $('.output', el), + 'runEl': $('.run', el), + 'fmtEl': $('.fmt', el), + 'shareEl': $('.share', el), + 'shareRedirect': 'http://play.golang.org/p/' + }); + + // Make the code textarea resize to fit content. + var resize = function() { + code.height(0); + var h = code[0].scrollHeight; + code.height(h+20); // minimize bouncing. + code.closest('.input').height(h); + }; + code.on('keydown', resize); + code.on('keyup', resize); + code.keyup(); // resize now. + }; + + // If example already visible, set up playground now. + if ($(el).is(':visible')) { + setup(); + return; + } + + // Otherwise, set up playground when example is expanded. + var built = false; + $(el).closest('.toggle').click(function() { + // Only set up once. + if (!built) { + setup(); + built = true; + } + }); + }); +} + // fixFocus tries to put focus to div#page so that keyboard navigation works. function fixFocus() { var page = $('div#page'); @@ -186,6 +232,15 @@ function toggleHash() { } } +function addPlusButtons() { + var po = document.createElement('script'); + po.type = 'text/javascript'; + po.async = true; + po.src = 'https://apis.google.com/js/plusone.js'; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(po, s); +} + $(document).ready(function() { bindSearchEvents(); generateTOC(); @@ -196,8 +251,16 @@ $(document).ready(function() { bindToggleLinks(".examplesLink", ""); bindToggleLinks(".indexLink", ""); setupDropdownPlayground(); + setupInlinePlayground(); fixFocus(); toggleHash(); + addPlusButtons(); + + // godoc.html defines window.initFuncs in the tag, and root.html and + // codewalk.js push their on-page-ready functions to the list. + // We execute those functions here, to avoid loading jQuery until the page + // content is loaded. + for (var i = 0; i < window.initFuncs.length; i++) window.initFuncs[i](); }); })(); diff --git a/doc/root.html b/doc/root.html index e2d53414e72..e952a76bb3d 100644 --- a/doc/root.html +++ b/doc/root.html @@ -116,7 +116,7 @@ function feedLoaded(result) { } } -$(function() { +window.initFuncs.push(function() { // Set up playground. playground({ "codeEl": "#learn .code", diff --git a/lib/godoc/godoc.html b/lib/godoc/godoc.html index bd524c22f29..ccf5b6ed6ab 100644 --- a/lib/godoc/godoc.html +++ b/lib/godoc/godoc.html @@ -8,14 +8,10 @@ The Go Programming Language {{end}} - -{{if .Playground}} - -{{end}} - {{if .SearchBox}} {{end}} + @@ -87,13 +83,12 @@ and code is licensed under a BSD license.
+ +{{if .Playground}} + +{{end}} + + - diff --git a/lib/godoc/package.html b/lib/godoc/package.html index d71e953ec56..8d28652fc34 100644 --- a/lib/godoc/package.html +++ b/lib/godoc/package.html @@ -224,53 +224,3 @@

Need more packages? Take a look at the Go Projects wiki page.

{{end}} {{end}} - -{{if $.Examples}} - -{{end}}