ah_toc.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true,
  11. plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true,
  12. indent: 2 */
  13. /*global $*/
  14. /**
  15. * Provide an appropriate toggle message depending on TOC display state
  16. *
  17. * @return string
  18. */
  19. function ahToggleGetText(state) {
  20. return state ? Drupal.t('[ + ]') : Drupal.t('[ - ]');
  21. }
  22. /**
  23. * Toggle the state of the headings list and the label of the toggle text.
  24. *
  25. * @return void
  26. */
  27. function ahTocToggle() {
  28. var display = $('.ah-toc ul').css('display');
  29. $('.ah-toc .ah-toggle').text(ahToggleGetText(display !== 'none'));
  30. $('.ah-toc ul').toggle(100);
  31. }
  32. /**
  33. * Build a TOC div from the Hn elements in a given context
  34. *
  35. * Extract the H[1-7] list in DOM order manually: with jQuery < 1.4,
  36. * neither $('h1, h2...') nor .add() return in DOM order, hence the use
  37. * of filter() on a * selector instead.
  38. *
  39. * @return jQuery
  40. * A list of the headings in the page
  41. */
  42. function ahTocBuildToc() {
  43. var context, // the context of the help page
  44. elements, // the list of headings
  45. element, // one of the headings
  46. list, // the string form of the toc list
  47. index, // a running index for missing heading IDs
  48. id; // the current id when iterating on the headings
  49. context = $('div.advanced-help-topic');
  50. elements = $('*', context).filter(function (index) {
  51. return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) !== -1;
  52. });
  53. /**
  54. * Only build the toc if there 2 headings or more in the page
  55. */
  56. if (elements.length <= 1) {
  57. return;
  58. }
  59. // Build the headings list
  60. list = "<ul>\n";
  61. index = 0;
  62. elements.each(function () {
  63. element = $(this);
  64. id = element.attr('id');
  65. if (id === '') {
  66. index += 1; // Avoid JSline warning on ++ use
  67. id = 'toc' + index;
  68. element.attr('id', id);
  69. }
  70. // @TODO use some form of l() instead of string concatenation
  71. list += '<li class="' + this.nodeName.toLowerCase() + '">' +
  72. '<a href="#' + id + '">' + element.text() + "</a></li>\n";
  73. });
  74. list += "</ul>\n";
  75. // Avoid creating multiple TOCs if this function is called several times
  76. $('div.ah-toc', context).remove();
  77. // @TODO use a theme function
  78. context.prepend('<div class="ah-toc"><div class="ah-toc-title">' + Drupal.t('Table of contents') + '</div></div>');
  79. $('div.ah-toc').append(list);
  80. $('<span class="ah-toggle">' + ahToggleGetText(false) +
  81. '</span>').appendTo($('div.ah-toc-title')).click(ahTocToggle);
  82. }
  83. $(ahTocBuildToc);