📚 The CoCalc Library - books, templates and other resources
License: OTHER
/*1* doctools.js2* ~~~~~~~~~~~3*4* Sphinx JavaScript utilities for all documentation.5*6* :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.7* :license: BSD, see LICENSE for details.8*9*/1011/**12* select a different prefix for underscore13*/14$u = _.noConflict();1516/**17* make the code below compatible with browsers without18* an installed firebug like debugger19if (!window.console || !console.firebug) {20var names = ["log", "debug", "info", "warn", "error", "assert", "dir",21"dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace",22"profile", "profileEnd"];23window.console = {};24for (var i = 0; i < names.length; ++i)25window.console[names[i]] = function() {};26}27*/2829/**30* small helper function to urldecode strings31*/32jQuery.urldecode = function(x) {33return decodeURIComponent(x).replace(/\+/g, ' ');34};3536/**37* small helper function to urlencode strings38*/39jQuery.urlencode = encodeURIComponent;4041/**42* This function returns the parsed url parameters of the43* current request. Multiple values per key are supported,44* it will always return arrays of strings for the value parts.45*/46jQuery.getQueryParameters = function(s) {47if (typeof s == 'undefined')48s = document.location.search;49var parts = s.substr(s.indexOf('?') + 1).split('&');50var result = {};51for (var i = 0; i < parts.length; i++) {52var tmp = parts[i].split('=', 2);53var key = jQuery.urldecode(tmp[0]);54var value = jQuery.urldecode(tmp[1]);55if (key in result)56result[key].push(value);57else58result[key] = [value];59}60return result;61};6263/**64* highlight a given string on a jquery object by wrapping it in65* span elements with the given class name.66*/67jQuery.fn.highlightText = function(text, className) {68function highlight(node) {69if (node.nodeType == 3) {70var val = node.nodeValue;71var pos = val.toLowerCase().indexOf(text);72if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) {73var span = document.createElement("span");74span.className = className;75span.appendChild(document.createTextNode(val.substr(pos, text.length)));76node.parentNode.insertBefore(span, node.parentNode.insertBefore(77document.createTextNode(val.substr(pos + text.length)),78node.nextSibling));79node.nodeValue = val.substr(0, pos);80}81}82else if (!jQuery(node).is("button, select, textarea")) {83jQuery.each(node.childNodes, function() {84highlight(this);85});86}87}88return this.each(function() {89highlight(this);90});91};9293/*94* backward compatibility for jQuery.browser95* This will be supported until firefox bug is fixed.96*/97if (!jQuery.browser) {98jQuery.uaMatch = function(ua) {99ua = ua.toLowerCase();100101var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||102/(webkit)[ \/]([\w.]+)/.exec(ua) ||103/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||104/(msie) ([\w.]+)/.exec(ua) ||105ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||106[];107108return {109browser: match[ 1 ] || "",110version: match[ 2 ] || "0"111};112};113jQuery.browser = {};114jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;115}116117/**118* Small JavaScript module for the documentation.119*/120var Documentation = {121122init : function() {123this.fixFirefoxAnchorBug();124this.highlightSearchWords();125this.initIndexTable();126127},128129/**130* i18n support131*/132TRANSLATIONS : {},133PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; },134LOCALE : 'unknown',135136// gettext and ngettext don't access this so that the functions137// can safely bound to a different name (_ = Documentation.gettext)138gettext : function(string) {139var translated = Documentation.TRANSLATIONS[string];140if (typeof translated == 'undefined')141return string;142return (typeof translated == 'string') ? translated : translated[0];143},144145ngettext : function(singular, plural, n) {146var translated = Documentation.TRANSLATIONS[singular];147if (typeof translated == 'undefined')148return (n == 1) ? singular : plural;149return translated[Documentation.PLURALEXPR(n)];150},151152addTranslations : function(catalog) {153for (var key in catalog.messages)154this.TRANSLATIONS[key] = catalog.messages[key];155this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')');156this.LOCALE = catalog.locale;157},158159/**160* add context elements like header anchor links161*/162addContextElements : function() {163$('div[id] > :header:first').each(function() {164$('<a class="headerlink">\u00B6</a>').165attr('href', '#' + this.id).166attr('title', _('Permalink to this headline')).167appendTo(this);168});169$('dt[id]').each(function() {170$('<a class="headerlink">\u00B6</a>').171attr('href', '#' + this.id).172attr('title', _('Permalink to this definition')).173appendTo(this);174});175},176177/**178* workaround a firefox stupidity179* see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075180*/181fixFirefoxAnchorBug : function() {182if (document.location.hash)183window.setTimeout(function() {184document.location.href += '';185}, 10);186},187188/**189* highlight the search words provided in the url in the text190*/191highlightSearchWords : function() {192var params = $.getQueryParameters();193var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];194if (terms.length) {195var body = $('div.body');196if (!body.length) {197body = $('body');198}199window.setTimeout(function() {200$.each(terms, function() {201body.highlightText(this.toLowerCase(), 'highlighted');202});203}, 10);204$('<p class="highlight-link"><a href="javascript:Documentation.' +205'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')206.appendTo($('#searchbox'));207}208},209210/**211* init the domain index toggle buttons212*/213initIndexTable : function() {214var togglers = $('img.toggler').click(function() {215var src = $(this).attr('src');216var idnum = $(this).attr('id').substr(7);217$('tr.cg-' + idnum).toggle();218if (src.substr(-9) == 'minus.png')219$(this).attr('src', src.substr(0, src.length-9) + 'plus.png');220else221$(this).attr('src', src.substr(0, src.length-8) + 'minus.png');222}).css('display', '');223if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) {224togglers.click();225}226},227228/**229* helper function to hide the search marks again230*/231hideSearchWords : function() {232$('#searchbox .highlight-link').fadeOut(300);233$('span.highlighted').removeClass('highlighted');234},235236/**237* make the url absolute238*/239makeURL : function(relativeURL) {240return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;241},242243/**244* get the current relative url245*/246getCurrentURL : function() {247var path = document.location.pathname;248var parts = path.split(/\//);249$.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {250if (this == '..')251parts.pop();252});253var url = parts.join('/');254return path.substring(url.lastIndexOf('/') + 1, path.length - 1);255},256257initOnKeyListeners: function() {258$(document).keyup(function(event) {259var activeElementType = document.activeElement.tagName;260// don't navigate when in search box or textarea261if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT') {262switch (event.keyCode) {263case 37: // left264var prevHref = $('link[rel="prev"]').prop('href');265if (prevHref) {266window.location.href = prevHref;267return false;268}269case 39: // right270var nextHref = $('link[rel="next"]').prop('href');271if (nextHref) {272window.location.href = nextHref;273return false;274}275}276}277});278}279};280281// quick alias for translations282_ = Documentation.gettext;283284$(document).ready(function() {285Documentation.init();286});287288