3 /* Creates table of contents for pages for h[1234] */
6 var dest
, content
, location
, stack
, oltoc
, numdigits
, wlh
, target
,
9 dest
= $('dl.toc dd.portletItem');
10 content
= $('#region-content,#content');
11 if (!content
|| !dest
.length
) {return;}
15 location
= window
.location
.href
;
16 if (window
.location
.hash
) {
17 location
= location
.substring(0, location
.lastIndexOf(window
.location
.hash
));
20 // Get headers in document order
21 $(content
).find('*').not('.comment > h3').filter(function() { return (/^h[1234]$/).test(this.tagName
.toLowerCase()); })
22 .not('.documentFirstHeading').each(function(i
) {
25 level
= this.nodeName
.charAt(1);
26 // size the stack to the current level
27 while (stack
.length
< level
) {
30 li
= $(stack
[stack
.length
- 1]).children('li:last');
32 // create a blank li for cases where, e.g., we have a subheading before any headings
33 li
= $('<li>').appendTo($(stack
[stack
.length
- 1]));
39 while (stack
.length
> level
) {stack
.pop();}
41 $(this).before($('<a name="section-' + i
+ '" />'));
43 $('<a />').attr('href', location
+ '#section-' + i
)
44 .text($(this).text()))
45 .appendTo($(stack
[stack
.length
- 1]));
49 var oltoc
= $(stack
[0]);
50 // first level is a level with at least two entries #11160
52 while(oltoc
.children('li').length
== 1){
57 if (i
<= stack
.length
) {
61 numdigits
= oltoc
.children().length
.toString().length
;
62 //Use a clever class name to add margin that's MUCH easier to customize
63 oltoc
.addClass("TOC"+numdigits
+"Digit");
66 //scroll to element now.
67 wlh
= window
.location
.hash
;
70 target
= target
.length
&& target
71 || $('[name=' + wlh
.slice(1) +']');
72 targetOffset
= target
.offset();
74 $('html,body').animate({scrollTop
: targetOffset
.top
}, 0);