2010-09-07 19:27:44 +08:00
|
|
|
/*
|
|
|
|
* L.DomUtil contains various utility functions for working with DOM
|
|
|
|
*/
|
|
|
|
|
|
|
|
L.DomUtil = {
|
2010-09-07 22:02:45 +08:00
|
|
|
get: function(id) {
|
|
|
|
return (typeof id == 'string' ? document.getElementById(id) : id);
|
|
|
|
},
|
2010-09-14 19:57:45 +08:00
|
|
|
|
|
|
|
getStyle: function(el, style) {
|
2010-09-27 23:34:56 +08:00
|
|
|
var value = el.style[style];
|
|
|
|
if ((typeof value == 'undefined') && el.currentStyle) {
|
|
|
|
value = el.currentStyle[style];
|
|
|
|
}
|
|
|
|
if (typeof value == 'undefined') {
|
|
|
|
var css = document.defaultView.getComputedStyle(el, null);
|
|
|
|
value = css ? css[style] : null;
|
|
|
|
}
|
|
|
|
return (value == 'auto' ? null : value);
|
2010-09-14 19:57:45 +08:00
|
|
|
},
|
|
|
|
|
2010-09-07 19:27:44 +08:00
|
|
|
getCumulativeOffset: function(el) {
|
|
|
|
var top = 0,
|
|
|
|
left = 0;
|
|
|
|
do {
|
|
|
|
top += (el.offsetTop - el.scrollTop) || 0;
|
|
|
|
left += el.offsetLeft || 0;
|
|
|
|
el = el.offsetParent;
|
|
|
|
} while (el);
|
|
|
|
return new L.Point(left, top);
|
|
|
|
},
|
2010-09-13 23:18:12 +08:00
|
|
|
|
2011-01-13 18:17:50 +08:00
|
|
|
create: function(tagName, className, container) {
|
|
|
|
var el = document.createElement(tagName);
|
|
|
|
el.className = className;
|
|
|
|
if (container) {
|
|
|
|
container.appendChild(el);
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2011-03-22 01:09:43 +08:00
|
|
|
disableTextSelection: function() {
|
|
|
|
if (document.selection && document.selection.empty) {
|
|
|
|
document.selection.empty();
|
|
|
|
}
|
|
|
|
if (!this._onselectstart) {
|
|
|
|
this._onselectstart = document.onselectstart;
|
|
|
|
document.onselectstart = L.Util.falseFn;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
enableTextSelection: function() {
|
|
|
|
document.onselectstart = this._onselectstart;
|
|
|
|
this._onselectstart = null;
|
|
|
|
},
|
|
|
|
|
2011-01-10 22:22:07 +08:00
|
|
|
|
|
|
|
//TODO refactor away this ugly translate/position mess
|
|
|
|
|
2011-01-15 00:43:40 +08:00
|
|
|
testProp: function(props) {
|
|
|
|
var style = document.documentElement.style;
|
|
|
|
|
|
|
|
for (var i = 0; i < props.length; i++) {
|
|
|
|
if (props[i] in style) {
|
|
|
|
return props[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2010-09-20 20:08:25 +08:00
|
|
|
getTranslateString: function(point) {
|
|
|
|
return L.DomUtil.TRANSLATE_OPEN +
|
|
|
|
point.x + 'px,' + point.y + 'px' +
|
|
|
|
L.DomUtil.TRANSLATE_CLOSE;
|
|
|
|
},
|
2010-09-16 21:05:42 +08:00
|
|
|
|
2011-01-15 00:43:40 +08:00
|
|
|
getScaleString: function(scale, origin) {
|
|
|
|
return L.DomUtil.getTranslateString(origin) +
|
|
|
|
' scale(' + scale + ') ' +
|
|
|
|
L.DomUtil.getTranslateString(origin.multiplyBy(-1));
|
|
|
|
},
|
|
|
|
|
2010-09-13 23:18:12 +08:00
|
|
|
setPosition: function(el, point) {
|
|
|
|
el._leaflet_pos = point;
|
|
|
|
if (L.Browser.webkit) {
|
2011-01-15 00:43:40 +08:00
|
|
|
el.style[L.DomUtil.TRANSFORM_PROPERTY] = L.DomUtil.getTranslateString(point);
|
2010-09-13 23:18:12 +08:00
|
|
|
} else {
|
2010-09-14 19:57:45 +08:00
|
|
|
el.style.left = point.x + 'px';
|
|
|
|
el.style.top = point.y + 'px';
|
2010-09-13 23:18:12 +08:00
|
|
|
}
|
|
|
|
},
|
2010-09-16 21:05:42 +08:00
|
|
|
|
2010-09-13 23:18:12 +08:00
|
|
|
getPosition: function(el) {
|
|
|
|
return el._leaflet_pos;
|
2010-09-07 19:27:44 +08:00
|
|
|
}
|
2011-01-15 00:43:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
L.Util.extend(L.DomUtil, {
|
|
|
|
TRANSITION_PROPERTY: L.DomUtil.testProp(['transition', 'webkitTransition', 'OTransition', 'MozTransition', 'msTransition']),
|
|
|
|
TRANSFORM_PROPERTY: L.DomUtil.testProp(['transformProperty', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']),
|
|
|
|
|
|
|
|
TRANSLATE_OPEN: 'translate' + (L.Browser.webkit3d ? '3d(' : '('),
|
|
|
|
TRANSLATE_CLOSE: L.Browser.webkit3d ? ',0)' : ')'
|
|
|
|
});
|