PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/_static/doctools.js

https://github.com/safl/safl.github.com
JavaScript | 326 lines | 223 code | 25 blank | 78 comment | 39 complexity | c30bb3eacf6fb76690494ef92962c8e0 MD5 | raw file
  1. /*
  2. * doctools.js
  3. * ~~~~~~~~~~~
  4. *
  5. * Sphinx JavaScript utilities for all documentation.
  6. *
  7. * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
  8. * :license: BSD, see LICENSE for details.
  9. *
  10. */
  11. /**
  12. * select a different prefix for underscore
  13. */
  14. $u = _.noConflict();
  15. /**
  16. * make the code below compatible with browsers without
  17. * an installed firebug like debugger
  18. if (!window.console || !console.firebug) {
  19. var names = ["log", "debug", "info", "warn", "error", "assert", "dir",
  20. "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace",
  21. "profile", "profileEnd"];
  22. window.console = {};
  23. for (var i = 0; i < names.length; ++i)
  24. window.console[names[i]] = function() {};
  25. }
  26. */
  27. /**
  28. * small helper function to urldecode strings
  29. *
  30. * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
  31. */
  32. jQuery.urldecode = function(x) {
  33. if (!x) {
  34. return x
  35. }
  36. return decodeURIComponent(x.replace(/\+/g, ' '));
  37. };
  38. /**
  39. * small helper function to urlencode strings
  40. */
  41. jQuery.urlencode = encodeURIComponent;
  42. /**
  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.
  46. */
  47. jQuery.getQueryParameters = function(s) {
  48. if (typeof s === 'undefined')
  49. s = document.location.search;
  50. var parts = s.substr(s.indexOf('?') + 1).split('&');
  51. var result = {};
  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]);
  56. if (key in result)
  57. result[key].push(value);
  58. else
  59. result[key] = [value];
  60. }
  61. return result;
  62. };
  63. /**
  64. * highlight a given string on a jquery object by wrapping it in
  65. * span elements with the given class name.
  66. */
  67. jQuery.fn.highlightText = function(text, className) {
  68. function highlight(node, addItems) {
  69. if (node.nodeType === 3) {
  70. var val = node.nodeValue;
  71. var pos = val.toLowerCase().indexOf(text);
  72. if (pos >= 0 &&
  73. !jQuery(node.parentNode).hasClass(className) &&
  74. !jQuery(node.parentNode).hasClass("nohighlight")) {
  75. var span;
  76. var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
  77. if (isInSVG) {
  78. span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
  79. } else {
  80. span = document.createElement("span");
  81. span.className = className;
  82. }
  83. span.appendChild(document.createTextNode(val.substr(pos, text.length)));
  84. node.parentNode.insertBefore(span, node.parentNode.insertBefore(
  85. document.createTextNode(val.substr(pos + text.length)),
  86. node.nextSibling));
  87. node.nodeValue = val.substr(0, pos);
  88. if (isInSVG) {
  89. var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
  90. var bbox = node.parentElement.getBBox();
  91. rect.x.baseVal.value = bbox.x;
  92. rect.y.baseVal.value = bbox.y;
  93. rect.width.baseVal.value = bbox.width;
  94. rect.height.baseVal.value = bbox.height;
  95. rect.setAttribute('class', className);
  96. addItems.push({
  97. "parent": node.parentNode,
  98. "target": rect});
  99. }
  100. }
  101. }
  102. else if (!jQuery(node).is("button, select, textarea")) {
  103. jQuery.each(node.childNodes, function() {
  104. highlight(this, addItems);
  105. });
  106. }
  107. }
  108. var addItems = [];
  109. var result = this.each(function() {
  110. highlight(this, addItems);
  111. });
  112. for (var i = 0; i < addItems.length; ++i) {
  113. jQuery(addItems[i].parent).before(addItems[i].target);
  114. }
  115. return result;
  116. };
  117. /*
  118. * backward compatibility for jQuery.browser
  119. * This will be supported until firefox bug is fixed.
  120. */
  121. if (!jQuery.browser) {
  122. jQuery.uaMatch = function(ua) {
  123. ua = ua.toLowerCase();
  124. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  125. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  126. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  127. /(msie) ([\w.]+)/.exec(ua) ||
  128. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  129. [];
  130. return {
  131. browser: match[ 1 ] || "",
  132. version: match[ 2 ] || "0"
  133. };
  134. };
  135. jQuery.browser = {};
  136. jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
  137. }
  138. /**
  139. * Small JavaScript module for the documentation.
  140. */
  141. var Documentation = {
  142. init : function() {
  143. this.fixFirefoxAnchorBug();
  144. this.highlightSearchWords();
  145. this.initIndexTable();
  146. if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) {
  147. this.initOnKeyListeners();
  148. }
  149. },
  150. /**
  151. * i18n support
  152. */
  153. TRANSLATIONS : {},
  154. PLURAL_EXPR : function(n) { return n === 1 ? 0 : 1; },
  155. LOCALE : 'unknown',
  156. // gettext and ngettext don't access this so that the functions
  157. // can safely bound to a different name (_ = Documentation.gettext)
  158. gettext : function(string) {
  159. var translated = Documentation.TRANSLATIONS[string];
  160. if (typeof translated === 'undefined')
  161. return string;
  162. return (typeof translated === 'string') ? translated : translated[0];
  163. },
  164. ngettext : function(singular, plural, n) {
  165. var translated = Documentation.TRANSLATIONS[singular];
  166. if (typeof translated === 'undefined')
  167. return (n == 1) ? singular : plural;
  168. return translated[Documentation.PLURALEXPR(n)];
  169. },
  170. addTranslations : function(catalog) {
  171. for (var key in catalog.messages)
  172. this.TRANSLATIONS[key] = catalog.messages[key];
  173. this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')');
  174. this.LOCALE = catalog.locale;
  175. },
  176. /**
  177. * add context elements like header anchor links
  178. */
  179. addContextElements : function() {
  180. $('div[id] > :header:first').each(function() {
  181. $('<a class="headerlink">\u00B6</a>').
  182. attr('href', '#' + this.id).
  183. attr('title', _('Permalink to this headline')).
  184. appendTo(this);
  185. });
  186. $('dt[id]').each(function() {
  187. $('<a class="headerlink">\u00B6</a>').
  188. attr('href', '#' + this.id).
  189. attr('title', _('Permalink to this definition')).
  190. appendTo(this);
  191. });
  192. },
  193. /**
  194. * workaround a firefox stupidity
  195. * see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075
  196. */
  197. fixFirefoxAnchorBug : function() {
  198. if (document.location.hash && $.browser.mozilla)
  199. window.setTimeout(function() {
  200. document.location.href += '';
  201. }, 10);
  202. },
  203. /**
  204. * highlight the search words provided in the url in the text
  205. */
  206. highlightSearchWords : function() {
  207. var params = $.getQueryParameters();
  208. var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
  209. if (terms.length) {
  210. var body = $('div.body');
  211. if (!body.length) {
  212. body = $('body');
  213. }
  214. window.setTimeout(function() {
  215. $.each(terms, function() {
  216. body.highlightText(this.toLowerCase(), 'highlighted');
  217. });
  218. }, 10);
  219. $('<p class="highlight-link"><a href="javascript:Documentation.' +
  220. 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')
  221. .appendTo($('#searchbox'));
  222. }
  223. },
  224. /**
  225. * init the domain index toggle buttons
  226. */
  227. initIndexTable : function() {
  228. var togglers = $('img.toggler').click(function() {
  229. var src = $(this).attr('src');
  230. var idnum = $(this).attr('id').substr(7);
  231. $('tr.cg-' + idnum).toggle();
  232. if (src.substr(-9) === 'minus.png')
  233. $(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
  234. else
  235. $(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
  236. }).css('display', '');
  237. if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) {
  238. togglers.click();
  239. }
  240. },
  241. /**
  242. * helper function to hide the search marks again
  243. */
  244. hideSearchWords : function() {
  245. $('#searchbox .highlight-link').fadeOut(300);
  246. $('span.highlighted').removeClass('highlighted');
  247. var url = new URL(window.location);
  248. url.searchParams.delete('highlight');
  249. window.history.replaceState({}, '', url);
  250. },
  251. /**
  252. * make the url absolute
  253. */
  254. makeURL : function(relativeURL) {
  255. return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;
  256. },
  257. /**
  258. * get the current relative url
  259. */
  260. getCurrentURL : function() {
  261. var path = document.location.pathname;
  262. var parts = path.split(/\//);
  263. $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
  264. if (this === '..')
  265. parts.pop();
  266. });
  267. var url = parts.join('/');
  268. return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
  269. },
  270. initOnKeyListeners: function() {
  271. $(document).keydown(function(event) {
  272. var activeElementType = document.activeElement.tagName;
  273. // don't navigate when in search box, textarea, dropdown or button
  274. if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
  275. && activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey
  276. && !event.shiftKey) {
  277. switch (event.keyCode) {
  278. case 37: // left
  279. var prevHref = $('link[rel="prev"]').prop('href');
  280. if (prevHref) {
  281. window.location.href = prevHref;
  282. return false;
  283. }
  284. break;
  285. case 39: // right
  286. var nextHref = $('link[rel="next"]').prop('href');
  287. if (nextHref) {
  288. window.location.href = nextHref;
  289. return false;
  290. }
  291. break;
  292. }
  293. }
  294. });
  295. }
  296. };
  297. // quick alias for translations
  298. _ = Documentation.gettext;
  299. $(document).ready(function() {
  300. Documentation.init();
  301. });