2009-09-17 09:05:12 -06:00
|
|
|
// Except as noted, this content is licensed under Creative Commons
|
|
|
|
// Attribution 3.0
|
|
|
|
|
|
|
|
/* A little code to ease navigation of these documents.
|
|
|
|
*
|
|
|
|
* On window load we:
|
|
|
|
* + Generate a table of contents (godocs_generateTOC)
|
|
|
|
* + Add links up to the top of the doc from each section (godocs_addTopLinks)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We want to do some stuff on page load (after the HTML is rendered).
|
|
|
|
So listen for that:
|
|
|
|
*/
|
2010-09-15 22:01:02 -06:00
|
|
|
function bindEvent(el, e, fn) {
|
|
|
|
if (el.addEventListener){
|
|
|
|
el.addEventListener(e, fn, false);
|
|
|
|
} else if (el.attachEvent){
|
|
|
|
el.attachEvent('on'+e, fn);
|
|
|
|
}
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
|
|
|
|
2010-09-15 22:01:02 -06:00
|
|
|
function godocs_bindSearchEvents() {
|
|
|
|
var search = document.getElementById('search');
|
2011-05-02 19:07:11 -06:00
|
|
|
if (!search) {
|
|
|
|
// no search box (index disabled)
|
|
|
|
return;
|
|
|
|
}
|
2010-09-15 22:01:02 -06:00
|
|
|
function clearInactive() {
|
|
|
|
if (search.className == "inactive") {
|
|
|
|
search.value = "";
|
|
|
|
search.className = "";
|
|
|
|
}
|
|
|
|
}
|
2011-04-15 11:48:45 -06:00
|
|
|
function restoreInactive() {
|
2012-03-22 19:23:53 -06:00
|
|
|
if (search.value !== "") {
|
2011-04-15 11:48:45 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (search.type != "search") {
|
|
|
|
search.value = search.getAttribute("placeholder");
|
|
|
|
}
|
|
|
|
search.className = "inactive";
|
|
|
|
}
|
|
|
|
restoreInactive();
|
2010-09-15 22:01:02 -06:00
|
|
|
bindEvent(search, 'focus', clearInactive);
|
2011-04-15 11:48:45 -06:00
|
|
|
bindEvent(search, 'blur', restoreInactive);
|
2010-09-15 22:01:02 -06:00
|
|
|
}
|
|
|
|
|
2012-03-22 19:23:53 -06:00
|
|
|
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
|
|
|
|
* Takes a node, returns a string.
|
|
|
|
*/
|
|
|
|
function godocs_nodeToText(node) {
|
|
|
|
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
|
|
|
|
|
|
|
|
var text = '';
|
|
|
|
for (var j = 0; j != node.childNodes.length; j++) {
|
|
|
|
var child = node.childNodes[j];
|
|
|
|
if (child.nodeType == TEXT_NODE) {
|
|
|
|
if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
|
|
|
|
text = text + child.nodeValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text = text + godocs_nodeToText(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2009-09-17 09:05:12 -06:00
|
|
|
/* Generates a table of contents: looks for h2 and h3 elements and generates
|
|
|
|
* links. "Decorates" the element with id=="nav" with this table of contents.
|
|
|
|
*/
|
|
|
|
function godocs_generateTOC() {
|
2011-10-18 14:23:35 -06:00
|
|
|
if (document.getElementById('manual-nav')) { return; }
|
2009-09-17 09:05:12 -06:00
|
|
|
var navbar = document.getElementById('nav');
|
|
|
|
if (!navbar) { return; }
|
|
|
|
|
|
|
|
var toc_items = [];
|
|
|
|
|
|
|
|
var i;
|
2012-02-13 03:22:36 -07:00
|
|
|
var seenNav = false;
|
2009-09-17 09:05:12 -06:00
|
|
|
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
|
|
|
|
var node = navbar.parentNode.childNodes[i];
|
2012-02-13 03:22:36 -07:00
|
|
|
if (!seenNav) {
|
|
|
|
if (node.id == 'nav') {
|
|
|
|
seenNav = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-19 00:59:06 -07:00
|
|
|
if ((node.tagName != 'h2') && (node.tagName != 'H2') &&
|
|
|
|
(node.tagName != 'h3') && (node.tagName != 'H3')) {
|
|
|
|
continue;
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
2012-01-19 00:59:06 -07:00
|
|
|
if (!node.id) {
|
|
|
|
node.id = 'tmp_' + i;
|
|
|
|
}
|
|
|
|
var text = godocs_nodeToText(node);
|
|
|
|
if (!text) { continue; }
|
2009-09-17 09:05:12 -06:00
|
|
|
|
2012-01-19 00:59:06 -07:00
|
|
|
var textNode = document.createTextNode(text);
|
2009-09-17 09:05:12 -06:00
|
|
|
|
2012-01-19 00:59:06 -07:00
|
|
|
var link = document.createElement('a');
|
|
|
|
link.href = '#' + node.id;
|
|
|
|
link.appendChild(textNode);
|
2009-09-17 09:05:12 -06:00
|
|
|
|
2012-01-19 00:59:06 -07:00
|
|
|
// Then create the item itself
|
|
|
|
var item;
|
|
|
|
if ((node.tagName == 'h2') || (node.tagName == 'H2')) {
|
|
|
|
item = document.createElement('dt');
|
|
|
|
} else { // h3
|
|
|
|
item = document.createElement('dd');
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
2012-01-19 00:59:06 -07:00
|
|
|
|
|
|
|
item.appendChild(link);
|
|
|
|
toc_items.push(item);
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
|
|
|
|
2011-05-19 22:26:00 -06:00
|
|
|
if (toc_items.length <= 1) { return; }
|
2009-09-17 09:05:12 -06:00
|
|
|
|
|
|
|
var dl1 = document.createElement('dl');
|
|
|
|
var dl2 = document.createElement('dl');
|
|
|
|
|
|
|
|
var split_index = (toc_items.length / 2) + 1;
|
|
|
|
if (split_index < 8) {
|
|
|
|
split_index = toc_items.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < split_index; i++) {
|
|
|
|
dl1.appendChild(toc_items[i]);
|
|
|
|
}
|
|
|
|
for (/* keep using i */; i < toc_items.length; i++) {
|
|
|
|
dl2.appendChild(toc_items[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tocTable = document.createElement('table');
|
|
|
|
navbar.appendChild(tocTable);
|
|
|
|
tocTable.className = 'unruled';
|
|
|
|
var tocBody = document.createElement('tbody');
|
|
|
|
tocTable.appendChild(tocBody);
|
|
|
|
|
|
|
|
var tocRow = document.createElement('tr');
|
|
|
|
tocBody.appendChild(tocRow);
|
|
|
|
|
|
|
|
// 1st column
|
|
|
|
var tocCell = document.createElement('td');
|
|
|
|
tocCell.className = 'first';
|
|
|
|
tocRow.appendChild(tocCell);
|
|
|
|
tocCell.appendChild(dl1);
|
|
|
|
|
|
|
|
// 2nd column
|
|
|
|
tocCell = document.createElement('td');
|
|
|
|
tocRow.appendChild(tocCell);
|
|
|
|
tocCell.appendChild(dl2);
|
|
|
|
}
|
|
|
|
|
2012-03-22 19:23:53 -06:00
|
|
|
function getElementsByClassName(base, clazz) {
|
|
|
|
if (base.getElementsByClassName) {
|
|
|
|
return base.getElementsByClassName(clazz);
|
|
|
|
}
|
|
|
|
var elements = base.getElementsByTagName('*'), foundElements = [];
|
|
|
|
for (var n in elements) {
|
|
|
|
if (clazz == elements[n].className) {
|
|
|
|
foundElements.push(elements[n]);
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 19:23:53 -06:00
|
|
|
return foundElements;
|
2009-09-17 09:05:12 -06:00
|
|
|
}
|
|
|
|
|
2012-03-25 21:10:27 -06:00
|
|
|
function godocs_bindToggle(el) {
|
|
|
|
var button = getElementsByClassName(el, "toggleButton");
|
2012-03-22 19:23:53 -06:00
|
|
|
var callback = function() {
|
2012-03-25 21:10:27 -06:00
|
|
|
if (el.className == "toggle") {
|
|
|
|
el.className = "toggleVisible";
|
2012-03-22 19:23:53 -06:00
|
|
|
} else {
|
2012-03-25 21:10:27 -06:00
|
|
|
el.className = "toggle";
|
2012-03-22 19:23:53 -06:00
|
|
|
}
|
|
|
|
};
|
2012-03-25 21:10:27 -06:00
|
|
|
for (var i = 0; i < button.length; i++) {
|
|
|
|
bindEvent(button[i], "click", callback);
|
2011-10-06 12:56:17 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-25 21:10:27 -06:00
|
|
|
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) {
|
2012-02-13 20:52:00 -07:00
|
|
|
bindEvent(l, "click", function() {
|
|
|
|
var i = l.href.indexOf("#"+prefix);
|
2012-03-22 19:23:53 -06:00
|
|
|
if (i < 0) {
|
2012-02-13 20:52:00 -07:00
|
|
|
return;
|
2012-03-22 19:23:53 -06:00
|
|
|
}
|
2012-02-13 20:52:00 -07:00
|
|
|
var id = prefix + l.href.slice(i+1+prefix.length);
|
|
|
|
var eg = document.getElementById(id);
|
2012-03-25 21:10:27 -06:00
|
|
|
eg.className = "toggleVisible";
|
2012-02-13 20:52:00 -07:00
|
|
|
});
|
|
|
|
}
|
2012-03-25 21:10:27 -06:00
|
|
|
function godocs_bindToggleLinks(className, prefix) {
|
|
|
|
var links = getElementsByClassName(document, className);
|
2012-03-22 19:23:53 -06:00
|
|
|
for (i = 0; i < links.length; i++) {
|
2012-03-25 21:10:27 -06:00
|
|
|
godocs_bindToggleLink(links[i], prefix);
|
2012-03-22 19:23:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function godocs_onload() {
|
|
|
|
godocs_bindSearchEvents();
|
|
|
|
godocs_generateTOC();
|
2012-03-25 21:10:27 -06:00
|
|
|
godocs_bindToggles("toggle");
|
|
|
|
godocs_bindToggles("toggleVisible");
|
|
|
|
godocs_bindToggleLinks("exampleLink", "example_");
|
|
|
|
godocs_bindToggleLinks("overviewLink", "");
|
2012-03-22 19:23:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bindEvent(window, 'load', godocs_onload);
|