ah_toc.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // $Id$
  2. /**
  3. * @file
  4. * TOC builder for advanced_help pages
  5. *
  6. * @copyright Ouest Systèmes Informatiques (OSInet)
  7. *
  8. * @license Licensed under the General Public License version 2 and later.
  9. */
  10. /**
  11. * Build a TOC div from the Hn elements in a given context
  12. *
  13. * Extract the H[1-7] list in DOM order manually: with jQuery < 1.4,
  14. * neither $('h1, h2...') nor .add() return in DOM order, hence the use
  15. * of filter() on a * selector instead.
  16. *
  17. * @return jQuery
  18. * A list of the headings in the page
  19. */
  20. function ahTocBuildToc() {
  21. var context = $('div.advanced-help-topic');
  22. var elements = $('*', context).filter(function (index) {
  23. return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) != -1;
  24. });
  25. /**
  26. * Only build the toc if there 2 headings or more in the page
  27. */
  28. if (elements.length <= 1) {
  29. return;
  30. }
  31. // Build the headings list
  32. var list = "<ul>\n";
  33. elements.each(function() {
  34. list += '<li class="' + this.nodeName.toLowerCase() + '">'
  35. + $(this).text() + "</li>\n";
  36. })
  37. list += "</ul>\n";
  38. // Avoid creating multiple TOCs if this function is called several times
  39. $('div.ah-toc', context).remove();
  40. // @TODO use a theme function
  41. context.prepend('<div class="ah-toc"><center>Contents</center></div>');
  42. $('div.ah-toc').append(list);
  43. return elements;
  44. }
  45. $(ahTocBuildToc);