Merge branch 'removeclass_fix'

Conflicts:
	src/dom/DomUtil.js
This commit is contained in:
Neno 2011-07-31 15:30:08 +01:00
commit 2b1a64c66d

View File

@ -1,149 +1,149 @@
/* /*
* L.DomUtil contains various utility functions for working with DOM * L.DomUtil contains various utility functions for working with DOM
*/ */
L.DomUtil = { L.DomUtil = {
get: function(id) { get: function(id) {
return (typeof id == 'string' ? document.getElementById(id) : id); return (typeof id == 'string' ? document.getElementById(id) : id);
}, },
getStyle: function(el, style) { getStyle: function(el, style) {
var value = el.style[style]; var value = el.style[style];
if (!value && el.currentStyle) { if (!value && el.currentStyle) {
value = el.currentStyle[style]; value = el.currentStyle[style];
} }
if (!value || value == 'auto') { if (!value || value == 'auto') {
var css = document.defaultView.getComputedStyle(el, null); var css = document.defaultView.getComputedStyle(el, null);
value = css ? css[style] : null; value = css ? css[style] : null;
} }
return (value == 'auto' ? null : value); return (value == 'auto' ? null : value);
}, },
getViewportOffset: function(element) { getViewportOffset: function(element) {
var top = 0, var top = 0,
left = 0, left = 0,
el = element, el = element,
docBody = document.body; docBody = document.body;
do { do {
if (el === docBody) break; if (el === docBody) break;
top += el.offsetTop || 0; top += el.offsetTop || 0;
left += el.offsetLeft || 0; left += el.offsetLeft || 0;
if (L.DomUtil.getStyle(el, 'position') == 'absolute') break; if (L.DomUtil.getStyle(el, 'position') == 'absolute') break;
} while (el = el.offsetParent); } while (el = el.offsetParent);
el = element; el = element;
do { do {
if (el === docBody) break; if (el === docBody) break;
top -= el.scrollTop || 0; top -= el.scrollTop || 0;
left -= el.scrollLeft || 0; left -= el.scrollLeft || 0;
} while (el = el.parentNode); } while (el = el.parentNode);
return new L.Point(left, top); return new L.Point(left, top);
}, },
create: function(tagName, className, container) { create: function(tagName, className, container) {
var el = document.createElement(tagName); var el = document.createElement(tagName);
el.className = className; el.className = className;
if (container) { if (container) {
container.appendChild(el); container.appendChild(el);
} }
return el; return el;
}, },
disableTextSelection: function() { disableTextSelection: function() {
if (document.selection && document.selection.empty) { if (document.selection && document.selection.empty) {
document.selection.empty(); document.selection.empty();
} }
if (!this._onselectstart) { if (!this._onselectstart) {
this._onselectstart = document.onselectstart; this._onselectstart = document.onselectstart;
document.onselectstart = L.Util.falseFn; document.onselectstart = L.Util.falseFn;
} }
}, },
enableTextSelection: function() { enableTextSelection: function() {
document.onselectstart = this._onselectstart; document.onselectstart = this._onselectstart;
this._onselectstart = null; this._onselectstart = null;
}, },
CLASS_RE: /(\\s|^)'+cls+'(\\s|$)/, CLASS_RE: /(\\s|^)'+cls+'(\\s|$)/,
hasClass: function(el, name) { hasClass: function(el, name) {
return (el.className.length > 0) && return (el.className.length > 0) &&
new RegExp("(^|\\s)" + name + "(\\s|$)").test(el.className); new RegExp("(^|\\s)" + name + "(\\s|$)").test(el.className);
}, },
addClass: function(el, name) { addClass: function(el, name) {
if (!L.DomUtil.hasClass(el, name)) { if (!L.DomUtil.hasClass(el, name)) {
el.className += (el.className ? ' ' : '') + name; el.className += (el.className ? ' ' : '') + name;
} }
}, },
removeClass: function(el, name) { removeClass: function(el, name) {
el.className = el.className.replace(/(\S+)\s*/g, function(w, match) { el.className = el.className.replace(/(\S+)\s*/g, function(w, match) {
if (match == name) return ''; if (match == name) return '';
return w; return w;
}).replace(/^\s+/, ''); }).replace(/^\s+/, '');
}, },
setOpacity: function(el, value) { setOpacity: function(el, value) {
if (L.Browser.ie) { if (L.Browser.ie) {
el.style.filter = 'alpha(opacity=' + Math.round(value * 100) + ')'; el.style.filter = 'alpha(opacity=' + Math.round(value * 100) + ')';
} else { } else {
el.style.opacity = value; el.style.opacity = value;
} }
}, },
//TODO refactor away this ugly translate/position mess //TODO refactor away this ugly translate/position mess
testProp: function(props) { testProp: function(props) {
var style = document.documentElement.style; var style = document.documentElement.style;
for (var i = 0; i < props.length; i++) { for (var i = 0; i < props.length; i++) {
if (props[i] in style) { if (props[i] in style) {
return props[i]; return props[i];
} }
} }
return false; return false;
}, },
getTranslateString: function(point) { getTranslateString: function(point) {
return L.DomUtil.TRANSLATE_OPEN + return L.DomUtil.TRANSLATE_OPEN +
point.x + 'px,' + point.y + 'px' + point.x + 'px,' + point.y + 'px' +
L.DomUtil.TRANSLATE_CLOSE; L.DomUtil.TRANSLATE_CLOSE;
}, },
getScaleString: function(scale, origin) { getScaleString: function(scale, origin) {
return L.DomUtil.getTranslateString(origin) + return L.DomUtil.getTranslateString(origin) +
' scale(' + scale + ') ' + ' scale(' + scale + ') ' +
L.DomUtil.getTranslateString(origin.multiplyBy(-1)); L.DomUtil.getTranslateString(origin.multiplyBy(-1));
}, },
setPosition: function(el, point) { setPosition: function(el, point) {
el._leaflet_pos = point; el._leaflet_pos = point;
if (L.Browser.webkit) { if (L.Browser.webkit) {
el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point); el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point);
} else { } else {
el.style.left = point.x + 'px'; el.style.left = point.x + 'px';
el.style.top = point.y + 'px'; el.style.top = point.y + 'px';
} }
}, },
getPosition: function(el) { getPosition: function(el) {
return el._leaflet_pos; return el._leaflet_pos;
} }
}; };
L.Util.extend(L.DomUtil, { L.Util.extend(L.DomUtil, {
TRANSITION: L.DomUtil.testProp(['transition', 'webkitTransition', 'OTransition', 'MozTransition', 'msTransition']), TRANSITION: L.DomUtil.testProp(['transition', 'webkitTransition', 'OTransition', 'MozTransition', 'msTransition']),
TRANSFORM: L.DomUtil.testProp(['transformProperty', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']), TRANSFORM: L.DomUtil.testProp(['transformProperty', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']),
TRANSLATE_OPEN: 'translate' + (L.Browser.webkit3d ? '3d(' : '('), TRANSLATE_OPEN: 'translate' + (L.Browser.webkit3d ? '3d(' : '('),
TRANSLATE_CLOSE: L.Browser.webkit3d ? ',0)' : ')' TRANSLATE_CLOSE: L.Browser.webkit3d ? ',0)' : ')'
}); });