source: Dev/trunk/src/client/dijit/Viewport.js @ 524

Last change on this file since 524 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 2.8 KB
Line 
1define([
2        "dojo/Evented",
3        "dojo/on",
4        "dojo/domReady",
5        "dojo/sniff",   // has("ie"), has("ios")
6        "dojo/window" // getBox()
7], function(Evented, on, domReady, has, winUtils){
8
9        // module:
10        //              dijit/Viewport
11
12        /*=====
13        return {
14                // summary:
15                //              Utility singleton to watch for viewport resizes, avoiding duplicate notifications
16                //              which can lead to infinite loops.
17                // description:
18                //              Usage: Viewport.on("resize", myCallback).
19                //
20                //              myCallback() is called without arguments in case it's _WidgetBase.resize(),
21                //              which would interpret the argument as the size to make the widget.
22        };
23        =====*/
24
25        var Viewport = new Evented();
26
27        var focusedNode;
28
29        domReady(function(){
30                var oldBox = winUtils.getBox();
31                Viewport._rlh = on(window, "resize", function(){
32                        var newBox = winUtils.getBox();
33                        if(oldBox.h == newBox.h && oldBox.w == newBox.w){ return; }
34                        oldBox = newBox;
35                        Viewport.emit("resize");
36                });
37
38                // Also catch zoom changes on IE8, since they don't naturally generate resize events
39                if(has("ie") == 8){
40                        var deviceXDPI = screen.deviceXDPI;
41                        setInterval(function(){
42                                if(screen.deviceXDPI != deviceXDPI){
43                                        deviceXDPI = screen.deviceXDPI;
44                                        Viewport.emit("resize");
45                                }
46                        }, 500);
47                }
48
49                // On iOS, keep track of the focused node so we can guess when the keyboard is/isn't being displayed.
50                if(has("ios")){
51                        on(document, "focusin", function(evt){
52                                focusedNode = evt.target;
53                        });
54                        on(document, "focusout", function(evt){
55                                focusedNode = null;
56                        });
57                }
58        });
59
60        Viewport.getEffectiveBox = function(/*Document*/ doc){
61                // summary:
62                //              Get the size of the viewport, or on mobile devices, the part of the viewport not obscured by the
63                //              virtual keyboard.
64
65                var box = winUtils.getBox(doc);
66
67                // Account for iOS virtual keyboard, if it's being shown.  Unfortunately no direct way to check or measure.
68                var tag = focusedNode && focusedNode.tagName && focusedNode.tagName.toLowerCase();
69                if(has("ios") && focusedNode && !focusedNode.readOnly && (tag == "textarea" || (tag == "input" &&
70                        /^(color|email|number|password|search|tel|text|url)$/.test(focusedNode.type)))){
71
72                        // Box represents the size of the viewport.  Some of the viewport is likely covered by the keyboard.
73                        // Estimate height of visible viewport assuming viewport goes to bottom of screen, but is covered by keyboard.
74                        box.h *= (orientation == 0 || orientation == 180 ? 0.66 : 0.40);
75
76                        // Above measurement will be inaccurate if viewport was scrolled up so far that it ends before the bottom
77                        // of the screen.   In this case, keyboard isn't covering as much of the viewport as we thought.
78                        // We know the visible size is at least the distance from the top of the viewport to the focused node.
79                        var rect = focusedNode.getBoundingClientRect();
80                        box.h = Math.max(box.h, rect.top + rect.height);
81                }
82
83                return box;
84        };
85
86        return Viewport;
87});
Note: See TracBrowser for help on using the repository browser.