Skip to content

Commit 3603ad5

Browse files
committed
adds scroll-selector
1 parent 790c12d commit 3603ad5

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ can be configured like:
7979
<bit-toc child-tag="ol"></bit-toc>
8080
```
8181

82+
### scroll-selector
83+
84+
If present, will scroll the `<bit-toc>` element with the `heading-container-selector`:
85+
86+
```html
87+
<bit-toc scroll-selector></bit-toc>
88+
```
89+
90+
`scroll-selector` can also be set to some other element to scroll:
91+
92+
```html
93+
<bit-toc scroll-selector="#some-parent"></bit-toc>
94+
```
95+
8296
## Methods
8397

8498
Call `.highlight()` to force an update of the `active` or `completed`

test/toc-control-test.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ describe("TableOfContents", function() {
1818

1919
afterEach(function() {
2020
$testArea.empty();
21-
2221
});
2322

2423
it("makes a flat list from headings", function() {
@@ -152,4 +151,69 @@ describe("TableOfContents", function() {
152151
}, "initialized correctly");
153152

154153
});
154+
155+
it("can scroll the container", function(){
156+
var headings = [
157+
"<h2>Bower</h2>",
158+
"<p>Install</p>",
159+
"<h2>NPM</h2>",
160+
"<p>Install</p>",
161+
"<h2>Configure</h2>",
162+
"<p>xyz</p>",
163+
"<h2>Writing Modules</h2>",
164+
"<p>writing modules</p>",
165+
"<h2>Extra</h2>",
166+
"<p>final</p>"
167+
];
168+
169+
$testArea.html("<article id='article'>"+headings.join("")+"</article>"+
170+
"<bit-toc headings-container-selector='#article' scroll-selector></bit-toc>");
171+
172+
$("article").css({
173+
position: "fixed",
174+
top: 0,
175+
height: 200,
176+
left: 200,
177+
width: 600,
178+
backgroundColor: "gray",
179+
overflowY: "auto"
180+
});
181+
182+
$("article p").css({
183+
height: "500px",
184+
border: "solid 1px red"
185+
});
186+
187+
$("bit-toc").css({
188+
display: "block",
189+
height: "50px",
190+
overflow: "auto"
191+
})[0].highlight();
192+
193+
function getCompletedAndActive(){
194+
var result = {completed: [], active: []}
195+
$("bit-toc li").each(function(i, node){
196+
if(node.classList.contains("completed")) {
197+
result.completed.push(node.textContent)
198+
}
199+
if(node.classList.contains("active")) {
200+
result.active.push(node.textContent)
201+
}
202+
});
203+
return result;
204+
}
205+
206+
assert.deepEqual(getCompletedAndActive(), {
207+
active: ["Bower"],
208+
completed: []
209+
}, "initialized correctly");
210+
211+
$("#article").scrollTop(1200);
212+
$("bit-toc")[0].highlight(); // so we don't have to wait for the throttling
213+
214+
215+
assert.ok( $("bit-toc")[0].scrollTop > 0, "scrollTop has moved");
216+
217+
});
218+
155219
});

toc-control.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ var prototype = {
7878
if (!titles.length) {
7979
this.parentNode.removeChild(this);
8080
return;
81-
} else {
82-
this.parentNode.style.display = 'block';
8381
}
8482

8583
// Append our template
@@ -112,6 +110,16 @@ var prototype = {
112110
return (typeof depth === "number" ? Math.min(depth, 6) : 1);
113111
}
114112
},
113+
get outlineScrollElement(){
114+
if(this.scrollSelector === "true" || this.scrollSelector === "") {
115+
return this;
116+
} else if(this.scrollSelector) {
117+
return document.querySelector(this.scroll);
118+
}
119+
else {
120+
return;
121+
}
122+
},
115123
get containerSelector(){
116124
return this.headingsContainerSelector || "article";
117125
},
@@ -131,7 +139,7 @@ var prototype = {
131139
setupHighlighting: function(){
132140
this.article = document.querySelector(this.containerSelector);
133141
if(this.article) {
134-
var highlight = debounce(this.highlight.bind(this),50);
142+
var highlight = debounce(this.highlight.bind(this),1);
135143
this.article.addEventListener("scroll",highlight);
136144
this.teardowns.push(function(){
137145
this.article.removeEventListener("scroll", highlight);
@@ -188,6 +196,13 @@ var prototype = {
188196
position.button.classList.add("active");
189197
}
190198
});
199+
var elementToScroll = this.outlineScrollElement;
200+
201+
if(elementToScroll) {
202+
// find out where the midpoint is
203+
var distance = (this.article.scrollTop + this.article.offsetHeight / 2) / this.article.scrollHeight;
204+
elementToScroll.scrollTop = (elementToScroll.scrollHeight * distance) - (elementToScroll.offsetHeight / 2);
205+
}
191206
},
192207
disconnectedCallback: function(){
193208
this.teardowns.forEach(function(teardown){

0 commit comments

Comments
 (0)