lab.nexedi.com will be down from Thursday, 20 March 2025, 07:30:00 UTC for a duration of approximately 2 hours

Commit 65dbe678 authored by Andrew Gerrand's avatar Andrew Gerrand

godoc: add jQuery to Go repository, rewrite godocs.js to use jQuery

For golang.org I intend to rewrite the jquery link in godoc.html
to point to the Google-hosted jquery.js.

R=dsymonds, minux.ma
CC=golang-dev
https://golang.org/cl/6589071
parent 7e2e4a73
// Except as noted, this content is licensed under Creative Commons // Copyright 2012 The Go Authors. All rights reserved.
// Attribution 3.0 // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/* A little code to ease navigation of these documents. /* A little code to ease navigation of these documents.
* *
* On window load we: * On window load we:
* + Generate a table of contents (godocs_generateTOC) * + Bind search box hint placeholder show/hide events (bindSearchEvents)
* + Add links up to the top of the doc from each section (godocs_addTopLinks) * + Generate a table of contents (generateTOC)
* + Bind foldable sections (bindToggles)
* + Bind links to foldable sections (bindToggleLinks)
*/ */
/* We want to do some stuff on page load (after the HTML is rendered). (function() {
So listen for that: 'use strict';
*/
function bindEvent(el, e, fn) {
if (el.addEventListener){
el.addEventListener(e, fn, false);
} else if (el.attachEvent){
el.attachEvent('on'+e, fn);
}
}
function godocs_bindSearchEvents() { function bindSearchEvents() {
var search = document.getElementById('search');
if (!search) { var search = $('#search');
// no search box (index disabled) if (search.length === 0) {
return; return; // no search box
} }
function clearInactive() { function clearInactive() {
if (search.className == "inactive") { if (search.is('.inactive')) {
search.value = ""; search.val('');
search.className = ""; search.removeClass('inactive');
} }
} }
function restoreInactive() { function restoreInactive() {
if (search.value !== "") { if (search.val() !== '') {
return; return;
} }
if (search.type != "search") { search.val(search.attr('placeholder'));
search.value = search.getAttribute("placeholder"); search.addClass('inactive');
}
search.className = "inactive";
} }
search.on('focus', clearInactive);
search.on('blur', restoreInactive);
restoreInactive(); restoreInactive();
bindEvent(search, 'focus', clearInactive);
bindEvent(search, 'blur', restoreInactive);
} }
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>. /* Generates a table of contents: looks for h2 and h3 elements and generates
* Takes a node, returns a string. * links. "Decorates" the element with id=="nav" with this table of contents.
*/ */
function godocs_nodeToText(node) { function generateTOC() {
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :( if ($('#manual-nav').length > 0) {
return;
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;
}
/* Generates a table of contents: looks for h2 and h3 elements and generates var nav = $('#nav');
* links. "Decorates" the element with id=="nav" with this table of contents. if (nav.length === 0) {
*/ return;
function godocs_generateTOC() { }
if (document.getElementById('manual-nav')) { return; }
var navbar = document.getElementById('nav');
if (!navbar) { return; }
var toc_items = []; var toc_items = [];
$(nav).nextAll('h2, h3').each(function() {
var i; var node = this;
var seenNav = false; var link = $('<a/>').attr('href', '#' + node.id).text($(node).text());
for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
var node = navbar.parentNode.childNodes[i];
if (!seenNav) {
if (node.id == 'nav') {
seenNav = true;
}
continue;
}
if ((node.tagName != 'h2') && (node.tagName != 'H2') &&
(node.tagName != 'h3') && (node.tagName != 'H3')) {
continue;
}
if (!node.id) {
node.id = 'tmp_' + i;
}
var text = godocs_nodeToText(node);
if (!text) { continue; }
var textNode = document.createTextNode(text);
var link = document.createElement('a');
link.href = '#' + node.id;
link.appendChild(textNode);
// Then create the item itself
var item; var item;
if ((node.tagName == 'h2') || (node.tagName == 'H2')) { if ($(node).is('h2')) {
item = document.createElement('dt'); item = $('<dt/>');
} else { // h3 } else { // h3
item = document.createElement('dd'); item = $('<dd/>');
} }
item.append(link);
item.appendChild(link);
toc_items.push(item); toc_items.push(item);
});
if (toc_items.length <= 1) {
return;
} }
if (toc_items.length <= 1) { return; } var dl1 = $('<dl/>');
var dl2 = $('<dl/>');
var dl1 = document.createElement('dl');
var dl2 = document.createElement('dl');
var split_index = (toc_items.length / 2) + 1; var split_index = (toc_items.length / 2) + 1;
if (split_index < 8) { if (split_index < 8) {
split_index = toc_items.length; split_index = toc_items.length;
} }
for (var i = 0; i < split_index; i++) {
for (i = 0; i < split_index; i++) { dl1.append(toc_items[i]);
dl1.appendChild(toc_items[i]);
} }
for (/* keep using i */; i < toc_items.length; i++) { for (/* keep using i */; i < toc_items.length; i++) {
dl2.appendChild(toc_items[i]); dl2.append(toc_items[i]);
} }
var tocTable = document.createElement('table'); var tocTable = $('<table class="unruled"/>').appendTo(nav);
navbar.appendChild(tocTable); var tocBody = $('<tbody/>').appendTo(tocTable);
tocTable.className = 'unruled'; var tocRow = $('<tr/>').appendTo(tocBody);
var tocBody = document.createElement('tbody');
tocTable.appendChild(tocBody);
var tocRow = document.createElement('tr');
tocBody.appendChild(tocRow);
// 1st column // 1st column
var tocCell = document.createElement('td'); $('<td class="first"/>').appendTo(tocRow).append(dl1);
tocCell.className = 'first';
tocRow.appendChild(tocCell);
tocCell.appendChild(dl1);
// 2nd column // 2nd column
tocCell = document.createElement('td'); $('<td/>').appendTo(tocRow).append(dl2);
tocRow.appendChild(tocCell);
tocCell.appendChild(dl2);
}
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]);
}
}
return foundElements;
} }
function godocs_bindToggle(el) { function bindToggle(el) {
var button = getElementsByClassName(el, "toggleButton"); $('.toggleButton', el).click(function() {
var callback = function() { if ($(el).is('.toggle')) {
if (el.className == "toggle") { $(el).addClass('toggleVisible').removeClass('toggle');
el.className = "toggleVisible";
} else { } else {
el.className = "toggle"; $(el).addClass('toggle').removeClass('toggleVisible');
} }
}; });
for (var i = 0; i < button.length; i++) {
bindEvent(button[i], "click", callback);
}
} }
function godocs_bindToggles(className) { function bindToggles(selector) {
var els = getElementsByClassName(document, className); $(selector).each(function(i, el) {
for (var i = 0; i < els.length; i++) { bindToggle(el);
godocs_bindToggle(els[i]); });
}
} }
function godocs_bindToggleLink(l, prefix) {
bindEvent(l, "click", function() { function bindToggleLink(el, prefix) {
var i = l.href.indexOf("#"+prefix); $(el).click(function() {
var href = $(el).attr('href');
var i = href.indexOf('#'+prefix);
if (i < 0) { if (i < 0) {
return; return;
} }
var id = prefix + l.href.slice(i+1+prefix.length); var id = '#' + prefix + href.slice(i+1+prefix.length);
var eg = document.getElementById(id); if ($(id).is('.toggle')) {
eg.className = "toggleVisible"; $(id).find('.toggleButton').first().click();
}
}); });
} }
function godocs_bindToggleLinks(className, prefix) { function bindToggleLinks(selector, prefix) {
var links = getElementsByClassName(document, className); $(selector).each(function(i, el) {
for (i = 0; i < links.length; i++) { bindToggleLink(el, prefix);
godocs_bindToggleLink(links[i], prefix); });
}
}
function godocs_onload() {
godocs_bindSearchEvents();
godocs_generateTOC();
godocs_bindToggles("toggle");
godocs_bindToggles("toggleVisible");
godocs_bindToggleLinks("exampleLink", "example_");
godocs_bindToggleLinks("overviewLink", "");
godocs_bindToggleLinks("examplesLink", "");
godocs_bindToggleLinks("indexLink", "");
} }
bindEvent(window, 'load', godocs_onload); $(document).ready(function() {
bindSearchEvents();
generateTOC();
bindToggles(".toggle");
bindToggles(".toggleVisible");
bindToggleLinks(".exampleLink", "example_");
bindToggleLinks(".overviewLink", "");
bindToggleLinks(".examplesLink", "");
bindToggleLinks(".indexLink", "");
});
})();
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
function godocs_bindPopups(data) {
$('#content span').bind('mouseenter', function() {
var id = $(this).attr('id');
//var txt = $(this).text();
if (typeof data[id] == 'undefined')
return;
var content = data[id];
var $el = $('.popup', this);
if (!$el.length) { // create it
$el = $('<div class="popup"></div>');
$el.prependTo(this).css($(this).offset()).text(content);
}
});
$('#content span').bind('mouseleave', function() {
$('.popup', this).remove();
});
}
...@@ -88,7 +88,6 @@ Linux, Mac OS X, Windows, and more. ...@@ -88,7 +88,6 @@ Linux, Mac OS X, Windows, and more.
<script type="text/javascript" src="/doc/play/playground.js"></script> <script type="text/javascript" src="/doc/play/playground.js"></script>
<script type="text/javascript"> <script type="text/javascript">
google.load("feeds", "1"); google.load("feeds", "1");
google.load("jquery", "1.7.1");
function feedLoaded(result) { function feedLoaded(result) {
if (result.error) { if (result.error) {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
<title>The Go Programming Language</title> <title>The Go Programming Language</title>
{{end}} {{end}}
<link type="text/css" rel="stylesheet" href="/doc/style.css"> <link type="text/css" rel="stylesheet" href="/doc/style.css">
<script type="text/javascript" src="/doc/jquery.js"></script>
<script type="text/javascript" src="/doc/godocs.js"></script> <script type="text/javascript" src="/doc/godocs.js"></script>
{{if .SearchBox}} {{if .SearchBox}}
<link rel="search" type="application/opensearchdescription+xml" title="godoc" href="/opensearch.xml" /> <link rel="search" type="application/opensearchdescription+xml" title="godoc" href="/opensearch.xml" />
...@@ -24,7 +25,7 @@ ...@@ -24,7 +25,7 @@
<a href="/pkg/">Packages</a> <a href="/pkg/">Packages</a>
<a href="/project/">The Project</a> <a href="/project/">The Project</a>
<a href="/help/">Help</a> <a href="/help/">Help</a>
<input type="text" id="search" name="q" class="inactive" value="Search"> <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
</div> </div>
<div id="heading"><a href="/">The Go Programming Language</a></div> <div id="heading"><a href="/">The Go Programming Language</a></div>
</form> </form>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment