// $Id$ /** * @file * TOC builder for advanced_help pages * * @copyright Ouest Systèmes Informatiques (OSInet) * * @license Licensed under the General Public License version 2 and later. */ /*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, indent: 2 */ /*global $*/ /** * Provide an appropriate toggle message depending on TOC display state * * @return string */ function ahToggleGetText(state) { return state ? Drupal.t('[ + ]') : Drupal.t('[ - ]'); } /** * Toggle the state of the headings list and the label of the toggle text. * * @return void */ function ahTocToggle() { var display = $('.ah-toc ul').css('display'); $('.ah-toc .ah-toggle').text(ahToggleGetText(display !== 'none')); $('.ah-toc ul').toggle(100); } /** * Build a TOC div from the Hn elements in a given context * * Extract the H[1-7] list in DOM order manually: with jQuery < 1.4, * neither $('h1, h2...') nor .add() return in DOM order, hence the use * of filter() on a * selector instead. * * @return jQuery * A list of the headings in the page */ function ahTocBuildToc() { var context, // the context of the help page elements, // the list of headings element, // one of the headings list, // the string form of the toc list index, // a running index for missing heading IDs id; // the current id when iterating on the headings context = $('div.advanced-help-topic'); elements = $('*', context).filter(function (index) { return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7'].indexOf(this.nodeName) !== -1; }); /** * Only build the toc if there 2 headings or more in the page */ if (elements.length <= 1) { return; } // Build the headings list list = "\n"; // Avoid creating multiple TOCs if this function is called several times $('div.ah-toc', context).remove(); // @TODO use a theme function context.prepend('
' + Drupal.t('Table of contents') + '
'); $('div.ah-toc').append(list); $('' + ahToggleGetText(false) + '').appendTo($('div.ah-toc-title')).click(ahTocToggle); } $(ahTocBuildToc);