PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/static/doctools.js

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