-
Notifications
You must be signed in to change notification settings - Fork 15
/
doctree.js
181 lines (172 loc) · 4.54 KB
/
doctree.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* PocketMine-Plugin-Tutorials
*
* Copyright (C) 2015 PEMapModder
*
* @author PEMapModder
*/
var runtimeAutoId = 0;
/**
* Registers a new spoiler.
* @param el
* @returns jQuery $(div of spoiler button)
*/
function registerSpoiler(el){
el.addClass("spoiler");
var id = runtimeAutoId++;
var onclick = 'switchSpoiler("' + id + '"); return false;';
var button = $("<button class='button spoiler-opener'>Show</button>");
var before = $("<div></div>");
button.appendTo(before);
before.append("<hr>");
button.attr("data-spoiler-name", id);
button.attr("onclick", onclick);
before.insertBefore(el);
el.attr("data-spoiler-name", id);
el.css("display", "none");
return before;
}
/**
* Toggles a spoiler.
* @param id
* @returns {boolean} true if spoiler is opened, false if spoiler is closed
*/
var switchSpoiler = function(id){
var el = $(".spoiler[data-spoiler-name='" + id + "']");
var opener = $(".spoiler-opener[data-spoiler-name='" + id + "']");
if(el.css("display") === "none"){
el.css("display", "block");
opener.text("Hide");
return true;
}
el.css("display", "none");
opener.text("Show");
return false;
};
function Tree(name, id, depthClass){
this.name = name;
this.id = id;
this.depthClass = depthClass;
this.children = {};
}
Tree.prototype.addChild = function(child){
this.children[child.name] = child;
};
Tree.prototype.toOlJQuery = function(){
var a = $("<a></a>");
a.addClass("branch");
a.attr("data-target", this.id);
a.attr("href", "#" + this.id);
a.text(this.name);
var out = $("<li></li>");
a.appendTo(out);
out.addClass(this.depthClass);
var $ol = $("<ol></ol>");
for(var name in this.children){
if(this.children.hasOwnProperty(name)){
this.children[name].toOlJQuery().appendTo($ol);
}
}
$ol.appendTo(out);
return out;
};
var tree;
var trees = {};
function gotoAnchor(anchor){
var target = $("a[name='" + anchor + "']");
target.parents(".tree").each(function(){
var $this = $(this);
if($this.css("display") == "none"){
switchSpoiler($this.attr("data-spoiler-name"));
}
});
$("html, body").animate({
scrollTop: Math.max(0, target.parent().prev().offset().top + window.innerHeight * (-0.1))
}, 200, "swing", function(){
var header = target.parent().prev();
header.css("background-color", "#B11D98");
header.animate({
backgroundColor: "#FFFFFF"
}, 600);
});
}
var hashBlocker = 0;
$(document).ready(function(){
var maxDepth = 12;
var nextAnchorId = 0;
$(".tree").each(function(){
var $this = $(this);
var name = $this.attr("data-name");
var parents = $this.parents(".tree");
var anchorId = "anchor-auto-" + (nextAnchorId++);
$this.prepend('<a name="' + anchorId + '"></a>');
var clazz = "depth-" + parents.length;
if(!$this.hasClass("no-index")){
if(parents.length == 0){
var tmpTree = new Tree(name, anchorId, clazz);
trees[name] = tmpTree;
if(this.id === "mainTree"){
tree = tmpTree;
}
}else{
var $parent = $(parents[0]);
var parentTree = trees[$parent.attr("data-name")];
var t;
parentTree.addChild(t = new Tree(name, anchorId, clazz));
trees[name] = t;
}
}
var depth = parents.length;
maxDepth = Math.max(maxDepth, depth);
var $div = registerSpoiler($this);
var bef = $("<span></span>");
bef.text(name + " ");
var id = $div.children("button").attr("data-spoiler-name");
var onclick = 'switchSpoiler("' + id + '");';
bef.attr("onclick", onclick);
bef.prependTo($div);
$div.attr("data-depth", depth);
$div.addClass("heading");
if(depth > 0){
$div.before("<br>");
}
});
$(".heading").each(function(){
var $this = $(this);
var depth = $this.attr("data-depth");
$this.css("font-size", (32 - Math.floor(depth / maxDepth * 20)) + "px");
});
switchSpoiler("0");
switchSpoiler("1");
var $contents = $("#index");
$contents.append("<p style='text-align: center;'><strong>Contents</strong></p>");
var ol = tree.toOlJQuery();
ol.children().each(function(){
$contents.append(this);
});
$("a").click(function(){
var $this = $(this);
if(typeof $this.attr("href") !== typeof undefined){
if($this.attr("href").charAt(0) === "#"){
gotoAnchor($this.attr("href").substring(1));
}
}
});
// $(".branch").click(function(){
// var $this = $(this);
// var targetName = $this.attr("data-target");
// gotoAnchor(targetName);
// });
$("#body").css("padding-bottom", $(window).height() / 3);
var hasher = function(){
if(hashBlocker > 0){
setTimeout(hasher, 100);
return;
}
var hash = window.location.hash;
if(hash.charAt(0) === "#"){
gotoAnchor(hash.substring(1));
}
};
setTimeout(hasher, 100);
});