-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathwith-scroll-reset.js
58 lines (52 loc) · 1.7 KB
/
with-scroll-reset.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
import { inject as service } from '@ember/service';
import config from 'ember-api-docs/config/environment';
import getOffset from 'ember-api-docs/utils/get-offset';
/**
* Add scroll reset behavior to a `Route`.
*
* @param {import('@ember/routing/route').default} SomeRoute A Route class to
* decorate with the scroll position handling.
* @returns the decorated class
*/
export function withScrollReset(SomeRoute) {
class WithScrollReset extends SomeRoute {
/** @type {import('@ember/routing/router-service').default} */
@service
router;
/** @type {import('../services/scroll-position-reset').default} */
@service
scrollPositionReset;
constructor() {
super(...arguments);
this.router.on('routeWillChange', (transition) =>
this.scrollPositionReset.scheduleReset(transition)
);
this.router.on('routeDidChange', () => {
if (
typeof FastBoot === 'undefined' &&
window.location.search === '?anchor='
) {
let elem = document.querySelector('#methods');
if (elem && elem.offsetHeight) {
const offsetToScroll = getOffset(
elem,
config.APP.scrollContainerSelector
);
const scrollContainer = document.querySelector(
config.APP.scrollContainerSelector
);
if (scrollContainer.scrollTo) {
scrollContainer.scrollTo(0, offsetToScroll - 10);
} else {
// fallback for IE11
scrollContainer.scrollTop = offsetToScroll - 10;
}
return;
}
}
this.scrollPositionReset.doReset();
});
}
}
return WithScrollReset;
}