Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * doctools.js |
michael@0 | 3 | * ~~~~~~~~~~~ |
michael@0 | 4 | * |
michael@0 | 5 | * Sphinx JavaScript utilities for all documentation. |
michael@0 | 6 | * |
michael@0 | 7 | * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. |
michael@0 | 8 | * :license: BSD, see LICENSE for details. |
michael@0 | 9 | * |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * select a different prefix for underscore |
michael@0 | 14 | */ |
michael@0 | 15 | $u = _.noConflict(); |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * make the code below compatible with browsers without |
michael@0 | 19 | * an installed firebug like debugger |
michael@0 | 20 | if (!window.console || !console.firebug) { |
michael@0 | 21 | var names = ["log", "debug", "info", "warn", "error", "assert", "dir", |
michael@0 | 22 | "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", |
michael@0 | 23 | "profile", "profileEnd"]; |
michael@0 | 24 | window.console = {}; |
michael@0 | 25 | for (var i = 0; i < names.length; ++i) |
michael@0 | 26 | window.console[names[i]] = function() {}; |
michael@0 | 27 | } |
michael@0 | 28 | */ |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * small helper function to urldecode strings |
michael@0 | 32 | */ |
michael@0 | 33 | jQuery.urldecode = function(x) { |
michael@0 | 34 | return decodeURIComponent(x).replace(/\+/g, ' '); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * small helper function to urlencode strings |
michael@0 | 39 | */ |
michael@0 | 40 | jQuery.urlencode = encodeURIComponent; |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * This function returns the parsed url parameters of the |
michael@0 | 44 | * current request. Multiple values per key are supported, |
michael@0 | 45 | * it will always return arrays of strings for the value parts. |
michael@0 | 46 | */ |
michael@0 | 47 | jQuery.getQueryParameters = function(s) { |
michael@0 | 48 | if (typeof s == 'undefined') |
michael@0 | 49 | s = document.location.search; |
michael@0 | 50 | var parts = s.substr(s.indexOf('?') + 1).split('&'); |
michael@0 | 51 | var result = {}; |
michael@0 | 52 | for (var i = 0; i < parts.length; i++) { |
michael@0 | 53 | var tmp = parts[i].split('=', 2); |
michael@0 | 54 | var key = jQuery.urldecode(tmp[0]); |
michael@0 | 55 | var value = jQuery.urldecode(tmp[1]); |
michael@0 | 56 | if (key in result) |
michael@0 | 57 | result[key].push(value); |
michael@0 | 58 | else |
michael@0 | 59 | result[key] = [value]; |
michael@0 | 60 | } |
michael@0 | 61 | return result; |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * small function to check if an array contains |
michael@0 | 66 | * a given item. |
michael@0 | 67 | */ |
michael@0 | 68 | jQuery.contains = function(arr, item) { |
michael@0 | 69 | for (var i = 0; i < arr.length; i++) { |
michael@0 | 70 | if (arr[i] == item) |
michael@0 | 71 | return true; |
michael@0 | 72 | } |
michael@0 | 73 | return false; |
michael@0 | 74 | }; |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * highlight a given string on a jquery object by wrapping it in |
michael@0 | 78 | * span elements with the given class name. |
michael@0 | 79 | */ |
michael@0 | 80 | jQuery.fn.highlightText = function(text, className) { |
michael@0 | 81 | function highlight(node) { |
michael@0 | 82 | if (node.nodeType == 3) { |
michael@0 | 83 | var val = node.nodeValue; |
michael@0 | 84 | var pos = val.toLowerCase().indexOf(text); |
michael@0 | 85 | if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) { |
michael@0 | 86 | var span = document.createElement("span"); |
michael@0 | 87 | span.className = className; |
michael@0 | 88 | span.appendChild(document.createTextNode(val.substr(pos, text.length))); |
michael@0 | 89 | node.parentNode.insertBefore(span, node.parentNode.insertBefore( |
michael@0 | 90 | document.createTextNode(val.substr(pos + text.length)), |
michael@0 | 91 | node.nextSibling)); |
michael@0 | 92 | node.nodeValue = val.substr(0, pos); |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | else if (!jQuery(node).is("button, select, textarea")) { |
michael@0 | 96 | jQuery.each(node.childNodes, function() { |
michael@0 | 97 | highlight(this); |
michael@0 | 98 | }); |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | return this.each(function() { |
michael@0 | 102 | highlight(this); |
michael@0 | 103 | }); |
michael@0 | 104 | }; |
michael@0 | 105 | |
michael@0 | 106 | /** |
michael@0 | 107 | * Small JavaScript module for the documentation. |
michael@0 | 108 | */ |
michael@0 | 109 | var Documentation = { |
michael@0 | 110 | |
michael@0 | 111 | init : function() { |
michael@0 | 112 | this.fixFirefoxAnchorBug(); |
michael@0 | 113 | this.highlightSearchWords(); |
michael@0 | 114 | this.initIndexTable(); |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * i18n support |
michael@0 | 119 | */ |
michael@0 | 120 | TRANSLATIONS : {}, |
michael@0 | 121 | PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; }, |
michael@0 | 122 | LOCALE : 'unknown', |
michael@0 | 123 | |
michael@0 | 124 | // gettext and ngettext don't access this so that the functions |
michael@0 | 125 | // can safely bound to a different name (_ = Documentation.gettext) |
michael@0 | 126 | gettext : function(string) { |
michael@0 | 127 | var translated = Documentation.TRANSLATIONS[string]; |
michael@0 | 128 | if (typeof translated == 'undefined') |
michael@0 | 129 | return string; |
michael@0 | 130 | return (typeof translated == 'string') ? translated : translated[0]; |
michael@0 | 131 | }, |
michael@0 | 132 | |
michael@0 | 133 | ngettext : function(singular, plural, n) { |
michael@0 | 134 | var translated = Documentation.TRANSLATIONS[singular]; |
michael@0 | 135 | if (typeof translated == 'undefined') |
michael@0 | 136 | return (n == 1) ? singular : plural; |
michael@0 | 137 | return translated[Documentation.PLURALEXPR(n)]; |
michael@0 | 138 | }, |
michael@0 | 139 | |
michael@0 | 140 | addTranslations : function(catalog) { |
michael@0 | 141 | for (var key in catalog.messages) |
michael@0 | 142 | this.TRANSLATIONS[key] = catalog.messages[key]; |
michael@0 | 143 | this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')'); |
michael@0 | 144 | this.LOCALE = catalog.locale; |
michael@0 | 145 | }, |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * add context elements like header anchor links |
michael@0 | 149 | */ |
michael@0 | 150 | addContextElements : function() { |
michael@0 | 151 | $('div[id] > :header:first').each(function() { |
michael@0 | 152 | $('<a class="headerlink">\u00B6</a>'). |
michael@0 | 153 | attr('href', '#' + this.id). |
michael@0 | 154 | attr('title', _('Permalink to this headline')). |
michael@0 | 155 | appendTo(this); |
michael@0 | 156 | }); |
michael@0 | 157 | $('dt[id]').each(function() { |
michael@0 | 158 | $('<a class="headerlink">\u00B6</a>'). |
michael@0 | 159 | attr('href', '#' + this.id). |
michael@0 | 160 | attr('title', _('Permalink to this definition')). |
michael@0 | 161 | appendTo(this); |
michael@0 | 162 | }); |
michael@0 | 163 | }, |
michael@0 | 164 | |
michael@0 | 165 | /** |
michael@0 | 166 | * workaround a firefox stupidity |
michael@0 | 167 | */ |
michael@0 | 168 | fixFirefoxAnchorBug : function() { |
michael@0 | 169 | if (document.location.hash && $.browser.mozilla) |
michael@0 | 170 | window.setTimeout(function() { |
michael@0 | 171 | document.location.href += ''; |
michael@0 | 172 | }, 10); |
michael@0 | 173 | }, |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * highlight the search words provided in the url in the text |
michael@0 | 177 | */ |
michael@0 | 178 | highlightSearchWords : function() { |
michael@0 | 179 | var params = $.getQueryParameters(); |
michael@0 | 180 | var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; |
michael@0 | 181 | if (terms.length) { |
michael@0 | 182 | var body = $('div.body'); |
michael@0 | 183 | window.setTimeout(function() { |
michael@0 | 184 | $.each(terms, function() { |
michael@0 | 185 | body.highlightText(this.toLowerCase(), 'highlighted'); |
michael@0 | 186 | }); |
michael@0 | 187 | }, 10); |
michael@0 | 188 | $('<p class="highlight-link"><a href="javascript:Documentation.' + |
michael@0 | 189 | 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>') |
michael@0 | 190 | .appendTo($('#searchbox')); |
michael@0 | 191 | } |
michael@0 | 192 | }, |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * init the domain index toggle buttons |
michael@0 | 196 | */ |
michael@0 | 197 | initIndexTable : function() { |
michael@0 | 198 | var togglers = $('img.toggler').click(function() { |
michael@0 | 199 | var src = $(this).attr('src'); |
michael@0 | 200 | var idnum = $(this).attr('id').substr(7); |
michael@0 | 201 | $('tr.cg-' + idnum).toggle(); |
michael@0 | 202 | if (src.substr(-9) == 'minus.png') |
michael@0 | 203 | $(this).attr('src', src.substr(0, src.length-9) + 'plus.png'); |
michael@0 | 204 | else |
michael@0 | 205 | $(this).attr('src', src.substr(0, src.length-8) + 'minus.png'); |
michael@0 | 206 | }).css('display', ''); |
michael@0 | 207 | if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) { |
michael@0 | 208 | togglers.click(); |
michael@0 | 209 | } |
michael@0 | 210 | }, |
michael@0 | 211 | |
michael@0 | 212 | /** |
michael@0 | 213 | * helper function to hide the search marks again |
michael@0 | 214 | */ |
michael@0 | 215 | hideSearchWords : function() { |
michael@0 | 216 | $('#searchbox .highlight-link').fadeOut(300); |
michael@0 | 217 | $('span.highlighted').removeClass('highlighted'); |
michael@0 | 218 | }, |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * make the url absolute |
michael@0 | 222 | */ |
michael@0 | 223 | makeURL : function(relativeURL) { |
michael@0 | 224 | return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL; |
michael@0 | 225 | }, |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * get the current relative url |
michael@0 | 229 | */ |
michael@0 | 230 | getCurrentURL : function() { |
michael@0 | 231 | var path = document.location.pathname; |
michael@0 | 232 | var parts = path.split(/\//); |
michael@0 | 233 | $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() { |
michael@0 | 234 | if (this == '..') |
michael@0 | 235 | parts.pop(); |
michael@0 | 236 | }); |
michael@0 | 237 | var url = parts.join('/'); |
michael@0 | 238 | return path.substring(url.lastIndexOf('/') + 1, path.length - 1); |
michael@0 | 239 | } |
michael@0 | 240 | }; |
michael@0 | 241 | |
michael@0 | 242 | // quick alias for translations |
michael@0 | 243 | _ = Documentation.gettext; |
michael@0 | 244 | |
michael@0 | 245 | $(document).ready(function() { |
michael@0 | 246 | Documentation.init(); |
michael@0 | 247 | }); |