|
/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
|
* full list of contributors). Published under the Clear BSD license. |
|
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
|
* full text of the license. */ |
|
|
|
/** |
|
* Namespace: OpenLayers.Element |
|
*/ |
|
OpenLayers.Element = { |
|
|
|
/** |
|
* APIFunction: visible |
|
* |
|
* Parameters: |
|
* element - {DOMElement} |
|
* |
|
* Returns: |
|
* {Boolean} Is the element visible? |
|
*/ |
|
visible: function(element) { |
|
return OpenLayers.Util.getElement(element).style.display != 'none'; |
|
}, |
|
|
|
/** |
|
* APIFunction: toggle |
|
* Toggle the visibility of element(s) passed in |
|
* |
|
* Parameters: |
|
* element - {DOMElement} Actually user can pass any number of elements |
|
*/ |
|
toggle: function() { |
|
for (var i=0, len=arguments.length; i<len; i++) { |
|
var element = OpenLayers.Util.getElement(arguments[i]); |
|
var display = OpenLayers.Element.visible(element) ? 'hide' |
|
: 'show'; |
|
OpenLayers.Element[display](element); |
|
} |
|
}, |
|
|
|
|
|
/** |
|
* APIFunction: hide |
|
* Hide element(s) passed in |
|
* |
|
* Parameters: |
|
* element - {DOMElement} Actually user can pass any number of elements |
|
*/ |
|
hide: function() { |
|
for (var i=0, len=arguments.length; i<len; i++) { |
|
var element = OpenLayers.Util.getElement(arguments[i]); |
|
if (element) { |
|
element.style.display = 'none'; |
|
} |
|
} |
|
}, |
|
|
|
/** |
|
* APIFunction: show |
|
* Show element(s) passed in |
|
* |
|
* Parameters: |
|
* element - {DOMElement} Actually user can pass any number of elements |
|
*/ |
|
show: function() { |
|
for (var i=0, len=arguments.length; i<len; i++) { |
|
var element = OpenLayers.Util.getElement(arguments[i]); |
|
if (element) { |
|
element.style.display = ''; |
|
} |
|
} |
|
}, |
|
|
|
/** |
|
* APIFunction: remove |
|
* Remove the specified element from the DOM. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} |
|
*/ |
|
remove: function(element) { |
|
element = OpenLayers.Util.getElement(element); |
|
element.parentNode.removeChild(element); |
|
}, |
|
|
|
/** |
|
* APIFunction: getHeight |
|
* |
|
* Parameters: |
|
* element - {DOMElement} |
|
* |
|
* Returns: |
|
* {Integer} The offset height of the element passed in |
|
*/ |
|
getHeight: function(element) { |
|
element = OpenLayers.Util.getElement(element); |
|
return element.offsetHeight; |
|
}, |
|
|
|
/** |
|
* APIFunction: getDimensions |
|
* *Deprecated*. Returns dimensions of the element passed in. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} |
|
* |
|
* Returns: |
|
* {Object} Object with 'width' and 'height' properties which are the |
|
* dimensions of the element passed in. |
|
*/ |
|
getDimensions: function(element) { |
|
element = OpenLayers.Util.getElement(element); |
|
if (OpenLayers.Element.getStyle(element, 'display') != 'none') { |
|
return {width: element.offsetWidth, height: element.offsetHeight}; |
|
} |
|
|
|
// All *Width and *Height properties give 0 on elements with display none, |
|
// so enable the element temporarily |
|
var els = element.style; |
|
var originalVisibility = els.visibility; |
|
var originalPosition = els.position; |
|
var originalDisplay = els.display; |
|
els.visibility = 'hidden'; |
|
els.position = 'absolute'; |
|
els.display = ''; |
|
var originalWidth = element.clientWidth; |
|
var originalHeight = element.clientHeight; |
|
els.display = originalDisplay; |
|
els.position = originalPosition; |
|
els.visibility = originalVisibility; |
|
return {width: originalWidth, height: originalHeight}; |
|
}, |
|
|
|
/** |
|
* Function: hasClass |
|
* Tests if an element has the given CSS class name. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} A DOM element node. |
|
* name - {String} The CSS class name to search for. |
|
* |
|
* Returns: |
|
* {Boolean} The element has the given class name. |
|
*/ |
|
hasClass: function(element, name) { |
|
var names = element.className; |
|
return (!!names && new RegExp("(^|\\s)" + name + "(\\s|$)").test(names)); |
|
}, |
|
|
|
/** |
|
* Function: addClass |
|
* Add a CSS class name to an element. Safe where element already has |
|
* the class name. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} A DOM element node. |
|
* name - {String} The CSS class name to add. |
|
* |
|
* Returns: |
|
* {DOMElement} The element. |
|
*/ |
|
addClass: function(element, name) { |
|
if(!OpenLayers.Element.hasClass(element, name)) { |
|
element.className += (element.className ? " " : "") + name; |
|
} |
|
return element; |
|
}, |
|
|
|
/** |
|
* Function: removeClass |
|
* Remove a CSS class name from an element. Safe where element does not |
|
* have the class name. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} A DOM element node. |
|
* name - {String} The CSS class name to remove. |
|
* |
|
* Returns: |
|
* {DOMElement} The element. |
|
*/ |
|
removeClass: function(element, name) { |
|
var names = element.className; |
|
if(names) { |
|
element.className = OpenLayers.String.trim( |
|
names.replace( |
|
new RegExp("(^|\\s+)" + name + "(\\s+|$)"), " " |
|
) |
|
); |
|
} |
|
return element; |
|
}, |
|
|
|
/** |
|
* Function: toggleClass |
|
* Remove a CSS class name from an element if it exists. Add the class name |
|
* if it doesn't exist. |
|
* |
|
* Parameters: |
|
* element - {DOMElement} A DOM element node. |
|
* name - {String} The CSS class name to toggle. |
|
* |
|
* Returns: |
|
* {DOMElement} The element. |
|
*/ |
|
toggleClass: function(element, name) { |
|
if(OpenLayers.Element.hasClass(element, name)) { |
|
OpenLayers.Element.removeClass(element, name); |
|
} else { |
|
OpenLayers.Element.addClass(element, name); |
|
} |
|
return element; |
|
}, |
|
|
|
/** |
|
* APIFunction: getStyle |
|
* |
|
* Parameters: |
|
* element - {DOMElement} |
|
* style - {?} |
|
* |
|
* Returns: |
|
* {?} |
|
*/ |
|
getStyle: function(element, style) { |
|
element = OpenLayers.Util.getElement(element); |
|
|
|
var value = null; |
|
if (element && element.style) { |
|
value = element.style[OpenLayers.String.camelize(style)]; |
|
if (!value) { |
|
if (document.defaultView && |
|
document.defaultView.getComputedStyle) { |
|
|
|
var css = document.defaultView.getComputedStyle(element, null); |
|
value = css ? css.getPropertyValue(style) : null; |
|
} else if (element.currentStyle) { |
|
value = element.currentStyle[OpenLayers.String.camelize(style)]; |
|
} |
|
} |
|
|
|
var positions = ['left', 'top', 'right', 'bottom']; |
|
if (window.opera && |
|
(OpenLayers.Util.indexOf(positions,style) != -1) && |
|
(OpenLayers.Element.getStyle(element, 'position') == 'static')) { |
|
value = 'auto'; |
|
} |
|
} |
|
|
|
return value == 'auto' ? null : value; |
|
} |
|
|
|
}; |
|
|