5 * Sphinx JavaScript utilities for all documentation.
7 * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
8 * :license: BSD, see LICENSE for details.
13 * select a different prefix for underscore
18 * make the code below compatible with browsers without
19 * an installed firebug like debugger
20 if (!window.console || !console.firebug) {
21 var names = ["log", "debug", "info", "warn", "error", "assert", "dir",
22 "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace",
23 "profile", "profileEnd"];
25 for (var i = 0; i < names.length; ++i)
26 window.console[names[i]] = function() {};
31 * small helper function to urldecode strings
33 jQuery.urldecode = function(x) {
34 return decodeURIComponent(x).replace(/\+/g, ' ');
38 * small helper function to urlencode strings
40 jQuery.urlencode = encodeURIComponent;
43 * This function returns the parsed url parameters of the
44 * current request. Multiple values per key are supported,
45 * it will always return arrays of strings for the value parts.
47 jQuery.getQueryParameters = function(s) {
48 if (typeof s == 'undefined')
49 s = document.location.search;
50 var parts = s.substr(s.indexOf('?') + 1).split('&');
52 for (var i = 0; i < parts.length; i++) {
53 var tmp = parts[i].split('=', 2);
54 var key = jQuery.urldecode(tmp[0]);
55 var value = jQuery.urldecode(tmp[1]);
57 result[key].push(value);
59 result[key] = [value];
65 * small function to check if an array contains
68 jQuery.contains = function(arr, item) {
69 for (var i = 0; i < arr.length; i++) {
77 * highlight a given string on a jquery object by wrapping it in
78 * span elements with the given class name.
80 jQuery.fn.highlightText = function(text, className) {
81 function highlight(node) {
82 if (node.nodeType == 3) {
83 var val = node.nodeValue;
84 var pos = val.toLowerCase().indexOf(text);
85 if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) {
86 var span = document.createElement("span");
87 span.className = className;
88 span.appendChild(document.createTextNode(val.substr(pos, text.length)));
89 node.parentNode.insertBefore(span, node.parentNode.insertBefore(
90 document.createTextNode(val.substr(pos + text.length)),
92 node.nodeValue = val.substr(0, pos);
95 else if (!jQuery(node).is("button, select, textarea")) {
96 jQuery.each(node.childNodes, function() {
101 return this.each(function() {
107 * Small JavaScript module for the documentation.
109 var Documentation = {
112 this.fixFirefoxAnchorBug();
113 this.highlightSearchWords();
114 this.initIndexTable();
121 PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; },
124 // gettext and ngettext don't access this so that the functions
125 // can safely bound to a different name (_ = Documentation.gettext)
126 gettext : function(string) {
127 var translated = Documentation.TRANSLATIONS[string];
128 if (typeof translated == 'undefined')
130 return (typeof translated == 'string') ? translated : translated[0];
133 ngettext : function(singular, plural, n) {
134 var translated = Documentation.TRANSLATIONS[singular];
135 if (typeof translated == 'undefined')
136 return (n == 1) ? singular : plural;
137 return translated[Documentation.PLURALEXPR(n)];
140 addTranslations : function(catalog) {
141 for (var key in catalog.messages)
142 this.TRANSLATIONS[key] = catalog.messages[key];
143 this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')');
144 this.LOCALE = catalog.locale;
148 * add context elements like header anchor links
150 addContextElements : function() {
151 $('div[id] > :header:first').each(function() {
152 $('<a class="headerlink">\u00B6</a>').
153 attr('href', '#' + this.id).
154 attr('title', _('Permalink to this headline')).
157 $('dt[id]').each(function() {
158 $('<a class="headerlink">\u00B6</a>').
159 attr('href', '#' + this.id).
160 attr('title', _('Permalink to this definition')).
166 * workaround a firefox stupidity
168 fixFirefoxAnchorBug : function() {
169 if (document.location.hash && $.browser.mozilla)
170 window.setTimeout(function() {
171 document.location.href += '';
176 * highlight the search words provided in the url in the text
178 highlightSearchWords : function() {
179 var params = $.getQueryParameters();
180 var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
182 var body = $('div.body');
183 window.setTimeout(function() {
184 $.each(terms, function() {
185 body.highlightText(this.toLowerCase(), 'highlighted');
188 $('<p class="highlight-link"><a href="javascript:Documentation.' +
189 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')
190 .appendTo($('#searchbox'));
195 * init the domain index toggle buttons
197 initIndexTable : function() {
198 var togglers = $('img.toggler').click(function() {
199 var src = $(this).attr('src');
200 var idnum = $(this).attr('id').substr(7);
201 $('tr.cg-' + idnum).toggle();
202 if (src.substr(-9) == 'minus.png')
203 $(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
205 $(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
206 }).css('display', '');
207 if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) {
213 * helper function to hide the search marks again
215 hideSearchWords : function() {
216 $('#searchbox .highlight-link').fadeOut(300);
217 $('span.highlighted').removeClass('highlighted');
221 * make the url absolute
223 makeURL : function(relativeURL) {
224 return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;
228 * get the current relative url
230 getCurrentURL : function() {
231 var path = document.location.pathname;
232 var parts = path.split(/\//);
233 $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
237 var url = parts.join('/');
238 return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
242 // quick alias for translations
243 _ = Documentation.gettext;
245 $(document).ready(function() {
246 Documentation.init();