96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
// $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 = "<ul>\n";
|
|
index = 0;
|
|
|
|
elements.each(function () {
|
|
element = $(this);
|
|
id = element.attr('id');
|
|
if (id === '') {
|
|
index += 1; // Avoid JSline warning on ++ use
|
|
id = 'toc' + index;
|
|
element.attr('id', id);
|
|
}
|
|
// @TODO use some form of l() instead of string concatenation
|
|
list += '<li class="' + this.nodeName.toLowerCase() + '">' +
|
|
'<a href="#' + id + '">' + element.text() + "</a></li>\n";
|
|
});
|
|
list += "</ul>\n";
|
|
|
|
// Avoid creating multiple TOCs if this function is called several times
|
|
$('div.ah-toc', context).remove();
|
|
|
|
// @TODO use a theme function
|
|
context.prepend('<div class="ah-toc"><div class="ah-toc-title">' + Drupal.t('Table of contents') + '</div></div>');
|
|
$('div.ah-toc').append(list);
|
|
$('<span class="ah-toggle">' + ahToggleGetText(false) +
|
|
'</span>').appendTo($('div.ah-toc-title')).click(ahTocToggle);
|
|
}
|
|
|
|
$(ahTocBuildToc);
|