@qooxdoo/framework
Version:
The JS Framework for Coders
288 lines (243 loc) • 10.3 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Sebastian Werner (wpbasti)
* Sebastian Fastner (fastner)
* Tino Butz (tbtz)
======================================================================
This class contains code based on the following work:
* Unify Project
Homepage:
http://unify-project.org
Copyright:
2009-2010 Deutsche Telekom AG, Germany, http://telekom.com
License:
MIT: http://www.opensource.org/licenses/mit-license.php
* Yahoo! UI Library
http://developer.yahoo.com/yui
Version 2.2.0
Copyright:
(c) 2007, Yahoo! Inc.
License:
BSD: http://developer.yahoo.com/yui/license.txt
----------------------------------------------------------------------
http://developer.yahoo.com/yui/license.html
Copyright (c) 2009, Yahoo! Inc.
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yahoo! Inc. nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission of Yahoo! Inc.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************ */
/**
* Includes library functions to work with the client's viewport (window).
* Orientation related functions are point to window.top as default.
*/
qx.Bootstrap.define("qx.bom.Viewport",
{
statics :
{
/**
* Returns the current width of the viewport (excluding the vertical scrollbar
* if present).
*
* @param win {Window?window} The window to query
* @return {Integer} The width of the viewable area of the page (excluding scrollbars).
*/
getWidth : function(win)
{
var win = win || window;
var doc = win.document;
return qx.bom.Document.isStandardMode(win) ? doc.documentElement.clientWidth : doc.body.clientWidth;
},
/**
* Returns the current height of the viewport (excluding the horizontal scrollbar
* if present).
*
* @param win {Window?window} The window to query
* @return {Integer} The Height of the viewable area of the page (excluding scrollbars).
*/
getHeight : function(win)
{
var win = win || window;
var doc = win.document;
// [BUG #7785] Document element's clientHeight is calculated wrong on iPad iOS7
if(qx.core.Environment.get("os.name") == "ios" && window.innerHeight != doc.documentElement.clientHeight) {
return window.innerHeight;
}
return qx.bom.Document.isStandardMode(win) ? doc.documentElement.clientHeight : doc.body.clientHeight;
},
/**
* Returns the scroll position of the viewport
*
* All clients except IE < 9 support the non-standard property <code>pageXOffset</code>.
* As this is easier to evaluate we prefer this property over <code>scrollLeft</code>.
* Since the window could differ from the one the application is running in, we can't
* use a one-time environment check to decide which property to use.
*
* @param win {Window?window} The window to query
* @return {Integer} Scroll position in pixels from left edge, always a positive integer or zero
*/
getScrollLeft : function(win)
{
var win = win ? win : window;
if (typeof win.pageXOffset !== "undefined") {
return win.pageXOffset;
}
// Firefox is using 'documentElement.scrollLeft' and Chrome is using
// 'document.body.scrollLeft'. For the other value each browser is returning
// 0, so we can use this check to get the positive value without using specific
// browser checks.
var doc = win.document;
return doc.documentElement.scrollLeft || doc.body.scrollLeft;
},
/**
* Returns the scroll position of the viewport
*
* All clients except MSHTML support the non-standard property <code>pageYOffset</code>.
* As this is easier to evaluate we prefer this property over <code>scrollTop</code>.
* Since the window could differ from the one the application is running in, we can't
* use a one-time environment check to decide which property to use.
*
* @param win {Window?window} The window to query
* @return {Integer} Scroll position in pixels from top edge, always a positive integer or zero
*/
getScrollTop : function(win)
{
var win = win ? win : window;
if (typeof win.pageYOffset !== "undefined") {
return win.pageYOffset;
}
// Firefox is using 'documentElement.scrollTop' and Chrome is using
// 'document.body.scrollTop'. For the other value each browser is returning
// 0, so we can use this check to get the positive value without using specific
// browser checks.
var doc = win.document;
return doc.documentElement.scrollTop || doc.body.scrollTop;
},
/**
* Returns an orientation normalizer value that should be added to device orientation
* to normalize behaviour on different devices.
*
* @param win {Window} The window to query
* @return {Map} Orientation normalizing value
*/
__getOrientationNormalizer : function(win)
{
// Calculate own understanding of orientation (0 = portrait, 90 = landscape)
var currentOrientation = this.getWidth(win) > this.getHeight(win) ? 90 : 0;
var deviceOrientation = win.orientation;
if (deviceOrientation == null || Math.abs( deviceOrientation % 180 ) == currentOrientation) {
// No device orientation available or device orientation equals own understanding of orientation
return {
"-270": 90,
"-180": 180,
"-90": -90,
"0": 0,
"90": 90,
"180": 180,
"270": -90
};
} else {
// Device orientation is not equal to own understanding of orientation
return {
"-270": 180,
"-180": -90,
"-90": 0,
"0": 90,
"90": 180,
"180": -90,
"270": 0
};
}
},
// Cache orientation normalizer map on start
__orientationNormalizer : null,
/**
* Returns the current orientation of the viewport in degree.
*
* All possible values and their meaning:
*
* * <code>-90</code>: "Landscape"
* * <code>0</code>: "Portrait"
* * <code>90</code>: "Landscape"
* * <code>180</code>: "Portrait"
*
* @param win {Window?window.top} The window to query. (Default = top window)
* @return {Integer} The current orientation in degree
*/
getOrientation : function(win)
{
// Set window.top as default, because orientationChange event is only fired top window
var win = win||window.top;
// The orientation property of window does not have the same behaviour over all devices
// iPad has 0degrees = Portrait, Playbook has 90degrees = Portrait, same for Android Honeycomb
//
// To fix this an orientationNormalizer map is calculated on application start
//
// The calculation of getWidth and getHeight returns wrong values if you are in an input field
// on iPad and rotate your device!
var orientation = win.orientation;
if (orientation == null) {
// Calculate orientation from window width and window height
orientation = this.getWidth(win) > this.getHeight(win) ? 90 : 0;
} else {
if (this.__orientationNormalizer == null) {
this.__orientationNormalizer = this.__getOrientationNormalizer(win);
}
// Normalize orientation value
orientation = this.__orientationNormalizer[orientation];
}
return orientation;
},
/**
* Whether the viewport orientation is currently in landscape mode.
*
* @param win {Window?window} The window to query
* @return {Boolean} <code>true</code> when the viewport orientation
* is currently in landscape mode.
*/
isLandscape : function(win) {
var orientation = this.getOrientation(win);
return orientation === -90 || orientation === 90;
},
/**
* Whether the viewport orientation is currently in portrait mode.
*
* @param win {Window?window} The window to query
* @return {Boolean} <code>true</code> when the viewport orientation
* is currently in portrait mode.
*/
isPortrait : function(win)
{
var orientation = this.getOrientation(win);
return orientation === 0 || orientation === 180;
}
}
});