adding all weblabels from weblabels.fsf.org
[weblabels.fsf.org.git] / www.fsf.org / 20131028 / files / static.fsf.org / plone2012 / toc.js
1
2 /* - toc.js - */
3 /* Creates table of contents for pages for h[1234] */
4
5 jQuery(function($) {
6 var dest, content, location, stack, oltoc, numdigits, wlh, target,
7 targetOffset;
8
9 dest = $('dl.toc dd.portletItem');
10 content = $('#region-content,#content');
11 if (!content || !dest.length) {return;}
12
13 dest.empty();
14
15 location = window.location.href;
16 if (window.location.hash) {
17 location = location.substring(0, location.lastIndexOf(window.location.hash));
18 }
19 stack = [];
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) {
23 var level, ol, li;
24
25 level = this.nodeName.charAt(1);
26 // size the stack to the current level
27 while (stack.length < level) {
28 ol = $('<ol>');
29 if (stack.length) {
30 li = $(stack[stack.length - 1]).children('li:last');
31 if (!li.length) {
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]));
34 }
35 li.append(ol);
36 }
37 stack.push(ol);
38 }
39 while (stack.length > level) {stack.pop();}
40
41 $(this).before($('<a name="section-' + i + '" />'));
42 $('<li>').append(
43 $('<a />').attr('href', location + '#section-' + i)
44 .text($(this).text()))
45 .appendTo($(stack[stack.length - 1]));
46 });
47
48 if (stack.length) {
49 var oltoc = $(stack[0]);
50 // first level is a level with at least two entries #11160
51 var i = 1;
52 while(oltoc.children('li').length == 1){
53 oltoc = $(stack[i]);
54 i += 1;
55 }
56
57 if (i <= stack.length) {
58 $('dl.toc').show();
59 }
60
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");
64 dest.append(oltoc);
65
66 //scroll to element now.
67 wlh = window.location.hash;
68 if (wlh) {
69 target = $(wlh);
70 target = target.length && target
71 || $('[name=' + wlh.slice(1) +']');
72 targetOffset = target.offset();
73 if (targetOffset) {
74 $('html,body').animate({scrollTop: targetOffset.top}, 0);
75 }
76 }
77 }
78 });
79