-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathviewport-binding-natural.js
292 lines (251 loc) · 7.97 KB
/
viewport-binding-natural.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
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Observable} from '../../core/data-structures/observable';
import {Services} from '../../services';
import {
ViewportBindingDef,
marginBottomOfLastChild,
} from './viewport-binding-def';
import {computedStyle, px, setImportantStyles} from '../../style';
import {dev} from '../../log';
import {layoutRectLtwh} from '../../core/math/layout-rect';
const TAG_ = 'Viewport';
/**
* Implementation of ViewportBindingDef based on the native window. It assumes
* that the native window is sized properly and events represent the actual
* scroll/resize events. This mode is applicable to a standalone document
* display or when an iframe has a fixed size.
*
* Visible for testing.
*
* @implements {ViewportBindingDef}
*/
export class ViewportBindingNatural_ {
/**
* @param {!../ampdoc-impl.AmpDoc} ampdoc
*/
constructor(ampdoc) {
/** @const {!../ampdoc-impl.AmpDoc} */
this.ampdoc = ampdoc;
/** @const {!Window} */
this.win = ampdoc.win;
/** @const {!../../service/platform-impl.Platform} */
this.platform_ = Services.platformFor(this.win);
/** @private @const {!Observable} */
this.scrollObservable_ = new Observable();
/** @private @const {!Observable} */
this.resizeObservable_ = new Observable();
/** @const {function()} */
this.boundScrollEventListener_ = this.handleScrollEvent_.bind(this);
/** @const {function()} */
this.boundResizeEventListener_ = () => this.resizeObservable_.fire();
dev().fine(TAG_, 'initialized natural viewport');
}
/** @private */
handleScrollEvent_() {
this.scrollObservable_.fire();
}
/** @override */
connect() {
this.win.addEventListener('scroll', this.boundScrollEventListener_);
this.win.addEventListener('resize', this.boundResizeEventListener_);
}
/** @override */
disconnect() {
this.win.removeEventListener('scroll', this.boundScrollEventListener_);
this.win.removeEventListener('resize', this.boundResizeEventListener_);
}
/** @override */
ensureReadyForElements() {
// Nothing.
}
/** @override */
getBorderTop() {
return 0;
}
/** @override */
requiresFixedLayerTransfer() {
return false;
}
/** @override */
overrideGlobalScrollTo() {
return false;
}
/** @override */
supportsPositionFixed() {
return true;
}
/** @override */
onScroll(callback) {
this.scrollObservable_.add(callback);
}
/** @override */
onResize(callback) {
this.resizeObservable_.add(callback);
}
/** @override */
updatePaddingTop(paddingTop) {
setImportantStyles(this.win.document.documentElement, {
'padding-top': px(paddingTop),
});
}
/** @override */
hideViewerHeader(transient, unusedLastPaddingTop) {
if (!transient) {
this.updatePaddingTop(0);
}
}
/** @override */
showViewerHeader(transient, paddingTop) {
if (!transient) {
this.updatePaddingTop(paddingTop);
}
}
/** @override */
disableScroll() {
// TODO(jridgewell): Recursively disable scroll
this.win.document.documentElement.classList.add(
'i-amphtml-scroll-disabled'
);
}
/** @override */
resetScroll() {
// TODO(jridgewell): Recursively disable scroll
this.win.document.documentElement.classList.remove(
'i-amphtml-scroll-disabled'
);
}
/** @override */
updateLightboxMode(unusedLightboxMode) {
// The layout is always accurate.
return Promise.resolve();
}
/** @override */
getSize() {
// Prefer window innerWidth/innerHeight but fall back to
// documentElement clientWidth/clientHeight.
// documentElement./*OK*/clientHeight is buggy on iOS Safari
// and thus cannot be used.
const winWidth = this.win./*OK*/ innerWidth;
const winHeight = this.win./*OK*/ innerHeight;
if (winWidth && winHeight) {
return {width: winWidth, height: winHeight};
}
const el = this.win.document.documentElement;
return {width: el./*OK*/ clientWidth, height: el./*OK*/ clientHeight};
}
/** @override */
getScrollTop() {
const pageScrollTop =
this.getScrollingElement()./*OK*/ scrollTop ||
this.win./*OK*/ pageYOffset;
const {host} = this.ampdoc.getRootNode();
return host
? pageScrollTop - /** @type {!HTMLElement} */ (host)./*OK*/ offsetTop
: pageScrollTop;
}
/** @override */
getScrollLeft() {
// The html is set to overflow-x: hidden so the document cannot be
// scrolled horizontally. The scrollLeft will always be 0.
return 0;
}
/** @override */
getScrollWidth() {
return this.getScrollingElement()./*OK*/ scrollWidth;
}
/** @override */
getScrollHeight() {
return this.getScrollingElement()./*OK*/ scrollHeight;
}
/** @override */
getContentHeight() {
// Don't use scrollHeight, since it returns `MAX(viewport_height,
// document_height)` (we only want the latter), and it doesn't account
// for margins. Also, don't use documentElement's rect height because
// there's no workable analog for either ios-embed-* modes.
const content = this.getScrollingElement();
const rect = content./*OK*/ getBoundingClientRect();
// The Y-position of `content` can be offset by the vertical margin
// of its first child, and this is _not_ accounted for in `rect.height`.
// This causes smaller than expected content height, so add it manually.
// Note this "top" value already includes padding-top of ancestor elements
// and getBorderTop().
const top = rect.top + this.getScrollTop();
// As of Safari 12.1.1, the getBoundingClientRect().height does not include
// the bottom margin of children and there's no other API that does.
const childMarginBottom = Services.platformFor(this.win).isSafari()
? marginBottomOfLastChild(this.win, content)
: 0;
const style = computedStyle(this.win, content);
return (
top +
parseInt(style.marginTop, 10) +
rect.height +
childMarginBottom +
parseInt(style.marginBottom, 10)
);
}
/** @override */
contentHeightChanged() {
// Nothing to do here.
}
/** @override */
getLayoutRect(el, opt_scrollLeft, opt_scrollTop) {
const b = el./*OK*/ getBoundingClientRect();
const scrollTop =
opt_scrollTop != undefined ? opt_scrollTop : this.getScrollTop();
const scrollLeft =
opt_scrollLeft != undefined ? opt_scrollLeft : this.getScrollLeft();
return layoutRectLtwh(
Math.round(b.left + scrollLeft),
Math.round(b.top + scrollTop),
Math.round(b.width),
Math.round(b.height)
);
}
/** @override */
getRootClientRectAsync() {
return Promise.resolve(null);
}
/** @override */
setScrollTop(scrollTop) {
this.getScrollingElement()./*OK*/ scrollTop = scrollTop;
}
/** @override */
getScrollingElement() {
const doc = this.win.document;
if (doc./*OK*/ scrollingElement) {
return doc./*OK*/ scrollingElement;
}
if (
doc.body &&
// Due to https://bugs.webkit.org/show_bug.cgi?id=106133, WebKit
// browsers have to use `body` and NOT `documentElement` for
// scrolling purposes. This has mostly being resolved via
// `scrollingElement` property, but this branch is still necessary
// for backward compatibility purposes.
this.platform_.isWebKit()
) {
return doc.body;
}
return doc.documentElement;
}
/** @override */
getScrollingElementScrollsLikeViewport() {
return true;
}
}