-
Notifications
You must be signed in to change notification settings - Fork 1
/
toc-control.js
235 lines (201 loc) · 5.92 KB
/
toc-control.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var viewTarget = require("can-view-target");
var makeTree = require("./make-tree");
var lazy = require("can-define-lazy-value");
var safeCustomElement = require("./safe-custom-element");
var attributeConnect = require("./attribute-connect");
function debounce(func, wait) {
var timeout;
return function executedFunction() {
var context = this;
var args = arguments;
var later = function() {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (!timeout) {
func.apply(context, args);
}
};
}
// data { childTag: childTag, node: node }
var renderNodeTarget = viewTarget([{
tag: "li",
children: [
{
tag: "a",
attrs: {
href: function(data){
return this.setAttribute("href","#"+data.node.id);
}
},
children: [function(data){ this.nodeValue = data.node.text; }]
},
function(data){
var container = document.createElement(data.childTag);
data.node.children.forEach(function(node){
container.appendChild(renderNodeTarget.hydrate({node: node, childTag: data.childTag}));
});
if(data.node.children.length) {
this.parentNode.replaceChild(container, this);
}
}
]
}]);
// data - { nodes: titles, childTag: this.childTag }
var template = function(data){
var container = document.createElement(data.childTag);
data.nodes.forEach(function(node){
container.appendChild(renderNodeTarget.hydrate({node: node, childTag: data.childTag}));
});
return container;
};
var connectAttribute = attributeConnect();
var BitToc = function(){
console.log("initialized");
connectAttribute.initialize(this);
this.teardowns = [];
};
var prototype = {
connectedCallback: function() {
var titles = this.titleTree;
// If there are no titles, bail
if (!titles.length) {
this.parentNode.removeChild(this);
return;
}
// Append our template
this.appendChild(template({
nodes: titles,
childTag: this.outlineTagName
}));
this.setupHighlighting();
},
get docObject() {
return window.docObject || {};
},
get outlineTagName(){
if(this.childTag) {
return this.childTag;
} else {
var docObject = this.docObject;
var outline = docObject.outline || {};
return (outline.tag === "ol") ? "ol" : "ul";
}
},
get outlineDepth(){
if(this.depth) {
return parseInt(this.depth);
} else {
var docObject = this.docObject;
var depth = docObject.outline && docObject.outline.depth;
return (typeof depth === "number" ? Math.min(depth, 6) : 1);
}
},
get outlineScrollElement(){
if(this.scrollSelector === "true" || this.scrollSelector === "") {
return this;
} else if(this.scrollSelector) {
return document.querySelector(this.scrollSelector);
}
else {
return;
}
},
get containerSelector(){
return this.headingsContainerSelector || "article";
},
makeSelector: function(tagName) {
var container = this.containerSelector;
return container + " " + tagName;
},
getHeadings: function() {
var headings = [];
for(var i = 0; i < this.outlineDepth; i++) {
headings.push("h" + (i + 2));
}
return headings;
},
setupHighlighting: function(){
this.article = document.querySelector(this.containerSelector);
if(this.article) {
var highlight = debounce(this.highlight.bind(this),1);
this.article.addEventListener("scroll",highlight);
this.teardowns.push(function(){
this.article.removeEventListener("scroll", highlight);
}.bind(this));
this.highlight();
}
},
// completed only once the one after it is in the page and 1/2 way visible.
//
highlight: function(){
var articleRect = this.article.getBoundingClientRect();
var buttons = this.buttons;
var positions = this.titles.map(function(header, i){
return {
header: header,
rect: header.getBoundingClientRect(),
button: buttons[i]
};
});
// this simulates a header at the end of the page
positions.push({
rect: {
top: articleRect.top + this.article.scrollHeight - this.article.scrollTop
}
});
// loop through the actual headers and their positions
positions.slice(0, positions.length - 1).forEach(function(position, index){
position.button.classList.remove("completed","active");
var curRect = position.rect;
var curDistance = curRect.top - articleRect.top;
var nextRect = positions[index+1].rect;
var nextDistance = nextRect.top - articleRect.top;
// =====[CUR=NEXT]===
if( nextDistance >= 0 && nextDistance <= articleRect.height && curDistance >= 0 && curDistance <= articleRect.height ) {
position.button.classList.add("active");
}
// =======CUR=====[=NEXT=|===]======
// =======CUR=======NEXT=====[===|===]===
else if( nextDistance < (articleRect.height / 2) ) {
position.button.classList.add("completed");
}
// ======[=CUR=|=NEXT=]======
// ======[=CUR=|===]=NEXT====
// ====CUR=[===|=NEXT]=======
// ====CUR=[===|===]=NEXT====
else if( nextDistance >= (articleRect.height / 2) && curDistance < (articleRect.height / 2) ) {
position.button.classList.add("active");
}
});
var elementToScroll = this.outlineScrollElement;
if(elementToScroll) {
// find out where the midpoint is
var distance = (this.article.scrollTop + this.article.offsetHeight / 2) / this.article.scrollHeight;
elementToScroll.scrollTop = (elementToScroll.scrollHeight * distance) - (elementToScroll.offsetHeight / 2);
}
},
disconnectedCallback: function(){
this.teardowns.forEach(function(teardown){
teardown();
});
},
attributeChangedCallback: connectAttribute.attributeChangedCallback
};
lazy(prototype,"titles", function(){
var selector = this.getHeadings()
.map(this.makeSelector.bind(this))
.join(",");
// we should save this ...
return selector ? Array.from( document.querySelectorAll(selector) ) : [];
});
lazy(prototype,"titleTree", function(){
return makeTree(this.titles);
});
lazy(prototype,"buttons", function(){
return this.querySelectorAll("li");
});
BitToc = safeCustomElement("bit-toc",BitToc, prototype);
module.exports = BitToc;