lib/leaflet/src/layer/marker/Marker.js in leaflet-js-0.6.beta4 vs lib/leaflet/src/layer/marker/Marker.js in leaflet-js-0.7.0
- old
+ new
@@ -7,12 +7,14 @@
includes: L.Mixin.Events,
options: {
icon: new L.Icon.Default(),
title: '',
+ alt: '',
clickable: true,
draggable: false,
+ keyboard: true,
zIndexOffset: 0,
opacity: 1,
riseOnHover: false,
riseOffset: 250
},
@@ -27,10 +29,11 @@
map.on('viewreset', this.update, this);
this._initIcon();
this.update();
+ this.fire('add');
if (map.options.zoomAnimation && map.options.markerZoomAnimation) {
map.on('zoomanim', this._animateZoom, this);
}
},
@@ -44,10 +47,11 @@
if (this.dragging) {
this.dragging.disable();
}
this._removeIcon();
+ this._removeShadow();
this.fire('remove');
map.off({
'viewreset': this.update,
@@ -75,21 +79,22 @@
return this;
},
setIcon: function (icon) {
- if (this._map) {
- this._removeIcon();
- }
this.options.icon = icon;
if (this._map) {
this._initIcon();
this.update();
}
+ if (this._popup) {
+ this.bindPopup(this._popup);
+ }
+
return this;
},
update: function () {
if (this._icon) {
@@ -102,70 +107,94 @@
_initIcon: function () {
var options = this.options,
map = this._map,
animation = (map.options.zoomAnimation && map.options.markerZoomAnimation),
- classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide',
- needOpacityUpdate = false;
+ classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide';
- if (!this._icon) {
- this._icon = options.icon.createIcon();
+ var icon = options.icon.createIcon(this._icon),
+ addIcon = false;
+ // if we're not reusing the icon, remove the old one and init new one
+ if (icon !== this._icon) {
+ if (this._icon) {
+ this._removeIcon();
+ }
+ addIcon = true;
+
if (options.title) {
- this._icon.title = options.title;
+ icon.title = options.title;
}
+
+ if (options.alt) {
+ icon.alt = options.alt;
+ }
+ }
- this._initInteraction();
- needOpacityUpdate = (this.options.opacity < 1);
+ L.DomUtil.addClass(icon, classToAdd);
- L.DomUtil.addClass(this._icon, classToAdd);
+ if (options.keyboard) {
+ icon.tabIndex = '0';
+ }
- if (options.riseOnHover) {
- L.DomEvent
- .on(this._icon, 'mouseover', this._bringToFront, this)
- .on(this._icon, 'mouseout', this._resetZIndex, this);
- }
+ this._icon = icon;
+
+ this._initInteraction();
+
+ if (options.riseOnHover) {
+ L.DomEvent
+ .on(icon, 'mouseover', this._bringToFront, this)
+ .on(icon, 'mouseout', this._resetZIndex, this);
}
- if (!this._shadow) {
- this._shadow = options.icon.createShadow();
+ var newShadow = options.icon.createShadow(this._shadow),
+ addShadow = false;
- if (this._shadow) {
- L.DomUtil.addClass(this._shadow, classToAdd);
- needOpacityUpdate = (this.options.opacity < 1);
- }
+ if (newShadow !== this._shadow) {
+ this._removeShadow();
+ addShadow = true;
}
- if (needOpacityUpdate) {
+ if (newShadow) {
+ L.DomUtil.addClass(newShadow, classToAdd);
+ }
+ this._shadow = newShadow;
+
+
+ if (options.opacity < 1) {
this._updateOpacity();
}
+
var panes = this._map._panes;
- panes.markerPane.appendChild(this._icon);
+ if (addIcon) {
+ panes.markerPane.appendChild(this._icon);
+ }
- if (this._shadow) {
+ if (newShadow && addShadow) {
panes.shadowPane.appendChild(this._shadow);
}
},
_removeIcon: function () {
- var panes = this._map._panes;
-
if (this.options.riseOnHover) {
L.DomEvent
.off(this._icon, 'mouseover', this._bringToFront)
.off(this._icon, 'mouseout', this._resetZIndex);
}
- panes.markerPane.removeChild(this._icon);
+ this._map._panes.markerPane.removeChild(this._icon);
+ this._icon = null;
+ },
+
+ _removeShadow: function () {
if (this._shadow) {
- panes.shadowPane.removeChild(this._shadow);
+ this._map._panes.shadowPane.removeChild(this._shadow);
}
-
- this._icon = this._shadow = null;
+ this._shadow = null;
},
_setPos: function (pos) {
L.DomUtil.setPosition(this._icon, pos);
@@ -181,11 +210,11 @@
_updateZIndex: function (offset) {
this._icon.style.zIndex = this._zIndex + offset;
},
_animateZoom: function (opt) {
- var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
+ var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center).round();
this._setPos(pos);
},
_initInteraction: function () {
@@ -197,10 +226,11 @@
var icon = this._icon,
events = ['dblclick', 'mousedown', 'mouseover', 'mouseout', 'contextmenu'];
L.DomUtil.addClass(icon, 'leaflet-clickable');
L.DomEvent.on(icon, 'click', this._onMouseClick, this);
+ L.DomEvent.on(icon, 'keypress', this._onKeyPress, this);
for (var i = 0; i < events.length; i++) {
L.DomEvent.on(icon, events[i], this._fireMouseEvent, this);
}
@@ -228,10 +258,19 @@
originalEvent: e,
latlng: this._latlng
});
},
+ _onKeyPress: function (e) {
+ if (e.keyCode === 13) {
+ this.fire('click', {
+ originalEvent: e,
+ latlng: this._latlng
+ });
+ }
+ },
+
_fireMouseEvent: function (e) {
this.fire(e.type, {
originalEvent: e,
latlng: this._latlng
@@ -242,17 +281,21 @@
if (e.type === 'contextmenu' && this.hasEventListeners(e.type)) {
L.DomEvent.preventDefault(e);
}
if (e.type !== 'mousedown') {
L.DomEvent.stopPropagation(e);
+ } else {
+ L.DomEvent.preventDefault(e);
}
},
setOpacity: function (opacity) {
this.options.opacity = opacity;
if (this._map) {
this._updateOpacity();
}
+
+ return this;
},
_updateOpacity: function () {
L.DomUtil.setOpacity(this._icon, this.options.opacity);
if (this._shadow) {