-
Notifications
You must be signed in to change notification settings - Fork 28
/
scrollIntoView.js
296 lines (275 loc) · 10.3 KB
/
scrollIntoView.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
289
290
291
292
293
294
295
296
define([
"ibm-decor/sniff"
], function (
has
) {
// True on Windows (IE, legacy Edge, Firefox, Chrome). False on Mac and iOS.
has.add("rtl-adjust-position-for-verticalScrollBar", function () {
var body = document.body;
var scrollable = document.createElement("div");
scrollable.style.cssText =
"overflow: scroll; overflow-x: visible; direction: rtl; visibility: hidden; " +
"position: absolute; left: 0; top: 0; width: 64px; height: 64px";
var div = document.createElement("div");
div.style.cssText = "overflow: hidden; direction: ltr";
scrollable.appendChild(div);
body.appendChild(scrollable);
var ret = position(div).x !== 0;
body.removeChild(scrollable);
return ret;
});
function position (node) {
var ret = node.getBoundingClientRect();
return { x: ret.left, y: ret.top, w: ret.right - ret.left, h: ret.bottom - ret.top };
}
function px (value) {
// style values can be floats, client code may want
// to round for integer pixels.
return parseFloat(value) || 0;
}
function getPadExtents (/*Element*/ node, /*Object*/ computedStyle) {
// summary:
// Returns object with special values specifically useful for node
// fitting.
// description:
// Returns an object with `w`, `h`, `l`, `t` properties:
// | l/t/r/b = left/top/right/bottom padding (respectively)
// | w = the total of the left and right padding
// | h = the total of the top and bottom padding
// If 'node' has position, l/t forms the origin for child nodes.
// The w/h are used for calculating boxes.
// Normally application code will not need to invoke this
// directly, and will use the ...box... functions instead.
// node: Element
// computedStyle: Object?
// This parameter accepts computed styles object.
// If this parameter is omitted, the functions will call
// getComputedStyle to get one. It is a better way, calling
// getComputedStyle once, and then pass the reference to this
// computedStyle parameter. Wherever possible, reuse the returned
// object of getComputedStyle().
var s = computedStyle || getComputedStyle(node),
l = px(node, s.paddingLeft), t = px(node, s.paddingTop), r = px(node, s.paddingRight),
b = px(node, s.paddingBottom);
return { l: l, t: t, r: r, b: b, w: l + r, h: t + b };
}
function getBorderExtents (/*Element*/ node, /*Object*/ computedStyle) {
// summary:
// returns an object with properties useful for noting the border
// dimensions.
// description:
// - l/t/r/b = the sum of left/top/right/bottom border (respectively)
// - w = the sum of the left and right border
// - h = the sum of the top and bottom border
//
// The w/h are used for calculating boxes.
// Normally application code will not need to invoke this
// directly, and will use the ...box... functions instead.
// node: Element
// computedStyle: Object?
// This parameter accepts computed styles object.
// If this parameter is omitted, the functions will call
// getComputedStyle to get one. It is a better way, calling
// getComputedStyle once, and then pass the reference to this
// computedStyle parameter. Wherever possible, reuse the returned
// object of getComputedStyle().
var s = computedStyle || getComputedStyle(node),
l = s.borderLeftStyle !== "none" ? px(node, s.borderLeftWidth) : 0,
t = s.borderTopStyle !== "none" ? px(node, s.borderTopWidth) : 0,
r = s.borderRightStyle !== "none" ? px(node, s.borderRightWidth) : 0,
b = s.borderBottomStyle !== "none" ? px(node, s.borderBottomWidth) : 0;
return { l: l, t: t, r: r, b: b, w: l + r, h: t + b };
}
function getPadBorderExtents (/*Element*/ node, /*Object*/ computedStyle) {
// summary:
// Returns object with properties useful for box fitting with
// regards to padding.
// description:
// - l/t/r/b = the sum of left/top/right/bottom padding and left/top/right/bottom border (respectively)
// - w = the sum of the left and right padding and border
// - h = the sum of the top and bottom padding and border
//
// The w/h are used for calculating boxes.
// Normally application code will not need to invoke this
// directly, and will use the ...box... functions instead.
// node: Element
// computedStyle: Object?
// This parameter accepts computed styles object.
// If this parameter is omitted, the functions will call
// getComputedStyle to get one. It is a better way, calling
// getComputedStyle once, and then pass the reference to this
// computedStyle parameter. Wherever possible, reuse the returned
// object of getComputedStyle().
var s = computedStyle || getComputedStyle(node),
p = getPadExtents(node, s),
b = getBorderExtents(node, s);
return {
l: p.l + b.l,
t: p.t + b.t,
r: p.r + b.r,
b: p.b + b.b,
w: p.w + b.w,
h: p.h + b.h
};
}
/**
* Test if node is a dropdown/popup than can overflow ancestors, even ancestors with overflow:auto or scroll.
* @param node
* @param computedStyle
* @returns {boolean}
*/
function isPopup (/*Element*/ node, /*Object*/ computedStyle) {
if (computedStyle.position === "fixed") {
return true;
} else if (computedStyle.position === "absolute") {
// This is the tricky case. From experimentation, the key factor is if there are any intervening
// position:relative nodes between "node" and its first scrollable ancestor.
for (var a = node.parentNode; a && a.tagName !== "BODY" && a.tagName !== "HTML"; a = a.parentNode) {
var ancestorCs = getComputedStyle(a);
if (ancestorCs.overflow === "auto" || ancestorCs.overflow === "scroll") {
return true;
} else if (ancestorCs.position === "relative") {
return false;
}
}
return true;
} else {
return false;
}
}
/**
* Scroll the passed node into view using minimal movement, if it is not already.
*
* Don't rely on node.scrollIntoView working just because the function is there since it forces the node to the
* page's bottom or top (and left or right in IE) without consideration for the minimal movement.
* WebKit's node.scrollIntoViewIfNeeded doesn't work either for inner scrollbars in right-to-left mode
* and when there's a fixed position scrollable element.
*/
// eslint-disable-next-line complexity
return function (/*Element*/ node, /*Object?*/ pos) {
try { // catch unexpected/unrecreatable errors since we can recover using a semi-acceptable native method
var doc = node.ownerDocument,
body = doc.body,
html = doc.documentElement || body.parentNode,
isIE = has("ie") || has("trident"),
isWK = has("webkit");
if (node === body || node === html) {
return;
}
// if an untested browser, then use the native method
if (!(has("ff") || isIE || isWK || has("edge")) && ("scrollIntoView" in node)) {
node.scrollIntoView(false);
return;
}
var rootWidth = Math.min(body.clientWidth || html.clientWidth, html.clientWidth || body.clientWidth),
rootHeight = Math.min(body.clientHeight || html.clientHeight, html.clientHeight || body.clientHeight),
scrollRoot = isWK ? body : html,
nodePos = pos || position(node),
el = node.parentNode,
scrollElementBy = function (elem, x, y) {
if (elem.tagName === "BODY" || elem.tagName === "HTML") {
scrollBy(x, y);
} else {
if (x) {
elem.scrollLeft += x;
}
if (y) {
elem.scrollTop += y;
}
}
};
if (getComputedStyle(node).position.toLowerCase() === "fixed") {
// Nothing to do.
return;
}
// Loop through all the ancestors of "node" up to and including "scrollRoot", scrolling each ancestor
// the minimal amount to scroll "node" into view within that ancestor. Each ancestor is referenced
// by the "el" variable.
while (el) {
if (el === body) {
el = scrollRoot;
}
var elPos = position(el),
cs = getComputedStyle(el),
cssPosition = cs.position.toLowerCase(),
rtl = cs.direction.toLowerCase() === "rtl";
if (el === scrollRoot) {
elPos.w = rootWidth;
elPos.h = rootHeight;
if (scrollRoot === html && isIE && rtl) {
elPos.x += scrollRoot.offsetWidth - elPos.w;// IE workaround where scrollbar causes negative x
}
elPos.x = 0;
elPos.y = 0;
} else {
var pb = getPadBorderExtents(el);
elPos.w -= pb.w;
elPos.h -= pb.h;
elPos.x += pb.l;
elPos.y += pb.t;
var clientSize = el.clientWidth,
scrollBarSize = elPos.w - clientSize;
if (clientSize > 0 && scrollBarSize > 0) {
if (rtl && has("rtl-adjust-position-for-verticalScrollBar")) {
elPos.x += scrollBarSize;
}
elPos.w = clientSize;
}
clientSize = el.clientHeight;
scrollBarSize = elPos.h - clientSize;
if (clientSize > 0 && scrollBarSize > 0) {
elPos.h = clientSize;
}
}
if (cssPosition === "fixed") { // bounded by viewport, not parents
if (elPos.y < 0) {
elPos.h += elPos.y;
elPos.y = 0;
}
if (elPos.x < 0) {
elPos.w += elPos.x;
elPos.x = 0;
}
if (elPos.y + elPos.h > rootHeight) {
elPos.h = rootHeight - elPos.y;
}
if (elPos.x + elPos.w > rootWidth) {
elPos.w = rootWidth - elPos.x;
}
}
// calculate overflow in all 4 directions
var l = nodePos.x - elPos.x, // beyond left: < 0
t = nodePos.y - elPos.y, // beyond top: < 0
r = l + nodePos.w - elPos.w, // beyond right: > 0
bot = t + nodePos.h - elPos.h; // beyond bottom: > 0
var s, old;
if (r * l > 0 && (!!el.scrollLeft || el === scrollRoot || el.scrollWidth > el.offsetHeight)) {
s = Math[l < 0 ? "max" : "min"](l, r);
if (rtl && has("trident") >= 5) {
s = -s;
}
old = el.scrollLeft;
scrollElementBy(el, s, 0);
s = el.scrollLeft - old;
nodePos.x -= s;
}
if (bot * t > 0 && (!!el.scrollTop || el === scrollRoot || el.scrollHeight > el.offsetHeight)) {
s = Math.ceil(Math[t < 0 ? "max" : "min"](t, bot));
old = el.scrollTop;
scrollElementBy(el, 0, s);
s = el.scrollTop - old;
nodePos.y -= s;
}
if (el === scrollRoot || isPopup(el, cs)) {
// Note: it doesn't make sense to scroll the ancestors of a popup node, especially since that may
// move the popup's anchor node out of view, triggering the popup to close.
break;
}
el = el.parentNode;
}
} catch (error) {
console.error("scrollIntoView: " + error);
node.scrollIntoView(false);
}
};
});