forked from digitalnature/php-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.js
59 lines (50 loc) · 2.03 KB
/
ref.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
window.addEventListener('load', function(){
var tip = document.createElement('div'),
refs = document.querySelectorAll('.ref');
for(var i = 0, m = refs.length; i < m; i++){
var kbds = refs[i].querySelectorAll('kbd'),
tippable = refs[i].querySelectorAll('b[data-tip]'),
tips = refs[i].querySelectorAll('q');
for(var j = 0, n = kbds.length; j < n; j++){
if(kbds[j].parentNode !== refs[i])
kbds[j].onclick = function(e){
('exp' in this.dataset) ? delete this.dataset.exp : this.dataset.exp = 1;
}
}
[].filter.call(tips, function(node){
return node.parentNode == refs[i];
});
for(var j = 0, n = tippable.length; j < n; j++){
tippable[j].tipRef = tips[tippable[j].dataset.tip];
tippable[j].onmouseover = function(){
tip.className = 'ref visible';
tip.innerHTML = this.tipRef.innerHTML;
window.clearTimeout(tip.fadeOut);
};
tippable[j].onmouseout = function(){
tip.className = 'ref visible fadingOut';
tip.fadeOut = window.setTimeout(function(){
tip.innerHTML = '';
tip.className = '';
}, 250);
};
}
refs[i].onmousemove = function(e){
if(tip.className.indexOf('visible') < 0)
return;
tip.style.top = ((document.documentElement.clientHeight - e.clientY) < tip.offsetHeight + 20 ? (e.pageY - tip.offsetHeight) : e.pageY) + 'px';
tip.style.left = ((document.documentElement.clientWidth - e.clientX) < tip.offsetWidth + 20 ? Math.max(e.pageX - tip.offsetWidth, 0) : e.pageX) + 'px';
};
}
tip.id = 'rTip';
document.body.appendChild(tip);
});
window.addEventListener('keydown', function(e){
if(e.keyCode != 88)
return;
var kbds = document.querySelectorAll('.ref div kbd'),
partlyExp = document.querySelectorAll('.ref div kbd[data-exp]').length !== kbds.length;
e.preventDefault();
for(var i = 0, m = kbds.length; i < m; i++)
partlyExp ? kbds[i].dataset.exp = 1 : delete kbds[i].dataset.exp;
});