YAHOO.util.Dom = function() {
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isSafari = (ua.indexOf('safari') > -1);
var isIE = (window.ActiveXObject);
var id_counter = 0;
var util = YAHOO.util;
var property_cache = {};
var logger = {};
logger.log = function() {YAHOO.log.apply(window, arguments)};
var toCamel = function(property) {
var convert = function(prop) {
var test = /(-[a-z])/i.exec(prop);
return prop.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase());
};
while(property.indexOf('-') > -1) {
property = convert(property);
}
return property;
};
var toHyphen = function(property) {
if (property.indexOf('-') > -1) {
return property;
}
var converted = '';
for (var i = 0, len = property.length;i < len; ++i) {
if (property.charAt(i) == property.charAt(i).toUpperCase()) {
converted = converted + '-' + property.charAt(i).toLowerCase();
} else {
converted = converted + property.charAt(i);
}
}
return converted;
};
var cacheConvertedProperties = function(property) {
property_cache[property] = {
camel: toCamel(property),
hyphen: toHyphen(property)
};
};
return {
get: function(el) {
if (typeof el != 'string' && !(el instanceof Array) ) {
logger.log('get(' + el + ') returning ' + el, 'info', 'Dom');
return el;
}
if (typeof el == 'string') {
logger.log('get("' + el + '") returning ' + document.getElementById(el), 'info', 'Dom');
return document.getElementById(el);
}
else {
var collection = [];
for (var i = 0, len = el.length; i < len; ++i) {
collection[collection.length] = util.Dom.get(el[i]);
}
logger.log('get("' + el + '") returning ' + collection, 'info', 'Dom');
return collection;
}
logger.log('element ' + el + ' not found', 'error', 'Dom');
return null;
},
getStyle: function(el, property) {
var f = function(el) {
var value = null;
var dv = document.defaultView;
if (!property_cache[property]) {
cacheConvertedProperties(property);
}
var camel = property_cache[property]['camel'];
var hyphen = property_cache[property]['hyphen'];
if (property == 'opacity' && el.filters) {
value = 1;
try {
value = el.filters.item('DXImageTransform.Microsoft.Alpha').opacity / 100;
} catch(e) {
try {
value = el.filters.item('alpha').opacity / 100;
} catch(e) {}
}
} else if (el.style[camel]) {
value = el.style[camel];
}
else if (isIE && el.currentStyle && el.currentStyle[camel]) {
value = el.currentStyle[camel];
}
else if ( dv && dv.getComputedStyle ) {
var computed = dv.getComputedStyle(el, '');
if (computed && computed.getPropertyValue(hyphen)) {
value = computed.getPropertyValue(hyphen);
}
}
logger.log('getStyle ' + property + ' returning ' + value, 'info', 'Dom');
return value;
};
return util.Dom.batch(el, f, util.Dom, true);
},
setStyle: function(el, property, val) {
if (!property_cache[property]) {
cacheConvertedProperties(property);
}
var camel = property_cache[property]['camel'];
var f = function(el) {
switch(property) {
case 'opacity' :
if (isIE && typeof el.style.filter == 'string') {
el.style.filter = 'alpha(opacity=' + val * 100 + ')';
if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1;
}
} else {
el.style.opacity = val;
el.style['-moz-opacity'] = val;
el.style['-khtml-opacity'] = val;
}
break;
default :
el.style[camel] = val;
}
logger.log('setStyle setting ' + property + ' to ' + val, 'info', 'Dom');
};
util.Dom.batch(el, f, util.Dom, true);
},
getXY: function(el) {
var f = function(el) {
if (el.parentNode === null || this.getStyle(el, 'display') == 'none') {
logger.log('getXY failed: element not available', 'error', 'Dom');
return false;
}
var parentNode = null;
var pos = [];
var box;
if (el.getBoundingClientRect) {
box = el.getBoundingClientRect();
var doc = document;
if ( !this.inDocument(el) ) {
var doc = parent.document;
while ( doc && !this.isAncestor(doc.documentElement, el) ) {
doc = parent.document;
}
}
var scrollTop = Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
var scrollLeft = Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
return [box.left + scrollLeft, box.top + scrollTop];
}
else {
pos = [el.offsetLeft, el.offsetTop];
parentNode = el.offsetParent;
if (parentNode != el) {
while (parentNode) {
pos[0] += parentNode.offsetLeft;
pos[1] += parentNode.offsetTop;
parentNode = parentNode.offsetParent;
}
}
if (isSafari && this.getStyle(el, 'position') == 'absolute' ) {
pos[0] -= document.body.offsetLeft;
pos[1] -= document.body.offsetTop;
}
}
if (el.parentNode) { parentNode = el.parentNode; }
else { parentNode = null; }
while (parentNode && parentNode.tagName.toUpperCase() != 'BODY' && parentNode.tagName.toUpperCase() != 'HTML')
{
pos[0] -= parentNode.scrollLeft;
pos[1] -= parentNode.scrollTop;
if (parentNode.parentNode) { parentNode = parentNode.parentNode; }
else { parentNode = null; }
}
logger.log('getXY returning ' + pos, 'info', 'Dom');
return pos;
};
return util.Dom.batch(el, f, util.Dom, true);
},
getX: function(el) {
return util.Dom.getXY(el)[0];
},
getY: function(el) {
return util.Dom.getXY(el)[1];
},
setXY: function(el, pos, noRetry) {
var f = function(el) {
var style_pos = this.getStyle(el, 'position');
if (style_pos == 'static') {
this.setStyle(el, 'position', 'relative');
style_pos = 'relative';
}
var pageXY = this.getXY(el);
if (pageXY === false) {
logger.log('setXY failed: element not available', 'error', 'Dom');
return false;
}
var delta = [
parseInt( this.getStyle(el, 'left'), 10 ),
parseInt( this.getStyle(el, 'top'), 10 )
];
if ( isNaN(delta[0]) ) {
delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
}
if ( isNaN(delta[1]) ) {
delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
}
if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
var newXY = this.getXY(el);
if (!noRetry && (newXY[0] != pos[0] || newXY[1] != pos[1]) ) {
this.setXY(el, pos, true);
}
logger.log('setXY setting position to ' + pos, 'info', 'Dom');
};
util.Dom.batch(el, f, util.Dom, true);
},
setX: function(el, x) {
util.Dom.setXY(el, [x, null]);
},
setY: function(el, y) {
util.Dom.setXY(el, [null, y]);
},
getRegion: function(el) {
var f = function(el) {
var region = new YAHOO.util.Region.getRegion(el);
logger.log('getRegion returning ' + region, 'info', 'Dom');
return region;
};
return util.Dom.batch(el, f, util.Dom, true);
},
getClientWidth: function() {
return util.Dom.getViewportWidth();
},
getClientHeight: function() {
return util.Dom.getViewportHeight();
},
getElementsByClassName: function(className, tag, root) {
var method = function(el) { return util.Dom.hasClass(el, className) };
return util.Dom.getElementsBy(method, tag, root);
},
hasClass: function(el, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
var f = function(el) {
logger.log('hasClass returning ' + re.test(el['className']), 'info', 'Dom');
return re.test(el['className']);
};
return util.Dom.batch(el, f, util.Dom, true);
},
addClass: function(el, className) {
var f = function(el) {
if (this.hasClass(el, className)) { return; }
logger.log('addClass adding ' + className, 'info', 'Dom');
el['className'] = [el['className'], className].join(' ');
};
util.Dom.batch(el, f, util.Dom, true);
},
removeClass: function(el, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)', 'g');
var f = function(el) {
if (!this.hasClass(el, className)) { return; }
logger.log('removeClass removing ' + className, 'info', 'Dom');
var c = el['className'];
el['className'] = c.replace(re, ' ');
if ( this.hasClass(el, className) ) {
this.removeClass(el, className);
}
};
util.Dom.batch(el, f, util.Dom, true);
},
replaceClass: function(el, oldClassName, newClassName) {
var re = new RegExp('(?:^|\\s+)' + oldClassName + '(?:\\s+|$)', 'g');
var f = function(el) {
logger.log('replaceClass replacing ' + oldClassName + ' with ' + newClassName, 'info', 'Dom');
el['className'] = el['className'].replace(re, ' ' + newClassName + ' ');
if ( this.hasClass(el, oldClassName) ) {
this.replaceClass(el, oldClassName, newClassName);
}
};
util.Dom.batch(el, f, util.Dom, true);
},
generateId: function(el, prefix) {
prefix = prefix || 'yui-gen';
el = el || {};
var f = function(el) {
if (el) {
el = util.Dom.get(el);
} else {
el = {};
}
if (!el.id) {
el.id = prefix + id_counter++;
logger.log('generateId generating ' + el.id, 'info', 'Dom');
}
logger.log('generateId returning ' + el.id, 'info', 'Dom');
return el.id;
};
return util.Dom.batch(el, f, util.Dom, true);
},
isAncestor: function(haystack, needle) {
haystack = util.Dom.get(haystack);
if (!haystack || !needle) { return false; }
var f = function(needle) {
if (haystack.contains && !isSafari) {
logger.log('isAncestor returning ' + haystack.contains(needle), 'info', 'Dom');
return haystack.contains(needle);
}
else if ( haystack.compareDocumentPosition ) {
logger.log('isAncestor returning ' + !!(haystack.compareDocumentPosition(needle) & 16), 'info', 'Dom');
return !!(haystack.compareDocumentPosition(needle) & 16);
}
else {
var parent = needle.parentNode;
while (parent) {
if (parent == haystack) {
logger.log('isAncestor returning true', 'info', 'Dom');
return true;
}
else if (parent.tagName.toUpperCase() == 'HTML') {
logger.log('isAncestor returning false', 'info', 'Dom');
return false;
}
parent = parent.parentNode;
}
logger.log('isAncestor returning false', 'info', 'Dom');
return false;
}
};
return util.Dom.batch(needle, f, util.Dom, true);
},
inDocument: function(el) {
var f = function(el) {
return this.isAncestor(document.documentElement, el);
};
return util.Dom.batch(el, f, util.Dom, true);
},
getElementsBy: function(method, tag, root) {
tag = tag || '*';
root = util.Dom.get(root) || document;
var nodes = [];
var elements = root.getElementsByTagName(tag);
if ( !elements.length && (tag == '*' && root.all) ) {
elements = root.all;
}
for (var i = 0, len = elements.length; i < len; ++i)
{
if ( method(elements[i]) ) { nodes[nodes.length] = elements[i]; }
}
logger.log('getElementsBy returning ' + nodes, 'info', 'Dom');
return nodes;
},
batch: function(el, method, o, override) {
var id = el;
el = util.Dom.get(el);
var scope = (override) ? o : window;
if (!el || el.tagName || !el.length) {
if (!el) {
logger.log(id + ' not available', 'error', 'Dom');
return false;
}
return method.call(scope, el, o);
}
var collection = [];
for (var i = 0, len = el.length; i < len; ++i) {
if (!el[i]) {
id = id[i];
logger.log(id + ' not available', 'error', 'Dom');
}
collection[collection.length] = method.call(scope, el[i], o);
}
return collection;
},
getDocumentHeight: function() {
var scrollHeight=-1,windowHeight=-1,bodyHeight=-1;
var marginTop = parseInt(util.Dom.getStyle(document.body, 'marginTop'), 10);
var marginBottom = parseInt(util.Dom.getStyle(document.body, 'marginBottom'), 10);
var mode = document.compatMode;
if ( (mode || isIE) && !isOpera ) {
switch (mode) {
case 'CSS1Compat':
scrollHeight = ((window.innerHeight && window.scrollMaxY) ? window.innerHeight+window.scrollMaxY : -1);
windowHeight = [document.documentElement.clientHeight,self.innerHeight||-1].sort(function(a, b){return(a-b);})[1];
bodyHeight = document.body.offsetHeight + marginTop + marginBottom;
break;
default:
scrollHeight = document.body.scrollHeight;
bodyHeight = document.body.clientHeight;
}
} else {
scrollHeight = document.documentElement.scrollHeight;
windowHeight = self.innerHeight;
bodyHeight = document.documentElement.clientHeight;
}
var h = [scrollHeight,windowHeight,bodyHeight].sort(function(a, b){return(a-b);});
logger.log('getDocumentHeight returning ' + h[2], 'info', 'Dom');
return h[2];
},
getDocumentWidth: function() {
var docWidth=-1,bodyWidth=-1,winWidth=-1;
var marginRight = parseInt(util.Dom.getStyle(document.body, 'marginRight'), 10);
var marginLeft = parseInt(util.Dom.getStyle(document.body, 'marginLeft'), 10);
var mode = document.compatMode;
if (mode || isIE) {
switch (mode) {
case 'CSS1Compat':
docWidth = document.documentElement.clientWidth;
bodyWidth = document.body.offsetWidth + marginLeft + marginRight;
winWidth = self.innerWidth || -1;
break;
default:
bodyWidth = document.body.clientWidth;
winWidth = document.body.scrollWidth;
break;
}
} else {
docWidth = document.documentElement.clientWidth;
bodyWidth = document.body.offsetWidth + marginLeft + marginRight;
winWidth = self.innerWidth;
}
var w = [docWidth,bodyWidth,winWidth].sort(function(a, b){return(a-b);});
logger.log('getDocumentWidth returning ' + w[2], 'info', 'Dom');
return w[2];
},
getViewportHeight: function() {
var height = -1;
var mode = document.compatMode;
if ( (mode || isIE) && !isOpera ) {
switch (mode) {
case 'CSS1Compat':
height = document.documentElement.clientHeight;
break;
default:
height = document.body.clientHeight;
}
} else {
height = self.innerHeight;
}
logger.log('getViewportHeight returning ' + height, 'info', 'Dom');
return height;
},
getViewportWidth: function() {
var width = -1;
var mode = document.compatMode;
if (mode || isIE) {
switch (mode) {
case 'CSS1Compat':
width = document.documentElement.clientWidth;
break;
default:
width = document.body.clientWidth;
}
} else {
width = self.innerWidth;
}
logger.log('getViewportWidth returning ' + width, 'info', 'Dom');
return width;
}
};
}();