From 0b762d9523a8b672143556739726d3d4e8ac6c94 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Mon, 26 Mar 2012 14:10:27 +1100 Subject: [PATCH] godoc: make 'Overview' section collapsable This makes packages with lengthly package comments easier to browse. R=golang-dev, r CC=golang-dev https://golang.org/cl/5901055 --- doc/godocs.js | 40 ++++++++++++++++++++++------------------ doc/style.css | 10 +++++----- lib/godoc/example.html | 6 +++--- lib/godoc/package.html | 13 ++++++++++--- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/doc/godocs.js b/doc/godocs.js index 37573136abc..8fd32fef0f4 100644 --- a/doc/godocs.js +++ b/doc/godocs.js @@ -164,21 +164,26 @@ function getElementsByClassName(base, clazz) { return foundElements; } -function godocs_bindExampleToggle(eg) { - var heading = getElementsByClassName(eg, "exampleHeading"); +function godocs_bindToggle(el) { + var button = getElementsByClassName(el, "toggleButton"); var callback = function() { - if (eg.className == "example") { - eg.className = "exampleVisible"; + if (el.className == "toggle") { + el.className = "toggleVisible"; } else { - eg.className = "example"; + el.className = "toggle"; } }; - for (var i = 0; i < heading.length; i++) { - bindEvent(heading[i], "click", callback); + for (var i = 0; i < button.length; i++) { + bindEvent(button[i], "click", callback); } } -function godocs_bindExampleLink(l) { - var prefix = "example_"; +function godocs_bindToggles(className) { + var els = getElementsByClassName(document, className); + for (var i = 0; i < els.length; i++) { + godocs_bindToggle(els[i]); + } +} +function godocs_bindToggleLink(l, prefix) { bindEvent(l, "click", function() { var i = l.href.indexOf("#"+prefix); if (i < 0) { @@ -186,24 +191,23 @@ function godocs_bindExampleLink(l) { } var id = prefix + l.href.slice(i+1+prefix.length); var eg = document.getElementById(id); - eg.className = "exampleVisible"; + eg.className = "toggleVisible"; }); } -function godocs_bindExamples() { - var examples = getElementsByClassName(document, "example"); - for (var i = 0; i < examples.length; i++) { - godocs_bindExampleToggle(examples[i]); - } - var links = getElementsByClassName(document, "exampleLink"); +function godocs_bindToggleLinks(className, prefix) { + var links = getElementsByClassName(document, className); for (i = 0; i < links.length; i++) { - godocs_bindExampleLink(links[i]); + godocs_bindToggleLink(links[i], prefix); } } function godocs_onload() { godocs_bindSearchEvents(); godocs_generateTOC(); - godocs_bindExamples(); + godocs_bindToggles("toggle"); + godocs_bindToggles("toggleVisible"); + godocs_bindToggleLinks("exampleLink", "example_"); + godocs_bindToggleLinks("overviewLink", ""); } bindEvent(window, 'load', godocs_onload); diff --git a/doc/style.css b/doc/style.css index 53f5e18cd18..ea66f988a67 100644 --- a/doc/style.css +++ b/doc/style.css @@ -364,11 +364,11 @@ div#blog .read { text-align: right; } -.exampleHeading { cursor: pointer; } -.example .collapsed { display: block; } -.example .expanded { display: none; } -.exampleVisible .collapsed { display: none; } -.exampleVisible .expanded { display: block; } +.toggleButton { cursor: pointer; } +.toggle .collapsed { display: block; } +.toggle .expanded { display: none; } +.toggleVisible .collapsed { display: none; } +.toggleVisible .expanded { display: block; } table.codetable { margin-left: auto; margin-right: auto; border-style: none; } hr { border-style: none; border-top: 1px solid black; } diff --git a/lib/godoc/example.html b/lib/godoc/example.html index 43ee4bddc19..ede31d61f9b 100644 --- a/lib/godoc/example.html +++ b/lib/godoc/example.html @@ -1,9 +1,9 @@ -
+
-

Example{{example_suffix .Name}}

+

Example{{example_suffix .Name}}

{{with .Doc}}

{{html .}}

{{end}}

Code:

{{.Code}}
diff --git a/lib/godoc/package.html b/lib/godoc/package.html index 848e68df775..1a580a20237 100644 --- a/lib/godoc/package.html +++ b/lib/godoc/package.html @@ -10,7 +10,7 @@
import "{{html .ImportPath}}"
-
Overview
+
Overview
Index
{{if $.Examples}}
Examples
@@ -20,9 +20,16 @@ {{end}}
-

Overview

- {{comment_html .Doc}} +
+ +
+

Overview ▾

+ {{comment_html .Doc}} +
+
{{example_html "" $.Examples $.FSet}}

Index