-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.js
97 lines (78 loc) · 2.69 KB
/
site.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
89
90
91
92
93
94
95
96
97
;(function () {
'use strict';
var debounce = function(func, waitTime) {
var timeout = false;
return function() {
if (timeout === false) {
setTimeout(function() {
func();
timeout = false;
}, waitTime);
timeout = true;
}
};
};
function loadToc($content, $toc, selector, topPad) {
var headerHeights = {};
var recacheHeights = function() {
headerHeights = {};
$content.find(selector).each(function() {
headerHeights[$(this).attr('id')] = $(this).offset().top;
});
};
var refreshToc = function() {
$toc.find(".active").removeClass("active");
var currentTop = $(document).scrollTop();
var best = null;
for (var name in headerHeights) {
if ((headerHeights[name] < currentTop + topPad && headerHeights[name] > headerHeights[best]) || best === null) {
best = name;
}
}
$(".toc a[href='#" + best + "']").addClass("active");
};
var generateToc = function() {
var tocHtml = $content.find(selector).map(function() {
var text = $(this).data("toc-title") || $(this).text();
var href;
if ($(this).attr("id") === undefined) {
$(this).attr("id", encodeURIComponent($(this).text().replace(/ /gi, "-")));
}
var href = "#" + $(this).attr("id");
return "<a class='toc-link' href='" + href + "'>" + text + "</a>";
}).get().join('');
$toc.prepend(tocHtml);
}
var makeToc = function() {
generateToc();
recacheHeights();
refreshToc();
// reload immediately after scrolling on toc click
$toc.find('a.toc-link').click(function() { // todo refactor out class names
setTimeout(function() {
refreshToc();
$('.toc-wrap').removeClass('visible'); // todo refactor out class names
$(window).scrollTop($(window).scrollTop() - topPad + 1);
}, 0);
});
$('a.toc-show-link').click(function() { // todo refactor out class names
$('.toc-wrap').toggleClass('visible'); // todo refactor out class names
return false;
});
$(window).scroll(debounce(refreshToc, 200));
$(window).resize(debounce(recacheHeights, 200));
};
$(makeToc);
window.recacheHeights = recacheHeights;
return function() {
var isDesktop = $(window).width() > 767;
$('.toc-wrap').each(function() {
if ($(this).offset().top <= $(document).scrollTop() + topPad && isDesktop) {
$(this).children('.toc').css("top", topPad + "px").addClass('floating');
} else {
$(this).children('.toc').css("top", "").removeClass('floating');
}
})
};
}
})();