forked from ibm-js/delite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Viewport.js
71 lines (63 loc) · 2.47 KB
/
Viewport.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
/**
* Utility singleton to watch for viewport resizes, avoiding duplicate notifications
* which can lead to infinite loops.
*
* Usage: `Viewport.on("resize", myCallback)`.
*
* myCallback() is called without arguments in case it's Widget.resize(),
* which would interpret the argument as the size to make the widget.
*
* @module delite/Viewport
*/
define([
"decor/Evented",
"decor/sniff", // has("ios")
"requirejs-domready/domReady!"
], function (Evented, has) {
var Viewport = new Evented();
var html = document.documentElement,
oldWidth = html.clientWidth,
oldHeight = html.clientHeight;
window.addEventListener("resize", function () {
var width = html.clientWidth,
height = html.clientHeight;
if (height === oldHeight && width === oldWidth) {
return;
}
oldWidth = width;
oldHeight = height;
Viewport.emit("resize");
});
/**
* Get the size of the viewport, or on mobile devices, the part of the viewport not obscured by the
* virtual keyboard.
* @function module:delite/Viewport.getEffectiveBox
* @param {Document} doc - The document, typically the global variable `document`.
*/
Viewport.getEffectiveBox = function (doc) {
var html = doc.documentElement,
box = {
w: html.clientWidth,
h: html.clientHeight,
t: doc.body.scrollTop,
l: doc.body.scrollLeft
};
// Account for iOS virtual keyboard, if it's being shown. Unfortunately no direct way to check or measure.
var focusedNode = doc.activeElement,
tag = focusedNode && focusedNode.tagName && focusedNode.tagName.toLowerCase();
if (has("ios") && focusedNode && !focusedNode.readOnly && (tag === "textarea" || (tag === "input" &&
/^(color|email|number|password|search|tel|text|url)$/.test(focusedNode.type)))) {
// Box represents the size of the viewport. Some of the viewport is likely covered by the keyboard.
// Estimate height of visible viewport assuming viewport goes to bottom of screen,
// but is covered by keyboard.
box.h *= (window.orientation === 0 || window.orientation === 180 ? 0.66 : 0.40);
// Above measurement will be inaccurate if viewport was scrolled up so far that it ends before the bottom
// of the screen. In this case, keyboard isn't covering as much of the viewport as we thought.
// We know the visible size is at least the distance from the top of the viewport to the focused node.
var rect = focusedNode.getBoundingClientRect();
box.h = Math.max(box.h, rect.top + rect.height);
}
return box;
};
return Viewport;
});