maxGraph/docs/js/toc.js

133 lines
2.9 KiB
JavaScript
Raw Normal View History

2012-05-21 20:32:26 +00:00
/**
* Creates a table of contents inside the given element.
*/
function maketoc(element, enableSections)
{
enableSections = (enableSections != null) ? enableSections : true;
2021-03-21 01:24:29 +00:00
let tmp = crawlDom(document.body, 2, 4, [], 30, enableSections);
2012-05-21 20:32:26 +00:00
if (tmp.childNodes.length > 0)
{
element.appendChild(tmp);
}
}
function crawlDom(parent, ignore, depth, chapters, indent, enableSections)
{
2021-03-21 01:24:29 +00:00
let doc = parent.ownerDocument;
let toc = doc.createElement('ul');
2012-05-21 20:32:26 +00:00
toc.style.listStyleType = 'none';
2021-03-21 01:24:29 +00:00
let child = parent.firstChild;
let lastLevel = 0;
2012-05-21 20:32:26 +00:00
while (child != null)
{
2021-03-21 01:24:29 +00:00
let name = child.nodeName.toLowerCase();
2012-05-21 20:32:26 +00:00
if (name.substring(0, 1) == 'h')
{
2021-03-21 01:24:29 +00:00
let tmp = name.substring(1, name.length);
let currentLevel = parseInt(tmp);
2012-05-21 20:32:26 +00:00
// 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]++;
2021-03-21 01:24:29 +00:00
let sect = '';
2012-05-21 20:32:26 +00:00
2021-03-21 01:24:29 +00:00
for (let i = 0; i < chapters.length; i++)
2012-05-21 20:32:26 +00:00
{
if (chapters[i] != null)
{
sect += chapters[i] + '.';
}
}
2021-03-21 01:24:29 +00:00
let tmp = child.firstChild;
2012-05-21 20:32:26 +00:00
while (tmp != null &&
tmp.nodeType != 3)
{
tmp = tmp.nextSibling;
}
if (tmp != null)
{
sect = sect.substring(0, sect.length - 1);
2021-03-21 01:24:29 +00:00
let title = tmp.nodeValue;
let anchor = null;
2012-05-21 20:32:26 +00:00
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);
}
if (enableSections)
{
anchor.appendChild(doc.createTextNode(sect+' '));
}
child.insertBefore(anchor, tmp);
// Adds entry in the table of contents
2021-03-21 01:24:29 +00:00
let listItem = doc.createElement('li');
2012-05-21 20:32:26 +00:00
listItem.style.paddingLeft = ((currentLevel - 1) * indent) + 'px';
var anchor2 = doc.createElement('a');
anchor2.setAttribute('href', '#'+sect);
if (enableSections)
{
anchor2.appendChild(doc.createTextNode(sect + ' ' + title));
}
else
{
anchor2.appendChild(doc.createTextNode(title));
}
listItem.appendChild(anchor2);
toc.appendChild(listItem);
}
}
else
{
ignore--;
}
}
}
2021-03-21 01:24:29 +00:00
let tmp = crawlDom(child, 0, depth, chapters, indent);
2012-05-21 20:32:26 +00:00
if (tmp.childNodes.length > 0)
{
toc.appendChild(tmp);
}
child = child.nextSibling;
}
return toc;
}