This repository was archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
442 lines (378 loc) · 12.2 KB
/
index.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
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
//
// Mobile Viewport Control v0.3.1
//
// Copyright (c) 2016 Shaun Williams
// ISC License
//
// GitHub: https://github.com/stripe/mobile-viewport-control
//
//---------------------------------------------------------------------------
// JS Module Boilerplate
//---------------------------------------------------------------------------
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('mobile-viewport-control', [], factory);
}
else if (typeof module === 'object' && module.exports) {
module.exports = factory();
}
else {
root.mobileViewportControl = factory();
}
}(this, function() {
//---------------------------------------------------------------------------
// Getting/Setting Scroll position
//---------------------------------------------------------------------------
function getScroll() {
return {
top: window.pageYOffset || document.documentElement.scrollTop,
left: window.pageXOffset || document.documentElement.scrollLeft
};
}
function setScroll(scroll) {
if (window.scrollTo) {
window.scrollTo(scroll.left, scroll.top);
} else {
document.documentElement.scrollTop = scroll.top;
document.documentElement.scrollLeft = scroll.left;
document.body.scrollTop = scroll.top;
document.body.scrollLeft = scroll.left;
}
}
//---------------------------------------------------------------------------
// Getting Initial Viewport from <meta name='viewport'> tags
// but we also include implicit defaults.
//---------------------------------------------------------------------------
function getInitialViewport(withDefaults) {
var viewport = {};
if (withDefaults) {
// These seem to be the defaults
viewport = {
'user-scalable': 'yes',
'minimum-scale': '0',
'maximum-scale': '10'
};
}
var tags = document.querySelectorAll('meta[name=viewport]');
var i,j,tag,content,keyvals,keyval;
for (i=0; i<tags.length; i++) {
tag = tags[i];
content = tag.getAttribute('content');
if (tag.id !== hookID && content) {
keyvals = content.split(',');
for (j=0; j<keyvals.length; j++) {
keyval = keyvals[j].split('=');
if (keyval.length === 2) {
viewport[keyval[0].trim()] = keyval[1].trim();
}
}
}
}
return viewport;
}
function getPrettyInitialViewport() {
var initial = getInitialViewport();
var keyvals = [];
for (var prop in initial) {
if (initial.hasOwnProperty(prop)) {
keyvals.push({key:prop, val:initial[prop]});
}
}
return (
keyvals.sort(function(a,b) {
if (a.key < b.key) return -1;
if (a.key > b.key) return 1;
return 0;
}).map(function(kv) {
return kv.key + '=' + kv.val;
}).join(',\n')
);
}
//---------------------------------------------------------------------------
// Calculating current viewport scale
// simplified from: http://menacingcloud.com/?c=viewportScale
//---------------------------------------------------------------------------
function getOrientation() {
var degrees = window.orientation;
var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;
if(degrees === undefined) {
return (w > h) ? 'landscape' : 'portrait';
}
return (degrees % 180 === 0) ? 'portrait' : 'landscape';
}
function getOrientedScreenWidth() {
var orientation = getOrientation();
var sw = screen.width;
var sh = screen.height;
return (orientation === 'portrait') ? Math.min(sw,sh) : Math.max(sw,sh);
}
function getScale() {
var visualViewportWidth = window.innerWidth;
var screenWidth = getOrientedScreenWidth();
return screenWidth / visualViewportWidth;
}
//---------------------------------------------------------------------------
// Get mobile OS
// from: http://stackoverflow.com/a/21742107
//---------------------------------------------------------------------------
function getMobileOS() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (userAgent.match(/iPad/i) ||
userAgent.match(/iPhone/i) ||
userAgent.match(/iPod/i)) {
return 'iOS';
}
else if (userAgent.match(/Android/i)) {
return 'Android';
}
}
function isFirefox() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
return userAgent.match(/Firefox/i) ? true : false;
}
//---------------------------------------------------------------------------
// State and Constants
//---------------------------------------------------------------------------
// A unique ID of the meta-viewport tag we must create
// to hook into and control the viewport.
var hookID = '__mobileViewportControl_hook__';
// A unique ID of the CSS style tag we must create to
// add rules for hiding the body.
var styleID = '__mobileViewPortControl_style__';
// An empirical guess for the maximum time that we have to
// wait before we are confident a viewport change has registered.
var refreshDelay = 200;
// Original viewport state before freezing.
var originalScale;
var originalScroll;
// Classes we use to make our css selector specific enough
// to hopefully override all other selectors.
// (mvc__ = mobileViewportControl prefix for uniqueness)
var hiddenClasses = [
'mvc__a',
'mvc__lot',
'mvc__of',
'mvc__classes',
'mvc__to',
'mvc__increase',
'mvc__the',
'mvc__odds',
'mvc__of',
'mvc__winning',
'mvc__specificity'
];
//---------------------------------------------------------------------------
// Isolating an Element
//---------------------------------------------------------------------------
function isolatedStyle(elementID) {
var classes = hiddenClasses.join('.');
return [
// We effectively clear the <html> and <body> background
// and sizing attributes.
'html.' + classes + ',',
'html.' + classes + ' > body {',
' background: #fff;',
' width: auto;',
' min-width: inherit;',
' max-width: inherit;',
' height: auto;',
' min-height: inherit;',
' max-height: inherit;',
' margin: 0;',
' padding: 0;',
' border: 0;',
'}',
// hide everything in the body...
'html.' + classes + ' > body > * {',
' display: none !important;',
'}',
// ...except the given element ID
'html.' + classes + ' > body > #' + elementID + ' {',
' display: block !important;',
'}'
].join('\n');
}
function isolate(elementID) {
// add classes to body tag to isolate all other elements
var classes = hiddenClasses.join(' ');
var html = document.documentElement;
html.className += ' ' + classes;
// add isolating style rules
var style = document.createElement('style');
style.id = styleID;
style.type = 'text/css';
style.appendChild(document.createTextNode(isolatedStyle(elementID)));
document.head.appendChild(style);
}
function undoIsolate() {
// remove isolating classes from body tag
var classes = hiddenClasses.join(' ');
var html = document.documentElement;
html.className = html.className.replace(classes, '');
// remove isolating style rules
var style = document.getElementById(styleID);
document.head.removeChild(style);
}
//---------------------------------------------------------------------------
// Freezing
//---------------------------------------------------------------------------
// Freeze the viewport to a given scale.
function freeze(scale) {
// optional arguments
var isolateID, onDone;
// get optional arguments using their type
var args = Array.prototype.slice.call(arguments, 1);
if (typeof args[0] === 'string') {
isolateID = args[0];
args.splice(0, 1);
}
if (typeof args[0] === 'function') {
onDone = args[0];
}
// save original viewport state
originalScroll = getScroll();
originalScale = getScale();
// isolate element if needed
if (isolateID) {
isolate(isolateID);
setScroll({x:0,y:0});
}
// validate scale
// (we cannot freeze scale at 1.0 on Android)
if (scale === 1) {
scale = 1.002;
}
// add our new meta viewport tag
var hook = document.getElementById(hookID);
if (!hook) {
hook = document.createElement('meta');
hook.id = hookID;
hook.name = 'viewport';
document.head.appendChild(hook);
}
// When freezing the viewport, we still enable
// user-scalability and allow a tight zooming
// margin. Without this, UIWebView would simply
// ignore attempts to set the scale. But with this
// solution, the next time the user pinch-zooms
// in this state, the viewport will auto-snap
// to our scale.
var includeWidth = (getMobileOS() === 'Android' && isFirefox());
hook.setAttribute('content', [
'user-scalable=yes',
'initial-scale='+scale,
'minimum-scale='+scale,
'maximum-scale='+(scale+0.004),
(includeWidth ? 'width=device-width' : null)
].filter(Boolean).join(','));
if (onDone) {
setTimeout(onDone, refreshDelay);
}
}
//---------------------------------------------------------------------------
// Thawing
//---------------------------------------------------------------------------
function thawWebkit(hook, initial, onDone) {
// Restore the user's manual zoom.
hook.setAttribute('content', [
'initial-scale='+originalScale,
'minimum-scale='+originalScale,
'maximum-scale='+originalScale
].join(','));
// Restore the page's zoom bounds.
hook.setAttribute('content', [
'user-scalable='+initial['user-scalable'],
'minimum-scale='+initial['minimum-scale'],
'maximum-scale='+initial['maximum-scale'],
(initial.width ? 'width='+initial.width : null)
].filter(Boolean).join(','));
// Remove our meta viewport hook.
document.head.removeChild(hook);
setScroll(originalScroll);
setTimeout(function() {
if (onDone)
onDone();
}, refreshDelay);
}
function thawGecko(hook, initial, onDone) {
// Restore the user's manual zoom.
hook.setAttribute('content', [
'initial-scale='+originalScale,
'minimum-scale='+originalScale,
'maximum-scale='+originalScale
].join(','));
// Updating the scroll here is too early,
// but it's used to force a refresh of the viewport
// with our current desired scale.
setScroll(originalScroll);
setTimeout(function(){
// Restore the page's zoom bounds.
hook.setAttribute('content', [
'user-scalable='+initial['user-scalable'],
'minimum-scale='+initial['minimum-scale'],
'maximum-scale='+initial['maximum-scale'],
(initial.width ? 'width='+initial.width : null)
].filter(Boolean).join(','));
// Restore the scroll again now that the scale is correct.
setScroll(originalScroll);
// Remove our meta viewport hook.
document.head.removeChild(hook);
if (onDone)
onDone();
}, refreshDelay);
}
function thawBlink(hook, initial, onDone) {
hook.setAttribute('content', [
'user-scalable='+initial['user-scalable'],
// WebView does not support this:
//'initial-scale='+originalScale
'initial-scale='+initial['initial-scale'],
'minimum-scale='+initial['minimum-scale'],
'maximum-scale='+initial['maximum-scale'],
(initial.width ? 'width='+initial.width : null)
].filter(Boolean).join(','));
setScroll(originalScroll);
setTimeout(function(){
document.head.removeChild(hook);
if (onDone)
onDone();
}, refreshDelay);
}
// Thaw the viewport, restoring the scale and scroll to what it
// was before freezing.
function thaw(onDone) {
// restore body visibility
var style = document.getElementById(styleID);
if (style) {
undoIsolate();
}
// exit if there is nothing to thaw
var hook = document.getElementById(hookID);
if (!hook) {
return;
}
var initial = getInitialViewport(true);
// thaw function defaults to webkit
var thawFunc = thawWebkit;
var os = getMobileOS();
if (os === 'Android') { thawFunc = isFirefox() ? thawGecko : thawBlink; }
else if (os === 'iOS') { thawFunc = thawWebkit; }
// call appropriate thaw function
thawFunc(hook, initial, onDone);
}
//---------------------------------------------------------------------------
// Public API
//---------------------------------------------------------------------------
return {
getInitialViewport: getInitialViewport,
getPrettyInitialViewport: getPrettyInitialViewport,
getScale: getScale,
isolate: isolate,
undoIsolate: undoIsolate,
// stable
version: '0.3.1',
freeze: freeze,
thaw: thaw
};
})); // end module scope