1
// Except as noted, this content is licensed under Creative Commons
1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
5
/* A little code to ease navigation of these documents.
7
* + Generate a table of contents (godocs_generateTOC)
8
* + Add links up to the top of the doc from each section (godocs_addTopLinks)
11
/* We want to do some stuff on page load (after the HTML is rendered).
14
function bindEvent(el, e, fn) {
15
if (el.addEventListener){
16
el.addEventListener(e, fn, false);
17
} else if (el.attachEvent){
18
el.attachEvent('on'+e, fn);
22
function godocs_bindSearchEvents() {
23
var search = document.getElementById('search');
25
// no search box (index disabled)
8
* + Bind search box hint placeholder show/hide events (bindSearchEvents)
9
* + Generate a table of contents (generateTOC)
10
* + Bind foldable sections (bindToggles)
11
* + Bind links to foldable sections (bindToggleLinks)
17
function bindSearchEvents() {
19
var search = $('#search');
20
if (search.length === 0) {
21
return; // no search box
28
24
function clearInactive() {
29
if (search.className == "inactive") {
31
search.className = "";
25
if (search.is('.inactive')) {
27
search.removeClass('inactive');
34
31
function restoreInactive() {
35
if (search.value !== "") {
32
if (search.val() !== '') {
38
if (search.type != "search") {
39
search.value = search.getAttribute("placeholder");
41
search.className = "inactive";
35
search.val(search.attr('placeholder'));
36
search.addClass('inactive');
39
search.on('focus', clearInactive);
40
search.on('blur', restoreInactive);
44
bindEvent(search, 'focus', clearInactive);
45
bindEvent(search, 'blur', restoreInactive);
48
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
49
* Takes a node, returns a string.
51
function godocs_nodeToText(node) {
52
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
55
for (var j = 0; j != node.childNodes.length; j++) {
56
var child = node.childNodes[j];
57
if (child.nodeType == TEXT_NODE) {
58
if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
59
text = text + child.nodeValue;
62
text = text + godocs_nodeToText(child);
68
45
/* Generates a table of contents: looks for h2 and h3 elements and generates
69
* links. "Decorates" the element with id=="nav" with this table of contents.
46
* links. "Decorates" the element with id=="nav" with this table of contents.
71
function godocs_generateTOC() {
72
if (document.getElementById('manual-nav')) { return; }
73
var navbar = document.getElementById('nav');
74
if (!navbar) { return; }
48
function generateTOC() {
49
if ($('#manual-nav').length > 0) {
54
if (nav.length === 0) {
76
58
var toc_items = [];
80
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
81
var node = navbar.parentNode.childNodes[i];
83
if (node.id == 'nav') {
88
if ((node.tagName != 'h2') && (node.tagName != 'H2') &&
89
(node.tagName != 'h3') && (node.tagName != 'H3')) {
95
var text = godocs_nodeToText(node);
96
if (!text) { continue; }
98
var textNode = document.createTextNode(text);
100
var link = document.createElement('a');
101
link.href = '#' + node.id;
102
link.appendChild(textNode);
104
// Then create the item itself
59
$(nav).nextAll('h2, h3').each(function() {
62
node.id = 'tmp_' + toc_items.length;
63
var link = $('<a/>').attr('href', '#' + node.id).text($(node).text());
106
if ((node.tagName == 'h2') || (node.tagName == 'H2')) {
107
item = document.createElement('dt');
65
if ($(node).is('h2')) {
109
item = document.createElement('dd');
112
item.appendChild(link);
113
71
toc_items.push(item);
73
if (toc_items.length <= 1) {
116
if (toc_items.length <= 1) { return; }
118
var dl1 = document.createElement('dl');
119
var dl2 = document.createElement('dl');
121
80
var split_index = (toc_items.length / 2) + 1;
122
81
if (split_index < 8) {
123
82
split_index = toc_items.length;
126
for (i = 0; i < split_index; i++) {
127
dl1.appendChild(toc_items[i]);
84
for (var i = 0; i < split_index; i++) {
85
dl1.append(toc_items[i]);
129
87
for (/* keep using i */; i < toc_items.length; i++) {
130
dl2.appendChild(toc_items[i]);
88
dl2.append(toc_items[i]);
133
var tocTable = document.createElement('table');
134
navbar.appendChild(tocTable);
135
tocTable.className = 'unruled';
136
var tocBody = document.createElement('tbody');
137
tocTable.appendChild(tocBody);
139
var tocRow = document.createElement('tr');
140
tocBody.appendChild(tocRow);
91
var tocTable = $('<table class="unruled"/>').appendTo(nav);
92
var tocBody = $('<tbody/>').appendTo(tocTable);
93
var tocRow = $('<tr/>').appendTo(tocBody);
143
var tocCell = document.createElement('td');
144
tocCell.className = 'first';
145
tocRow.appendChild(tocCell);
146
tocCell.appendChild(dl1);
96
$('<td class="first"/>').appendTo(tocRow).append(dl1);
149
tocCell = document.createElement('td');
150
tocRow.appendChild(tocCell);
151
tocCell.appendChild(dl2);
154
function getElementsByClassName(base, clazz) {
155
if (base.getElementsByClassName) {
156
return base.getElementsByClassName(clazz);
158
var elements = base.getElementsByTagName('*'), foundElements = [];
159
for (var n in elements) {
160
if (clazz == elements[n].className) {
161
foundElements.push(elements[n]);
164
return foundElements;
167
function godocs_bindToggle(el) {
168
var button = getElementsByClassName(el, "toggleButton");
169
var callback = function() {
170
if (el.className == "toggle") {
171
el.className = "toggleVisible";
98
$('<td/>').appendTo(tocRow).append(dl2);
101
function bindToggle(el) {
102
$('.toggleButton', el).click(function() {
103
if ($(el).is('.toggle')) {
104
$(el).addClass('toggleVisible').removeClass('toggle');
173
el.className = "toggle";
106
$(el).addClass('toggle').removeClass('toggleVisible');
176
for (var i = 0; i < button.length; i++) {
177
bindEvent(button[i], "click", callback);
180
function godocs_bindToggles(className) {
181
var els = getElementsByClassName(document, className);
182
for (var i = 0; i < els.length; i++) {
183
godocs_bindToggle(els[i]);
186
function godocs_bindToggleLink(l, prefix) {
187
bindEvent(l, "click", function() {
188
var i = l.href.indexOf("#"+prefix);
110
function bindToggles(selector) {
111
$(selector).each(function(i, el) {
116
function bindToggleLink(el, prefix) {
117
$(el).click(function() {
118
var href = $(el).attr('href');
119
var i = href.indexOf('#'+prefix);
192
var id = prefix + l.href.slice(i+1+prefix.length);
193
var eg = document.getElementById(id);
194
eg.className = "toggleVisible";
197
function godocs_bindToggleLinks(className, prefix) {
198
var links = getElementsByClassName(document, className);
199
for (i = 0; i < links.length; i++) {
200
godocs_bindToggleLink(links[i], prefix);
123
var id = '#' + prefix + href.slice(i+1+prefix.length);
124
if ($(id).is('.toggle')) {
125
$(id).find('.toggleButton').first().click();
129
function bindToggleLinks(selector, prefix) {
130
$(selector).each(function(i, el) {
131
bindToggleLink(el, prefix);
135
function setupDropdownPlayground() {
136
if (!$('#page').is('.wide')) {
137
return; // don't show on front page
204
function godocs_onload() {
205
godocs_bindSearchEvents();
206
godocs_generateTOC();
207
godocs_bindToggles("toggle");
208
godocs_bindToggles("toggleVisible");
209
godocs_bindToggleLinks("exampleLink", "example_");
210
godocs_bindToggleLinks("overviewLink", "");
213
bindEvent(window, 'load', godocs_onload);
139
var button = $('#playgroundButton');
140
var div = $('#playground');
142
button.toggle(function() {
143
button.addClass('active');
150
'codeEl': $('.code', div),
151
'outputEl': $('.output', div),
152
'runEl': $('.run', div),
153
'fmtEl': $('.fmt', div),
154
'shareEl': $('.share', div),
155
'shareRedirect': 'http://play.golang.org/p/'
159
button.removeClass('active');
163
$('#menu').css('min-width', '+=60');
166
// fixFocus tries to put focus to div#page so that keyboard navigation works.
167
function fixFocus() {
168
var page = $('div#page');
169
var topbar = $('div#topbar');
170
page.css('outline', 0); // disable outline when focused
171
page.attr('tabindex', -1); // and set tabindex so that it is focusable
172
$(window).resize(function (evt) {
173
// only focus page when the topbar is at fixed position (that is, it's in
174
// front of page, and keyboard event will go to the former by default.)
175
// by focusing page, keyboard event will go to page so that up/down arrow,
176
// space, etc. will work as expected.
177
if (topbar.css('position') == "fixed")
182
function toggleHash() {
183
var hash = $(window.location.hash);
184
if (hash.is('.toggle')) {
185
hash.addClass('toggleVisible').removeClass('toggle');
189
$(document).ready(function() {
192
bindToggles(".toggle");
193
bindToggles(".toggleVisible");
194
bindToggleLinks(".exampleLink", "example_");
195
bindToggleLinks(".overviewLink", "");
196
bindToggleLinks(".examplesLink", "");
197
bindToggleLinks(".indexLink", "");
198
setupDropdownPlayground();