-
Notifications
You must be signed in to change notification settings - Fork 56
/
router-scroll.js
288 lines (242 loc) · 7.93 KB
/
router-scroll.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import Service, { inject as service } from '@ember/service';
import { set, get, action } from '@ember/object';
import { typeOf } from '@ember/utils';
import { assert } from '@ember/debug';
import { getOwner } from '@ember/application';
import { scheduleOnce } from '@ember/runloop';
import { addListener, removeListener, sendEvent } from '@ember/object/events';
import { setupRouter, whenRouteIdle } from 'ember-app-scheduler';
let ATTEMPTS = 0;
const MAX_ATTEMPTS = 100; // rAF runs every 16ms ideally, so 60x a second
let requestId;
let callbackRequestId;
/**
* By default, we start checking to see if the document height is >= the last known `y` position
* we want to scroll to. This is important for content heavy pages that might try to scrollTo
* before the content has painted
*
* @method tryScrollRecursively
* @param {Function} fn
* @param {Object} scrollHash
* @param {Element} [element]
* @param {string?} url
* @void
*/
function tryScrollRecursively(fn, scrollHash, element, url) {
let documentHeight;
// read DOM outside of rAF
if (element) {
documentHeight = Math.max(
element.scrollHeight,
element.offsetHeight,
element.clientHeight
);
} else {
const { body, documentElement: html } = document;
documentHeight = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
}
callbackRequestId = window.requestAnimationFrame(() => {
if (url && url.indexOf('#') > -1) {
const hashElement = document.getElementById(url.split('#').pop());
if (hashElement) {
scrollHash = { x: hashElement.offsetLeft, y: hashElement.offsetTop };
}
}
// write DOM (scrollTo causes reflow)
if (documentHeight >= scrollHash.y || ATTEMPTS >= MAX_ATTEMPTS) {
ATTEMPTS = 0;
fn.call(null, scrollHash.x, scrollHash.y);
} else {
ATTEMPTS++;
tryScrollRecursively(fn, scrollHash, element, url);
}
});
}
// to prevent scheduleOnce calling multiple times, give it the same ref to this function
const CALLBACK = function (transition) {
this.updateScrollPosition(transition);
};
class RouterScroll extends Service {
@service router;
get isFastBoot() {
const fastboot = getOwner(this).lookup('service:fastboot');
return fastboot ? fastboot.get('isFastBoot') : false;
}
key;
targetElement;
scrollElement = 'window';
isFirstLoad = true;
preserveScrollPosition = false;
// ember-app-scheduler properties
scrollWhenIdle = false;
scrollWhenAfterRender = false;
constructor() {
super(...arguments);
// https://github.com/ember-app-scheduler/ember-app-scheduler/pull/773
setupRouter(this.router);
}
// eslint-disable-next-line ember/classic-decorator-hooks
init(...args) {
super.init(...args);
this._loadConfig();
set(this, 'scrollMap', {
default: {
x: 0,
y: 0,
},
});
addListener(this.router, 'routeWillChange', this._routeWillChange);
addListener(this.router, 'routeDidChange', this._routeDidChange);
}
willDestroy() {
removeListener(this.router, 'routeWillChange', this._routeWillChange);
removeListener(this.router, 'routeDidChange', this._routeDidChange);
if (requestId) {
window.cancelAnimationFrame(requestId);
}
if (callbackRequestId) {
window.cancelAnimationFrame(callbackRequestId);
}
super.willDestroy(...arguments);
}
/**
* Updates the scroll position
* it will be a single transition
* @method updateScrollPosition
* @param {transition|transition[]} transition If before Ember 3.6, this will be an array of transitions, otherwise
*/
updateScrollPosition(transition) {
if (this.isFirstLoad) {
this.unsetFirstLoad();
}
let scrollPosition = this.position;
// If `preserveScrollPosition` was not set on the controller, attempt fallback to `preserveScrollPosition` which was set on the router service.
let preserveScrollPosition =
(transition.router.currentRouteInfos || []).some(
(routeInfo) => routeInfo.route.controller.preserveScrollPosition
) || this.preserveScrollPosition;
if (!preserveScrollPosition) {
const { scrollElement, targetElement, currentURL } = this;
if (targetElement || 'window' === scrollElement) {
tryScrollRecursively(window.scrollTo, scrollPosition, null, currentURL);
} else if ('#' === scrollElement.charAt(0)) {
const element = document.getElementById(scrollElement.substring(1));
if (element) {
let fn = (x, y) => {
element.scrollLeft = x;
element.scrollTop = y;
};
tryScrollRecursively(fn, scrollPosition, element, currentURL);
}
}
}
sendEvent(this, 'didScroll', transition);
}
@action
_routeWillChange() {
if (this.isFastBoot) {
return;
}
this.update();
}
@action
_routeDidChange(transition) {
if (this.isFastBoot) {
return;
}
const scrollWhenIdle = this.scrollWhenIdle;
const scrollWhenAfterRender = this.scrollWhenAfterRender;
if (!scrollWhenIdle && !scrollWhenAfterRender) {
// out of the option, this happens on the tightest schedule
scheduleOnce('render', this, CALLBACK, transition);
} else if (scrollWhenAfterRender && !scrollWhenIdle) {
// out of the option, this happens on the second tightest schedule
scheduleOnce('afterRender', this, CALLBACK, transition);
} else {
whenRouteIdle().then(() => {
this.updateScrollPosition(transition);
});
}
}
unsetFirstLoad() {
set(this, 'isFirstLoad', false);
}
update() {
if (this.isFastBoot || this.isFirstLoad) {
return;
}
const scrollElement = this.scrollElement;
const targetElement = this.targetElement;
const scrollMap = this.scrollMap;
const key = this.key;
let x;
let y;
if (targetElement) {
let element = document.querySelector(targetElement);
if (element) {
x = element.offsetLeft;
y = element.offsetTop;
// if we are looking to where to transition to next, we need to set the default to the position
// of the targetElement on screen
set(scrollMap, 'default', {
x,
y,
});
}
} else if ('window' === scrollElement) {
x = window.scrollX;
y = window.scrollY;
} else if ('#' === scrollElement.charAt(0)) {
let element = document.getElementById(scrollElement.substring(1));
if (element) {
x = element.scrollLeft;
y = element.scrollTop;
}
}
// only a `key` present after first load
if (key && 'number' === typeOf(x) && 'number' === typeOf(y)) {
set(scrollMap, key, {
x,
y,
});
}
}
_loadConfig() {
const config = getOwner(this).resolveRegistration('config:environment');
if (config && config.routerScroll) {
const scrollElement = config.routerScroll.scrollElement;
const targetElement = config.routerScroll.targetElement;
assert(
'You defined both scrollElement and targetElement in your config. We currently only support definining one of them',
!(scrollElement && targetElement)
);
if ('string' === typeOf(scrollElement)) {
set(this, 'scrollElement', scrollElement);
}
if ('string' === typeOf(targetElement)) {
set(this, 'targetElement', targetElement);
}
const { scrollWhenIdle = false, scrollWhenAfterRender = false } =
config.routerScroll;
set(this, 'scrollWhenIdle', scrollWhenIdle);
set(this, 'scrollWhenAfterRender', scrollWhenAfterRender);
}
}
}
Object.defineProperty(RouterScroll.prototype, 'position', {
configurable: true,
get() {
const scrollMap = this.scrollMap;
const stateUuid = window.history.state?.uuid;
set(this, 'key', stateUuid);
const key = this.key || '-1';
return get(scrollMap, key) || scrollMap.default;
},
});
export default RouterScroll;