jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > sizeandposition.js (source view)
Search:
 
Filters
/*!
 * Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
 *
 * This Software is licenced under the LGPL Licence (GNU Lesser General 
 * Public License).
 * In addition to the LGPL Licence the Software is subject to the 
 * following conditions:
 * 
 * 	i	every modification must be public and comunicated to the Author
 * 	ii	every "jsbean" added to this library must be self consistent 
 * 		except for the dependence from jsbeans-x.x.x.js
 * 	iii	copyright notice and this permission notice shall be included 
 * 		in all copies or substantial portions of the Software
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
/**
 * A collection of utilities for size and position
 * @namespace jsbeans
 * @class sizeandposition
 * @static
 */

jsbeans.define("jsbeans.sizeandposition");

/**
 * Returns a JSON with cursor coordinates.
 * @method getCursorPosition
 * @param event {event}
 * @return {JSON} a JSON with properties <code>x</code> and <code>y</code>
 * @static
 * */
jsbeans.sizeandposition.getCursorPosition = function(/*Event*/e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +  (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
};

/**
 * Returns a JSON with {@param} <code class="param">node</code>'s coordinates from its top-left corner.
 * @method getObjectPosition
 * @param node {DOM} the DOM Object
 * @return {JSON} a JSON with properties <code>x</code> and <code>y</code>
 * @static
 * */
jsbeans.sizeandposition.getObjectPosition = function(obj) {
	if (typeof(obj.offsetParent) != 'undefined') {
		for (var posX = 0, posY = 0; obj; obj = obj.offsetParent) {
			posX += obj.offsetLeft;
			posY += obj.offsetTop;
		}
		return {x: posX, y: posY};
	} 
	else {
		return {x: obj.offsetLeft || 0, y: obj.offsetTop || 0};
  	}
};

/**
 * Moves {@param} <code class="param">node</code> using style absolute position starting from {@param} <code class="param">top</code>/{@param} <code class="param">left</code>.
 * @method setObjectPosition
 * @param node {DOM} the DOM Object to move
 * @param top {Integer} distance from top border (in pixels)
 * @param left {Integer} distance from left border (in pixels)
 * @static
 * */
jsbeans.sizeandposition.setObjectPosition = function(obj, top, left) {
	obj.style.position = "absolute";
	obj.style.top = top + "px";
	obj.style.left = left + "px";
};

/**
 * Returns a JSON containing information on whole document dimension.
 * @method getDocumentDimension
 * @return {JSON}
 * <pre>
 * width: Integer, // the document width
 * w: Integer, // same as width, just shorter
 * height: Integer, // the document height
 * h: Integer // same as height, just shorter
 * </pre>
 * @static
 * */
jsbeans.sizeandposition.getDocumentDimension = function() {
	var h = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;
	var w = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
	
	return {w: w, h: h, width: w, height: h};
};

/**
 * Returns a JSON containing information on page dimension. It's at least as big as document.
 * @method getPageDimension
 * @return {JSON}
 * <pre>
 * width: Integer, // the document width
 * w: Integer, // same as width, just shorter
 * height: Integer, // the document height
 * h: Integer // same as height, just shorter
 * </pre>
 * @static
 * */
jsbeans.sizeandposition.getPageDimension = function() {
	var h = (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight;
	var w = (document.compatMode == 'CSS1Compat') ? document.documentElement.clientWidth : document.body.clientWidth;
	
	return {w: w, h: h, width: w, height: h};
};
/**
 * Alias for jsbeans.sizeandposition.getPageDimension
 * @method getViewportDimension
 * @return {JSON}
 * @static
 * */
jsbeans.sizeandposition.getViewportDimension = function() {
	return jsbeans.sizeandposition.getPageDimension();
};

Copyright © 2016 Francesco Mele. All rights reserved.