-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatalog.js
125 lines (114 loc) · 3.23 KB
/
catalog.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$(document).ready(function() {
var headers = $(":header").filter(function(index) {
return $(this).find("a[href^='#']").length !== 0;
});
var rootNode = buildHeadTree(headers);
generateJstreeData(rootNode);
var element = '<div id="jstree-container" class="stgt-tree is-hidden"></div>';
$("body").append(element);
$(function() {
$('#jstree-container').jstree({
'core' : {
'data' : rootNode,
"themes" : {
"icons" : false,
'ellipsis': false,
'dots':false,
'stripes':false,
},
}
})
.bind("ready.jstree", function (event, data) {
$(this).jstree("open_all");
});
});
$('#jstree-container').on("changed.jstree", function (e, data) {
jumpAnchor(data.instance.get_selected(true)[0].original.href);
});
$('#jstree-container').on("select_node.jstree", function (e, data) {
headers.each(function(i){
text = $(this).text().trim()
if (text === data.node.text) {
$(this).css('border', '2px solid red')
var that = this
setTimeout(function() {
$({alpha:1}).animate({alpha:0}, {
duration: 1000,
step: function(){
$(that).css('border-color','rgba(255,0,0,'+this.alpha+')');
}
});
}, 1000)
}
})
});
$('#jstree-container').on('mouseover', displayJstree);
$('#jstree-container').on('mouseout', disappearJstree);
$('#jstree-container').on('click', displayJstree);
$('input,textarea').on('focus', disappearJstreeRightNow)
});
function jumpAnchor(href) {
window.location = window.location.origin + window.location.pathname + href;
}
function buildHeadTree(headers) {
var rootNode = newTreeNode('H0', '[TOC]', -1, '')
var currentNode = rootNode;
headers.each(function(i) {
var text = $(this).text().trim();
var href = $(this).find("a[href^='#']").attr('href');
var a = currentNode.value;
var b = $(this).prop("tagName");
var ans = compare(a, b);
if(text.length == 0) { return true; }
if (ans === -1 || a === -1) {
var newNode = newTreeNode(b, text, currentNode, href)
currentNode.children.push(newNode);
currentNode = newNode;
} else {
while(ans === 1 || ans === 0) {
currentNode = currentNode.parent
a = currentNode.value;
ans = compare(a, b);
}
var newNode = newTreeNode(b, text, currentNode, href)
currentNode.children.push(newNode);
currentNode = newNode;
}
})
return rootNode;
}
function generateJstreeData(rootNode) {
dfs(rootNode)
}
function dfs(rootNode) {
delete rootNode.value;
delete rootNode.parent;
if (rootNode.children.length === 0) {
delete rootNode.children;
return;
}
rootNode.children.forEach(function(childrenNode) {
dfs(childrenNode)
})
}
function newTreeNode(value, text, parentNode, href) {
return {
value: value,
text: text,
state: {opened: parentNode === -1 ? true : false},
children: [],
parent: parentNode,
href: href
}
}
function compare(a, b) {
var aNumStr = a.replace( /^\D+/g, '');
var bNumStr = b.replace( /^\D+/g, '');
if (aNumStr < bNumStr) {
return -1;
} else if (aNumStr === bNumStr) {
return 0;
} else {
return 1;
}
};