1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/**
* Creates a table of contents inside the given element.
*/
function maketoc(element)
{
var tmp = crawlDom(document.body, 2, 4, [], 30);
if (tmp.childNodes.length > 0)
{
element.appendChild(tmp);
}
}
function crawlDom(parent, ignore, depth, chapters, indent)
{
var doc = parent.ownerDocument;
var toc = doc.createElement('ul');
toc.style.listStyleType = 'none';
var child = parent.firstChild;
var lastLevel = 0;
while (child != null)
{
var name = child.nodeName.toLowerCase();
if (name.substring(0, 1) == 'h')
{
var tmp = name.substring(1, name.length);
var currentLevel = parseInt(tmp);
// Checks if rest of string is numeric and
// header level is not beyond depth
if (currentLevel == tmp && (depth == 0 || currentLevel <= depth))
{
// Deletes chapter numbers which are no longer used
if (currentLevel < lastLevel)
{
chapters = chapters.slice(0, currentLevel + 1);
}
lastLevel = currentLevel;
if (ignore <= 0)
{
if (chapters[currentLevel] == null)
{
chapters[currentLevel] = 0;
}
chapters[currentLevel]++;
var sect = '';
for (var i = 0; i < chapters.length; i++)
{
if (chapters[i] != null)
{
sect += chapters[i] + '.';
}
}
var tmp = child.firstChild;
while (tmp != null &&
tmp.nodeType != 3)
{
tmp = tmp.nextSibling;
}
if (tmp != null)
{
sect = sect.substring(0, sect.length - 1);
var title = tmp.nodeValue;
var anchor = null;
if (navigator.userAgent.indexOf('MSIE') >= 0)
{
// Setting the name tag here is a workaround for IE which sets the
// submitName attribute instead when using setAttribute('name', sect)
anchor = doc.createElement('<a name="'+sect+'"></a>');
}
else
{
anchor = doc.createElement('a');
anchor.setAttribute('name', sect);
}
anchor.appendChild(doc.createTextNode(sect+' '));
child.insertBefore(anchor, tmp);
// Adds entry in the table of contents
var listItem = doc.createElement('li');
listItem.style.paddingLeft = ((currentLevel - 1) * indent) + 'px';
var anchor2 = doc.createElement('a');
anchor2.setAttribute('href', '#'+sect);
anchor2.appendChild(doc.createTextNode(sect + ' ' + title));
listItem.appendChild(anchor2);
toc.appendChild(listItem);
}
}
else
{
ignore--;
}
}
}
var tmp = crawlDom(child, 0, depth, chapters, indent);
if (tmp.childNodes.length > 0)
{
toc.appendChild(tmp);
}
child = child.nextSibling;
}
return toc;
}
|