forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.html
448 lines (372 loc) · 11.7 KB
/
viewer.html
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Viewer</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<link href='https://fonts.googleapis.com/css?family=Questrial' rel='stylesheet' type='text/css'>
<style>
html, body {
overflow: hidden;
}
body {
font-family: 'Questrial', Arial;
}
body, viewer {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
margin: 0;
padding: 0;
}
viewer {
background: #eee;
}
header {
position: absolute;
z-index: 1;
top: 0;
left: 0;
right: 0;
height: 50px;
background: #4285F4;
opacity: 0.7;
color: #fff;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
padding: 0 8px;
}
@media screen and (min-width: 500px) {
header {
height: 64px;
}
}
header h1 {
font-size: 22px;
flex-grow: 1;
}
header .show-container-anchor {
flex-grow: 0;
flex-shrink: 0;
padding: 8px;
}
header #visibilityToggle {
flex-grow: 0;
flex-shrink: 0;
padding: 8px;
}
.wait {
position: absolute;
z-index: 3;
top: 100px;
left: 20px;
font-size: 12px;
}
container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow-x: hidden;
visibility: hidden;
}
viewer[data-show-container="1"] #container1 {
visibility: visible;
}
viewer[data-show-container="1"] #show-container1-anchor {
text-decoration: underline;
}
viewer[data-show-container="2"] #container2 {
visibility: visible;
}
viewer[data-show-container="2"] #show-container2-anchor {
text-decoration: underline;
}
viewer.natural container {
overflow-y: hidden;
}
viewer.virtual container {
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
container iframe {
border: 0;
margin: 0;
padding: 0;
width: 100%;
}
viewer.natural container iframe {
height: 100%;
}
</style>
<script src="./viewer-integr-messaging.js"></script>
<script>
var SENTINEL = '__AMP__';
var allViewers = [];
function Viewer(containerId, id, visible) {
this.id = id;
this.alreadyLoaded_ = false;
this.stackIndex_ = 0;
this.viewportType_ = 'natural'; // "natural" or "virtual"
this.visibilityState_ = visible ? 'visible' : 'hidden';
this.prerenderSize_ = 1;
this.isIos_ = /iPhone|iPad|iPod/i.test(window.navigator.userAgent);
this.viewer = document.querySelector('viewer');
this.header = document.querySelector('header');
this.container = document.getElementById(containerId);
this.iframe = document.createElement('iframe');
// TODO(dvoytenko): Set to the final value when crbug/577330 is fixed:
// this.iframe.setAttribute('sandbox',
// 'allow-popups allow-scripts allow-forms allow-pointer-lock' +
// ' allow-popups-to-escape-sandbox allow-same-origin');
this.visibilityToggle_ = document.getElementById('visibilityToggle');
this.visibilityToggle_.onclick = function() {
this.toggleVisibility(this.visibilityState_ != 'visible');
}.bind(this);
if (visible) {
this.updateVisibilityToggle_();
}
this.viewer.classList.add(this.viewportType_);
if (this.viewportType_ == 'natural' && !this.isIos_) {
this.iframe.setAttribute('scrolling', 'yes');
} else {
this.iframe.setAttribute('scrolling', 'no');
}
if (this.viewportType_ == 'virtual') {
this.container.addEventListener('scroll', this.onScroll_.bind(this));
}
window.addEventListener('resize', this.onResize_.bind(this));
window.addEventListener('popstate', this.onPopState_.bind(this));
};
Viewer.prototype.start = function() {
allViewers.push(this);
var params = {
history: 1,
viewportType: this.viewportType_,
width: this.container./*OK*/offsetWidth,
height: this.container./*OK*/offsetHeight,
paddingTop: this.header./*OK*/offsetHeight,
visibilityState: this.visibilityState_,
prerenderSize: this.prerenderSize_
};
log('Params:' + JSON.stringify(params));
var url = './article-access.amp.max.html#' + paramsStr(params);
if (window.location.hash && window.location.hash.length > 1) {
url += '&' + window.location.hash.substring(1);
}
log('AMP URL = ', url);
this.iframe.style.display = 'none';
this.container.appendChild(this.iframe);
// Notice that name can only be set once the IFrame is in the DOM.
this.iframe.contentWindow.name = SENTINEL + paramsStr(params);
this.messaging_ = new ViewerMessaging(this.iframe.contentWindow,
this.processRequest_.bind(this));
this.iframe.onload = this.loaded_.bind(this);
setTimeout(function() {
this.iframe.style.visibility = 'hidden';
if (this.viewportType_ == 'virtual') {
this.iframe.style.height = this.container./*OK*/offsetHeight + 'px';
}
this.iframe.style.display = '';
this.iframe.setAttribute('src', url);
}.bind(this));
};
Viewer.prototype.toggleVisibility = function(visible) {
log('Viewer ' + this.id + ' visibility -> ', visible);
this.visibilityState_ = visible ? 'visible' : 'hidden';
this.messaging_.sendRequest('visibilitychange', {
state: this.visibilityState_,
prerenderSize: this.prerenderSize
}, true);
if (visible) {
this.updateVisibilityToggle_();
}
};
Viewer.prototype.loaded_ = function() {
if (this.alreadyLoaded_) {
return;
}
log('AMP Loaded');
this.alreadyLoaded_ = true;
var waiter = this.container.querySelector('.wait');
if (waiter) {
waiter.parentElement.removeChild(waiter);
}
this.iframe.style.display = '';
this.iframe.style.visibility = '';
};
Viewer.prototype.documentReady_ = function() {
log('AMP document ready');
this.loaded_();
return Promise.resolve();
};
Viewer.prototype.documentHeight_ = function(documentHeight) {
log('AMP document height:', documentHeight);
if (this.viewportType_ == 'virtual') {
this.iframe.style.height = Math.max(documentHeight,
this.container./*OK*/offsetHeight) + 'px';
}
return Promise.resolve();
};
Viewer.prototype.onScroll_ = function() {
this.messaging_.sendRequest('viewport', {
scrollTop: this.container./*OK*/scrollTop
}, false);
};
Viewer.prototype.onResize_ = function() {
log('Resized to ', this.container./*OK*/offsetWidth,
this.container./*OK*/offsetHeight,
this.header./*OK*/offsetHeight,
this.container./*OK*/scrollTop);
this.messaging_.sendRequest('viewport', {
scrollTop: this.container./*OK*/scrollTop,
width: this.container./*OK*/offsetWidth,
height: this.container./*OK*/offsetHeight,
paddingTop: this.header./*OK*/offsetHeight
}, false);
};
Viewer.prototype.requestFullOverlay_ = function() {
log('requestFullOverlay');
this.header.style.opacity = 0;
return Promise.resolve();
};
Viewer.prototype.cancelFullOverlay_ = function() {
log('cancelFullOverlay');
this.header.style.opacity = 1;
return Promise.resolve();
};
Viewer.prototype.pushHistory_ = function(stackIndex) {
log('push history to ', stackIndex);
// Super trivial. Only one step allowed.
if (stackIndex != this.stackIndex_ + 1) {
throw new Error('Only one step push allowed');
}
this.stackIndex_ = stackIndex;
window.history.pushState({}, '');
};
Viewer.prototype.popHistory_ = function(stackIndex) {
log('pop history at ', stackIndex);
// Super trivial. Only one step allowed.
if (stackIndex != this.stackIndex_) {
throw new Error('Only one step pop allowed');
}
this.stackIndex_ = stackIndex;
window.history.go(-1);
};
Viewer.prototype.onPopState_ = function() {
// Even more trivial. Always assumes that only one step was popped in
// history.
this.stackIndex_--;
this.messaging_.sendRequest('historyPopped', {
newStackIndex: this.stackIndex_
}, false);
log('history popped to ', this.stackIndex_);
};
Viewer.prototype.updateVisibilityToggle_ = function() {
this.visibilityToggle_.textContent = this.visibilityState_ == 'visible' ?
'Visible' : 'Prerendering';
};
Viewer.prototype.broadcast_ = function(message) {
log('broadcast: ', message);
var self = this;
allViewers.forEach(function(viewer) {
if (viewer != self) {
viewer.messaging_.sendRequest('broadcast', message, false);
}
});
};
Viewer.prototype.processRequest_ = function(type, data, awaitResponse) {
if (type == 'documentLoaded') {
return this.documentReady_();
}
if (type == 'documentResized') {
return this.documentHeight_(data.height);
}
if (type == 'requestFullOverlay') {
return this.requestFullOverlay_();
}
if (type == 'cancelFullOverlay') {
return this.cancelFullOverlay_();
}
if (type == 'pushHistory') {
return this.pushHistory_(data.stackIndex);
}
if (type == 'popHistory') {
return this.popHistory_(data.stackIndex);
}
if (type == 'broadcast') {
return this.broadcast_(data);
}
return Promise.reject('request not supported: ' + type);
};
function log() {
var var_args = Array.prototype.slice.call(arguments, 0);
var_args.unshift('[VIEWER]');
console/*OK*/.log.apply(console, var_args);
}
function paramsStr(params) {
var s = '';
for (var k in params) {
var v = params[k];
if (v === null || v === undefined) {
continue;
}
if (s.length > 0) {
s += '&';
}
s += encodeURIComponent(k) + '=' + encodeURIComponent(v);
}
return s;
}
function loadAmpDoc() {
new Viewer('container1', '1', true).start();
addShowContainer('1');
new Viewer('container2', '2', false).start();
addShowContainer('2');
showContainer('1');
}
function addShowContainer(id) {
var anchor = document.getElementById('show-container' + id + '-anchor');
anchor.onclick = function() {
showContainer(id);
};
}
function showContainer(id) {
log('Show container: ', id);
var viewerEl = document.querySelector('viewer');
viewerEl.setAttribute('data-show-container', id);
allViewers.forEach(function(viewer) {
viewer.toggleVisibility(viewer.id == id);
});
}
window.onload = loadAmpDoc;
</script>
</head>
<body>
<viewer>
<header>
<h1>Viewer</h1>
<a class="show-container-anchor" id="show-container1-anchor">One</a>
<a class="show-container-anchor" id="show-container2-anchor">Two</a>
<a>|</a>
<a id="visibilityToggle">Visible</a>
</header>
<container id="container1">
<div class="wait">
Please wait, the AMP doc will appear here...
</div>
</container>
<container id="container2">
<div class="wait">
Please wait, the AMP doc will appear here...
</div>
</container>
</viewer>
</body>
</html>