-
Notifications
You must be signed in to change notification settings - Fork 612
/
Copy pathfootnotes.js
98 lines (87 loc) · 2.96 KB
/
footnotes.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
98
// Shows footnotes as popups, requires jQuery.
// Originally written by Lukas Mathis:
// http://ignorethecode.net/blog/2010/04/20/footnotes/
// Adapted by Andres Raba, public domain.
$(document).ready(function () {
var popup = "#footnote_popup";
var Footnotes = {
footnotetimeout: false,
setup: function () {
var body = $("section");
var footnotelinks = $("a.footnote_link");
body.attr('onclick', '');
footnotelinks.attr('tabindex', '0');
// Don't follow footnote link on click:
footnotelinks.click(function () { return false; });
body.unbind('click', Footnotes.bodyclick);
footnotelinks.unbind('click', Footnotes.footnoteclick);
footnotelinks.unbind('blur', Footnotes.footnoteout);
$(document).unbind('keydown', Footnotes.keydown);
body.bind('click', Footnotes.bodyclick);
// Bind new behaviour where click will pop up the footnote:
footnotelinks.bind('click', Footnotes.footnoteclick);
// ...and click outside of popup will make it disappear:
footnotelinks.bind('blur', Footnotes.footnoteout);
// ...escape key should also do the job:
$(document).bind('keydown', Footnotes.keydown);
},
bodyclick: function () { return; },
keydown: function (event) {
// Capture escape key.
if (event.which == 27) {
Footnotes.footnoteout();
}
},
footnoteclick: function () {
clearTimeout(Footnotes.footnotetimeout);
$(popup).stop();
$(popup).remove();
var id = $(this).attr('href').substr(1);
var position = $(this).offset();
var div = $(document.createElement('div'));
div.attr('id', popup.substr(1));
// To be able to fire blur event when clicked outside:
div.attr('tabindex', '0');
div.bind('click', Footnotes.divclick);
div.bind('blur', Footnotes.footnoteout);
var el = document.getElementById(id);
div.html($(el).html());
var popup_width = $("section").width();
div.css({
position: 'absolute',
width: popup_width,
opacity: 1
});
$(document.body).append(div);
var left = $("section").offset().left - 35;
// Popup opens below the link unless there is
// not enough room below and enough above.
var top = position.top + 5;
if ((top + div.height() + 25 >
$(window).height() + $(window).scrollTop())
&&
(top - div.height() - 15 > $(window).scrollTop())) {
top = position.top - div.height() - 35;
}
div.css({ left: left,
top: top });
},
footnoteout: function () {
Footnotes.footnotetimeout = setTimeout(function () {
$(popup).animate({
opacity: 0
}, 800, function () {
$(popup).remove();
});
}, 0);
},
divclick: function () {
clearTimeout(Footnotes.footnotetimeout);
$(popup).stop();
$(popup).css({
opacity: 1
});
}
};
Footnotes.setup();
});