ah_toc.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 buildToc() {
  21. var contextSelector = 'div.advanced-help-topic';
  22. var context = $(contextSelector);
  23. var elements = $('*', contextSelector).filter(function (index) {
  24. return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) != -1;
  25. });
  26. /**
  27. * Only build the toc if there 2 headings or more in the page
  28. */
  29. if (elements.length <= 1) {
  30. return;
  31. }
  32. // Build the headings list
  33. var list = "<ul>\n";
  34. elements.each(function() {
  35. list += '<li class="' + this.nodeName.toLowerCase() + '">'
  36. + $(this).text() + "</li>\n";
  37. })
  38. list += "</ul>\n";
  39. // Avoid creating multiple TOCs if this function is called several times
  40. $('div.ah-toc', context).remove();
  41. // @TODO use a theme function
  42. context.prepend('<div class="ah-toc"><center>Contents</center></div>');
  43. $('div.ah-toc').append(list);
  44. return elements;
  45. }
  46. $(buildToc);