-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.js
89 lines (75 loc) · 1.78 KB
/
index.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
$(function() {
var selectingTopicTimer,
selectingSubtopicTimer,
selected;
//
// When the user clicks on a topic,
// assume the intent is top open the ToC
//
$('#toc > ul > li > a').click(function(e) {
clearTimeout(selectingTopicTimer);
var topic = this;
//
// Don't re-open the same topic twice
//
if($(topic).hasClass('selected')) {
// console.log('already selected, dont reopen');
return false;
}
openTopic(topic);
return false;
});
//
// Touch support. Since there is no mouse out on
// touch devices, we need to have an event to hide
// the toc.
//
$('#article').click(function() {
$('#toc').mouseleave();
});
//
// When the user's mouse leaves the ToC,
// assume the intent is to close the ToC
//
$('#toc').mouseleave(function(e){
clearTimeout(selectingTopicTimer);
selectingTopicTimer = setTimeout(function() {
closeTopic();
}, 400);
});
//
// For users with an impatient intent,
// instantly close ToC if they click anywhere outside the ToC
//
$('body').click(function(e){
if($(e.target).get(0).tagName !== 'UL'){
if(isTopicSelected()){
closeTopic();
}
}
});
//
// Opens the Table Of Contents
//
function openTopic(topic) {
$('#toc > ul > li > ul').hide();
$(topic).next('ul').show();
$('#toc > ul > li > a').removeClass('selected');
$(topic).addClass('selected');
$('#mask').fadeIn('fast');
}
//
// Closes the Table Of Contents
//
function closeTopic() {
$('#toc > ul > li > ul').hide();
$('#mask').fadeOut('fast');
$('a').removeClass('selected');
}
function isTopicSelected() {
if($('#toc > ul > li > a.selected').length === 1) {
return true;
}
return false
}
});