-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathpage.js
294 lines (264 loc) · 7.09 KB
/
page.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
/**
* Copyright 2019 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 {ViewportRelativePos} from './visibility-observer';
import {VisibilityState} from '../../../src/visibility-state';
import {devAssert} from '../../../src/log';
/** @enum {number} */
export const PageState = {
QUEUED: 1,
FETCHING: 2,
LOADED: 3,
FAILED: 4,
INSERTED: 5,
PAUSED: 6,
};
export const VISIBLE_DOC_CLASS = 'amp-next-page-visible';
export const HIDDEN_DOC_CLASS = 'amp-next-page-hidden';
/**
* @typedef {{
* url: string,
* image: string,
* title: string,
* }}
*/
export let PageMeta;
export class Page {
/**
* @param {!./service.NextPageService} manager
* @param {!PageMeta} meta
*/
constructor(manager, meta) {
/** @private @const {!./service.NextPageService} */
this.manager_ = manager;
/** @private @const {string} */
this.title_ = meta.title;
/** @private {string} */
this.url_ = meta.url;
/** @private {string} */
this.initialUrl_ = meta.url;
/** @private @const {string} */
this.image_ = meta.image;
/** @private {?../../../src/runtime.ShadowDoc} */
this.shadowDoc_ = null;
/** @private {?Element} */
this.container_ = null;
/** @private {?Document} */
this.content_ = null;
/** @private {!PageState} */
this.state_ = PageState.QUEUED;
/** @private {!VisibilityState} */
this.visibilityState_ = VisibilityState.PRERENDER;
/** @private {!ViewportRelativePos} */
this.relativePos_ = ViewportRelativePos.OUTSIDE_VIEWPORT;
}
/** @return {string} */
get url() {
return this.url_;
}
/**
* @param {string} url
*/
set url(url) {
this.url_ = url;
}
/** @return {string} */
get initialUrl() {
return this.initialUrl_;
}
/** @return {string} */
get image() {
return this.image_;
}
/** @return {string} */
get title() {
return this.title_;
}
/** @return {!ViewportRelativePos} */
get relativePos() {
return this.relativePos_;
}
/** @return {!Document|!ShadowRoot|undefined} */
get document() {
if (!this.shadowDoc_) {
return;
}
return this.shadowDoc_.ampdoc.getRootNode();
}
/** @return {?Element} */
get container() {
return this.container_;
}
/** @return {?../../../src/runtime.ShadowDoc} */
get shadowDoc() {
return this.shadowDoc_;
}
/** @param {!ViewportRelativePos} position */
set relativePos(position) {
this.relativePos_ = position;
}
/**
* @param {!PageState} state
* @return {boolean}
*/
is(state) {
return this.state_ === state;
}
/**
* @return {boolean}
*/
isLoaded() {
return (
this.state_ === PageState.LOADED ||
this.state_ === PageState.INSERTED ||
this.state_ === PageState.PAUSED
);
}
/**
* @return {boolean}
*/
isVisible() {
return this.isLoaded() && this.visibilityState_ === VisibilityState.VISIBLE;
}
/**
* @param {VisibilityState} visibilityState
*/
setVisibility(visibilityState) {
if (!this.isLoaded() || visibilityState == this.visibilityState_) {
return;
}
//Reload the page if necessary
if (
this.is(PageState.PAUSED) &&
visibilityState === VisibilityState.VISIBLE
) {
this.resume();
}
// Update visibility internally and at the shadow doc level
this.visibilityState_ = visibilityState;
if (this.shadowDoc_) {
this.shadowDoc_.setVisibilityState(visibilityState);
this.shadowDoc_.ampdoc
.getBody()
.classList.toggle(VISIBLE_DOC_CLASS, this.isVisible());
this.shadowDoc_.ampdoc
.getBody()
.classList.toggle(HIDDEN_DOC_CLASS, !this.isVisible());
}
}
/**
* Creates a placeholder in place of the original page and unloads
* the shadow root from memory
* @return {!Promise}
*/
pause() {
if (!this.shadowDoc_) {
return Promise.resolve();
}
return this.shadowDoc_.close().then(() => {
return this.manager_.closeDocument(this /** page */).then(() => {
this.shadowDoc_ = null;
this.visibilityState_ = VisibilityState.HIDDEN;
this.state_ = PageState.PAUSED;
});
});
}
/**
* Removes the placeholder and re-renders the page after its shadow
* root has been removed
* @return {!Promise}
*/
resume() {
return this.attach_();
}
/**
* Asks the next-page manager to fetch the document's HTML
* and inserts it
* @return {!Promise}
*/
fetch() {
// TOOD(wassgha): Should we re-fetch on failure? or show a failed state?
// or skip to the next available document? (currently breaks silently)
if (
this.state_ === PageState.INSERTED ||
this.state_ === PageState.FETCHING ||
this.state_ === PageState.LOADED ||
this.state_ === PageState.FAILED
) {
return Promise.resolve();
}
this.state_ = PageState.FETCHING;
return this.manager_
.fetchPageDocument(this)
.then((content) => {
this.state_ = PageState.LOADED;
this.content_ = content;
this.container_ = this.manager_.createDocumentContainerForPage(
this /** page */
);
// TODO(wassgha): To further optimize, this should ideally
// be parsed from the service worker instead of stored in memory
return this.attach_();
})
.catch(() => {
this.state_ = PageState.FAILED;
});
}
/**
* Inserts the fetched (or cached) HTML as the document's content
* @return {!Promise}
*/
attach_() {
return this.manager_
.attachDocumentToPage(
this /** page */,
/** @type {!Document} */ (devAssert(this.content_)),
this.is(PageState.PAUSED) /** force */
)
.then((shadowDoc) => {
if (!shadowDoc) {
this.state_ = PageState.FAILED;
return;
}
this.state_ = PageState.INSERTED;
this.shadowDoc_ = shadowDoc;
if (!this.is(PageState.PAUSED)) {
this.manager_.setLastFetchedPage(this);
}
});
}
}
export class HostPage extends Page {
/**
* @param {!./service.NextPageService} manager
* @param {{ url: string, title: string, image: string }} meta
* @param {!PageState} initState
* @param {!VisibilityState} initVisibility
* @param {!Document} doc
*/
constructor(manager, meta, initState, initVisibility, doc) {
super(manager, meta);
/** @override */
this.state_ = initState;
/** @override */
this.visibilityState_ = initVisibility;
/** @private {!Document} */
this.document_ = doc;
}
/** @override */
get document() {
return this.document_;
}
}