Add table of contents to documentation pages
[exim-website.git] / templates / web / doc / chapter.js
1 // Sidebar table of contents
2         (function( $ ){
3                 
4                 var click_func = function(e){
5                         if( $('#toc').data('opened') ){
6                                 $('#toc > *').animate({ left: '-=' + $('#toc > ul').width() + 'px' },'fast');
7                                 $('#toc').removeData('opened');
8                         } else {
9                                 $('#toc > *').animate({ left: '+=' + $('#toc > ul').width() + 'px' },'fast');
10                                 $('#toc').data('opened',1);
11                         }
12                 };
13
14                 var type = document.location.pathname.match(/\/doc\/html\/spec_html\/filter/) ? 'filter' : 'spec';
15                 
16                 // Get the relevant table of contents
17                 $.get( type === 'spec' ? 'index_toc.xml' : 'filter_toc.xml',
18                         function( xml ){
19
20                                 // Remove the main list from the DOM for performance
21                                 var $ul = $('#toc > ul').remove();
22
23                                 // Traverse chapters
24                                 var chapter_id = 0;
25                                 $(xml).find('c').each(function(){
26                                         ++chapter_id;
27                                         var chapter_title = $(this).children('t').text();
28                                         var chapter_url   = $(this).children('u').text();
29
30                                         var chapter_li = $('<li/>').append(
31                                                 $('<a/>').attr({
32                                                         href:   chapter_url,
33                                                         title:  chapter_title
34                                                 }).text( chapter_id + '. ' + chapter_title ),
35                                                 $('<ul/>').hide()
36                                         ).appendTo( $ul );
37                                 });
38
39                                 $('#toc img').fadeIn('slow',function(){
40                                         // Add the main list back to the DOM
41                                         $ul
42                                                 .removeClass( 'hidden' )
43                                                 .css( 'visibility', 'hidden' )
44                                                 .appendTo( '#toc' )
45                                                 .css( 'left', '-' + $ul.width() + 'px' )
46                                                 .css( 'visibility', 'visible' );
47                                                 $('#toc > img').mousedown(click_func);
48                                                 $('#toc > ul').click(click_func);
49                                                 $('#toc a').click(function(e){e.stopPropagation()});
50                                 });
51                         }
52                 );
53         })( jQuery );
54