refactor and cleanup marker bringToFront option

This commit is contained in:
Vladimir Agafonkin 2012-10-31 01:25:03 +02:00
parent b0a911516f
commit 8077faa2e3

View File

@ -13,8 +13,8 @@ L.Marker = L.Class.extend({
draggable: false, draggable: false,
zIndexOffset: 0, zIndexOffset: 0,
opacity: 1, opacity: 1,
bringToFront: true, riseOnHover: false,
bringToFrontZOffset: 250 riseOffset: 250
}, },
initialize: function (latlng, options) { initialize: function (latlng, options) {
@ -109,10 +109,10 @@ L.Marker = L.Class.extend({
L.DomUtil.addClass(this._icon, classToAdd); L.DomUtil.addClass(this._icon, classToAdd);
if (options.bringToFront) { if (options.riseOnHover) {
L.DomEvent L.DomEvent
.on(this._icon, 'mouseover', this._bringToFront, this) .on(this._icon, 'mouseover', this._bringToFront, this)
.on(this._icon, 'mouseout', this._sendToBack, this); .on(this._icon, 'mouseout', this._resetZIndex, this);
} }
} }
@ -141,10 +141,10 @@ L.Marker = L.Class.extend({
_removeIcon: function () { _removeIcon: function () {
var panes = this._map._panes; var panes = this._map._panes;
if (this.options.bringToFront) { if (this.options.riseOnHover) {
L.DomEvent L.DomEvent
.off(this._icon, 'mouseover', this._bringToFront) .off(this._icon, 'mouseover', this._bringToFront)
.off(this._icon, 'mouseout', this._sendToBack); .off(this._icon, 'mouseout', this._resetZIndex);
} }
panes.markerPane.removeChild(this._icon); panes.markerPane.removeChild(this._icon);
@ -165,11 +165,10 @@ L.Marker = L.Class.extend({
this._zIndex = pos.y + this.options.zIndexOffset; this._zIndex = pos.y + this.options.zIndexOffset;
this._updateZIndex(); this._resetZIndex();
}, },
_updateZIndex: function () { _updateZIndex: function (offset) {
var offset = this._broughtToFrontOffset || 0;
this._icon.style.zIndex = this._zIndex + offset; this._icon.style.zIndex = this._zIndex + offset;
}, },
@ -238,15 +237,11 @@ L.Marker = L.Class.extend({
}, },
_bringToFront: function () { _bringToFront: function () {
this._broughtToFrontOffset = this.options.bringToFrontZOffset; this._updateZIndex(this.options.riseOffset);
this._updateZIndex();
}, },
_sendToBack: function () { _resetZIndex: function () {
this._broughtToFrontOffset = 0; this._updateZIndex(0);
this._updateZIndex();
} }
}); });