-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jqTOC.js
88 lines (80 loc) · 3.09 KB
/
jquery.jqTOC.js
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
/* Copyright 2009 David Gilbert.
This file is jquery.jqTOC.js; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
For documentation refer to: http://solidgone.org/Jqtoc
*/
(function($){
$.fn.jqTOC = function(settings) {
function tocToggleDisplay(e){
$('#'+settings.tocContainer+' .toc_content')[e.data.mode]();
}
settings = $.extend({
tocWidth: '220px',
tocTitle: 'Content',
tocStart: 1,
tocEnd : 3,
tocContainer : 'toc_container',
tocAutoClose : true,
tocShowOnClick : true,
tocTopLink : 'top'
}, settings || {});
// create the main content container if not already created
if (document.getElementById(settings.tocContainer) == null)
$('body').append('<div id="'+settings.tocContainer+'"></div>');
$('#'+settings.tocContainer).css('width',settings.tocWidth).append(
(settings.tocTitle?'<div class="toc_header">'+ settings.tocTitle + '</div>':'') +
'<div class="toc_content"></div>'
);
var t = $('#'+settings.tocContainer+' .toc_content');
var headerLevel,headerId;
// Find the highest level heading used within the range tocStart..tocEnd. Prevents indenting when no higher level exists.
var start=settings.tocEnd;
this.children().each(function(i) {
headerLevel = this.nodeName.substr(1);
if(
this.nodeName.match(/^H\d+$/)
&& headerLevel >= settings.tocStart
&& headerLevel <= settings.tocEnd
&& this.nodeName.substr(1) < start
) {
start = this.nodeName.substr(1);
}
if (start == settings.tocStart) {
return false;
}
});
settings.tocStart=start;
this.children().each(function(i) {
headerLevel = this.nodeName.substr(1);
if(
this.nodeName.match(/^H\d+$/)
&& headerLevel >= settings.tocStart
&& headerLevel <= settings.tocEnd
) {
headerId = this.id || 'jqTOC_link' + i;
t.append('<a href="#'+ headerId +'" style="margin-left: ' + (headerLevel-settings.tocStart+1)*1.4 +'em;" ' +
(headerLevel != settings.tocStart ? 'class="indent" ': '') +
'>'+ $(this).text() +'</a>'
);
this.id = headerId;
if (settings.tocTopLink) {
$(this).before('<div class="toc_top"><a href="#">'+settings.tocTopLink+'</a></div>');
}
}
});
if (settings.tocShowOnClick) {
if (settings.tocTitle) {
$('#'+settings.tocContainer+' .toc_header').bind('click', {mode: 'toggle'}, function(e){tocToggleDisplay(e);});
}
if (settings.tocAutoClose) {
$('#'+settings.tocContainer+' .toc_content a').bind('click', {mode: 'hide'}, function(e){tocToggleDisplay(e);});
}
} else {
$('#'+settings.tocContainer+' .toc_content').show();
}
if (settings.tocTopLink) {
$('.toc_top').bind('click', function(){window.scrollTo(0,0);});
}
return this;
}
})(jQuery);