make className utils work for SVG elements in IE9, ref #2164

This commit is contained in:
Vladimir Agafonkin 2013-11-12 18:52:13 +02:00
parent 9babc039f0
commit c500d5ac1f
2 changed files with 24 additions and 10 deletions

View File

@ -104,32 +104,46 @@ L.DomUtil = {
},
hasClass: function (el, name) {
if ('classList' in el) {
if (el.classList !== undefined) {
return el.classList.contains(name);
}
return (el.className.length > 0) &&
new RegExp('(^|\\s)' + name + '(\\s|$)').test(el.className);
var className = L.DomUtil._getClass(el);
return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
},
addClass: function (el, name) {
if ('classList' in el) {
if (el.classList !== undefined) {
var classes = L.Util.splitWords(name);
for (var i = 0, len = classes.length; i < len; i++) {
el.classList.add(classes[i]);
}
} else if (!L.DomUtil.hasClass(el, name)) {
el.className += (el.className ? ' ' : '') + name;
var className = L.DomUtil._getClass(el);
L.DomUtil._setClass(el, (className ? className + ' ' : '') + name);
}
},
removeClass: function (el, name) {
if ('classList' in el) {
if (el.classList !== undefined) {
el.classList.remove(name);
} else {
el.className = L.Util.trim((' ' + el.className + ' ').replace(' ' + name + ' ', ' '));
L.DomUtil._setClass(el, L.Util.trim((' ' + L.DomUti._getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
}
},
_setClass: function (el, name) {
if (el.className.baseVal === undefined) {
el.className = name;
} else {
// in case of SVG element
el.className.baseVal = name;
}
},
_getClass: function (el) {
return el.className.baseVal === undefined ? el.className : el.className.baseVal;
},
setOpacity: function (el, value) {
if ('opacity' in el.style) {

View File

@ -110,7 +110,7 @@ L.Path = L.Path.extend({
_initEvents: function () {
if (this.options.clickable) {
if (L.Browser.svg || !L.Browser.vml) {
this._path.setAttribute('class', 'leaflet-clickable');
L.DomUtil.addClass(this._path, 'leaflet-clickable');
}
L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
@ -160,14 +160,14 @@ L.Map.include({
this._panes.overlayPane.appendChild(this._pathRoot);
if (this.options.zoomAnimation && L.Browser.any3d) {
this._pathRoot.setAttribute('class', ' leaflet-zoom-animated');
L.DomUtil.addClass(this._pathRoot, 'leaflet-zoom-animated');
this.on({
'zoomanim': this._animatePathZoom,
'zoomend': this._endPathZoom
});
} else {
this._pathRoot.setAttribute('class', ' leaflet-zoom-hide');
L.DomUtil.addClass(this._pathRoot, 'leaflet-zoom-hide');
}
this.on('moveend', this._updateSvgViewport);