forked from dojo/dijit-oldmirror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Viewport.js
79 lines (67 loc) · 2.15 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
72
73
74
75
76
77
78
79
define([
"dojo/Evented",
"dojo/on",
"dojo/domReady",
"dojo/sniff", // has("ie"), has("ios")
"dojo/_base/window", // global
"dojo/window" // getBox()
], function(Evented, on, domReady, has, win, winUtils){
// module:
// dijit/Viewport
/*=====
return {
// summary:
// Utility singleton to watch for viewport resizes, avoiding duplicate notifications
// which can lead to infinite loops.
// description:
// Usage: Viewport.on("resize", myCallback).
//
// myCallback() is called without arguments in case it's _WidgetBase.resize(),
// which would interpret the argument as the size to make the widget.
};
=====*/
var Viewport = new Evented();
var focusedNode;
domReady(function(){
var oldBox = winUtils.getBox();
Viewport._rlh = on(win.global, "resize", function(){
var newBox = winUtils.getBox();
if(oldBox.h == newBox.h && oldBox.w == newBox.w){ return; }
oldBox = newBox;
Viewport.emit("resize");
});
// Also catch zoom changes on IE8, since they don't naturally generate resize events
if(has("ie") == 8){
var deviceXDPI = screen.deviceXDPI;
setInterval(function(){
if(screen.deviceXDPI != deviceXDPI){
deviceXDPI = screen.deviceXDPI;
Viewport.emit("resize");
}
}, 500);
}
// On iOS, keep track of the focused node so we can guess when the keyboard is/isn't being displayed.
if(true || has("ios")){
on(document, "focusin", function(evt){
focusedNode = evt.target;
});
on(document, "focusout", function(evt){
focusedNode = null;
});
}
});
Viewport.getEffectiveBox = function(/*Document*/ doc){
// summary:
// Get the size of the viewport, or on mobile devices, the part of the viewport not obscured by the
// virtual keyboard.
var box = winUtils.getBox(doc);
// Account for iOS virtual keyboard, if it's being shown. Unfortunately no direct way to check or measure.
var tag = focusedNode && focusedNode.tagName.toLowerCase();
if(has("ios") && (tag == "textarea" || (tag == "input" &&
/^(color|email|number|password|search|tel|text|url)$/.test(focusedNode.type)))){
box.h *= (box.h > box.w ? 0.66 : 0.40);
}
return box;
};
return Viewport;
});