-
Notifications
You must be signed in to change notification settings - Fork 0
/
footnotes-es6.js
101 lines (58 loc) · 1.74 KB
/
footnotes-es6.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
99
100
101
class Footnotes {
constructor() {
var tag = document.getElementsByClassName( 'footnotes' );
if( !tag.length ) {
return;
}
var footnotes = tag[0].firstChild.childNodes;
for( var i = footnotes.length; i--; ) {
var note = footnotes[i],
id = note.getAttribute( 'id' ).slice( -1 ),
origin = document.getElementById( 'fnref:' + id );
note.getElementsByClassName( 'reversefootnote' )[0].remove();
this.spawnPlacebo( note, origin, id );
}
this.popup = document.querySelector( '.note' );
}
openNote( me, event ) {
var popup = this.popup,
status = popup.classList.contains( 'shown' );
if( status ) {
var current = document.querySelector( '[data-note].open' );
this.closeNote( current );
}
document.body.addEventListener( 'click', function( event ) {
this.closeNote( me );
}.bind( this ));
popup.innerHTML = me.getAttribute( 'data-note' );
setTimeout( function() {
popup.className = popup.className + ' shown';
popup.addEventListener( 'click', function( event ) {
event.stopPropagation();
});
}, 100 );
me.setAttribute( 'class', 'open' );
}
closeNote( ele ) {
this.popup.setAttribute( 'class', 'note' );
ele.removeAttribute( 'class' );
document.body.onclick = null;
}
spawnPlacebo( note, ref, id ) {
var placebo = document.createElement( 'span' ),
content = note.firstChild.innerHTML,
it = this;
placebo.setAttribute( 'data-note', content );
placebo.innerHTML = id;
placebo.addEventListener( 'click', function( event ) {
if( this.classList.contains( 'open' ) ) {
it.closeNote( this );
} else {
it.openNote( this, event );
}
event.stopPropagation();
});
ref.parentNode.replaceChild( placebo, ref );
}
}
new Footnotes();