-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.toc.js
More file actions
181 lines (162 loc) · 6.03 KB
/
jquery.toc.js
File metadata and controls
181 lines (162 loc) · 6.03 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// https://github.com/ghiculescu/jekyll-table-of-contents
// Updated by http://mazhuang.org
(function($){
$.fn.toc = function(options) {
var defaults = {
noBackToTopLinks: false,
title: '文章目录',
minimumHeaders: 2,
headers: 'h1, h2, h3, h4, h5, h6',
listType: 'ul', // values: [ol|ul]
showEffect: 'show', // values: [show|slideDown|fadeIn|none]
showSpeed: 0 // set to 0 to deactivate effect
},
settings = $.extend(defaults, options);
function fixedEncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
var headers = $(settings.headers).filter(function() {
// get all headers with an ID
var previousSiblingName = $(this).prev().attr( "name" );
if (!this.id && previousSiblingName) {
this.id = $(this).attr( "id", previousSiblingName.replace(/\./g, "-") );
}
return this.id;
}), output = $(this);
if (!headers.length || headers.length < settings.minimumHeaders || !output.length) {
$(this).hide();
$('.post-directory-title').css('display', 'none');
return;
}
if (0 === settings.showSpeed) {
settings.showEffect = 'none';
}
var render = {
show: function() { output.hide().html(html).show(settings.showSpeed); },
slideDown: function() { output.hide().html(html).slideDown(settings.showSpeed); },
fadeIn: function() { output.hide().html(html).fadeIn(settings.showSpeed); },
none: function() { output.html(html); }
};
var get_level = function(ele) { return parseInt(ele.nodeName.replace("H", ""), 10); }
var highest_level = headers.map(function(_, ele) { return get_level(ele); }).get().sort()[0];
var return_to_top = '<i class="icon-arrow-up back-to-top"> </i>';
var level = get_level(headers[0]),
this_level,
html = "<p><strong class=\"toc-title\">" + settings.title + "</strong></p>\n";
html += " <"+settings.listType+" class=\"toc\">";
headers.on('click', function() {
if (!settings.noBackToTopLinks) {
window.location.hash = this.id;
}
})
.addClass('clickable-header')
.each(function(_, header) {
this_level = get_level(header);
if (!settings.noBackToTopLinks && this_level === highest_level) {
$(header).addClass('top-level-header').after(return_to_top);
}
if (this_level === level) {// same level as before; same indenting
html += "<li class=\"toc-item toc-level-" + this_level + "\">";
html += "<a class=\"jumper\" href='#" + fixedEncodeURIComponent(header.id) + "'>";
html += "<span class='toc-text'>" + header.innerHTML + "</span>";
html += "</a>";
} else if (this_level <= level){ // higher level than before; end parent ol
for(i = this_level; i < level; i++) {
html += "</li></"+settings.listType+">"
}
html += "<li class='toc-item toc-level-" + this_level + "'><a class=\"jumper\" href='#" + fixedEncodeURIComponent(header.id) + "'>";
html += "<span class='toc-text'>" + header.innerHTML + "</span>";
html += "</a>";
}
else if (this_level > level) { // lower level than before; expand the previous to contain a ol
for(i = this_level; i > level; i--) {
html += "<"+settings.listType+" class='toc-child'><li class='toc-item toc-level-" + i + "'>"
}
html += "<a class=\"jumper\" href='#" + fixedEncodeURIComponent(header.id) + "'>";
html += "<span class='toc-text'>" + header.innerHTML + "</span>";
html += "</a>";
}
level = this_level; // update for the next one
});
html += "</"+settings.listType+">";
if (!settings.noBackToTopLinks) {
$(document).on('click', '.back-to-top', function() {
$(window).scrollTop(0);
window.location.hash = '';
});
}
render[settings.showEffect]();
};
})(jQuery);
$(document).ready(function(){
$('.post-directory').toc();
var fixmeTop = $('#post-directory-module').offset().top;
var tocSections = $('.clickable-header');
var tocSectionOffsets = [];
var calculateTocSections = function(){
tocSectionOffsets = [];
tocSections.each(function(index, section){
tocSectionOffsets.push(section.offsetTop);
})
}
calculateTocSections();
// Calculates the toc section offsets, which can change as images get loaded
$(window).bind('load', calculateTocSections);
var highlightTocSection = function(){
var highlightIndex = 0;
var sectionsCount = tocSectionOffsets.length;
var currentScroll = $(window).scrollTop();
if (currentScroll+60 > tocSectionOffsets[sectionsCount-1]) {
highlightIndex = sectionsCount;
} else {
for (var i=0; i<sectionsCount; i++) {
if (currentScroll+60 <= tocSectionOffsets[i]) {
highlightIndex = i;
break;
}
}
}
if (highlightIndex == 0) {
highlightIndex += 1;
}
$('.toc-item .jumper').removeClass('on');
$('.toc-item .jumper').eq(highlightIndex-1).addClass('on');
}
highlightTocSection();
var updateTocHeight = function() {
var height = document.documentElement.clientHeight;
height = height || 'auto';
$('.post-directory').css('max-height', height);
}
$(window).scroll(function() {
var currentScroll = $(window).scrollTop();
if (currentScroll >= fixmeTop) {
$('#post-directory-module').css({
top: '0',
position: 'fixed',
width: 'inherit'
});
$('.post-directory').css('overflow', 'auto');
} else {
$('#post-directory-module').css({
position: 'inherit',
width: 'inherit'
});
$('.post-directory').css('overflow', 'hidden');
$('.post-directory').scrollTop(0);
}
highlightTocSection();
});
updateTocHeight();
$(window).on("resize", function() {
updateTocHeight();
});
});
$(".jumper").on("click", function( e ) {
e.preventDefault();
$("body, html").animate({
scrollTop: $( $(this).attr('href') ).offset().top
}, 600);
});